Skip to content

Commit 7614a08

Browse files
Rolando Santamaria MasoRolando Santamaria Maso
authored andcommitted
supporting req.query object forwarding to origin
1 parent a1e5df4 commit 7614a08

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
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)

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: 26 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,12 @@ 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+
})
61+
remote.get('/qs2', (req, res) => {
62+
res.send(req.query)
63+
})
5864

5965
await remote.start(3000)
6066
})
@@ -178,7 +184,7 @@ describe('API Gateway', () => {
178184
.get('/users/info')
179185
.expect(200)
180186
.then((response) => {
181-
expect(response.body.name).to.equal('fastify-gateway')
187+
expect(response.body.name).to.equal('fast-gateway')
182188
})
183189
})
184190

@@ -305,6 +311,24 @@ describe('API Gateway', () => {
305311
})
306312
})
307313

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

0 commit comments

Comments
 (0)