{"version":3,"file":"apiHandler.mjs","names":[],"sources":["../../src/api/apiHandler.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { ApiError } from '@/api/ApiError';\nimport { errorHandler } from '@/error/errorHandler';\nimport { HumanReadableSchemaError } from '@/validation/standardSchema';\nimport { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, Context, EventBridgeEvent } from 'aws-lambda';\nimport { getReasonPhrase, StatusCodes } from 'http-status-codes';\nimport { z, ZodError } from 'zod';\nimport ServerLogger from '@smooai/logger/AwsServerLogger';\n\nconst logger = new ServerLogger();\n\nexport type LambdaResponseOrError<T = any> = Omit<APIGatewayProxyStructuredResultV2, 'body'> & {\n    body?: T | string | { error: { message: string } & Record<string, any> };\n};\n\nexport async function lambdaApiHandler<T = any>(\n    event: APIGatewayProxyEventV2,\n    context: Context,\n    handler: (event: APIGatewayProxyEventV2, context: Context) => Promise<LambdaResponseOrError<T>>,\n): Promise<APIGatewayProxyStructuredResultV2> {\n    try {\n        logger.addLambdaContext(event, context);\n        return (await errorHandler<[APIGatewayProxyEventV2, Context], APIGatewayProxyStructuredResultV2>(\n            async (event: APIGatewayProxyEventV2, context: Context) => {\n                const response = await handler(event, context);\n                const awsLambdaResponse: APIGatewayProxyStructuredResultV2 = {\n                    ...response,\n                    body: response.body ? (typeof response.body === 'string' ? response.body : JSON.stringify(response.body)) : undefined,\n                };\n                return awsLambdaResponse;\n            },\n            event,\n            context,\n        )) as Promise<APIGatewayProxyStructuredResultV2>;\n    } catch (error) {\n        if (error instanceof ApiError) {\n            logger.error(error, `An API error occurred: Status: ${error.status} (${error.statusText}); Message: ${error.message}`);\n            return {\n                body: JSON.stringify({\n                    error: { message: error.message, statusText: error.statusText },\n                }),\n                statusCode: error.status,\n                headers: { 'Content-type': 'application/json' },\n            };\n        } else if (error instanceof HumanReadableSchemaError) {\n            logger.error(error, `A schema validation error occurred: ${error.message}`);\n            return {\n                body: JSON.stringify({\n                    error: {\n                        message: error.message,\n                        statusText: getReasonPhrase(StatusCodes.BAD_REQUEST),\n                    },\n                }),\n                statusCode: StatusCodes.BAD_REQUEST,\n                headers: { 'Content-type': 'application/json' },\n            };\n        } else if (error instanceof ZodError) {\n            const prettyError = z.prettifyError(error);\n            logger.error(error, `A validation error occurred: ${prettyError}`);\n            return {\n                body: JSON.stringify({\n                    error: {\n                        message: prettyError,\n                        statusText: getReasonPhrase(StatusCodes.BAD_REQUEST),\n                    },\n                }),\n                statusCode: StatusCodes.BAD_REQUEST,\n                headers: { 'Content-type': 'application/json' },\n            };\n        } else if (error instanceof Error) {\n            logger.error(error, `An unexpected error occurred: ${error.message}`);\n            return {\n                body: JSON.stringify({\n                    error: {\n                        message: error.message,\n                        statusText: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR),\n                    },\n                }),\n                statusCode: StatusCodes.INTERNAL_SERVER_ERROR,\n                headers: { 'Content-type': 'application/json' },\n            };\n        } else {\n            logger.error(error, `An unexpected error occurred: ${error}`);\n            return {\n                body: JSON.stringify({\n                    error: {\n                        message: 'An unexpected error occurred.',\n                        statusCode: StatusCodes.INTERNAL_SERVER_ERROR,\n                        statusText: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR),\n                        headers: { 'Content-type': 'application/json' },\n                    },\n                }),\n            };\n        }\n    }\n}\n\nexport async function eventBridgeHandler(\n    event: EventBridgeEvent<any, any>,\n    context: Context,\n    handler: (event: EventBridgeEvent<any, any>, context: Context) => Promise<void>,\n): Promise<void> {\n    try {\n        logger.addLambdaContext(undefined, context);\n        return (await errorHandler<[EventBridgeEvent<any, any>, Context], void>(\n            async (event: EventBridgeEvent<any, any>, context: Context) => {\n                await handler(event, context);\n            },\n            event,\n            context,\n        )) as void;\n    } catch (error) {\n        if (error instanceof ApiError) {\n            logger.error(error, `An API error occurred: Status: ${error.status} (${error.statusText}); Message: ${error.message}`);\n        } else if (error instanceof HumanReadableSchemaError) {\n            logger.error(error, `A schema validation error occurred: ${error.message}`);\n        } else if (error instanceof ZodError) {\n            const prettyError = z.prettifyError(error);\n            logger.error(error, `A validation error occurred: ${prettyError}`);\n        } else if (error instanceof Error) {\n            logger.error(error, `An unexpected error occurred: ${error.message}`);\n        } else {\n            logger.error(error, `An unexpected error occurred: ${error}`);\n        }\n    }\n}\n"],"mappings":";;;;;;;AASA,MAAM,SAAS,IAAI,aAAa;AAMhC,eAAsB,iBAClB,OACA,SACA,SAC0C;CAC1C,IAAI;EACA,OAAO,iBAAiB,OAAO,OAAO;EACtC,OAAQ,MAAM,aACV,OAAO,OAA+B,YAAqB;GACvD,MAAM,WAAW,MAAM,QAAQ,OAAO,OAAO;GAK7C,OAAO;IAHH,GAAG;IACH,MAAM,SAAS,OAAQ,OAAO,SAAS,SAAS,WAAW,SAAS,OAAO,KAAK,UAAU,SAAS,IAAI,IAAK,KAAA;GAEzF;EAC3B,GACA,OACA,OACJ;CACJ,SAAS,OAAO;EACZ,IAAI,iBAAiB,UAAU;GAC3B,OAAO,MAAM,OAAO,kCAAkC,MAAM,OAAO,IAAI,MAAM,WAAW,cAAc,MAAM,SAAS;GACrH,OAAO;IACH,MAAM,KAAK,UAAU,EACjB,OAAO;KAAE,SAAS,MAAM;KAAS,YAAY,MAAM;IAAW,EAClE,CAAC;IACD,YAAY,MAAM;IAClB,SAAS,EAAE,gBAAgB,mBAAmB;GAClD;EACJ,OAAO,IAAI,iBAAiB,0BAA0B;GAClD,OAAO,MAAM,OAAO,uCAAuC,MAAM,SAAS;GAC1E,OAAO;IACH,MAAM,KAAK,UAAU,EACjB,OAAO;KACH,SAAS,MAAM;KACf,YAAY,gBAAgB,YAAY,WAAW;IACvD,EACJ,CAAC;IACD,YAAY,YAAY;IACxB,SAAS,EAAE,gBAAgB,mBAAmB;GAClD;EACJ,OAAO,IAAI,iBAAiB,UAAU;GAClC,MAAM,cAAc,EAAE,cAAc,KAAK;GACzC,OAAO,MAAM,OAAO,gCAAgC,aAAa;GACjE,OAAO;IACH,MAAM,KAAK,UAAU,EACjB,OA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