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.11 kB
import { createUnsupportedMediaType } from '@chubbyts/chubbyts-http-error/dist/http-error'; export const createContentTypeNegotiationMiddleware = (contentTypeNegotiator) => { return async (request, handler) => { if (typeof request.headers['content-type'] === 'undefined') { const supportedValues = contentTypeNegotiator.supportedValues; throw createUnsupportedMediaType({ detail: `Missing content-type: "${supportedValues.join('", "')}"`, supportedValues, }); } const negotiatedValue = contentTypeNegotiator.negotiate(request.headers['content-type'].join(',')); if (!negotiatedValue) { const supportedValues = contentTypeNegotiator.supportedValues; throw createUnsupportedMediaType({ detail: `Allowed content-types: "${supportedValues.join('", "')}"`, supportedValues, }); } return handler({ ...request, attributes: { ...request.attributes, contentType: negotiatedValue.value }, }); }; };