Files
SnowLuma/.pnpm-store/v11/files/d4/895dada6520eaaae2865b2b3935f5fb95aaff4613e633cab07ad2e2020fa1a4e57e37351cef4bb6336eea07801f455807a08db255939641bd334f791a4b14e
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

66 lines
1.9 KiB
Plaintext

//#region src/client/fetch-result-please.ts
/**
* @description This file is a modified version of `fetch-result-please` (`ofetch`), minimalized and adapted to Hono's custom needs.
*
* @link https://www.npmjs.com/package/fetch-result-please
*/
const nullBodyResponses = /* @__PURE__ */ new Set([
101,
204,
205,
304
]);
/**
* Smartly parses and return the consumable result from a fetch `Response`.
*
* Throwing a structured error if the response is not `ok`. ({@link DetailedError})
*/
async function fetchRP(fetchRes) {
const _fetchRes = await fetchRes;
if ((_fetchRes.body || _fetchRes._bodyInit) && !nullBodyResponses.has(_fetchRes.status)) _fetchRes._data = await _fetchRes[detectResponseType(_fetchRes)]();
if (!_fetchRes.ok) throw new DetailedError(`${_fetchRes.status} ${_fetchRes.statusText}`, {
statusCode: _fetchRes?.status,
detail: {
data: _fetchRes?._data,
statusText: _fetchRes?.statusText
}
});
return _fetchRes._data;
}
var DetailedError = class extends Error {
/**
* Additional `message` that will be logged AND returned to client
*/
detail;
/**
* Additional `code` that will be logged AND returned to client
*/
code;
/**
* Additional value that will be logged AND NOT returned to client
*/
log;
/**
* Optionally set the status code to return, in a web server context
*/
statusCode;
constructor(message, options = {}) {
super(message);
this.name = "DetailedError";
this.log = options.log;
this.detail = options.detail;
this.code = options.code;
this.statusCode = options.statusCode;
}
};
const jsonRegex = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(?:;.+)?$/i;
function detectResponseType(response) {
const _contentType = response.headers.get("content-type");
if (!_contentType) return "text";
const contentType = _contentType.split(";").shift();
if (jsonRegex.test(contentType)) return "json";
return "text";
}
//#endregion
export { DetailedError, fetchRP };