UNPKG

@noony-serverless/core

Version:

A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript

93 lines 3.39 kB
import { Context, NoonyRequest, NoonyResponse } from './core'; /** * Interface representing a base structure for middleware with optional lifecycle methods. * * This interface is designed to provide hooks for execution during different * stages of a middleware's lifecycle. It allows for defining logic to be * executed before and after a process, as well as handling errors that * occur during the process. * * @template T - The type of the request data. Defaults to unknown. */ export interface BaseMiddleware<T = unknown> { before?: (context: Context<T>) => Promise<void>; after?: (context: Context<T>) => Promise<void>; onError?: (error: Error, context: Context<T>) => Promise<void>; } /** * The Handler class is responsible for managing and executing middleware functions * and a main handler function in a sequential and controlled manner. * * This class provides a mechanism for registering middlewares that can * process a request/response flow either before the main handler (via `before`), * after the main handler (via `after`), or handle errors (via `onError`). * * @example * ```typescript * interface MessagePayload { * action: string; * data: Record<string, unknown>; * } * * const handler = new Handler<MessagePayload>() * .use(errorHandler()) * .use(bodyParser()) * .handle(async (context) => { * const { req } = context; * // Handle the request with type-safe access to req.validatedBody * }); * ``` * @template T Type for the input request data. */ export declare class Handler<T = unknown> { private baseMiddlewares; private handler; private reversedMiddlewares; private errorMiddlewares; private middlewaresPrecomputed; static use<T = unknown>(middleware: BaseMiddleware<T>): Handler<T>; use(middleware: BaseMiddleware<T>): Handler<T>; handle(handler: (context: Context<T>) => Promise<void>): Handler<T>; /** * Performance optimization: Pre-compute middleware arrays to avoid runtime array operations */ private precomputeMiddlewareArrays; /** * Universal execute method that works with any HTTP framework * Automatically detects and adapts GCP, Express, AWS Lambda, Fastify, etc. */ execute(nativeReq: unknown, nativeRes: unknown): Promise<void>; /** * Execute before middlewares with optimized batching for independent middlewares */ private executeBeforeMiddlewares; /** * Execute after middlewares using pre-computed reversed array */ private executeAfterMiddlewares; /** * Execute error middlewares using pre-computed array */ private executeErrorMiddlewares; /** * Universal request adapter - converts any framework's request to NoonyRequest */ private adaptToNoonyRequest; /** * Universal response adapter - converts any framework's response to NoonyResponse */ private adaptToNoonyResponse; /** * Create response adapter for AWS Lambda */ private createAWSLambdaResponse; /** * Create response adapter for standard HTTP frameworks (GCP, Express, Fastify) */ private createStandardHTTPResponse; /** * @deprecated Use execute() instead - automatically detects framework */ executeGeneric(req: NoonyRequest<T>, res: NoonyResponse): Promise<void>; } //# sourceMappingURL=handler.d.ts.map