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.04 kB
import { createNotAcceptable } from '@chubbyts/chubbyts-http-error/dist/http-error'; export const createAcceptNegotiationMiddleware = (acceptNegotiator) => { return async (request, handler) => { if (typeof request.headers['accept'] === 'undefined') { const supportedValues = acceptNegotiator.supportedValues; throw createNotAcceptable({ detail: `Missing accept: "${supportedValues.join('", "')}"`, supportedValues, }); } const negotiatedValue = acceptNegotiator.negotiate(request.headers['accept'].join(',')); if (!negotiatedValue) { const supportedValues = acceptNegotiator.supportedValues; throw createNotAcceptable({ detail: `Allowed accepts: "${supportedValues.join('", "')}"`, supportedValues, }); } return handler({ ...request, attributes: { ...request.attributes, accept: negotiatedValue.value }, }); }; };