Files
SnowLuma/.pnpm-store/v11/files/1e/0879b5853f84696878b7635b9a127a3ec21f7c6afcbe164befdbbcc87cf69f103d48ee7df3cd071387474b5897da93dd5d85ae8b7e9a2fcb1defc7ac59c59d
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

55 lines
1.7 KiB
Plaintext

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils_accept = require("../../utils/accept.js");
//#region src/helper/accepts/accepts.ts
const matchType = (acceptType, supportedType) => {
const accept = acceptType.toLowerCase();
const supported = supportedType.toLowerCase();
if (accept === supported) return true;
if (accept === "*/*" || accept === "*") return false;
if (accept.endsWith("/*")) {
const [acceptMain] = accept.split("/");
const [supportedMain] = supported.split("/");
return acceptMain === supportedMain;
}
return false;
};
const getSpecificity = (type) => {
if (type === "*/*" || type === "*") return 1;
if (type.endsWith("/*")) return 2;
return 3;
};
const defaultMatch = (accepts, config) => {
const { supports, default: defaultSupport } = config;
const sortedAccepts = accepts.filter((accept) => accept.q > 0).sort((a, b) => {
if (b.q !== a.q) return b.q - a.q;
return getSpecificity(b.type) - getSpecificity(a.type);
});
for (const accept of sortedAccepts) {
const matched = supports.find((supported) => matchType(accept.type, supported));
if (matched) return matched;
}
return defaultSupport;
};
/**
* Match the accept header with the given options.
* @example
* ```ts
* app.get('/users', (c) => {
* const lang = accepts(c, {
* header: 'Accept-Language',
* supports: ['en', 'zh'],
* default: 'en',
* })
* })
* ```
*/
const accepts = (c, options) => {
const acceptHeader = c.req.header(options.header);
if (!acceptHeader) return options.default;
const accepts = require_utils_accept.parseAccept(acceptHeader);
return (options.match || defaultMatch)(accepts, options);
};
//#endregion
exports.accepts = accepts;
exports.defaultMatch = defaultMatch;