@chubbyts/chubbyts-negotiation
v4.5.1
Published
A simple negotiation library.
Maintainers
Readme
chubbyts-negotiation
Description
A simple negotiation library.
Requirements
- node: 22
- @chubbyts/chubbyts-dic-config-factory: ^1.0.0
- @chubbyts/chubbyts-dic-types: ^2.3.0
Installation
Through NPM as @chubbyts/chubbyts-negotiation.
npm i @chubbyts/chubbyts-negotiation@^4.5.1Usage
Accept-Language
import { createAcceptLanguageNegotiator } from '@chubbyts/chubbyts-negotiation/dist/accept-language-negotiator';
const negotiator = createAcceptLanguageNegotiator(['en', 'de']);
const value = negotiator.negotiate('de,en;q=0.3,en-US;q=0.7');Accept
import { createAcceptNegotiator } from '@chubbyts/chubbyts-negotiation/dist/accept-negotiator';
const negotiator = createAcceptNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
const value = negotiator.negotiate('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');Content-Type
import { createContentTypeNegotiator } from '@chubbyts/chubbyts-negotiation/dist/content-type-negotiator';
const negotiator = createContentTypeNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
const value = negotiator.negotiate('application/xml; charset=UTF-8');Service factories (chubbyts-dic-config)
The package ships service factories (abstract factories built on chubbyts-dic-config-factory) for a chubbyts-dic-config (or any chubbyts-dic-types compatible) container within @chubbyts/chubbyts-negotiation/dist/service-factory.
Each negotiator reads its supported values from a <negotiator>SupportedValues service if registered, and falls back to a default otherwise:
acceptLanguageNegotiatorServiceFactory:acceptLanguageNegotiatorSupportedValues, default[]acceptNegotiatorServiceFactory:acceptNegotiatorSupportedValues, default thecontentTypesof theencoderservicecontentTypeNegotiatorServiceFactory:contentTypeNegotiatorSupportedValues, default thecontentTypesof thedecoderservice
The defaults are designed to reuse the type decoders / encoders of chubbyts-decode-encode (or anything else shaped like { contentTypes: Array<string> }), so the negotiators support exactly what can be decoded / encoded:
import type { ConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
import { createContainerByConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';
import type { Negotiator } from '@chubbyts/chubbyts-negotiation/dist/negotiation';
import {
acceptLanguageNegotiatorServiceFactory,
acceptNegotiatorServiceFactory,
contentTypeNegotiatorServiceFactory,
} from '@chubbyts/chubbyts-negotiation/dist/service-factory';
const container = createContainerByConfigFactory({
dependencies: {
factories: new Map<string, ConfigFactory>([
['decoder', decoderServiceFactory()],
['encoder', encoderServiceFactory()],
['acceptLanguageNegotiator', acceptLanguageNegotiatorServiceFactory()],
['acceptLanguageNegotiatorSupportedValues', (): Array<string> => ['en', 'de']],
['acceptNegotiator', acceptNegotiatorServiceFactory()],
['contentTypeNegotiator', contentTypeNegotiatorServiceFactory()],
]),
},
})();
const acceptLanguageNegotiator = container.get<Negotiator>('acceptLanguageNegotiator'); // en, de
const acceptNegotiator = container.get<Negotiator>('acceptNegotiator'); // all encoder content types
const contentTypeNegotiator = container.get<Negotiator>('contentTypeNegotiator'); // all decoder content typesTo override the supported values, register the related service:
const container = createContainerByConfigFactory({
dependencies: {
factories: new Map<string, ConfigFactory>([
['acceptNegotiator', acceptNegotiatorServiceFactory()],
['acceptNegotiatorSupportedValues', (): Array<string> => ['application/json']],
]),
},
})();With names
To negotiate different parts of an application with different supported values, the same factories can be registered multiple times with a name, which gets appended to each service id (acceptNegotiatorapi, acceptNegotiatorSupportedValuesapi, encoderapi, ...).
const container = createContainerByConfigFactory({
dependencies: {
factories: new Map<string, ConfigFactory>([
['decoderapi', decoderServiceFactory('api')],
['encoderapi', encoderServiceFactory('api')],
['acceptNegotiatorapi', acceptNegotiatorServiceFactory('api')],
['contentTypeNegotiatorapi', contentTypeNegotiatorServiceFactory('api')],
['acceptNegotiatoradmin', acceptNegotiatorServiceFactory('admin')],
['acceptNegotiatorSupportedValuesadmin', (): Array<string> => ['text/html']],
]),
},
})();
const apiAcceptNegotiator = container.get<Negotiator>('acceptNegotiatorapi'); // content types of encoderapi
const adminAcceptNegotiator = container.get<Negotiator>('acceptNegotiatoradmin'); // text/htmlCopyright
2026 Dominik Zogg
