Skip to content

Commit

Permalink
optional pathParameterUsedAsPath option, fixes #182
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Oct 29, 2023
1 parent e1ea3c2 commit 7448918
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ $ npm i @fastify/aws-lambda
| decorationPropertyName | The default property name for request decoration | `awsLambda` |
| callbackWaitsForEmptyEventLoop | See: [Official Documentation](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html#nodejs-prog-model-context-properties) | `undefined` |
| retainStage | Retain the stage part of the API Gateway URL | `false` |
| pathParameterUsedAsPath | Use a defined pathParameter as path (i.e. `'proxy'`) | `false` |

## 📖Example

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = (app, options) => {
options.serializeLambdaArguments = options.serializeLambdaArguments !== undefined ? options.serializeLambdaArguments : false
options.decorateRequest = options.decorateRequest !== undefined ? options.decorateRequest : true
options.retainStage = options.retainStage !== undefined ? options.retainStage : false
options.proxiedPathParameterAsPath = options.proxiedPathParameterAsPath !== undefined ? options.proxiedPathParameterAsPath : false

This comment has been minimized.

Copy link
@hfhchan-plb

hfhchan-plb Oct 30, 2023

Should this be pathParameterUsedAsPath instead?

This comment has been minimized.

Copy link
@hfhchan-plb

hfhchan-plb Oct 30, 2023

Probably need to update LambdaFastifyOptions as well in types/index.d.ts.

This comment has been minimized.

Copy link
@adrai

adrai Oct 30, 2023

Author Member

v3.4.2

let currentAwsArguments = {}
if (options.decorateRequest) {
options.decorationPropertyName = options.decorationPropertyName || 'awsLambda'
Expand All @@ -39,8 +40,7 @@ module.exports = (app, options) => {
event.body = event.body || ''

const method = event.httpMethod || (event.requestContext && event.requestContext.http ? event.requestContext.http.method : undefined)
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path`
let url = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path || event.rawPath || '/' // seen rawPath for HTTP-API
let url = (options.pathParameterUsedAsPath && event.pathParameters && event.pathParameters[options.pathParameterUsedAsPath] && `/${event.pathParameters[options.pathParameterUsedAsPath]}`) || event.path || event.rawPath || '/' // seen rawPath for HTTP-API
// NOTE: if used directly via API Gateway domain and /stage
if (!options.retainStage && event.requestContext && event.requestContext.stage &&
event.requestContext.resourcePath && (url).indexOf(`/${event.requestContext.stage}/`) === 0 &&
Expand Down
105 changes: 103 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ test('proxy in pathParameters with http api', async (t) => {
proxy: 'projects'
}
}
app.get('/projects', async (request, reply) => {
app.get('/prod/projects', async (request, reply) => {
t.equal(request.headers['x-my-header'], 'wuuusaaa')
t.equal(request.headers['user-agent'], 'lightMyRequest')
t.equal(request.headers.host, 'localhost:80')
Expand All @@ -505,6 +505,51 @@ test('proxy in pathParameters with http api', async (t) => {
t.equal(ret.headers.connection, 'keep-alive')
})

test('proxy in pathParameters with http api (pathParameterUsedAsPath = "proxy")', async (t) => {
t.plan(13)

const app = fastify()
const evt = {
version: '2.0',
routeKey: 'GET /prod/{proxy+}',
rawPath: '/prod/projects',
rawQueryString: 't=1698604776681',
requestContext: {
http: {
method: 'GET',
path: '/prod/projects'
}
},
headers: {
'X-My-Header': 'wuuusaaa'
},
queryStringParameters: {
t: '1698604776681'
},
pathParameters: {
proxy: 'projects'
}
}
app.get('/projects', async (request, reply) => {
t.equal(request.headers['x-my-header'], 'wuuusaaa')
t.equal(request.headers['user-agent'], 'lightMyRequest')
t.equal(request.headers.host, 'localhost:80')
t.equal(request.headers['content-length'], '0')
t.equal(request.query.t, '1698604776681')
reply.send({ hello: 'world' })
})
const proxy = awsLambdaFastify(app, { pathParameterUsedAsPath: 'proxy' })
const ret = await proxy(evt)
t.equal(ret.statusCode, 200)
t.equal(ret.body, '{"hello":"world"}')
t.equal(ret.isBase64Encoded, false)
t.ok(ret.headers)
t.equal(ret.headers['content-type'], 'application/json; charset=utf-8')
t.equal(ret.headers['content-length'], '17')
t.ok(ret.headers.date)
t.equal(ret.headers.connection, 'keep-alive')
})

test('proxy in pathParameters with rest api', async (t) => {
t.plan(13)

Expand Down Expand Up @@ -541,7 +586,7 @@ test('proxy in pathParameters with rest api', async (t) => {
stage: 'dev'
}
}
app.get('/projects', async (request, reply) => {
app.get('/area/projects', async (request, reply) => {
t.equal(request.headers['x-my-header'], 'wuuusaaa')
t.equal(request.headers['user-agent'], 'lightMyRequest')
t.equal(request.headers.host, 'localhost:80')
Expand All @@ -560,3 +605,59 @@ test('proxy in pathParameters with rest api', async (t) => {
t.ok(ret.headers.date)
t.equal(ret.headers.connection, 'keep-alive')
})

test('proxy in pathParameters with rest api (pathParameterUsedAsPath = "proxy")', async (t) => {
t.plan(13)

const app = fastify()
const evt = {
resource: '/area/project',
path: '/area/projects',
rawPath: '/prod/projects',
httpMethod: 'GET',
headers: {
'X-My-Header': 'wuuusaaa'
},
multiValueHeaders: {
'X-My-Header': [
'wuuusaaa'
]
},
queryStringParameters: {
t: '1698604776681'
},
multiValueQueryStringParameters: {
t: [
'1698604776681'
]
},
pathParameters: {
proxy: 'projects'
},
stageVariables: null,
requestContext: {
resourcePath: '/area/{proxy+}',
httpMethod: 'GET',
path: '/dev/area/projects',
stage: 'dev'
}
}
app.get('/projects', async (request, reply) => {
t.equal(request.headers['x-my-header'], 'wuuusaaa')
t.equal(request.headers['user-agent'], 'lightMyRequest')
t.equal(request.headers.host, 'localhost:80')
t.equal(request.headers['content-length'], '0')
t.equal(request.query.t, '1698604776681')
reply.send({ hello: 'world' })
})
const proxy = awsLambdaFastify(app, { pathParameterUsedAsPath: 'proxy' })
const ret = await proxy(evt)
t.equal(ret.statusCode, 200)
t.equal(ret.body, '{"hello":"world"}')
t.equal(ret.isBase64Encoded, false)
t.ok(ret.headers)
t.equal(ret.headers['content-type'], 'application/json; charset=utf-8')
t.equal(ret.headers['content-length'], '17')
t.ok(ret.headers.date)
t.equal(ret.headers.connection, 'keep-alive')
})

0 comments on commit 7448918

Please sign in to comment.