Skip to content

Commit

Permalink
feat: Add "x-content-type" header with parsed data from "content-type…
Browse files Browse the repository at this point in the history
…" lib. Will make schema validation simple, not needing regexp anymore.
  • Loading branch information
andreidmt committed Feb 18, 2020
1 parent f6f8a9f commit bf64145
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/middleware/req-bootstrap.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/route-default.schema.js
Original file line number Diff line number Diff line change
@@ -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",
],
},
},
},
Expand Down

0 comments on commit bf64145

Please sign in to comment.