{"version":3,"file":"hono.mjs","names":["logger","AwsServerLogger"],"sources":["../../src/api/hono.ts"],"sourcesContent":["import { isRunningLocally } from '@/env';\nimport { HumanReadableSchemaError } from '@/validation/standardSchema';\nimport { APIGatewayProxyEventV2, Context } from 'aws-lambda';\nimport { Hono } from 'hono';\nimport { handle, LambdaContext, LambdaEvent } from 'hono/aws-lambda';\nimport { HTTPException } from 'hono/http-exception';\nimport { logger as honoLogger } from 'hono/logger';\nimport { prettyJSON } from 'hono/pretty-json';\nimport { requestId } from 'hono/request-id';\nimport { z, ZodError } from 'zod';\nimport AwsServerLogger from '@smooai/logger/AwsServerLogger';\n\nconst logger = new AwsServerLogger();\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- accepting any hono generic\nexport function addHonoMiddleware(_app: Hono<any>): Hono<any> {\n    const app = _app ?? new Hono();\n\n    app.use(requestId());\n    app.use(async (c, next) => {\n        const start = Date.now();\n        const namespace = `${c.req.method} ${c.req.path}`;\n        logger.addRequestContext(c.req);\n        logger.setNamespace(namespace);\n        logger.addContext({\n            honoRequestId: c.get('requestId'),\n        });\n        logger.info(`Request started`);\n        await next();\n        await logger.cloneAndAddResponseContext(c.res);\n        logger.addTelemetryFields({\n            duration: Date.now() - start,\n        });\n        logger.info(`Request completed`);\n    });\n\n    app.use(async (c, next) => {\n        honoLogger((str, ...rest) => {\n            logger.info(str, ...rest);\n        });\n        await next();\n    });\n    if (isRunningLocally()) {\n        app.use(prettyJSON());\n    }\n\n    app.onError((error, c) => {\n        if (error instanceof HumanReadableSchemaError) {\n            logger.error(error, `A schema validation error occurred`);\n\n            return c.json(\n                {\n                    error: error.message,\n                    schemaError: error.schemaError,\n                },\n                400,\n            );\n        } else if (error instanceof ZodError) {\n            const prettyError = z.prettifyError(error);\n            logger.error(error, `A validation error occurred`);\n\n            return c.json(\n                {\n                    error: prettyError,\n                    details: error.issues,\n                },\n                400,\n            );\n        } else if (error instanceof HTTPException) {\n            logger.error(error, `An HTTP error occurred`);\n\n            return error.getResponse();\n        }\n\n        logger.error(error, `An unknown error occurred`);\n        return c.json(\n            {\n                error: error.message,\n            },\n            500,\n        );\n    });\n\n    return app;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- accepting any hono generic\nexport function createHonoAwsLambdaHandler(app: Hono<any>): ReturnType<typeof handle> {\n    return (event: LambdaEvent, lambdaContext?: LambdaContext) => {\n        logger.addLambdaContext(event as unknown as APIGatewayProxyEventV2, lambdaContext as unknown as Context);\n        return handle(app)(event, lambdaContext);\n    };\n}\n"],"mappings":";;;;;;;;;;;AAYA,MAAMA,WAAS,IAAIC,aAAgB;AAGnC,SAAgB,kBAAkB,MAA4B;CAC1D,MAAM,MAAM,QAAQ,IAAI,KAAK;CAE7B,IAAI,IAAI,UAAU,CAAC;CACnB,IAAI,IAAI,OAAO,GAAG,SAAS;EACvB,MAAM,QAAQ,KAAK,IAAI;EACvB,MAAM,YAAY,GAAG,EAAE,IAAI,OAAO,GAAG,EAAE,IAAI;EAC3C,SAAO,kBAAkB,EAAE,GAAG;EAC9B,SAAO,aAAa,SAAS;EAC7B,SAAO,WAAW,EACd,eAAe,EAAE,IAAI,WAAW,EACpC,CAAC;EACD,SAAO,KAAK,iBAAiB;EAC7B,MAAM,KAAK;EACX,MAAMD,SAAO,2BAA2B,EAAE,GAAG;EAC7C,SAAO,mBAAmB,EACtB,UAAU,KAAK,IAAI,IAAI,MAC3B,CAAC;EACD,SAAO,KAAK,mBAAmB;CACnC,CAAC;CAED,IAAI,IAAI,OAAO,GAAG,SAAS;EACvB,QAAY,KAAK,GAAG,SAAS;GACzB,SAAO,KAAK,KAAK,GAAG,IAAI;EAC5B,CAAC;EACD,MAAM,KAAK;CACf,CAAC;CACD,IAAI,iBAAiB,GACjB,IAAI,IAAI,WAAW,CAAC;CAGxB,IAAI,SAAS,OAAO,MAAM;EACtB,IAAI,iBAAiB,0BAA0B;GAC3C,SAAO,MAAM,OAAO,oCAAoC;GAExD,OAAO,EAAE,KACL;IACI,O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