Skip to content

Commit 2220802

Browse files
authored
Merge pull request #48 from jkyberneees/support-req-query-forwarding
Support req query forwarding
2 parents e2a63bc + be33922 commit 2220802

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ module.exports.handler = serverless(service)
155155
// // we can optionally reply from here if required
156156
// res.end('Hello World!')
157157
//
158+
// // we can optionally update the request query params from here if required
159+
// req.query.category = 'js'
160+
//
158161
// return true // truthy value returned will abort the request forwarding
159162
},
160163
onResponse (req, res, stream) {

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ const handler = (route, proxy, proxyHandler) => async (req, res, next) => {
9797
const proxyOpts = Object.assign({
9898
request: {
9999
timeout: req.timeout || route.timeout
100-
}
100+
},
101+
queryString: req.query
101102
}, route.hooks)
102103

103104
proxyHandler(req, res, req.url, proxy, proxyOpts)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@polka/send-type": "^0.5.2",
3131
"fast-proxy": "^1.7.0",
3232
"http-cache-middleware": "^1.3.5",
33-
"restana": "^4.7.1",
33+
"restana": "^4.7.3",
3434
"stream-to-array": "^2.3.0"
3535
},
3636
"files": [
@@ -41,7 +41,7 @@
4141
"LICENSE"
4242
],
4343
"devDependencies": {
44-
"aws-sdk": "^2.725.0",
44+
"aws-sdk": "^2.742.0",
4545
"chai": "^4.2.0",
4646
"cors": "^2.8.5",
4747
"express": "^4.17.1",
@@ -50,7 +50,7 @@
5050
"fg-multiple-hooks": "1.2.0",
5151
"helmet": "^3.23.3",
5252
"http-lambda-proxy": "^1.0.1",
53-
"mocha": "^8.1.0",
53+
"mocha": "^8.1.3",
5454
"nyc": "^15.1.0",
5555
"opossum": "^4.2.4",
5656
"request-ip": "^2.1.3",

test/config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ module.exports = async () => {
7373
target: 'http://localhost:3000',
7474
methods: ['GET', 'POST']
7575
},
76+
{
77+
pathRegex: '',
78+
prefix: '/qs',
79+
prefixRewrite: '/qs',
80+
target: 'http://localhost:3000',
81+
methods: ['GET'],
82+
hooks: {
83+
onRequest: (req) => {
84+
req.query.name = 'fast-gateway'
85+
}
86+
}
87+
},
88+
{
89+
pathRegex: '',
90+
prefix: '/qs2',
91+
prefixRewrite: '/qs',
92+
target: 'http://localhost:3000',
93+
methods: ['GET'],
94+
hooks: {
95+
queryString: {
96+
name: 'qs-overwrite'
97+
}
98+
}
99+
},
76100
{
77101
pathRegex: '',
78102
prefix: '/endpoint-proxy-methods-put',

test/smoke.test.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('API Gateway', () => {
1919
name: 'endpoint-proxy'
2020
}))
2121
remote.get('/info', (req, res) => res.send({
22-
name: 'fastify-gateway'
22+
name: 'fast-gateway'
2323
}))
2424
remote.get('/chunked', (req, res) => {
2525
res.write('user')
@@ -55,6 +55,9 @@ describe('API Gateway', () => {
5555
remote.post('/endpoint-proxy-methods', (req, res) => res.send({
5656
name: 'endpoint-proxy-methods'
5757
}))
58+
remote.get('/qs', (req, res) => {
59+
res.send(req.query)
60+
})
5861

5962
await remote.start(3000)
6063
})
@@ -178,7 +181,7 @@ describe('API Gateway', () => {
178181
.get('/users/info')
179182
.expect(200)
180183
.then((response) => {
181-
expect(response.body.name).to.equal('fastify-gateway')
184+
expect(response.body.name).to.equal('fast-gateway')
182185
})
183186
})
184187

@@ -305,6 +308,24 @@ describe('API Gateway', () => {
305308
})
306309
})
307310

311+
it('GET /qs - 200', async () => {
312+
await request(gateway)
313+
.get('/qs')
314+
.expect(200)
315+
.then((response) => {
316+
expect(response.body.name).to.equal('fast-gateway')
317+
})
318+
})
319+
320+
it('GET /qs2 - 200', async () => {
321+
await request(gateway)
322+
.get('/qs2?name=fast-gateway')
323+
.expect(200)
324+
.then((response) => {
325+
expect(response.body.name).to.equal('qs-overwrite')
326+
})
327+
})
328+
308329
it('GET /lambda/hi', async () => {
309330
await request(gateway)
310331
.get('/lambda/hi')

0 commit comments

Comments
 (0)