-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrest.js
More file actions
37 lines (34 loc) · 969 Bytes
/
rest.js
File metadata and controls
37 lines (34 loc) · 969 Bytes
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
29
30
31
32
33
34
35
36
37
const acceptedJsonContentTypes = new Set([
"application/json",
"application/ld+json"
])
export function httpError(message, status = 500, payload) {
const error = new Error(message)
error.status = status
if (payload !== undefined) {
error.payload = payload
}
return error
}
function getContentType(req) {
const rawContentType = req.headers?.["content-type"]
if (!rawContentType) {
return ""
}
if (rawContentType.includes(",")) {
return "multiple"
}
return rawContentType.split(";")[0].trim().toLowerCase()
}
export function verifyJsonContentType(req, res, next) {
const contentType = getContentType(req)
if (contentType === "multiple") {
res.status(415).send("Unsupported Media Type. Multiple Content-Type values are not allowed.")
return
}
if (acceptedJsonContentTypes.has(contentType)) {
next()
return
}
res.status(415).send("Unsupported Media Type. Use application/json or application/ld+json.")
}