1
1
const { name, version } = require ( '../package.json' )
2
- const { text, send } = require ( 'micro' )
2
+ const { text, json , send } = require ( 'micro' )
3
3
const { router, del, get, options, patch, post, put } = require ( 'microrouter' )
4
4
const { URL } = require ( 'whatwg-url' )
5
5
const UrlPattern = require ( 'url-pattern' )
@@ -111,8 +111,9 @@ const requestHeaders = headers => {
111
111
} , { } )
112
112
113
113
const defaultHeaders = {
114
+ origin : getOrigin ( origin , referer ) ,
114
115
'x-forwarded-by' : `${ name } -${ version } ` ,
115
- 'x-forwarded-origin' : getOrigin ( origin , referer ) ,
116
+ 'x-forwarded-origin' : origin ,
116
117
'x-forwarded-referer' : referer
117
118
}
118
119
@@ -148,17 +149,17 @@ const allowHeaders = headers => {
148
149
return allowedHeaders
149
150
}
150
151
151
- const json = response => {
152
+ const handleResponse = response => {
152
153
return response
153
154
. text ( )
154
155
. then ( text => {
155
156
// console.log('processRequest, text', text)
156
157
return text
157
158
} )
158
159
. 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
162
163
} )
163
164
}
164
165
@@ -181,7 +182,7 @@ const processRequest = (res, origin, url, options) => {
181
182
}
182
183
return send ( res , response . status || 500 , errorResponse )
183
184
} else {
184
- return json ( response )
185
+ return handleResponse ( response )
185
186
. then ( data => {
186
187
// console.log('processRequest, data', data)
187
188
if ( origin ) {
@@ -251,23 +252,16 @@ const handleProxy = async (req, res) => {
251
252
}
252
253
253
254
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 )
255
259
// console.log('txt', txt)
256
- if ( txt && txt !== '' ) {
257
- let body
258
260
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
270
263
}
264
+ // console.log('fetchOptions.body', fetchOptions.body)
271
265
}
272
266
// console.log('fetchOptions', fetchOptions)
273
267
return processRequest ( res , req . headers . origin , destinationURL , fetchOptions )
0 commit comments