import { APIGatewayProxyEvent, APIGatewayProxyEventV2, Callback, Context as ApiGatewayContext } from "aws-lambda"; import { APIGatewayProxyResult } from "aws-lambda/trigger/api-gateway-proxy"; import { AnySchema, Schema, ValidationError } from "joi"; import { LogLevel } from "ts-injection"; import { BigsbyInstance } from "../bigsby"; import { ResponseBuilder } from "../response"; import { InferredType, ParameterInstructionTarget } from "./enums"; import { AuthenticationError, RequestInvalidError, RequestParseError, ResponseInvalidError, TypeCoercionError } from "./errors"; export declare type DeepPartial = { [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; }; export interface ParameterInstruction { paramIndex: number; mapsTo: ParameterInstructionTarget; searchKey: string; type?: InferredType; } export declare type ApiEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2; export declare type HandlerFunction = (event: ApiEvent, context: ApiGatewayContext, callback?: Callback) => Promise; export declare type LifecycleErrors = AuthenticationError | ResponseInvalidError | RequestInvalidError | RequestParseError; export declare type HandlerClassesInput = ApiHandlerConstructor | { [VersionId: string]: ApiHandlerConstructor; } | ApiHandlerConstructor[]; export interface ApiResponse extends Omit { body?: any; } export interface ApiErrorResponseBody { code: string; message: string; url: string; } export interface ApiHandlerConstructor { new (...args: any[]): ApiHandler; } export declare type ApiHandlerInvokeFunction = (...args: any[]) => Promise; export declare type RawHandlerInvokeFunction = (event: ApiEvent, context: ApiGatewayContext, config: ApiConfig) => Promise; export interface ApiHandler { invoke: ApiHandlerInvokeFunction; } export interface RequestContext { rawEvent: APIGatewayProxyEvent | APIGatewayProxyEventV2; event: StandardizedEvent; apiGwContext: ApiGatewayContext; config: ApiConfig; bigsby: BigsbyInstance; state: Record; apiVersion: string; } export declare type StandardizedEvent = Omit & { body: string | Record | undefined; method: string; path: string; protocol: string; version: "1.0" | "2.0"; }; export declare type RequestValidationSchema = { body?: AnySchema; headers?: AnySchema; pathParameters?: AnySchema; queryStringParameters?: AnySchema; }; export interface ResponseValidationSchemaMap { [statusCode: number]: Schema; } export declare type Authenticator = (context: RequestContext) => Promise; export declare type AuthMethod = Authenticator | AuthSchemeName; export declare type AuthSchemeName = string; export interface AuthScheme { name: AuthSchemeName; authenticator: Authenticator; } export declare enum VersioningMethod { HEADER = "HEADER", PATH = "PATH" } export declare type BigsbyPluginFunction = (bigsby: BigsbyInstance, options?: Record) => Promise; export interface BigsbyPlugin { name: string; onRegister: BigsbyPluginFunction; } export interface BigsbyPluginRegistration { plugin: BigsbyPlugin | string; options?: Record; } export interface RegisteredPlugin extends BigsbyPluginRegistration { plugin: BigsbyPlugin; } export declare type ApiLifecycle = Required["lifecycle"]>; export declare type ApiHookNames = keyof ApiLifecycle; export interface BigsbyConfig { api: ApiConfig; logging: LoggingConfig; } export declare type HookChain = HookMethod[]; export declare type HookResult = { response: ResponseBuilder; immediate?: boolean; } | void; export declare type HookInput = InputType & { response?: ResponseBuilder; }; export interface LoggingConfig { enabled: boolean; level: LogLevel; logger?: BigsbyLogger; } export interface BigsbyLogger { debug: LogFunction; info: LogFunction; warn: LogFunction; error: LogFunction; } export declare type LogFunction = (msg: string, ...meta: any[]) => void; declare type ContextInputs = HookInput<{ context: RequestContext; }>; export declare type OnInitInputs = { bigsby: BigsbyInstance; }; export declare type OnInitHook = HookChain<(inputs: OnInitInputs) => Promise>; export declare type PreInvokeInputs = ContextInputs; export declare type PreInvokeHook = HookChain<(inputs: PreInvokeInputs) => Promise>; export declare type PreAuthInputs = ContextInputs; export declare type PreAuthHook = HookChain<(inputs: PreAuthInputs) => Promise>; export declare type OnAuthFailInputs = HookInput<{ error: ErrorType; context: RequestContext; }>; export declare type OnAuthFailHook = HookChain<((inputs: OnAuthFailInputs) => Promise)>; export declare type PreParseInputs = ContextInputs; export declare type PreValidateInputs = ContextInputs; export declare type OnRequestInvalidInputs = HookInput<{ error: ValidationError | RequestParseError; context: RequestContext; }>; export declare type PreExecuteInputs = ContextInputs; export declare type PreResponseInputs = HookInput<{ handlerResponse: ApiResponse; context: RequestContext; }>; export declare type OnResponseInvalidInputs = HookInput<{ error: ValidationError | TypeCoercionError; context: RequestContext; }>; export declare type OnErrorInputs = HookInput<{ error: unknown; context: RequestContext; }>; export declare type PreParseHook = HookChain<(inputs: PreParseInputs) => Promise>; export declare type PreValidateHook = HookChain<(inputs: PreValidateInputs) => Promise>; export declare type OnRequestInvalidHook = HookChain<(inputs: OnRequestInvalidInputs) => Promise>; export declare type PreExecuteHook = HookChain<(inputs: PreExecuteInputs) => Promise>; export declare type PreResponseHook = HookChain<(inputs: PreResponseInputs) => Promise>; export declare type OnResponseInvalidHook = HookChain<(inputs: OnResponseInvalidInputs) => Promise>; export declare type OnErrorHook = HookChain<(inputs: OnErrorInputs) => Promise>; export interface ApiConfig { request: { enableTypeCoercion: boolean; auth?: AuthMethod; schema?: RequestValidationSchema; }; response: { enableInferContentType: boolean; schema?: ResponseValidationSchemaMap; headers?: { [headerName: string]: string; }; }; versioning?: { method: VersioningMethod; key: string; defaultVersion: string; }; lifecycle?: { onInit?: OnInitHook; preInvoke?: PreInvokeHook; preAuth?: PreAuthHook; onAuthFail?: OnAuthFailHook; preParse?: PreParseHook; preValidate?: PreValidateHook; onRequestInvalid?: OnRequestInvalidHook; preExecute?: PreExecuteHook; preResponse?: PreResponseHook; onResponseInvalid?: OnResponseInvalidHook; onError?: OnErrorHook; }; } export {};