Skip to content

Commit fe99c85

Browse files
committed
feat: add custom mapping example
* Request mapping functions must now return the body * Renamed CONTEXT_SUCCEED to CONTEXT * socketPath no longer needs to be returned by request mapping function
1 parent a9c01a3 commit fe99c85

File tree

22 files changed

+2631
-52
lines changed

22 files changed

+2631
-52
lines changed

README.md

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ npm install aws-serverless-express
1313

1414
```js
1515
// lambda.js
16-
const awsServerlessExpress = require('aws-serverless-express')
16+
const { configure } = require('aws-serverless-express')
1717
const app = require('./app')
18-
const servererlessExpress = awsServerlessExpress.configure({
19-
})
20-
21-
exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }
18+
const servererlessExpress = configure({ app })
19+
exports.handler = servererlessExpress.handler
2220
```
2321

24-
[Package and create your Lambda function](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html), then configure a simple proxy API using Amazon API Gateway and integrate it with your Lambda function.
22+
[Package and create your Lambda function](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html), then configure a simple proxy API using Amazon API Gateway and integrate it with your Lambda function. See the [basic starter example](examples/basic-starter) to get started.
2523

2624
## Quick Start/Example
2725

@@ -32,7 +30,60 @@ Want to get up and running quickly? [Check out our basic starter example](exampl
3230
- [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model)/[CloudFormation](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) template
3331
- Helper scripts to configure, deploy, and manage your application
3432

35-
## Accessing the event and context objects
33+
## API
34+
35+
## resolutionMode
36+
37+
Lambda supports three methods to end the execution and return a result: context, callback, and promise. By default, aws-serverless-express uses promise resolution, but you can specify 'CONTEXT' or 'CALLBACK' ('PROMISE' by default) if you need to change this. If you specify 'CALLBACK', then `context.callbackWaitsForEmptyEventLoop = false` is also set for you.
38+
39+
```js
40+
configure({
41+
app,
42+
resolutionMode: 'CALLBACK'
43+
})
44+
```
45+
46+
## eventFns
47+
48+
aws-serverless-express natively supports API Gateway, ALB, and Lambda@Edge. If you want to use Express with other AWS Services which can invoke a Lambda Function you can provide your own custom request/response mappings via `eventFns`. See the [custom-mapper-dynamodb example](examples/custom-mapper-dynamodb).
49+
50+
```js
51+
function requestMapper ({ event }) {
52+
// Your logic here...
53+
54+
return {
55+
method,
56+
path,
57+
headers
58+
}
59+
}
60+
61+
function responseMapper ({
62+
statusCode,
63+
body,
64+
headers,
65+
isBase64Encoded
66+
}) {
67+
// Your logic here...
68+
69+
return {
70+
statusCode,
71+
body,
72+
headers,
73+
isBase64Encoded
74+
}
75+
}
76+
77+
configure({
78+
app,
79+
eventFns: {
80+
request: requestMapper,
81+
response: responseMapper
82+
}
83+
})
84+
```
85+
86+
### Accessing the event and context objects
3687

3788
This package exposes a function to easily get the `event` and `context` objects Lambda receives from the event source.
3889

__tests__/unit.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,35 @@ function mapApiGatewayEventToHttpRequest (multiValueHeaders = {}) {
7575
const context = {
7676
'foo': 'bar'
7777
}
78-
const socketPath = '/tmp/server0.sock'
79-
const httpRequest = awsServerlessExpressEventMappings.mapApiGatewayEventToHttpRequest({ event, context, socketPath })
78+
const httpRequest = awsServerlessExpressEventMappings.mapApiGatewayEventToHttpRequest({ event, context })
8079

8180
return {httpRequest, eventClone, context}
8281
}
8382

8483
test('mapApiGatewayEventToHttpRequest: with headers', () => {
8584
const r = mapApiGatewayEventToHttpRequest({'x-foo': ['foo']})
86-
85+
expect(r.httpRequest.body).toBeInstanceOf(Buffer)
86+
delete r.httpRequest.body
8787
expect(r.httpRequest).toEqual({
8888
method: 'GET',
8989
path: '/foo',
9090
headers: {
9191
'x-foo': 'foo',
9292
'Content-Length': Buffer.byteLength('Hello serverless!')
93-
},
94-
socketPath: '/tmp/server0.sock'
93+
}
9594
})
9695
})
9796

