Skip to content

Commit 6926782

Browse files
committed
feat: use event.pathParameters.proxy if available ({proxy+}); fall back to event.path, fixes #182
1 parent eea33fa commit 6926782

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ module.exports = (app, options) => {
3939
event.body = event.body || ''
4040

4141
const method = event.httpMethod || (event.requestContext && event.requestContext.http ? event.requestContext.http.method : undefined)
42-
let url = event.path || event.rawPath || '/' // seen rawPath for HTTP-API
42+
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path`
43+
let url = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path || event.rawPath || '/' // seen rawPath for HTTP-API
4344
// NOTE: if used directly via API Gateway domain and /stage
4445
if (!options.retainStage && event.requestContext && event.requestContext.stage &&
4546
event.requestContext.resourcePath && (url).indexOf(`/${event.requestContext.stage}/`) === 0 &&

test/basic.test.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,104 @@ test('with existing onRequest hook', async (t) => {
459459
// t.same(ret.multiValueHeaders['set-cookie'], ['qwerty=one', 'qwerty=two'])
460460
t.same(ret.cookies, ['qwerty=one', 'qwerty=two'])
461461
})
462+
463+
test('proxy in pathParameters with http api', async (t) => {
464+
t.plan(13)
465+
466+
const app = fastify()
467+
const evt = {
468+
version: '2.0',
469+
routeKey: 'GET /prod/{proxy+}',
470+
rawPath: '/prod/projects',
471+
rawQueryString: 't=1698604776681',
472+
requestContext: {
473+
http: {
474+
method: 'GET',
475+
path: '/prod/projects'
476+
}
477+
},
478+
headers: {
479+
'X-My-Header': 'wuuusaaa'
480+
},
481+
queryStringParameters: {
482+
t: '1698604776681'
483+
},
484+
pathParameters: {
485+
proxy: 'projects'
486+
}
487+
}
488+
app.get('/projects', async (request, reply) => {
489+
t.equal(request.headers['x-my-header'], 'wuuusaaa')
490+
t.equal(request.headers['user-agent'], 'lightMyRequest')
491+
t.equal(request.headers.host, 'localhost:80')
492+
t.equal(request.headers['content-length'], '0')
493+
t.equal(request.query.t, '1698604776681')
494+
reply.send({ hello: 'world' })
495+
})
496+
const proxy = awsLambdaFastify(app)
497+
const ret = await proxy(evt)
498+
t.equal(ret.statusCode, 200)
499+
t.equal(ret.body, '{"hello":"world"}')
500+
t.equal(ret.isBase64Encoded, false)
501+
t.ok(ret.headers)
502+
t.equal(ret.headers['content-type'], 'application/json; charset=utf-8')
503+
t.equal(ret.headers['content-length'], '17')
504+
t.ok(ret.headers.date)
505+
t.equal(ret.headers.connection, 'keep-alive')
506+
})
507+
508+
test('proxy in pathParameters with rest api', async (t) => {
509+
t.plan(13)
510+
511+
const app = fastify()
512+
const evt = {
513+
resource: '/area/project',
514+
path: '/area/projects',
515+
rawPath: '/prod/projects',
516+
httpMethod: 'GET',
517+
headers: {
518+
'X-My-Header': 'wuuusaaa'
519+
},
520+
multiValueHeaders: {
521+
'X-My-Header': [
522+
'wuuusaaa'
523+
]
524+
},
525+
queryStringParameters: {
526+
t: '1698604776681'
527+
},
528+
multiValueQueryStringParameters: {
529+
t: [
530+
'1698604776681'
531+
]
532+
},
533+
pathParameters: {
534+
proxy: 'projects'
535+
},
536+
stageVariables: null,
537+
requestContext: {
538+
resourcePath: '/area/{proxy+}',
539+
httpMethod: 'GET',
540+
path: '/dev/area/projects',
541+
stage: 'dev'
542+
}
543+
}
544+
app.get('/projects', async (request, reply) => {
545+
t.equal(request.headers['x-my-header'], 'wuuusaaa')
546+
t.equal(request.headers['user-agent'], 'lightMyRequest')
547+
t.equal(request.headers.host, 'localhost:80')
548+
t.equal(request.headers['content-length'], '0')
549+
t.equal(request.query.t, '1698604776681')
550+
reply.send({ hello: 'world' })
551+
})
552+
const proxy = awsLambdaFastify(app)
553+
const ret = await proxy(evt)
554+
t.equal(ret.statusCode, 200)
555+
t.equal(ret.body, '{"hello":"world"}')
556+
t.equal(ret.isBase64Encoded, false)
557+
t.ok(ret.headers)
558+
t.equal(ret.headers['content-type'], 'application/json; charset=utf-8')
559+
t.equal(ret.headers['content-length'], '17')
560+
t.ok(ret.headers.date)
561+
t.equal(ret.headers.connection, 'keep-alive')
562+
})

0 commit comments

Comments
 (0)