@fastify/accepts
TypeScript icon, indicating that this package has built-in type declarations

4.3.0 • Public • Published

@fastify/accepts

CI NPM version js-standard-style

Add accepts parser to fastify

Install

npm i @fastify/accepts

Usage

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('<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

Readme

Keywords

none

Package Sidebar

Install

npm i @fastify/accepts

Weekly Downloads

157,814

Version

4.3.0

License

MIT

Unpacked Size

17.7 kB

Total Files

12

Last publish

Collaborators

  • gurgunday
  • metcoder95
  • galvez
  • simenb
  • coopflow
  • simoneb
  • rafaelgss
  • starptech
  • delvedor
  • matteo.collina
  • allevo
  • jsumners
  • zekth
  • eomm
  • fox1t
  • airhorns
  • kibertoad
  • climba03003
  • is2ei
  • fdawgs