# serverlith

> Library for writing REST APIs in AWS Lambda

Latest version **0.0.11** (published 2019-10-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install serverlith
pnpm add serverlith
yarn add serverlith
bun add serverlith
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.11 |
| Published | 2019-10-06 |
| First published | 2019-05-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 29.3 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Donnie Mattingly |
| Maintainers | donniemattingly |
| Keywords | serverless, aws-lambda |

## Links

- npm: https://www.npmjs.com/package/serverlith
- Repository: https://github.com/donniemattingly/serverlith
- Homepage: https://github.com/donniemattingly/serverlith#readme
- Issues: https://github.com/donniemattingly/serverlith/issues
- npm.io page: https://npm.io/package/serverlith

## Dependencies (8)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.449.0
- [aws-lambda](https://npm.io/package/aws-lambda.md) ^0.1.2
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.123
- [@types/aws-sdk](https://npm.io/package/@types/aws-sdk.md) ^2.7.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^3.0.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@types/aws-lambda](https://npm.io/package/@types/aws-lambda.md) ^8.10.25

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.0.11 (latest) — 2019-10-06
- 0.0.10 — 2019-10-06
- 0.0.9 — 2019-10-06
- 0.0.8 — 2019-05-31
- 0.0.7 — 2019-05-30
- 0.0.6 — 2019-05-05
- 0.0.5 — 2019-05-05
- 0.0.4 — 2019-05-05
- 0.0.3 — 2019-05-04
- 0.0.2 — 2019-05-03
- 0.0.1 — 2019-05-03

## README

# Serverlith

[![npm version](https://badge.fury.io/js/serverlith.svg)](https://badge.fury.io/js/serverlith)

*Serverless Monoliths*

Serverlith is a TypeScript library for writing APIs with AWS Lambda and API Gateway more easily.
Serverlith handles conversion and routing of Amazon API Gateway events received by your lambda 
function. 

Serverlith operates based off the usage of [proxy resources](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html)
in Amazon API Gateway to route all possible methods and paths to the same Lambda function.

**Note:** Serverlith is a work in progress and is definitely *not* finished. Please don't use this right now, as
basically every aspect of the library can change.


## Examples

The entry point into a Serverlith app is a handler to give to AWS Lambda as with any function, and you
register your handlers with the Serverlith router.

```typescript
export const handler = async (event: APIGatewayEvent) => {
    return await router(...handlers)
        .handleEvent(event);
};
```
The `APIGatewayEvent` is transformed into a `Request` with the following interface:

```typescript
interface Request {
    httpMethod: string;
    body: any;
    queryParams: any;
    pathParams: any;
    path: string;
}
```

and handlers are expected to return `Response | Promise<Response>` with `Response` being:

```typescript
interface Response {
    statusCode: string;
    body: any;
    headers: {[name: string]: any};
}
```

You can use either decorator based or function based routing.

## Routing

Choosing which routing style to use is mostly a personal preference - the decorators are just syntactic sugar
around the functional approach. Though the functional approach does enforce correct types, the class based approach
avoids issues caused by `this` being undefined in the handlers if not bound with `Function.prototype.bind`.

Serverlith uses [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) for routing, so check out that documentation
for all that's possible.

#### Decorator Based

You decorate a class with `@Handler` to enable routing for methods in the class, and use one of `@GET`, `@PUT`, 
`@POST`, `@DELETE`, or `@OPTIONS` to register that method as a handler for requests to that path.

Below we're registering `GET` requests to `/users/:id/` and `POST`s at `/users`

The methods you decorate should take an argument that extends `Request` and returns one that extends `Response`

```typescript
// note importing from serverlith/decorators
import {GET, Handler, POST} from "serverlith/decorators"; 

@Handler({path: '/users'})
export class UserHandler {

    @GET('/:id')
    public getUser(request: Request): Response {
        
        // extracting the path parameter 'id'
        console.log(`user id was: ${request.pathParams['id']}`)
        return fail('not implemented');
    }

    @POST('')
    public async createUser(request: CreateUserRequest): Promise<Response> {
        const newUser = await service.createUser(request);
        return success(newUser);
    }
}

```

#### Function Based

To accomplish the same as above with functional routing:

```typescript
// importing from serverlith/functional here
import {GET, handle, POST} from 'serverlith/functional';

const handler = handle('/users',
    GET('/:id', async (request: Request): Promise<Response> => {
        return fail('not implemented');
    }),
    POST('', async (request: Request): Promise<Response> => {
        return fail('not implemented');
    })
);
```

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