@chubbyts/chubbyts-api
Version:
[](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [ • 1.04 kB
JavaScript
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 },
});
};
};