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

    2.1.0 • Public • Published

    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

    Keywords

    none

    Install

    npm i fastify-accepts

    DownloadsWeekly Downloads

    25,814

    Version

    2.1.0

    License

    MIT

    Unpacked Size

    13.7 kB

    Total Files

    11

    Last publish

    Collaborators

    • coopflow
    • simoneb
    • rafaelgss
    • starptech
    • delvedor
    • matteo.collina
    • allevo
    • jsumners
    • zekth
    • ethan_arrowood
    • eomm
    • fox1t
    • salmanm
    • davidmarkclements
    • airhorns
    • kibertoad
    • climba03003