@thrty/api
TypeScript icon, indicating that this package has built-in type declarations

3.0.0-alpha.11 • Public • Published

thirty
@thrty/api

Middlewares used to describe an API endpoint

Installation

npm install @thrty/api

Usage

import { APIGatewayProxyHandler } from 'aws-lambda';
import { compose, types } from '@thrty/core';
import { authorizer, get } from '@thrty/api';
import { inject } from '@thrty/inject';
import { responseBody } from '@thrty/api-zod';
import { NotFoundError } from '@thrty/http-errors';
import { httpErrorHandler } from '@thrty/http-error-handler';

export const handler = compose(
  typesOf<APIGatewayProxyHandler>(),
  inject({
    ...todoRepositoryProviders,
  }),
  httpErrorHandler(),
  get('/todos/{todoId}'),
  authorizer('default'),
  responseBody(z.object({
    id: z.string(),
    title: z.string(),
    completed: z.boolean(),
    createdAt: z.string(),
    updatedAt: z.string(),
  })),
)(async event => {
  const { todoRepository } = event.deps;
  const todo = await todoRepository.findById(event.routeParams.todoId);

  if (!todo) {
    throw new NotFoundError('Todo not found');
  }

  return {
    statusCode: 200,
    body: todo,
  };
});

Use @thrty/api-cdk to create an API Gateway out of defined handlers.

import { Api } from '@thrty/api-cdk';

export class CustomStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    new Api(this, 'Api', {
      restApiName: 'Todos',
      pattern: join(__dirname, '..', '__fixtures__', '*Lambda.ts'),
      basePathMapping: {
        path: 'todos',
        domainNames: ['api.thrty.com'],
      },
      authorizers: {
        default: {
          type: 'REQUEST',
          lambdaArn: 'arn:aws:lambda:eu-central-1:123456789:function:defaultauthorizer',
        },
      },
    });
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @thrty/api

Weekly Downloads

187

Version

3.0.0-alpha.11

License

ISC

Unpacked Size

22.7 kB

Total Files

8

Last publish

Collaborators

  • robinbuschmann