From bf641451f52d39d9c649c742318ba0a77ffe762f Mon Sep 17 00:00:00 2001 From: Andrei Dumitrescu <5057797+andreidcm@users.noreply.github.com> Date: Tue, 18 Feb 2020 12:14:25 +0100 Subject: [PATCH] feat: Add "x-content-type" header with parsed data from "content-type" lib. Will make schema validation simple, not needing regexp anymore. --- src/middleware/req-bootstrap.js | 12 ++++++++++++ src/plugins/route-default.schema.js | 10 +++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/middleware/req-bootstrap.js b/src/middleware/req-bootstrap.js index 6151c99..1c63aea 100644 --- a/src/middleware/req-bootstrap.js +++ b/src/middleware/req-bootstrap.js @@ -1,14 +1,26 @@ const debug = require("debug")("Blocks:BootstrapMiddleware") import cuid from "cuid" +import contentType from "content-type" import { pick } from "@mutantlove/m" module.exports = () => (req, res, next) => { req.ctx = { id: cuid(), startAt: process.hrtime(), + body: {}, ...pick(["query", "pathname"])(req._parsedUrl), } + + try { + req.headers["x-content-type"] = contentType.parse( + req.headers["content-type"] + ).type + } catch (error) { + // do nothing, let JSON schemas do the validation + req.headers["x-content-type"] = req.headers["content-type"] + } + res.ctx = {} next() diff --git a/src/plugins/route-default.schema.js b/src/plugins/route-default.schema.js index 799793a..1050c9c 100644 --- a/src/plugins/route-default.schema.js +++ b/src/plugins/route-default.schema.js @@ -1,10 +1,14 @@ module.exports = { headers: { type: "object", - required: ["content-type"], + required: ["x-content-type"], properties: { - "content-type": { - enum: ["application/json; charset=UTF-8", "application/json"], + "x-content-type": { + enum: [ + "application/json", + "application/x-www-form-urlencoded", + "multipart/form-data", + ], }, }, },