项目作者: enter-at

项目描述 :
An opinionated Typescript package that facilitates specifying AWS Lambda handlers including input validation, error handling and response formatting.
高级语言: TypeScript
项目地址: git://github.com/enter-at/node-aws-lambda-handlers.git
创建时间: 2019-11-29T07:09:39Z
项目社区:https://github.com/enter-at/node-aws-lambda-handlers

开源协议:Apache License 2.0

下载


node-aws-lambda-handlers

Build Status @enter-at/lambda-handlers"">Release @enter-at/lambda-handlers"">Install size Code Climate Maintainability Code Climate Test Coverage

An opinionated Typescript package that facilitates specifying AWS Lambda handlers including input validation,
error handling and response formatting.


It’s 100% Open Source and licensed under the APACHE2.

Quick Start

Install from NPM:

  1. npm install @enter-at/lambda-handlers

Api

Status code

  1. import {APIGatewayProxyHandler} from '@enter-at/lambda-handlers';
  2. @APIGatewayProxyHandler()
  3. export function handler(event, context) {
  4. return {
  5. message: `Hello ${event.queryStringParameters.name}!`
  6. };
  7. }

Let’s invoke the function:

  1. payload='{"queryStringParameters": {"name": "Peter"}}'
  2. aws lambda invoke --function-name hello-world --payload $payload /tmp/response.json

Responds with:

  1. {
  2. "headers":{
  3. "Access-Control-Allow-Origin": "*",
  4. "Access-Control-Allow-Credentials": true,
  5. "Content-Type": "application/json"
  6. },
  7. "statusCode": 200,
  8. "body": "\"Hello Peter!\""
  9. }

Default headers and status code have been added.

Respond with a specific status code

  1. import {APIGatewayProxyHandler, created} from '@enter-at/lambda-handlers';
  2. @APIGatewayProxyHandler()
  3. export function handler(event, context) {
  4. const resource = {id: 1, name: event.body.name};
  5. return created(resource);
  6. }
  1. payload='{"body": "{\"name\": \"Peter\"}"}'
  2. aws lambda invoke --function-name create-resource --payload $payload /tmp/response.json

Responds with:

  1. {
  2. "headers":{
  3. "Access-Control-Allow-Origin": "*",
  4. "Access-Control-Allow-Credentials": true,
  5. "Content-Type": "application/json"
  6. },
  7. "statusCode": 201,
  8. "body": "{\"id\":1,\"name\":\"Peter\"}"
  9. }

Error handling

  1. import {APIGatewayProxyHandler, BadRequestError} from '@enter-at/lambda-handlers';
  2. @APIGatewayProxyHandler()
  3. export function handler(event, context) {
  4. throw new BadRequestError('missing email');
  5. }
  1. aws lambda invoke --function-name create-resource $payload /tmp/response.json

Responds with:

  1. {
  2. "headers":{
  3. "Access-Control-Allow-Origin": "*",
  4. "Access-Control-Allow-Credentials": true,
  5. "Content-Type": "application/json"
  6. },
  7. "statusCode": 400,
  8. "body": "{\"errors\":[{\"name\": \"BadRequestError\", \"details\": [\"missing email\"]}]}"
  9. }

Headers

Cors

  1. import {APIGatewayProxyHandler, cors} from '@enter-at/lambda-handlers';
  2. @APIGatewayProxyHandler({
  3. cors: cors('example.com', false)
  4. })
  5. export function handler(event, context) {
  6. return {
  7. message: 'Hello World!'
  8. };
  9. }
  1. aws lambda invoke --function-name cors /tmp/response.json

Responds with:

  1. {
  2. "headers":{
  3. "Access-Control-Allow-Origin": "example.com",
  4. "Content-Type": "application/json"
  5. },
  6. "statusCode": 201,
  7. "body": "\"Hello World!\""
  8. }

Errors

  1. LambdaHandlerError
  1. BadRequestError
  1. ForbiddenError
  1. InternalServerError
  1. NotFoundError
  1. FormatError
  1. ValidationError

Share the Love

Like this project?
Please give it a ★ on our GitHub!

Check out these related projects.

  • python-aws-lambda-handlers - An opinionated Python module that facilitates specifying AWS Lambda handlers including
    input validation, error handling and response formatting.

Help

Got a question?

File a GitHub issue.

Contributing

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or file feature requests.

Developing

If you are interested in being a contributor and want to get involved in developing this project, we would love to hear from you!

In general, PRs are welcome. We follow the typical “fork-and-pull” Git workflow.

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Commit changes to your own branch
  4. Push your work back up to your fork
  5. Submit a Pull Request so that we can review your changes

NOTE: Be sure to merge the latest changes from “upstream” before making a pull request!

License

License

See LICENSE for full details.

  1. Licensed to the Apache Software Foundation (ASF) under one
  2. or more contributor license agreements. See the NOTICE file
  3. distributed with this work for additional information
  4. regarding copyright ownership. The ASF licenses this file
  5. to you under the Apache License, Version 2.0 (the
  6. "License"); you may not use this file except in compliance
  7. with the License. You may obtain a copy of the License at
  8. https://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing,
  10. software distributed under the License is distributed on an
  11. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND, either express or implied. See the License for the
  13. specific language governing permissions and limitations
  14. under the License.