0.0.45 • Published 2 months ago

@hattip/headers v0.0.45

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@hattip/headers

Header parsing and content negotiation utilities for Hattip.

parseHeaderValue

parseHeaderValue parses a header value and returns an array of objects with a value and directives property. Each object represents a value (comma-separated) and its directives (semicolon-separated). It understands basic quoting and comments between parentheses.

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

Performs content negotiation on the Accept header:

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 values and subtype and type wildcards requests are supported.

acceptLanguage

Performs content negotiation on the Accept-Language header:

const handler = acceptLanguage("en-US, fr;q=0.8", {
  "en-US": () => "Hello!",
  fr: () => "Bonjour!",
  "*": () => "Hello!",
});

const result = handler();
console.assert(result === "Hello!");
0.0.45

2 months ago

0.0.44

2 months ago

0.0.43

3 months ago

0.0.42

3 months ago

0.0.41

4 months ago

0.0.40

4 months ago

0.0.39

4 months ago

0.0.38

4 months ago

0.0.36

5 months ago

0.0.37

5 months ago

0.0.35

8 months ago

0.0.35-canary.7

9 months ago

0.0.35-canary

10 months ago

0.0.35-canary.2

10 months ago

0.0.34

12 months ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago