@hattip/headers
Hattip的页眉解析和内容谈判工具.
parseHeaderValue
parseHeaderValue 解析页眉值并返回带有 value 财务报告和审定财务报表 directives 属性。 每个对象代表一个值(comma-separated)及其指令(semicroma-separated). 它理解括号间的基本引文和评论.
const parsed = parseHeaderValue(`en-US, en;q=0.9, fr;q=0.8`);
assert.deepStrictEqual(parsed, [
{ value: "en-US", directives: {} },
{ value: "en", directives: { q: "0.9" } },
{ value: "fr", directives: { q: "0.8" } },
]);
accept
进行内容谈判 Accept 页眉 :
const handler = accept("text/html, application/json", {
"text/html": () => html("<h1>Hello world!</h1>"),
"application/json": () => json({ message: "Hello world!" }),
"*": () => new Response("Unacceptable", { status: 406 }),
});
const response = handler();
console.assert(response.headers.get("content-type") === "text/html");
q 支持数值和子类型及类型通配符请求.
acceptLanguage
进行内容谈判 Accept-Language 页眉 :
const handler = acceptLanguage("en-US, fr;q=0.8", {
"en-US": () => "Hello!",
fr: () => "Bonjour!",
"*": () => "Hello!",
});
const result = handler();
console.assert(result === "Hello!");