Skip to content

Commit d5b43aa

Browse files
author
cgohlke
committed
improve readme
1 parent 0e1d8c9 commit d5b43aa

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

README.md

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ A small library for [AWS Lambda](https://aws.amazon.com/lambda/details) providin
1313
* Simplifies writing lambda handlers (in nodejs)
1414
* Lambda Proxy Resource support for AWS API Gateway
1515
* Enable CORS for the requests
16+
* No dependencies
17+
* Currently there are two `processors` (caller for Lambda) implemented: API Gateway ANY method (called proxyIntegration) and SNS.
1618

1719
## Installation
1820
Install via npm.
@@ -29,8 +31,7 @@ This is an simple using of `aws-lambda-router` in connection with ANY method and
2931
const router = require('aws-lambda-router');
3032

3133
// handler for an api gateway event
32-
exports.handler = router.handler(
33-
{
34+
exports.handler = router.handler({
3435
// for handling an http-call from an AWS Apigateway proxyIntegration we provide the following config:
3536
proxyIntegration: {
3637
routes: [
@@ -46,51 +47,82 @@ exports.handler = router.handler(
4647
]
4748
}
4849
}
49-
5050
```
5151
52-
## Usage
52+
53+
## Enable CORS
54+
55+
To activate CORS on all http methods (OPTIONS requests are handled automatically) you only sets the parameter `cors` to `true` of the `proxyIntegration` rule.
56+
57+
See the following example:
58+
5359
5460
```js
5561
const router = require('aws-lambda-router');
5662

57-
exports.handler = router.handler(
58-
// the router-config contains configs for every type of 'processor'
59-
{
63+
exports.handler = router.handler({
6064
// for handling an http-call from an AWS Apigateway proxyIntegration we provide the following config:
6165
proxyIntegration: {
62-
// activate CORS on all http-methods (OPTIONS requests are handled automagically);
63-
// if set to true, these default headers will be sent on every response:
64-
// "Access-Control-Allow-Origin" = "'*'"
65-
// "Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD,PATCH'"
66-
// "Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
6766
cors: true,
6867
routes: [
6968
{
70-
// the request-path-pattern to match:
7169
path: '/graphql',
72-
// http method to match
7370
method: 'POST',
7471
// provide a function to be called with the propriate data
7572
action: (request, context) => doAnything(request.body)
76-
},
73+
}
74+
]
75+
}
76+
});
77+
```
78+
79+
If CORS is activated, these default headers will be sent on every response:
80+
81+
"Access-Control-Allow-Origin" = "'*'"
82+
"Access-Control-Allow-Methods" = "'GET,POST,PUT,DELETE,HEAD,PATCH'"
83+
"Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
84+
85+
86+
## Errormapping
87+
88+
```js
89+
const router = require('aws-lambda-router');
90+
91+
exports.handler = router.handler({
92+
// for handling an http-call from an AWS Apigateway proxyIntegration we provide the following config:
93+
proxyIntegration: {
94+
routes: [
7795
{
78-
path: '/:id',
79-
method: 'DELETE',
80-
action: (request, context) => deleteSomething(request.paths.id)
96+
path: '/graphql',
97+
method: 'POST',
98+
action: (request, context) => doAnything(request.body)
8199
}
82100
],
83101
debug: true,
84-
// custom mapping of thrown errors to http response code error:
85-
// the action can throw an object like
86-
// "throw {reason: 'NotFound', message: 'object id not found'}"
87-
// the http response then contains the configured value as response code and the message as the body
88102
errorMapping: {
89103
'NotFound': 404,
90104
'ServerError': 500
91105
}
92-
},
93-
// for handling calls initiated from AWS-SNS:
106+
}
107+
});
108+
```
109+
110+
With the key word `errorMapping` shown in the example above you can custom mapping of thrown errors to http response code error.
111+
The action can throw an object like
112+
113+
"throw {reason: 'NotFound', message: 'object id not found'}"
114+
115+
and the http response then contains the configured value as response code and the message as the body.
116+
117+
118+
## SNS to Lambda Integrations
119+
120+
For handling calls in Lambdas initiated from AWS-SNS you can use the following code snippet:
121+
122+
```js
123+
const router = require('aws-lambda-router');
124+
125+
exports.handler = router.handler({
94126
sns: {
95127
routes: [
96128
{
@@ -128,7 +160,7 @@ return {
128160
};
129161
```
130162
131-
## local developement
163+
## Local developement
132164
133165
The best is to work with ```yarn link```
134166

0 commit comments

Comments
 (0)