Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
@fastify/accepts
Advanced tools
@fastify/accepts is a Fastify plugin that provides a simple way to handle HTTP Accept headers. It allows you to easily determine the best content type, language, encoding, and charset to respond with based on the client's request.
Content Type Negotiation
This feature allows you to determine the best content type to respond with based on the client's Accept header. The code sample demonstrates how to respond with either JSON or HTML depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const type = accept.type(['json', 'html']);
if (type === 'json') {
reply.send({ message: 'Hello, JSON!' });
} else if (type === 'html') {
reply.type('text/html').send('<h1>Hello, HTML!</h1>');
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Language Negotiation
This feature allows you to determine the best language to respond with based on the client's Accept-Language header. The code sample demonstrates how to respond with either English or Spanish depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const lang = accept.language(['en', 'es']);
if (lang === 'en') {
reply.send({ message: 'Hello!' });
} else if (lang === 'es') {
reply.send({ message: '¡Hola!' });
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Encoding Negotiation
This feature allows you to determine the best encoding to respond with based on the client's Accept-Encoding header. The code sample demonstrates how to respond with either GZIP or Deflate encoding depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const encoding = accept.encoding(['gzip', 'deflate']);
if (encoding === 'gzip') {
reply.header('Content-Encoding', 'gzip').send('Hello, GZIP!');
} else if (encoding === 'deflate') {
reply.header('Content-Encoding', 'deflate').send('Hello, Deflate!');
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Charset Negotiation
This feature allows you to determine the best charset to respond with based on the client's Accept-Charset header. The code sample demonstrates how to respond with either UTF-8 or ISO-8859-1 charset depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const charset = accept.charset(['utf-8', 'iso-8859-1']);
if (charset === 'utf-8') {
reply.header('Content-Type', 'text/plain; charset=utf-8').send('Hello, UTF-8!');
} else if (charset === 'iso-8859-1') {
reply.header('Content-Type', 'text/plain; charset=iso-8859-1').send('Hello, ISO-8859-1!');
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
The 'accepts' package is a content negotiation library for Node.js. It provides similar functionality to @fastify/accepts but is framework-agnostic, meaning it can be used with any Node.js server framework, not just Fastify.
The 'negotiator' package is another content negotiation library for Node.js. It provides a more low-level API compared to 'accepts' and @fastify/accepts, giving you more control over the negotiation process but requiring more manual handling.
Add an accepts parser to Fastify.
npm i @fastify/accepts
Plugin version | Fastify version |
---|---|
^5.x | ^5.x |
^4.x | ^4.x |
^2.x | ^3.x |
^1.x | ^2.x |
^1.x | ^1.x |
Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin in the table above. See Fastify's LTS policy for more details.
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()
})
decorateReply
If true
, the Reply
object will be decorated with the requestAccepts
, requestTypes
, requestCharsets
, requestEncodings
, and requestLanguages
methods, which will return the corresponding values from the Request
object. Default: false
.Licensed under MIT.
FAQs
Add accept parser to fastify
We found that @fastify/accepts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 20 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
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.