import { Validator } from './validator.js'; import { ApiResponse } from './api_response.js'; import { Event } from './event.js'; import { Handler } from './handler.js'; import { Hook } from './hook.js'; import { UserResponse } from './user_response.js'; import { Text } from './text_enum.js'; export class LambdaApi { #apiResponse = null; #handlers = []; #errorResponses = []; #beforeHooks = []; #afterHooks = []; #transformRequest = []; /** * Creates a new Lambda Api * * @param {Object} args * @param {Object} [args.headers={}] Any headers to be included in all responses. Defaults to none. * @param {'camelcase'|'snakecase'} [args.transformRequest=false] * Transform the request query string, parameters and body to camelCase or snake_case. * Defaults to no transformation. * @param {'camelcase'|'snakecase'} [args.transformResponse=false] * Transform the response body to camelCase or snake_case * Defaults to no transformation. */ constructor( { headers = {}, transformRequest = false, transformResponse = false } = {} ) { Validator.transformRequest( transformRequest ); Validator.transformResponse( transformResponse ); this.#transformRequest = transformRequest; this.#apiResponse = new ApiResponse( { headers, transform: transformResponse } ); } /** * Register a function that will run before the matching route (only if matches) * * @param {Object} args * @param {function} args.fn A function */ addBeforeHook( { fn } = {} ) { this.#beforeHooks.push( new Hook( { fn } ) ); } /** * Register a function that will run after the matching route (only if matches) * * @param {Object} args * @param {function} args.fn A function */ addAfterHook( { fn } = {} ) { this.#afterHooks.push( new Hook( { fn } ) ); } /** * Register a handler for a given request method and optionally a path * * @param {Object} args * @param {string} args.method The method to match this handler * @param {function} args.fn The handler function * @param {string} [args.route] A route to match this handler * @param {string} [args.routeIncludes] A part of the route to match this handler * @param {string} [args.routeNotIncludes] A part of the route to not match this handler * @param {RegExp} [args.routeMatches] A RegExp to match the route * @param {string} [args.path] A path to match this handler * @param {string} [args.pathIncludes] A part of the path to match this handler * @param {string} [args.pathNotIncludes] A part of the path to not match this handler * @param {RegExp} [args.pathMatches] A RegExp to match the path */ addHandler( { method, fn, ...matchers } = {} ) { this.#handlers.push( new Handler( { method, fn, ...matchers } ) ); } /** * Register an automatic error code response for given error class (constructor name) * * @param {Object} args * @param {class} args.errorType The error class * @param {number} args.code The HTTP status code to return * @param {string} [args.message=null] Optional message to return for the status code, if not present will default to Error.message */ addErrorHandler( { errorType, code, message = null } = {} ) { Validator.statusCode( code ); Validator.errorType( errorType ); this.#errorResponses.push( { errorType, code, message } ); } /** * Init the flow using a given AWS Lambda APIGateway event * * @param {Object} ApiGatewayPayload The raw API Gateway event * @returns {Object} The http response with status, body and headers */ async process( awsEvent ) { const event = new Event( { transform: this.#transformRequest } ); event.parseFromAwsEvent( awsEvent ); const handler = this.#handlers.find( h => h.match( event ) ); if ( !handler ) { if ( event.method === 'HEAD' ) { return this.#apiResponse.setContent( 204 ).toJSON(); } return this.#apiResponse.setContent( 405, Text.ERROR_405 ).toJSON(); } const chain = [ ...this.#beforeHooks.map( b => b.fn ), async ev => { const result = await handler.fn( ev ); const response = new UserResponse( result ); this.#apiResponse.setContent( ...response.values ).toJSON(); }, ...this.#afterHooks.map( a => a.fn ) ]; try { for ( const fn of chain ) { await fn( event ); } return this.#apiResponse.toJSON(); } catch ( error ) { console.error( 'Lambda API Error', { error, event } ); const response = this.#errorResponses.find( e => error instanceof e.errorType ); if ( response ) { return this.#apiResponse.setContent( response.code, response.message ?? error.message ).toJSON(); } return this.#apiResponse.setContent( 500, Text.ERROR_500 ).toJSON(); } } };