# @softchef/lambda-events

> [![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

Latest version **1.1.81** (published 2023-11-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @softchef/lambda-events
pnpm add @softchef/lambda-events
yarn add @softchef/lambda-events
bun add @softchef/lambda-events
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.81 |
| Published | 2023-11-23 |
| First published | 2021-06-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 87 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| Author | SoftChef |
| Maintainers | softchef-iot-lab |

## Links

- npm: https://www.npmjs.com/package/@softchef/lambda-events
- npm.io page: https://npm.io/package/@softchef/lambda-events

## Dependencies (11)

- [joi](https://npm.io/package/joi.md) ^17.11.0
- [axios](https://npm.io/package/axios.md) ^0.27.2
- [sinon](https://npm.io/package/sinon.md) ^13.0.2
- [busboy](https://npm.io/package/busboy.md) ^1.6.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [semver](https://npm.io/package/semver.md) ^7.5.4
- [@types/sinon](https://npm.io/package/@types/sinon.md) ^10.0.20
- [@types/busboy](https://npm.io/package/@types/busboy.md) ^1.5.3
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.202
- [@types/semver](https://npm.io/package/@types/semver.md) ^7.5.6
- [@aws-sdk/client-cognito-identity-provider](https://npm.io/package/@aws-sdk/client-cognito-identity-provider.md) ^3.454.0

## Recent versions

- 1.1.81 (latest) — 2023-11-23
- 1.1.80 — 2022-07-20
- 1.1.79 — 2022-07-19
- 1.1.78 — 2022-07-18
- 1.1.77 — 2022-07-17
- 1.1.76 — 2022-07-16
- 1.1.75 — 2022-07-15
- 1.1.74 — 2022-07-14
- 1.1.73 — 2022-07-13
- 1.1.72 — 2022-07-12
- 1.1.71 — 2022-07-11
- 1.1.70 — 2022-07-09
- 1.1.69 — 2022-07-08
- 1.1.68 — 2022-07-07
- 1.1.67 — 2022-07-06
- … 269 more at https://npm.io/package/@softchef/lambda-events/versions

## README

# Lambda Events

[![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/@softchef/lambda-events?label=NPM%20Downloads&color=orange)

## 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...

---
_Source: https://npm.io/package/@softchef/lambda-events · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
