@softchef/lambda-events
v1.1.81
Published
[![npm version](https://badge.fury.io/js/%40softchef%2Flambda-events.svg)](https://badge.fury.io/js/%40softchef%2Flambda-events) ![Release](https://github.com/SoftChef/lambda-events/workflows/Release/badge.svg) ![npm](https://img.shields.io/npm/dt/@softch
Downloads
648
Readme
Lambda Events
Installation
// NPM
npm install @softchef/lambda-events
// Yarn
yarn add @softchef/lambda-events
RestApi
JavaScript require
const { RestApi } = require('@softchef/lambda-events');
or
const { Request, Response } = require('@softchef/lambda-events');
TypeScript import
import { RestApi } from '@softchef/lambda-events';
or
import { Request, Response } from '@softchef/lambda-events';
RestApi.Request / Request class:
const request = new RestApi.Request(event);
or
const request = new Request(event);
Methods
Get Parameters
const value = request.parameter(key);
// Get URL path paramter, ex: /Users/{username},
const username = request.parameter('username');
Get QueryStrings
const value = request.get(key, defaultValue);
// Get query string, ex: ?filter=hello
const value = request.get('filter', null);
Get Post data / Body
const value = request.input(key, defaultValue);
// Get post field data, ex: name=John
const name = request.input('name', 'Who');
const inputs = request.inputs(keys);
// Get post mulitple fields data, ex: name=John&enabled=true,
const inputs = request.inputs(['name', 'enabled']);
// inputs: { name: 'John', enabled: true }
Validate Inputs or QueryStrings
const validated = request.validate(keysProvider);
// Validate input data, keysProvider is an callback, keysProvider(joi) please return a Joi schemas
RestApi.Response / Response class:
const response = new RestApi.Response();
or
const response = new Response();
Methods
Response JSON
response.json(data, httpStatusCode);
// If the API request are success, use response.json and give a JSON object data to return client. ex:
response.json({ hello: "world"}, 200);
Response error
response.error(error, httpStatusCode);
// If the API request are failure, use response.error and give a Error object to return client. ex:
response.error(
new Error('Invalid input data'),
422
)
CustomResource
CloudFormation can use CustomResource to invoke a Lambda function. You will get request type, properties from event, and response to stacks when process success/failure.
// JavaScript require
const { CustomResource } = require('@softchef/lambda-events');
// TypeScript import
import { CustomResource } from '@softchef/lambda-events';
CustomResource.Request class:
const request = new CustomResource.Request(event);
Methods
Get Properties
const value = request.property(key);
// Get the CustomResource property by key. ex:
const tableName = request.property('DynamoDbTableName');
// Will return the reference table name from stacks.
Get RequestType
const isOn = request.on(requestType);
// The request type is the stacks process status, allow: Create / Update / Delete. ex:
const isCreate = request.on('create');
// When stacks has first time to create will be true
const isCreateOrUpdate = request.on(['create', 'update']);
// When stacks has first time to create or any update will be true
CustomResource.Response class:
const response = new CustomResource.Response(event);
Methods
Response success to stacks
response.success(returnData): Promise;
// You can return data to stacks. ex:
await response.success({
time: Date.now()
});
// In the stacks can reference. CDK ex:
new CfnOutput(stack, id, {
value: customResource.getAtt('time')
});
Response failure to stacks
response.failed(error): Promise;
// If process has any error, you can report the error message to response stacks. ex:
await response.failed(
new Error('Something wrong.')
);
// The stacks will rollback and display your error message.
Future
- Support more lambda events
- Cognito Trigger
- S3 Trigger
- EventBridge event source
- SQS event source
- Kinesis Data Firehose event source
- more...