fastify-accepts

Add accept parser to fastify

Usage no npm install needed!

<script type="module">
  import fastifyAccepts from 'https://cdn.skypack.dev/fastify-accepts';
</script>

README

fastify-accepts

CI NPM version Known Vulnerabilities Coverage Status js-standard-style

Add accepts parser to fastify

Install

npm install --save fastify-accepts

Usage

const fastify = require('fastify')
const Boom = require('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('<b>hello, world!</b>')
      break
    default:
      reply.send(Boom.notAcceptable('unacceptable'))
      break
  }
})

See accepts package for all available APIs.

This plugin adds to Request object all Accepts object methods.

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