# @hattip/headers

> Header parsing utilities for Hattip

Latest version **0.0.49** (published 2024-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hattip/headers
pnpm add @hattip/headers
yarn add @hattip/headers
bun add @hattip/headers
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.0.49 |
| Published | 2024-11-02 |
| First published | 2023-03-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 9.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1380 |
| Author | Fatih Aygün |
| Maintainers | cyco130 |

## Links

- npm: https://www.npmjs.com/package/@hattip/headers
- Repository: https://github.com/hattipjs/hattip
- Homepage: https://github.com/hattipjs/hattip#readme
- Issues: https://github.com/hattipjs/hattip/issues
- npm.io page: https://npm.io/package/@hattip/headers

## Dependencies (1)

- [@hattip/core](https://npm.io/package/@hattip/core.md) 0.0.49

## Recent versions

- 0.0.49 (latest) — 2024-11-02
- 0.0.35-canary.7 (canary) — 2023-08-27
- 0.0.48 — 2024-09-08
- 0.0.47 — 2024-08-11
- 0.0.46 — 2024-06-18
- 0.0.45 — 2024-03-27
- 0.0.44 — 2024-03-09
- 0.0.43 — 2024-02-17
- 0.0.42 — 2024-02-07
- 0.0.41 — 2024-01-22
- 0.0.40 — 2024-01-17
- 0.0.39 — 2024-01-15
- 0.0.38 — 2024-01-03
- 0.0.37 — 2023-12-29
- 0.0.36 — 2023-12-29
- … 7 more at https://npm.io/package/@hattip/headers/versions

## README

# `@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.

```ts
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:

```ts
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:

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

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

---
_Source: https://npm.io/package/@hattip/headers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
