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", + ], }, }, },