9897
test('mapApiGatewayEventToHttpRequest: without headers', () => {
9998
const r = mapApiGatewayEventToHttpRequest()
100-
99+
expect(r.httpRequest.body).toBeInstanceOf(Buffer)
100+
delete r.httpRequest.body
101101
expect(r.httpRequest).toEqual({
102102
method: 'GET',
103103
path: '/foo',
104104
headers: {
105105
'Content-Length': Buffer.byteLength('Hello serverless!')
106-
},
107-
socketPath: '/tmp/server0.sock'
106+
}
108107
})
109108
})
110109

@@ -376,13 +375,13 @@ describe('forwardResponse: content-type encoding', () => {
376375
})
377376

378377
describe('makeResolver', () => {
379-
test('CONTEXT_SUCCEED (specified)', () => {
378+
test('CONTEXT (specified)', () => {
380379
return new Promise(
381380
(resolve) => {
382381
const context = new MockContext(resolve)
383382
const contextResolver = awsServerlessExpressTransport.makeResolver({
384383
context,
385-
resolutionMode: 'CONTEXT_SUCCEED'
384+
resolutionMode: 'CONTEXT'
386385
})
387386

388387
return contextResolver.succeed({

examples/alb/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Development
22

3-
1. `npm pack ../..`
4-
2. `npm install ./aws-serverless-express-3.3.5.tgz`
5-
3. `npm install --prefix ./src ./`
6-
4. `node local.js`
3+
```bash
4+
npm pack ../..
5+
npm install ./aws-serverless-express-3.3.5.tgz
6+
npm install --prefix ./src ./
7+
node local.js
8+
```

examples/alb/src/lambda.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
// const awsServerlessExpress = require(process.env.NODE_ENV === 'test' ? '../../index' : 'aws-serverless-express')
21
const awsServerlessExpress = require('aws-serverless-express')
32
const app = require('./app')
43

54
const ase = awsServerlessExpress.configure({
6-
app,
7-
resolutionMode: 'PROMISE'
8-
// eventSource: ''
5+
app
96
})
107

118
exports.handler = ase.handler

examples/basic-starter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"config": {
77
"s3BucketName": "sls-xpress2",
88
"region": "us-east-1",
9-
"cloudFormationStackName": "AwsServerlessExpressStackNew",
9+
"cloudFormationStackName": "ServerlessExpress",
1010
"functionName": "AwsServerlessExpressStackNew-ExpressLambdaFunction-GU4XXGPADJ0E"
1111
},
1212
"scripts": {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
template.packaged.yaml
2+
*.tgz
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Development
2+
3+
```
4+
npm pack ../..
5+
npm install ./aws-serverless-express-3.3.5.tgz
6+
npm install --prefix ./src ./
7+
node local.js
8+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"Records": [
3+
{
4+
"eventID": "93eea905b34daefbe0fe8a21858065a8",
5+
"eventName": "INSERT",
6+
"eventVersion": "1.1",
7+
"eventSource": "aws:dynamodb",
8+
"awsRegion": "us-east-1",
9+
"dynamodb": {
10+
"ApproximateCreationDateTime": 1560902007,
11+
"Keys": {
12+
"id": {
13+
"S": "b"
14+
}
15+
},
16+
"NewImage": {
17+
"id": {
18+
"S": "b"
19+
}
20+
},
21+
"SequenceNumber": "200000000000235577791",
22+
"SizeBytes": 6,
23+
"StreamViewType": "NEW_IMAGE"
24+
},
25+
"eventSourceARN": "arn:aws:dynamodb:us-east-1:123123123:table/aws-serverless-express-dynamodb-example-Table-191RH2M1OU01V/stream/2019-06-18T23:42:52.038"
26+
}
27+
]
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const lambdaFunction = require('./src/lambda.js')
2+
const albEvent = require('./dynamodb-event.json')
3+
4+
const server = lambdaFunction.handler(albEvent, {})
5+
.then(v => {
6+
console.info(v)
7+
process.exit(0)
8+
})
9+
.catch(v => {
10+
console.error(v)
11+
process.exit(1)
12+
})
13+
14+
process.stdin.resume()
15+
16+
function exitHandler (options, err) {
17+
if (options.cleanup && server && server.close) {
18+
server.close()
19+
}
20+
21+
if (err) console.error(err.stack)
22+
if (options.exit) process.exit()
23+
}
24+
25+
process.on('exit', exitHandler.bind(null, { cleanup: true }))
26+
process.on('SIGINT', exitHandler.bind(null, { exit: true })) // ctrl+c event
27+
process.on('SIGTSTP', exitHandler.bind(null, { exit: true })) // ctrl+v event
28+
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))

0 commit comments

Comments
 (0)