An HTTP content negotiator for Deno. Based on https://github.com/jshttp/negotiator
.
import Negotiator from "https://deno.land/x/negotiator/mod.ts";
const availableMediaTypes = ['text/html', 'text/plain', 'application/json'];
// The negotiator constructor receives a Headers object
const negotiator = new Negotiator(header)
// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'
const negotiator.mediaTypes()
// -> ['text/html', 'image/jpeg', 'application/*']
const negotiator.mediaTypes(availableMediaTypes)
// -> ['text/html', 'application/json']
const negotiator.mediaType(availableMediaTypes)
// -> 'text/html'
Returns the most preferred media type from the client.
Returns the most preferred media type from a list of available media types.
Returns an array of preferred media types ordered by the client preference.
Returns an array of preferred media types ordered by priority from a list of available media types.
const negotiator = new Negotiator(header)
const availableLanguages = ['en', 'es', 'fr']
// Let's say Accept-Language header is 'en;q=0.8, es, pt'
const negotiator.languages()
// -> ['es', 'pt', 'en']
const negotiator.languages(availableLanguages)
// -> ['es', 'en']
const language = negotiator.language(availableLanguages)
// -> 'es'
Returns the most preferred language from the client.
Returns the most preferred language from a list of available languages.
Returns an array of preferred languages ordered by the client preference.
Returns an array of preferred languages ordered by priority from a list of available languages.
const availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
const negotiator = new Negotiator(header)
// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'
const negotiator.charsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']
const negotiator.charsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']
const negotiator.charset(availableCharsets)
// -> 'utf-8'
Returns the most preferred charset from the client.
Returns the most preferred charset from a list of available charsets.
Returns an array of preferred charsets ordered by the client preference.
Returns an array of preferred charsets ordered by priority from a list of available charsets.
const availableEncodings = ['identity', 'gzip']
const negotiator = new Negotiator(header)
// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
const negotiator.encodings()
// -> ['gzip', 'identity', 'compress']
const negotiator.encodings(availableEncodings)
// -> ['gzip', 'identity']
const negotiator.encoding(availableEncodings)
// -> 'gzip'
Returns the most preferred encoding from the client.
Returns the most preferred encoding from a list of available encodings.
Returns an array of preferred encodings ordered by the client preference.
Returns an array of preferred encodings ordered by priority from a list of available encodings.