# @tinyhttp/accepts

> accepts rewrite in TypeScript

Latest version **2.3.1** (published 2026-07-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tinyhttp/accepts
pnpm add @tinyhttp/accepts
yarn add @tinyhttp/accepts
bun add @tinyhttp/accepts
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.3.1 |
| Published | 2026-07-18 |
| First published | 2020-09-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=14.13.1 |
| Dependencies | 1 |
| Unpacked size | 42.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 2901 |
| Maintainers | dropthebeatbro, v1rtl |

## Links

- npm: https://www.npmjs.com/package/@tinyhttp/accepts
- Repository: https://github.com/tinyhttp/tinyhttp
- Homepage: https://tinyhttp.v1rtl.site
- Funding: https://github.com/tinyhttp/tinyhttp?sponsor=1
- npm.io page: https://npm.io/package/@tinyhttp/accepts

## Dependencies (1)

- [mrmime](https://npm.io/package/mrmime.md) ^2.0.1

## Recent versions

- 2.3.1 (latest) — 2026-07-18
- 2.3.0 — 2026-02-01
- 2.2.4 — 2025-09-14
- 2.2.3 — 2024-09-11
- 2.2.2 — 2024-06-26
- 2.2.1 — 2024-01-06
- 2.2.0 — 2023-09-28
- 2.1.0 — 2023-05-23
- 2.0.9 — 2023-05-14
- 2.0.8 — 2023-01-11
- 2.0.7 — 2022-09-15
- 2.0.6 — 2022-06-05
- 2.0.5 — 2022-01-22
- 2.0.4 — 2021-10-23
- 2.0.3 — 2021-10-06
- … 29 more at https://npm.io/package/@tinyhttp/accepts/versions

## README

# @tinyhttp/accepts

> [`accepts`](https://github.com/jshttp/accepts) rewrite in TypeScript.

Higher level content negotiation based on
[negotiator](https://www.npmjs.com/package/negotiator). Extracted from
[koa](https://www.npmjs.com/package/koa) for general use.

In addition to negotiator, it allows:

- Allows types as an array or arguments list, ie
  `(['text/html', 'application/json'])` as well as
  `('text/html', 'application/json')`.
- Allows type shorthands such as `json`.
- Returns `false` when no types match
- Treats non-existent headers as `*`

## Install

```sh
pnpm i @tinyhttp/accepts
```

## API

```ts
import { Accepts } from '@tinyhttp/accepts'
```

### accepts(req)

Create a new `Accepts` object for the given `req`.

#### `.charset(charsets)`

Return the first accepted charset. If nothing in `charsets` is accepted, then
`false` is returned.

#### `.charsets()`

Return the charsets that the request accepts, in the order of the client's
preference (most preferred first).

#### `.encoding(encodings)`

Return the first accepted encoding. If nothing in `encodings` is accepted, then
`false` is returned.

#### `.encodings()`

Return the encodings that the request accepts, in the order of the client's
preference (most preferred first).

#### `.language(languages)`

Return the first accepted language. If nothing in `languages` is accepted, then
`false` is returned.

#### `.languages()`

Return the languages that the request accepts, in the order of the client's
preference (most preferred first).

#### `.type(types)`

Return the first accepted type (and it is returned as the same text as what
appears in the `types` array). If nothing in `types` is accepted, then `false`
is returned.

The `types` array can contain full MIME types or file extensions. Any value that
is not a full MIME types is passed to `require('mime-types').lookup`.

#### `.types()`

Return the types that the request accepts, in the order of the client's
preference (most preferred first).

## Example

This simple example shows how to use `accepts` to return a different typed
respond body based on what the client wants to accept. The server lists it's
preferences in order and will get back the best match between the client and
server.

```ts
import Accepts from '@tinyhttp/accepts'
import { createServer } from 'node:http'

createServer((req, res) => {
  const accept = new Accepts(req)

  // the order of this list is significant; should be server preferred order
  switch (accept.type(['json', 'html'])) {
    case 'json':
      res.setHeader('Content-Type', 'application/json')
      res.write('{"hello":"world!"}')
      break
    case 'html':
      res.setHeader('Content-Type', 'text/html')
      res.write('<b>hello, world!</b>')
      break
    default:
      // the fallback is text/plain, so no need to specify it above
      res.setHeader('Content-Type', 'text/plain')
      res.write('hello, world!')
      break
  }

  res.end()
}).listen(3000)
```

You can test this out with the cURL program:

```sh
curl -I -H 'Accept: text/html' http://localhost:3000/
```

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