Skip to content

Commit a4cc4de

Browse files
committed
fixed not overriding content-type, if set to anything other that text/plain
(since that is done automatically by the browser)
1 parent ba736d0 commit a4cc4de

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

src/mw/json.mjs

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,69 @@
1-
import * as metro from '../metro.mjs'
1+
export default function jsonmw(options)
2+
{
3+
options = Object.assign({
4+
contentType: 'application/json',
5+
reviver: null,
6+
replacer: null,
7+
space: ''
8+
}, options)
29

3-
export default function jsonmw(options) {
4-
options = Object.assign({
5-
mimetype: 'application/json',
6-
reviver: null,
7-
replacer: null,
8-
space: ''
9-
}, options)
10-
11-
return async function json(req, next) {
12-
if (!isJSON(req.headers.get('Accept'))) {
13-
req = req.with({
14-
headers: {
15-
'Accept': options.mimetype
16-
}
17-
})
18-
}
19-
if (['POST','PUT','PATCH','QUERY'].includes(req.method)) {
20-
if (req.data && typeof req.data=='object' && !(req.data instanceof ReadableStream)) {
21-
if (!isJSON(req.headers.get('content-type'))) {
22-
req = req.with({
23-
headers: {
24-
'Content-Type':options.mimetype,
25-
}
26-
})
27-
}
28-
req = req.with({
29-
body: JSON.stringify(req.data, options.replacer, options.space)
30-
})
31-
}
32-
}
33-
let res = await next(req)
34-
if (isJSON(res.headers.get('content-type'))) {
35-
let tempRes = res.clone()
36-
let body = await tempRes.text()
37-
try {
38-
let json = JSON.parse(body, options.reviver)
39-
return res.with({
40-
body: json
41-
})
42-
} catch(e) {
43-
// ignore parse errors
44-
}
45-
}
46-
return res
47-
}
10+
return async function json(req, next) {
11+
if (!req.headers.get('Accept')) {
12+
req = req.with({
13+
headers: {
14+
'Accept': options.accept ?? options.contentType
15+
}
16+
})
17+
}
18+
if (req.method!=='GET' && req.method!=='HEAD') {
19+
//https://developer.mozilla.org/en-US/docs/Web/API/Request/body
20+
if (req.data && typeof req.data=='object' && !(req.data instanceof ReadableStream)) {
21+
const contentType = req.headers.get('Content-Type')
22+
if (!contentType || isPlainText(contentType)) {
23+
req = req.with({
24+
headers: {
25+
'Content-Type':options.contentType,
26+
}
27+
})
28+
}
29+
if (isJSON(req.headers.get('Content-Type'))) {
30+
req = req.with({
31+
body: JSON.stringify(req.data, options.replacer, options.space)
32+
})
33+
}
34+
}
35+
}
36+
let res = await next(req)
37+
if (isJSON(res.headers.get('Content-Type'))) {
38+
let tempRes = res.clone()
39+
let body = await tempRes.text()
40+
try {
41+
let json = JSON.parse(body, options.reviver)
42+
return res.with({
43+
body: json
44+
})
45+
} catch(e) {
46+
// ignore parse errors
47+
}
48+
}
49+
return res
50+
}
4851
}
4952

53+
/*
54+
this matches:
55+
- application/json
56+
- application/ld+json
57+
- application/json; charset=utf-8
58+
- application/ld+json; charset=utf-8
59+
*/
5060
const jsonRE = /^application\/([a-zA-Z0-9\-_]+\+)?json\b/
51-
function isJSON(contentType) {
52-
return jsonRE.exec(contentType)
61+
function isJSON(contentType)
62+
{
63+
return jsonRE.exec(contentType)
64+
}
65+
66+
function isPlainText(contentType)
67+
{
68+
return /^text\/plain\b/.exec(contentType)
5369
}

0 commit comments

Comments
 (0)