Socket
Socket
Sign inDemoInstall

@hapi/accept

Package Overview
Dependencies
2
Maintainers
7
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @hapi/accept

HTTP Accept-* headers parsing


Version published
Maintainers
7
Created

Package description

What is @hapi/accept?

@hapi/accept is a utility library for HTTP content negotiation. It helps in parsing and matching HTTP headers like Accept, Accept-Charset, Accept-Encoding, and Accept-Language, allowing you to determine the best content to serve based on the client's preferences.

What are @hapi/accept's main functionalities?

Parsing Accept Header

This feature allows you to parse the 'Accept' header from an HTTP request and get an array of media types sorted by their quality values.

const Accept = require('@hapi/accept');
const headers = { 'accept': 'text/html, application/json;q=0.9, */*;q=0.8' };
const mediaTypes = Accept.mediaTypes(headers.accept);
console.log(mediaTypes); // ['text/html', 'application/json', '*/*']

Parsing Accept-Language Header

This feature allows you to parse the 'Accept-Language' header and get an array of languages sorted by their quality values.

const Accept = require('@hapi/accept');
const headers = { 'accept-language': 'en-US,en;q=0.9,fr;q=0.8' };
const languages = Accept.languages(headers['accept-language']);
console.log(languages); // ['en-US', 'en', 'fr']

Parsing Accept-Charset Header

This feature allows you to parse the 'Accept-Charset' header and get an array of charsets sorted by their quality values.

const Accept = require('@hapi/accept');
const headers = { 'accept-charset': 'utf-8, iso-8859-1;q=0.5' };
const charsets = Accept.charsets(headers['accept-charset']);
console.log(charsets); // ['utf-8', 'iso-8859-1']

Parsing Accept-Encoding Header

This feature allows you to parse the 'Accept-Encoding' header and get an array of encodings sorted by their quality values.

const Accept = require('@hapi/accept');
const headers = { 'accept-encoding': 'gzip, deflate;q=0.8, br;q=0.6' };
const encodings = Accept.encodings(headers['accept-encoding']);
console.log(encodings); // ['gzip', 'deflate', 'br']

Matching Media Types

This feature allows you to match the best media type from a list of available types based on the 'Accept' header.

const Accept = require('@hapi/accept');
const headers = { 'accept': 'text/html, application/json;q=0.9, */*;q=0.8' };
const mediaType = Accept.mediaType(headers.accept, ['application/json', 'text/html']);
console.log(mediaType); // 'text/html'

Other packages similar to @hapi/accept

Readme

Source

@hapi/accept

HTTP Accept-* headers parsing.

accept is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi – they work even better together.

Visit the hapi.dev Developer Portal for tutorials, documentation, and support

Useful resources

Keywords

FAQs

Last updated on 10 Oct 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc