A simple framework for building AWS Lambda functions that can handle multiple AWS API Gateway requests.
Install by npm
npm install --save magiclambda
Modify your tsconfig.json
to enable annotations
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
To have the MagicLambda handle API Gateway requests you will need to declare a Controller class with route handler methods.
// controller.ts
import { Controller, Get, PathParam, Response, ok } from 'magiclambda'
@Controller('/example')
export class ExampleController {
@Get('/hello/{name}')
getHelloName (@PathParam('name') name: string): Response {
return ok(`Hello ${name}`)
}
}
Then you can use controllerHandler
from monorepo
library to automaticly create API Gateway compatible request handler, which will be able to handle all the requests defined in your controller.
// index.ts
import { controllerHandler } from 'magiclambda'
import { ExampleController } from './controller'
export const handler = controllerHandler(ExampleController)
Marks the annotated class as a Controller and assigns a basePath
to all routes that it has defined.
Registers the annotated method as GET
request handler for given path
. Path params should be specified in {paramName}
format.
Registers the annotated method as POST
request handler for given path
. Path params should be specified in {paramName}
format.
Registers the annotated method as PUT
request handler for given path
. Path params should be specified in {paramName}
format.
Registers the annotated method as PATCH
request handler for given path
. Path params should be specified in {paramName}
format.
Registers the annotated method as DELETE
request handler for given path
. Path params should be specified in {paramName}
format.
Marks the annotated argument as the receiver of the path param name
which was specified in the path
of the Router
Marks the annotated argument as the receiver of the query param name
. Setting required
to true
instructs the handler to fail the request if the query parameter is not present.
Marks the annotated argument as the receiver of the HTTP request body. Setting required
to true
instructs the handler to fail the request if the request body is not present.
Instructs the handler to validate the annotated parameter (Path, Query or RequestBody) against given Joi schema.
MagicLambda provides utilities to make it easy to return a response body with common status codes. Simply return
one of the following function results from your routers.
Builds response with status code 200
and response body
if provided
Builds response with status code 400
and response body
if provided
Builds response with status code 404
and response body
if provided
Builds response with status code 500
and response body
if provided
Builds response with status code status
and response body
if provided