https://github.com/fastify/fastify-accepts
Add an accepts parser to Fastify
https://github.com/fastify/fastify-accepts
accept-header encoding fastify fastify-plugin
Last synced: 12 days ago
JSON representation
Add an accepts parser to Fastify
- Host: GitHub
- URL: https://github.com/fastify/fastify-accepts
- Owner: fastify
- License: mit
- Created: 2017-07-21T18:16:22.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-03-30T21:21:44.000Z (20 days ago)
- Last Synced: 2025-04-01T14:52:17.853Z (19 days ago)
- Topics: accept-header, encoding, fastify, fastify-plugin
- Language: JavaScript
- Homepage: https://npmjs.com/package/@fastify/accepts
- Size: 164 KB
- Stars: 71
- Watchers: 15
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - fastify-accepts
- awesome-fastify - `fastify-accepts`
README
# @fastify/accepts
[](https://github.com/fastify/fastify-accepts/actions/workflows/ci.yml)
[](https://www.npmjs.com/package/@fastify/accepts)
[](https://github.com/neostandard/neostandard)Add an accepts parser to Fastify.
## Install
`npm i @fastify/accepts`
### Compatibility
| Plugin version | Fastify version |
| ---------------|-----------------|
| `>=5.x` | `^5.x` |
| `^4.x` | `^4.x` |
| `>=2.x <4.x` | `^3.x` |
| `^1.x` | `^2.x` |
| `^1.x` | `^1.x` |Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
in the table above.
See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.## Usage
```js
const fastify = require('fastify')
const Boom = require('@hapi/boom')fastify.register(require('@fastify/accepts'))
fastify.post('/', function (req, reply) {
const accept = req.accepts() // Accepts object
switch(accept.type(['json', 'html'])) {
case 'json':
reply.type('application/json').send({hello: 'world'})
break
case 'html':
reply.type('text/html').send('hello, world!')
break
default:
reply.send(Boom.notAcceptable('unacceptable'))
break
}
})
```See [accepts package](https://www.npmjs.com/package/accepts) for all available APIs.
This plugin adds to `Request` object all `Accepts` object methods.
```js
fastify.post('/', function (req, reply) {
req.charset(['utf-8'])
req.charsets()
req.encoding(['gzip', 'compress'])
req.encodings()
req.language(['es', 'en'])
req.languages()
req.type(['image/png', 'image/tiff'])
req.types()
})
```### Options
- `decorateReply` If `true`, the `Reply` object will be decorated with the `requestAccepts`, `requestTypes`, `requestCharsets`, `requestEncodings`, and `requestLanguages` methods, which will return the corresponding values from the `Request` object. Default: `false`.
## License
Licensed under [MIT](./LICENSE).