New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/accepts

Package Overview
Dependencies
Maintainers
20
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/accepts

Add accept parser to fastify

  • 5.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
20
Created

What is @fastify/accepts?

@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.

What are @fastify/accepts's main functionalities?

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');
});

Other packages similar to @fastify/accepts

FAQs

Package last updated on 04 Dec 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc