A simple negotiation library.
- node: 12
- @chubbyjs/psr-http-message: ^1.2.1
Through NPM as @chubbyjs/chubbyjs-negotiation.
npm i @chubbyjs/chubbyjs-negotiation@1.0.0
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']])
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']])
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']])
Dominik Zogg 2021