
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@fastify/accepts-serializer
Advanced tools
Serialize according to the Accept header.
npm i @fastify/accepts-serializer
| Plugin version | Fastify version | 
|---|---|
>=6.x | ^5.x | 
^5.x | ^4.x | 
>=3.x <5.x | ^3.x | 
^2.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 for more details.
const protobuf = require('protobufjs')
const YAML = require('yamljs')
const msgpack = require('msgpack5')()
const root = protobuf.loadSync('test/awesome.proto')
const AwesomeMessage = root.lookupType('awesomepackage.AwesomeMessage')
const fastify = require('fastify')()
// Global serializers
fastify.register(require('@fastify/accepts-serializer'), {
  serializers: [
    {
      regex: /^application\/yaml$/,
      serializer: body => YAML.stringify(body)
    },
    {
      regex: /^application\/x-msgpack$/,
      serializer: body => msgpack.encode(body)
    }
  ],
  default: 'application/yaml' // MIME type used if Accept header does not match anything
})
// Per-router serializers
const config = {
  serializers: [
    {
      regex: /^application\/x-protobuf$/,
      serializer: body => AwesomeMessage.encode(AwesomeMessage.create(body)).finish()
    }
  ]
}
fastify.get('/request', { config }, function (req, reply) {
  reply.send({pippo: 'pluto'})
})
For each route, a SerializerManager is defined, which has both per-route and global serializer definitions.
The MIME type application/json is always handled by fastify if no serializer is registered for that MIME type.
If no default key is specified in configuration, all requests with an unknown Accept header will be replied to with a 406 response (a boom error is used).
Licensed under MIT.
FAQs
Serializer according to the accept header
The npm package @fastify/accepts-serializer receives a total of 816 weekly downloads. As such, @fastify/accepts-serializer popularity was classified as not popular.
We found that @fastify/accepts-serializer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 16 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.