Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 63x 63x 63x 7742x 7739x 7742x 461x 461x 455x 6x | import { RequestHandler } from 'express'; import { is } from 'type-is'; const DEFAULT_ACCEPTED_CONTENT_TYPE = 'application/json'; /** * Builds an express middleware checking the content-type header * returning 415 if the header is not either `application/json` or in the array * passed into the function of valid content-types * @param {String} acceptedContentTypes * @returns {function(Request, Response, NextFunction): void} */ export default function requireContentType( ...acceptedContentTypes: string[] ): RequestHandler { if (acceptedContentTypes.length === 0) { acceptedContentTypes.push(DEFAULT_ACCEPTED_CONTENT_TYPE); } return (req, res, next) => { const contentType = req.header('Content-Type'); if (is(contentType, acceptedContentTypes)) { next(); } else { res.status(415).end(); } }; } |