Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ $ npm i @fastify/aws-lambda

**@fastify/aws-lambda** can take options by passing them with : `awsLambdaFastify(app, options)`

| property | description | default value |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ | ------------- |
| binaryMimeTypes | Array of binary MimeTypes to handle | `[]` |
| enforceBase64 | Function that receives the response and returns a boolean indicating if the response content is binary or not and should be base64-encoded | `undefined` |
| serializeLambdaArguments | Activate the serialization of lambda Event and Context in http header `x-apigateway-event` `x-apigateway-context` | `false` *(was `true` for <v2.0.0)* |
| decorateRequest | Decorates the fastify request with the lambda Event and Context `request.awsLambda.event` `request.awsLambda.context` | `true` |
| 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` |
| property | description | default value |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------- |
| binaryMimeTypes | Array of binary MimeTypes to handle | `[]` |
| enforceBase64 | Function that receives the response and returns a boolean indicating if the response content is binary or not and should be base64-encoded | `undefined` |
| serializeLambdaArguments | Activate the serialization of lambda Event and Context in http header `x-apigateway-event` `x-apigateway-context` | `false` *(was `true` for <v2.0.0)* |
| decorateRequest | Decorates the fastify request with the lambda Event and Context `request.awsLambda.event` `request.awsLambda.context` | `true` |
| 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` |

## 📖Example

Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = (app, options) => {
options.binaryMimeTypes = options.binaryMimeTypes || []
options.serializeLambdaArguments = options.serializeLambdaArguments !== undefined ? options.serializeLambdaArguments : false
options.decorateRequest = options.decorateRequest !== undefined ? options.decorateRequest : true
options.retainStage = options.retainStage !== undefined ? options.retainStage : false
let currentAwsArguments = {}
if (options.decorateRequest) {
options.decorationPropertyName = options.decorationPropertyName || 'awsLambda'
Expand All @@ -40,8 +41,8 @@ module.exports = (app, options) => {
const method = event.httpMethod || (event.requestContext && event.requestContext.http ? event.requestContext.http.method : undefined)
let url = event.path || event.rawPath || '/' // seen rawPath for HTTP-API
// NOTE: if used directly via API Gateway domain and /stage
if (event.requestContext && event.requestContext.stage && event.requestContext.resourcePath &&
(url).indexOf(`/${event.requestContext.stage}/`) === 0 &&
if (!options.retainStage && event.requestContext && event.requestContext.stage &&
event.requestContext.resourcePath && (url).indexOf(`/${event.requestContext.stage}/`) === 0 &&
event.requestContext.resourcePath.indexOf(`/${event.requestContext.stage}/`) !== 0) {
url = url.substring(event.requestContext.stage.length + 1)
}
Expand Down
30 changes: 30 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,36 @@ test('subpath', async (t) => {
t.same(ret.multiValueHeaders['set-cookie'], ['qwerty=one', 'qwerty=two'])
})

test('subpath retain stage', async (t) => {
t.plan(7)

const app = fastify()
app.get('/dev/test', async (request, reply) => {
t.equal(request.headers['x-my-header'], 'wuuusaaa')
t.equal(request.headers['x-apigateway-event'], '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Fdev%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%2C%22requestContext%22%3A%7B%22resourcePath%22%3A%22%2Ftest%22%2C%22stage%22%3A%22dev%22%7D%7D')
t.equal(request.headers['user-agent'], 'lightMyRequest')
t.equal(request.headers.host, 'localhost:80')
t.equal(request.headers['content-length'], '0')
reply.header('Set-Cookie', 'qwerty=one')
reply.header('Set-Cookie', 'qwerty=two')
reply.send({ hello: 'world' })
})
const proxy = awsLambdaFastify(app, { retainStage: true, serializeLambdaArguments: true })
const ret = await proxy({
httpMethod: 'GET',
path: '/dev/test',
headers: {
'X-My-Header': 'wuuusaaa'
},
requestContext: {
resourcePath: '/test',
stage: 'dev'
}
})
t.equal(ret.statusCode, 200)
t.equal(ret.body, '{"hello":"world"}')
})

test('serializeLambdaArguments = false', async (t) => {
t.plan(14)

Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare namespace awsLambdaFastify {
decorateRequest?: boolean;
decorationPropertyName?: string;
enforceBase64?: (response: LightMyRequestResponse) => boolean;
retainStage?: boolean;
}

export interface LambdaResponse {
Expand Down
12 changes: 12 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ expectAssignable<LambdaFastifyOptions>({
return false;
},
});
expectAssignable<LambdaFastifyOptions>({
binaryMimeTypes: ["foo", "bar"],
callbackWaitsForEmptyEventLoop: true,
serializeLambdaArguments: false,
decorateRequest: true,
decorationPropertyName: "myAWSstuff",
enforceBase64: (response) => {
expectType<LightMyRequestResponse>(response);
return false;
},
retainStage: true,
});

expectError(awsLambdaFastify());
expectError(awsLambdaFastify(app, { neh: "definition" }));