0.6.3 • Published 2 years ago

negotiator v0.6.3

Weekly downloads
17,861,431
License
MIT
Repository
github
Last release
2 years ago

negotiator

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

An HTTP content negotiator for Node.js

Installation

$ npm install negotiator

API

var Negotiator = require('negotiator')

Accept Negotiation

availableMediaTypes = ['text/html', 'text/plain', 'application/json']

// The negotiator constructor receives a request object
negotiator = new Negotiator(request)

// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'

negotiator.mediaTypes()
// -> ['text/html', 'image/jpeg', 'application/*']

negotiator.mediaTypes(availableMediaTypes)
// -> ['text/html', 'application/json']

negotiator.mediaType(availableMediaTypes)
// -> 'text/html'

You can check a working example at examples/accept.js.

Methods

mediaType()

Returns the most preferred media type from the client.

mediaType(availableMediaType)

Returns the most preferred media type from a list of available media types.

mediaTypes()

Returns an array of preferred media types ordered by the client preference.

mediaTypes(availableMediaTypes)

Returns an array of preferred media types ordered by priority from a list of available media types.

Accept-Language Negotiation

negotiator = new Negotiator(request)

availableLanguages = ['en', 'es', 'fr']

// Let's say Accept-Language header is 'en;q=0.8, es, pt'

negotiator.languages()
// -> ['es', 'pt', 'en']

negotiator.languages(availableLanguages)
// -> ['es', 'en']

language = negotiator.language(availableLanguages)
// -> 'es'

You can check a working example at examples/language.js.

Methods

language()

Returns the most preferred language from the client.

language(availableLanguages)

Returns the most preferred language from a list of available languages.

languages()

Returns an array of preferred languages ordered by the client preference.

languages(availableLanguages)

Returns an array of preferred languages ordered by priority from a list of available languages.

Accept-Charset Negotiation

availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']

negotiator = new Negotiator(request)

// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'

negotiator.charsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']

negotiator.charsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']

negotiator.charset(availableCharsets)
// -> 'utf-8'

You can check a working example at examples/charset.js.

Methods

charset()

Returns the most preferred charset from the client.

charset(availableCharsets)

Returns the most preferred charset from a list of available charsets.

charsets()

Returns an array of preferred charsets ordered by the client preference.

charsets(availableCharsets)

Returns an array of preferred charsets ordered by priority from a list of available charsets.

Accept-Encoding Negotiation

availableEncodings = ['identity', 'gzip']

negotiator = new Negotiator(request)

// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'

negotiator.encodings()
// -> ['gzip', 'identity', 'compress']

negotiator.encodings(availableEncodings)
// -> ['gzip', 'identity']

negotiator.encoding(availableEncodings)
// -> 'gzip'

You can check a working example at examples/encoding.js.

Methods

encoding()

Returns the most preferred encoding from the client.

encoding(availableEncodings)

Returns the most preferred encoding from a list of available encodings.

encodings()

Returns an array of preferred encodings ordered by the client preference.

encodings(availableEncodings)

Returns an array of preferred encodings ordered by priority from a list of available encodings.

See Also

The accepts module builds on this module and provides an alternative interface, mime type validation, and more.

License

MIT

acceptsmake-fetch-happenyubo-appcomponennentteasy-select-rnchinjowwchinjowvuedragdropuploadimagesreact-native-bluetooth2killi8n-react-native-fast-imageextensible-node-serverrn-send-sms@arisageha/react-lazyload@arisageha/react-lazyload-fixreact-native-template-rfbasemeiroo-steedos-serverairscanairscan-examplees-acceptsbb-chatreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@frxf/frxfdeneme323112@texttree/demo-bsa-reference-rcl@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamesreact-native-covid-sdkanime-randomeni-chatmidlyreact-native-thanh-toast-libraryresource-machine@thanhnguyen14797/react-native-thanh-toast-library@l1nyanm1ng/react-picture-viewer@xornot/sparrow@saeon/ol-react@saeon/quick-formcthpb-plugin-socialexpress-easy-staticgarretreact-native-printer-brothersrn-pdf-reader-offlinestatic-file-servereact-native-shekhar-bridge-testcogoportutilslevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29wilscanner@oiti/documentoscopy-react-nativestmp@mink-opn/build-tokensquoc-testreact-native-slider-kfnodejs-fileshareunblock-block-save-variableshexa-node-common@infinitebrahmanuniverse/nolb-negcclibyarntest@saaspe/componentshyperpass-sdkexpand-react-bridgeexpress-api-pack-tropea-bootstraapnode-gioluminos-ui-corelizeknushiliyahaotadiesklif-ui-kitsklif-api@everything-registry/sub-chunk-2254jawwy-sdkjawwy_gamification_release@314oner_npm/universal-components-libraryreact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgonddmidz-hapi-content-negociationp149-tablesklif-uireact-native-jawwy_samplelife-serverlink-tyy2828ka-flow.jskafirchain-tetriskwikemonlitepie-datepicker-gabeleviapitest1levicommontestlevicommontest1levicotestlevilibtest15
0.6.3

2 years ago

0.6.2

5 years ago

0.6.1

8 years ago

0.6.0

9 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.9

9 years ago

0.4.8

10 years ago

0.4.7

10 years ago

0.4.6

10 years ago

0.4.5

10 years ago

0.4.4

10 years ago

0.4.3

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.8

11 years ago

0.2.7

11 years ago

0.2.6

11 years ago

0.2.5

12 years ago

0.2.4

12 years ago

0.2.3

12 years ago

0.1.0

12 years ago