@goa/negotiator
v1.0.1
Published
[fork] HTTP Content Negotiation In ES6 Optimised With Google Closure Compiler.
Downloads
242
Maintainers
Readme
@goa/negotiator
@goa/negotiator
is a fork of HTTP Content Negotiation In ES6 And Optimised With A JavaScript Compiler For The Best Performance.
The original module has been updated to be used in @goa/koa
: Koa web server compiled with Google Closure Compiler using Depack into a single file library (0 dependencies).
yarn add @goa/negotiator
Table Of Contents
API
The package is available by importing its default function:
import Negotiator from '@goa/negotiator'
class Negotiator
Instances of this class will allow to negotiate languages, charsets, media types and encodings.
constructor(
request: IncomingMessage,
): Negotiator
Creates a new instance based on the request from the client.
/* alanode example/ */
import aqt from '@rqt/aqt'
import { createServer } from 'http'
import Negotiator from '@goa/negotiator'
const s = createServer((message, response) => {
const n = new Negotiator(message)
const availableEncodings = ['identity', 'gzip']
console.log('encodings', n.encodings())
console.log('available encodings', n.encodings(availableEncodings))
console.log('encoding', n.encoding(availableEncodings), '\n')
const availableLanguages = ['en', 'es', 'fr']
console.log('languages', n.languages())
console.log('available languages', n.languages(availableLanguages))
console.log('language', n.language(availableLanguages), '\n')
const availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
console.log('charsets', n.charsets())
console.log('available charsets', n.charsets(availableCharsets))
console.log('charset', n.charset(availableCharsets), '\n')
const availableMediaTypes = ['text/html', 'text/plain', 'application/json']
console.log('media types', n.mediaTypes())
console.log('available media types', n.mediaTypes(availableMediaTypes))
console.log('media type', n.mediaType(availableMediaTypes))
response.end()
})
s.listen(0, async () => {
const url = `http://localhost:${s.address().port}`
await aqt(url, {
headers: {
'accept-encoding': 'gzip, compress;q=0.2, identity;q=0.5',
'accept-charset': 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2',
'accept-language': 'en;q=0.8, es, pt',
accept: 'text/html, application/*;q=0.2, image/jpeg;q=0.8',
},
})
s.close()
})
encodings [ 'gzip', 'deflate', 'identity' ]
available encodings [ 'gzip', 'identity' ]
encoding gzip
languages [ 'es', 'pt', 'en' ]
available languages [ 'es', 'en' ]
language es
charsets [ 'utf-8', 'iso-8859-1', 'utf-7' ]
available charsets [ 'utf-8', 'iso-8859-1' ]
charset utf-8
media types [ 'text/html', 'image/jpeg', 'application/*' ]
available media types [ 'text/html', 'application/json' ]
media type text/html
_goa.Negotiator
: HTTP Content Negotiation In ES6.
| Name | Type | Description | | --------------- | -------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | | charset* | function(!Array<string>=): string | Returns the most preferred charset from the client. | | charsets* | function(!Array<string>=): !Array<string> | Returns an array of preferred charsets ordered by the client preference. | | encoding* | function(!Array<string>=): string | Returns the most preferred encoding from the client. | | encodings* | function(!Array<string>=): !Array<string> | Returns an array of preferred encodings ordered by the client preference. | | language* | function(!Array<string>=): string | Returns the most preferred language from the client. | | languages* | function(!Array<string>=): !Array<string> | Returns an array of preferred languages ordered by priority from a list of available languages. | | mediaType* | function(!Array<string>=): string | Returns the most preferred media type from the client. | | mediaTypes* | function(!Array<string>=): !Array<string> | Returns an array of preferred media types ordered by priority from a list of available media types. |
Copyright
The original programming work, documentation and NPM package by Federico Romero, Isaac Z. Schlueter, Douglas Christopher Wilson.