# typesafe-api-gateway

> This is types package that I've made as extension of `@types/aws-lambda` package with idea of giving much more generic interface for API Gateway events.

Latest version **1.0.1** (published 2021-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install typesafe-api-gateway
pnpm add typesafe-api-gateway
yarn add typesafe-api-gateway
bun add typesafe-api-gateway
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-10-29 |
| First published | 2021-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mrdck |

## Links

- npm: https://www.npmjs.com/package/typesafe-api-gateway
- npm.io page: https://npm.io/package/typesafe-api-gateway

## Recent versions

- 1.0.1 (latest) — 2021-10-29
- 1.0.0 — 2021-10-28

## README

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [typesafe-api-gateway](#typesafe-api-gateway)
  - [Usage](#usage)
  - [Setup](#setup)
  - [Contribution](#contribution)
  - [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# typesafe-api-gateway

This is types package that I've made as extension of `@types/aws-lambda` package with idea of giving much more generic interface for API Gateway events.

Before you start please take a look below to see explanation why generic API Gateway Event is needed.

```typescript
interface Payload {
  foo: string
}

const handler = async ({ body }: APIGatwayEventProxy): APIGatewayProxyResult => {
   let data: Payload

    try {
      data = JSON.parse(body) as unknown as Payload
    } catch {
     // exception handle
    }
}
```

In the example from above `body` property most-likely will get `string` or `null` type since this is what type definition has inside `APIGatewayProxyEventBase`
```
export interface APIGatewayProxyEventBase<TAuthorizerContext> {
    body: string | null;
}
```

The reason for `string` as body type is most likely due fact that body in event is usually in raw form which needs to get parsed.

However, if there is body-parser used as middleware or other mechanism applied that pass parsed body to event there is need to use type correction

```typescript
interface Payload {
    foo: string
}

const handler = async ({ body }: APIGatwayEventProxy): APIGatewayProxyResult => {
  let data = body as unknown as Payload
}
```

This is the place where this library comes with help with more generic typings

## Usage

Typing request parts

```typescript
interface Body {
  bar: string
}

interface Headers {
  baz: string
}

const handler = async (event: APIGatwayEventProxy<{ body: Foo,  headers: Headers }>): APIGatewayProxyResult => {
  //
}
```
## Setup

In order to make types available with existing code base You need to specify types.
There are two ways for including types in project:

Using `types` property in `tsconfig.json`
```json
{
  "types": [
    "typesafe-api-gateway"
  ]
}
```

Using `import` in `types.d.ts` file
```typescript
import 'typesafe-api-gateway'
```

## Contribution

Feel free to submit an issue or PR

## License

MIT

---
_Source: https://npm.io/package/typesafe-api-gateway · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
