There are three routing modes: directory, pattern and list; directory and pattern routing mode requires your project files to be placed in a particular way; list does not require any structure, as you define every route and it's corresponding file. Below are the three ways configure your router:
import{Router}from'acai-ts';import{APIGatewayProxyEventV2}from'aws-lambda';constrouter=newRouter({mode:'directory',basePath:'api',// for use with custom apigateway domainroutesPath:'api/handler'});router.autoLoad()// optional; pulls in files from disc into memory and shares on with concurrent lambdasexportconstroute=async(event:APIGatewayProxyEventV2)=>{returnrouter.route(event);};
import{Router}from'acai-ts';import{APIGatewayProxyEventV2}from'aws-lambda';exportconstroute=async(event:APIGatewayProxyEventV2)=>{constrouter=newRouter({mode:'pattern',basePath:'api',// for use with custom apigateway domainroutesPath:'api/**/*.controller.ts'});returnrouter.route(event);};
It may be more maintainable to store your routes list in a separate file, this example does not have that for brevity
Warning
Even though you are matching your files to your routes, the handler files must have functions that match HTTP method (see endpoint examples here)
Danger
This is not the preferred routing mode to use; this can lead to a sloppy, unpredictable project architecture which will be hard to maintain and extend. This is NOT RECOMMENDED.
import{Router}from'acai-ts';import{APIGatewayProxyEventV2}from'aws-lambda';constrouter=newRouter({mode:'list',basePath:'api',// for use with custom apigateway domainroutes:[{method:'GET',path:'/grower',handler:'api/routes/grower.ts'},{method:'POST',path:'/farm',handler:'api/routes/farm.ts'},{method:'PUT',path:'/farm/{farmId}/field/{fieldId}',handler:'api/routes/farm-field.ts'}]});router.autoLoad()// optional; pulls in files from disc into memory and shares on with concurrent lambdasexportconstroute=async(event:APIGatewayProxyEventV2)=>{returnrouter.route(event);};
This is the modern TypeScript approach using class-based endpoints with decorators. Perfect for type-safe development with clean, declarative configuration.
Every endpoint file should contain a function which matches an HTTP method in lower case. Most common are post, get, put, patch, delete, but this library does support custom methods, if you so choose. As long as the method of the request matches the function name, it will work.
import{Request,Response}from'acai-ts';exportconstrequirements={post:{requiredBody:'CreateItemRequest'},get:{requiredHeaders:['authorization']}};exportconstpost=async(request:Request,response:Response):Promise<Response>=>{response.body={post:true};returnresponse;};exportconstget=async(request:Request,response:Response):Promise<Response>=>{response.body={get:true};returnresponse;};exportconstpatch=async(request:Request,response:Response):Promise<Response>=>{response.body={patch:true};returnresponse;};exportconstput=async(request:Request,response:Response):Promise<Response>=>{response.body={put:true};returnresponse;};exportconstdelete=async(request:Request,response:Response):Promise<Response>=>{response.body={delete:true};returnresponse;};// this is a non-compliant, custom http method; this will work.exportconstquery=async(request:Request,response:Response):Promise<Response>=>{response.body=[{query:true}];returnresponse;};
import{BaseEndpoint,Validate,Auth,Request,Response}from'acai-ts';exportclassItemEndpointextendsBaseEndpoint{@Validate({requiredBody:'CreateItemRequest'})asyncpost(request:Request,response:Response):Promise<Response>{response.body={post:true};returnresponse;}@Auth()asyncget(request:Request,response:Response):Promise<Response>{response.body={get:true};returnresponse;}asyncpatch(request:Request,response:Response):Promise<Response>{response.body={patch:true};returnresponse;}asyncput(request:Request,response:Response):Promise<Response>{response.body={put:true};returnresponse;}asyncdelete(request:Request,response:Response):Promise<Response>{response.body={delete:true};returnresponse;}// this is a non-compliant, custom http method; this will work.asyncquery(request:Request,response:Response):Promise<Response>{response.body=[{query:true}];returnresponse;}}