UNPKG

6.33 kBMarkdownView Raw
1<!--
2
3 ** DO NOT EDIT THIS FILE
4 **
5 ** This file was automatically generated by the `build-harness`.
6 ** 1) Make all changes to `README.yaml`
7 ** 2) Run `make init` (you only need to do this once)
8 ** 3) Run`make readme` to rebuild this file.
9 **
10
11 -->
12
13[<img src="https://res.cloudinary.com/enter-at/image/upload/v1576145406/static/logo-svg.svg" alt="enter-at" width="100">][website]
14
15# node-aws-lambda-handlers [![Build Status](https://github.com/enter-at/node-aws-lambda-handlers/workflows/Lint%20&%20Test/badge.svg)](https://github.com/enter-at/node-aws-lambda-handlers/actions) [![Release](https://img.shields.io/npm/v/@enter-at/node-aws-lambda-handlers.svg)](https://www.npmjs.com/package/@enter-at/node-aws-lambda-handlers) [![Install size](https://packagephobia.now.sh/badge?p=@enter-at/node-aws-lambda-handlers)](https://packagephobia.now.sh/result?p=@enter-at/node-aws-lambda-handlers)
16
17
18An opinionated Typescript package that facilitates specifying AWS Lambda handlers including input validation,
19error handling and response formatting.
20
21
22---
23
24
25It's 100% Open Source and licensed under the [APACHE2](LICENSE).
26
27
28
29
30
31
32## Quick Start
33
34Install from NPM:
35```bash
36npm install @enter-at/lambda-handlers
37```
38
39
40
41
42
43
44## Api
45### Status code
46
47```typescript
48import {APIGatewayProxyHandler} from '@enter-at/lambda-handlers';
49
50@APIGatewayProxyHandler()
51export function handler(event, context) {
52 return {
53 message: `Hello ${event.queryStringParameters.name}!`
54 };
55}
56```
57
58Let's invoke the function:
59
60```bash
61payload='{"queryStringParameters": {"name": "Peter"}}'
62aws lambda invoke --function-name hello-world --payload $payload /tmp/response.json
63```
64
65Responds with:
66
67```json
68{
69 "headers":{
70 "Access-Control-Allow-Origin": "*",
71 "Access-Control-Allow-Credentials": true,
72 "Content-Type": "application/json"
73 },
74 "statusCode": 200,
75 "body": "\"Hello Peter!\""
76}
77```
78
79Default headers and status code have been added.
80
81#### Respond with a specific status code
82
83```typescript
84import {APIGatewayProxyHandler, created} from '@enter-at/lambda-handlers';
85
86@APIGatewayProxyHandler()
87export function handler(event, context) {
88 const resource = {id: 1, name: event.body.name};
89 return created(resource);
90}
91```
92
93```bash
94payload='{"body": "{\"name\": \"Peter\"}"}'
95aws lambda invoke --function-name create-resource --payload $payload /tmp/response.json
96```
97
98Responds with:
99
100```json
101{
102 "headers":{
103 "Access-Control-Allow-Origin": "*",
104 "Access-Control-Allow-Credentials": true,
105 "Content-Type": "application/json"
106 },
107 "statusCode": 201,
108 "body": "{\"id\":1,\"name\":\"Peter\"}"
109}
110```
111
112#### Error handling
113
114```typescript
115import {APIGatewayProxyHandler, BadRequestError} from '@enter-at/lambda-handlers';
116
117@APIGatewayProxyHandler()
118export function handler(event, context) {
119 throw new BadRequestError('missing email');
120}
121```
122
123```bash
124aws lambda invoke --function-name create-resource $payload /tmp/response.json
125```
126
127Responds with:
128
129```json
130{
131 "headers":{
132 "Access-Control-Allow-Origin": "*",
133 "Access-Control-Allow-Credentials": true,
134 "Content-Type": "application/json"
135 },
136 "statusCode": 400,
137 "body": "{\"errors\":[{\"name\": \"BadRequestError\", \"details\": [\"missing email\"]}]}"
138}
139```
140### Headers
141
142#### Cors
143
144```typescript
145import {APIGatewayProxyHandler, cors} from '@enter-at/lambda-handlers';
146
147@APIGatewayProxyHandler({
148 cors: cors('example.com', false)
149})
150export function handler(event, context) {
151 return {
152 message: 'Hello World!'
153 };
154}
155```
156
157```bash
158aws lambda invoke --function-name cors /tmp/response.json
159```
160
161Responds with:
162
163```json
164{
165 "headers":{
166 "Access-Control-Allow-Origin": "example.com",
167 "Content-Type": "application/json"
168 },
169 "statusCode": 201,
170 "body": "\"Hello World!\""
171}
172```
173### Errors
174
175```
176LambdaHandlerError
177```
178```
179BadRequestError
180```
181```
182ForbiddenError
183```
184```
185InternalServerError
186```
187```
188NotFoundError
189```
190```
191FormatError
192```
193```
194ValidationError
195```
196
197
198
199## Help
200
201**Got a question?**
202
203File a GitHub [issue](https://github.com/enter-at/node-aws-lambda-handlers/issues).
204
205## Contributing
206
207### Bug Reports & Feature Requests
208
209Please use the [issue tracker](https://github.com/enter-at/node-aws-lambda-handlers/issues) to report any bugs or file feature requests.
210
211### Developing
212
213If you are interested in being a contributor and want to get involved in developing this project, we would love to hear from you!
214
215In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
216
217 1. **Fork** the repo on GitHub
218 2. **Clone** the project to your own machine
219 3. **Commit** changes to your own branch
220 4. **Push** your work back up to your fork
221 5. Submit a **Pull Request** so that we can review your changes
222
223**NOTE:** Be sure to merge the latest changes from "upstream" before making a pull request!
224
225
226
227
228
229## License
230
231[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
232
233See [LICENSE](LICENSE) for full details.
234
235 Licensed to the Apache Software Foundation (ASF) under one
236 or more contributor license agreements. See the NOTICE file
237 distributed with this work for additional information
238 regarding copyright ownership. The ASF licenses this file
239 to you under the Apache License, Version 2.0 (the
240 "License"); you may not use this file except in compliance
241 with the License. You may obtain a copy of the License at
242
243 https://www.apache.org/licenses/LICENSE-2.0
244
245 Unless required by applicable law or agreed to in writing,
246 software distributed under the License is distributed on an
247 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
248 KIND, either express or implied. See the License for the
249 specific language governing permissions and limitations
250 under the License.
251
252
253
254
255### Contributors
256
257[![Steffen Leistner][sleistner_avatar]][sleistner_homepage][![Martin Pirkl][pirklmar_avatar]][pirklmar_homepage]
258
259 [sleistner_homepage]: https://github.com/sleistner
260 [sleistner_avatar]: https://avatars0.githubusercontent.com/u/12568?s=128&v=4
261
262 [pirklmar_homepage]: https://github.com/pirklmar
263 [pirklmar_avatar]: https://avatars3.githubusercontent.com/u/2109537?s=128&v=4
264
265
266
267
268 [website]: https://github.com/enter-at