Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 52e1e7f

Browse files
authored
Merge pull request #22 from uniquelyparticular/fix/postjsonbody
fix: ensure POST w application/json header > body contains JSON string
2 parents 101f371 + 0a2d753 commit 52e1e7f

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/index.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { name, version } = require('../package.json')
2-
const { text, send } = require('micro')
2+
const { text, json, send } = require('micro')
33
const { router, del, get, options, patch, post, put } = require('microrouter')
44
const { URL } = require('whatwg-url')
55
const UrlPattern = require('url-pattern')
@@ -111,8 +111,9 @@ const requestHeaders = headers => {
111111
}, {})
112112

113113
const defaultHeaders = {
114+
origin: getOrigin(origin, referer),
114115
'x-forwarded-by': `${name}-${version}`,
115-
'x-forwarded-origin': getOrigin(origin, referer),
116+
'x-forwarded-origin': origin,
116117
'x-forwarded-referer': referer
117118
}
118119

@@ -148,17 +149,17 @@ const allowHeaders = headers => {
148149
return allowedHeaders
149150
}
150151

151-
const json = response => {
152+
const handleResponse = response => {
152153
return response
153154
.text()
154155
.then(text => {
155156
// console.log('processRequest, text', text)
156157
return text
157158
})
158159
.then((response = {}) => {
159-
const json = JSON.parse(response)
160-
// console.log('processRequest, json', json)
161-
return json
160+
const jsonResponse = JSON.parse(response)
161+
// console.log('processRequest, jsonResponse', jsonResponse)
162+
return jsonResponse
162163
})
163164
}
164165

@@ -181,7 +182,7 @@ const processRequest = (res, origin, url, options) => {
181182
}
182183
return send(res, response.status || 500, errorResponse)
183184
} else {
184-
return json(response)
185+
return handleResponse(response)
185186
.then(data => {
186187
// console.log('processRequest, data', data)
187188
if (origin) {
@@ -251,23 +252,16 @@ const handleProxy = async (req, res) => {
251252
}
252253

253254
if (req.method !== 'GET') {
254-
const txt = await text(req)
255+
const body =
256+
req.headers['content-type'] === 'application/json'
257+
? JSON.stringify((await json(req)) || {})
258+
: await text(req)
255259
// console.log('txt', txt)
256-
if (txt && txt !== '') {
257-
let body
258260

259-
if (req.headers['content-type'] === 'application/json') {
260-
body = JSON.parse(txt)
261-
} else {
262-
body = txt
263-
}
264-
265-
// console.log('body', body)
266-
if (body) {
267-
fetchOptions.body = body
268-
}
269-
// console.log('fetchOptions.body', fetchOptions.body)
261+
if (body) {
262+
fetchOptions.body = body
270263
}
264+
// console.log('fetchOptions.body', fetchOptions.body)
271265
}
272266
// console.log('fetchOptions', fetchOptions)
273267
return processRequest(res, req.headers.origin, destinationURL, fetchOptions)

0 commit comments

Comments
 (0)