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

Commit 98fa907

Browse files
authored
Merge pull request #37 from uniquelyparticular/fix/emptyResponse
release: tokenization>utils, +safeParse, fix _toJSON
2 parents 4af0778 + 74cf4e9 commit 98fa907

File tree

4 files changed

+78
-62
lines changed

4 files changed

+78
-62
lines changed

now.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"routes": [{ "src": "/(.*)", "dest": "/src" }],
1313
"builds": [
1414
{
15-
"src": "src/*.js",
15+
"src": "src/index.js",
1616
"use": "@now/node"
1717
}
1818
]

src/index.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ const { URL } = require('whatwg-url')
55
const UrlPattern = require('url-pattern')
66
const cors = require('micro-cors')()
77
const fetch = require('node-fetch')
8-
const { filterByPrefix, mustachReplace } = require('./utils/tokenization')
8+
const { filterByPrefix, mustachReplace, safeParse } = require('./utils')
99

10-
const _toJSON = error => {
11-
return !error
12-
? ''
13-
: Object.getOwnPropertyNames(error).reduce(
10+
const _toJSON = errorObj => {
11+
return !errorObj
12+
? {}
13+
: typeof errorObj === 'string'
14+
? errorObj
15+
: Object.getOwnPropertyNames(errorObj).reduce(
1416
(jsonError, key) => {
15-
return { ...jsonError, [key]: error[key] }
17+
return { ...jsonError, [key]: errorObj[key] }
1618
},
1719
{ type: 'error' }
1820
)
@@ -157,15 +159,15 @@ const handleResponse = response => {
157159
return text
158160
})
159161
.then((response = {}) => {
160-
const jsonResponse = JSON.parse(response)
162+
const jsonResponse = safeParse(response)
161163
// console.log('processRequest, jsonResponse', jsonResponse)
162164
return jsonResponse
163165
})
164166
}
165167

166168
const processRequest = (res, origin, url, options) => {
167-
// console.log('url', url)
168-
// console.log('options', options)
169+
// console.log('processRequest, url', url)
170+
// console.log('processRequest, options', options)
169171
return fetch(url, options)
170172
.then(response => {
171173
// console.log('processRequest, response', response)
@@ -192,14 +194,14 @@ const processRequest = (res, origin, url, options) => {
192194
return send(res, 200, data)
193195
})
194196
.catch(error => {
195-
const jsonError = _toJSON(error)
196-
return send(res, error.statusCode || 500, jsonError)
197+
console.error('processRequest, handleResponse, error', error)
198+
return send(res, error.statusCode || 500, _toJSON(error))
197199
})
198200
}
199201
})
200202
.catch(error => {
201-
const jsonError = _toJSON(error)
202-
return send(res, error.statusCode || 500, jsonError)
203+
console.error('processRequest, error', error)
204+
return send(res, error.statusCode || 500, _toJSON(error))
203205
})
204206
}
205207

@@ -211,19 +213,18 @@ const handleOptions = async (req, res) => {
211213
}
212214

213215
const handleProxy = async (req, res) => {
214-
// console.log('called proxy')
215-
// console.log('req.method', req.method)
216+
// console.log('handleProxy, req.method', req.method)
216217
if (req.method === 'OPTIONS') {
217218
return handleOptions(req, res)
218219
}
219220

220221
try {
221222
const path = req.url
222-
// console.log('path', path)
223-
// console.log('req.rawHeaders',req.rawHeaders)
224-
// console.log('req.headers.referer', req.headers.referer)
225-
// console.log('req.headers.origin', req.headers.origin)
226-
// console.log('req.headers',req.headers)
223+
// console.log('handleProxy,path', path)
224+
// console.log('handleProxy,req.rawHeaders',req.rawHeaders)
225+
// console.log('handleProxy,req.headers.referer', req.headers.referer)
226+
// console.log('handleProxy,req.headers.origin', req.headers.origin)
227+
// console.log('handleProxy,req.headers',req.headers)
227228
if (!req.headers.referer) {
228229
return noReferer(req, res)
229230
}
@@ -237,7 +238,7 @@ const handleProxy = async (req, res) => {
237238
return notAuthorized(req, res)
238239
}
239240

240-
// console.log('proxyPrefix', proxyPrefix)
241+
// console.log('handleProxy,proxyPrefix', proxyPrefix)
241242
const destinationURL = decodeURIComponent(
242243
decodeURIComponent(path.replace(`/${proxyPrefix}/`, ''))
243244
)
@@ -256,18 +257,18 @@ const handleProxy = async (req, res) => {
256257
req.headers['content-type'] === 'application/json'
257258
? JSON.stringify((await json(req)) || {})
258259
: await text(req)
259-
// console.log('txt', txt)
260+
// console.log('handleProxy,txt', txt)
260261

261262
if (body) {
262263
fetchOptions.body = body
263264
}
264-
// console.log('fetchOptions.body', fetchOptions.body)
265+
// console.log('handleProxy,fetchOptions.body', fetchOptions.body)
265266
}
266-
// console.log('fetchOptions', fetchOptions)
267+
// console.log('handleProxy,fetchOptions', fetchOptions)
267268
return processRequest(res, req.headers.origin, destinationURL, fetchOptions)
268269
} catch (error) {
269-
const jsonError = _toJSON(error)
270-
return send(res, error.statusCode || 500, jsonError)
270+
console.error('handleProxy, error', error)
271+
return send(res, error.statusCode || 500, _toJSON(error))
271272
}
272273
}
273274

src/utils.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const safeParse = object => {
2+
if (
3+
object &&
4+
typeof object === 'string' &&
5+
(object.startsWith('{') || object.startsWith('['))
6+
) {
7+
object = JSON.parse(object)
8+
}
9+
return object
10+
}
11+
12+
const filterByPrefix = (input, ...matchPrefixes) => {
13+
return Object.keys(input)
14+
.filter(key => {
15+
// return only objects within input who's keys start with any envPrefix
16+
let matched = false
17+
matchPrefixes.forEach(prefix => {
18+
matched = matched || key.startsWith(prefix)
19+
})
20+
return matched
21+
})
22+
.reduce((obj, rawKey) => {
23+
// remove any envPrefix that object startswith
24+
let key = rawKey
25+
matchPrefixes.forEach(prefix => {
26+
key = key.startsWith(prefix) ? key.replace(prefix, '') : key
27+
})
28+
obj[key] = input[rawKey]
29+
return obj
30+
}, {})
31+
}
32+
33+
const mustachReplace = (input, replacements, ...mustachPrefixes) => {
34+
// will search for input containing {{mustachePrefix.XYZ}}
35+
return input.replace(/{{((.*?)\.(.*?))}}/g, (match, ...groups) => {
36+
if (mustachPrefixes.includes(groups[1])) {
37+
return groups[2] !== 'undefined'
38+
? replacements[groups[2].toUpperCase()] || match
39+
: match
40+
} else {
41+
return match
42+
}
43+
})
44+
}
45+
46+
module.exports = {
47+
safeParse,
48+
filterByPrefix,
49+
mustachReplace
50+
}

src/utils/tokenization.js

-35
This file was deleted.

0 commit comments

Comments
 (0)