UNPKG

@chubbyts/chubbyts-api

Version:

[![CI](https://github.com/chubbyts/chubbyts-api/workflows/CI/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-api/badge.svg?branch=maste

25 lines (24 loc) 1.13 kB
import { createNotAcceptable } from '@chubbyts/chubbyts-http-error/dist/http-error'; export const createAcceptLanguageNegotiationMiddleware = (acceptLanguageNegotiator) => { return async (request, handler) => { if (typeof request.headers['accept-language'] === 'undefined') { const supportedValues = acceptLanguageNegotiator.supportedValues; throw createNotAcceptable({ detail: `Missing accept-language: "${supportedValues.join('", "')}"`, supportedValues, }); } const negotiatedValue = acceptLanguageNegotiator.negotiate(request.headers['accept-language'].join(',')); if (!negotiatedValue) { const supportedValues = acceptLanguageNegotiator.supportedValues; throw createNotAcceptable({ detail: `Allowed accept-languages: "${supportedValues.join('", "')}"`, supportedValues, }); } return handler({ ...request, attributes: { ...request.attributes, 'accept-language': negotiatedValue.value }, }); }; };