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

Commit

Permalink
Merge pull request #22 from uniquelyparticular/fix/postjsonbody
Browse files Browse the repository at this point in the history
fix: ensure POST w application/json header > body contains JSON string
  • Loading branch information
agrohs committed Jun 3, 2019
2 parents 101f371 + 0a2d753 commit 52e1e7f
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { name, version } = require('../package.json')
const { text, send } = require('micro')
const { text, json, send } = require('micro')
const { router, del, get, options, patch, post, put } = require('microrouter')
const { URL } = require('whatwg-url')
const UrlPattern = require('url-pattern')
Expand Down Expand Up @@ -111,8 +111,9 @@ const requestHeaders = headers => {
}, {})

const defaultHeaders = {
origin: getOrigin(origin, referer),
'x-forwarded-by': `${name}-${version}`,
'x-forwarded-origin': getOrigin(origin, referer),
'x-forwarded-origin': origin,
'x-forwarded-referer': referer
}

Expand Down Expand Up @@ -148,17 +149,17 @@ const allowHeaders = headers => {
return allowedHeaders
}

const json = response => {
const handleResponse = response => {
return response
.text()
.then(text => {
// console.log('processRequest, text', text)
return text
})
.then((response = {}) => {
const json = JSON.parse(response)
// console.log('processRequest, json', json)
return json
const jsonResponse = JSON.parse(response)
// console.log('processRequest, jsonResponse', jsonResponse)
return jsonResponse
})
}

Expand All @@ -181,7 +182,7 @@ const processRequest = (res, origin, url, options) => {
}
return send(res, response.status || 500, errorResponse)
} else {
return json(response)
return handleResponse(response)
.then(data => {
// console.log('processRequest, data', data)
if (origin) {
Expand Down Expand Up @@ -251,23 +252,16 @@ const handleProxy = async (req, res) => {
}

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

if (req.headers['content-type'] === 'application/json') {
body = JSON.parse(txt)
} else {
body = txt
}

// console.log('body', body)
if (body) {
fetchOptions.body = body
}
// console.log('fetchOptions.body', fetchOptions.body)
if (body) {
fetchOptions.body = body
}
// console.log('fetchOptions.body', fetchOptions.body)
}
// console.log('fetchOptions', fetchOptions)
return processRequest(res, req.headers.origin, destinationURL, fetchOptions)
Expand Down

1 comment on commit 52e1e7f

@vercel
Copy link

@vercel vercel bot commented on 52e1e7f Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.