1.0.0 • Published 3 years ago

@chubbyjs/chubbyjs-negotiation v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

chubbyjs-negotiation

CI Coverage Status Infection MSI npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A simple negotiation library.

Requirements

Installation

Through NPM as @chubbyjs/chubbyjs-negotiation.

npm i @chubbyjs/chubbyjs-negotiation@1.0.0

Usage

AcceptLanguageNegotiator

import AcceptLanguageNegotiator from '@chubbyjs/chubbyjs-negotiation/dist/AcceptLanguageNegotiator';

const request = ...withHeader('Accept-Language', 'de,en;q=0.3,en-US;q=0.7');

const negotiator = new AcceptLanguageNegotiator(['en', 'de']);
const value = negotiator.negotiate(request); // NegotiatedValue
value.getValue(); // de
value.getAttributes(); // new Map([['q' => '1.0']])

AcceptNegotiator

import AcceptNegotiator from '@chubbyjs/chubbyjs-negotiation/dist/AcceptNegotiator';

const request = ...withHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');

const negotiator = new AcceptNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
const value = negotiator.negotiate(request); // NegotiatedValue
value.getValue(); // application/xml
value.getAttributes(); // new Map([['q' => '0.9']])

ContentTypeNegotiator

import ContentTypeNegotiator from '@chubbyjs/chubbyjs-negotiation/dist/ContentTypeNegotiator';

const request = ...withHeader('Content-Type', 'application/xml; charset=UTF-8');

const negotiator = new ContentTypeNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
const value = negotiator.negotiate($request); // NegotiatedValue
value->getValue(); // application/xml
value->getAttributes(); // new Map([['charset' => 'UTF-8']])

Copyright

Dominik Zogg 2021