Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fastify/fastify-accepts
Add accepts parser to fastify
https://github.com/fastify/fastify-accepts
accept-header encoding fastify fastify-plugin
Last synced: 27 days ago
JSON representation
Add 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: master
- Last Pushed: 2024-09-21T09:17:03.000Z (about 1 month ago)
- Last Synced: 2024-09-28T11:21:00.005Z (about 1 month ago)
- Topics: accept-header, encoding, fastify, fastify-plugin
- Language: JavaScript
- Homepage:
- Size: 141 KB
- Stars: 69
- Watchers: 17
- Forks: 9
- 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
![CI](https://github.com/fastify/fastify-accepts/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/@fastify/accepts.svg?style=flat)](https://www.npmjs.com/package/@fastify/accepts)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)Add accepts parser to fastify
## Install
`npm i @fastify/accepts`
## 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()
})
```## License
Licensed under [MIT](./LICENSE)