{"version":3,"file":"hono.cjs","names":["AwsServerLogger","Hono","isRunningLocally","HumanReadableSchemaError","ZodError","z","HTTPException"],"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,MAAM,SAAS,IAAIA,+BAAAA,QAAgB;AAGnC,SAAgB,kBAAkB,MAA4B;CAC1D,MAAM,MAAM,QAAQ,IAAIC,KAAAA,KAAK;CAE7B,IAAI,KAAA,GAAA,gBAAA,WAAc,CAAC;CACnB,IAAI,IAAI,OAAO,GAAG,SAAS;EACvB,MAAM,QAAQ,KAAK,IAAI;EACvB,MAAM,YAAY,GAAG,EAAE,IAAI,OAAO,GAAG,EAAE,IAAI;EAC3C,OAAO,kBAAkB,EAAE,GAAG;EAC9B,OAAO,aAAa,SAAS;EAC7B,OAAO,WAAW,EACd,eAAe,EAAE,IAAI,WAAW,EACpC,CAAC;EACD,OAAO,KAAK,iBAAiB;EAC7B,MAAM,KAAK;EACX,MAAM,OAAO,2BAA2B,EAAE,GAAG;EAC7C,OAAO,mBAAmB,EACtB,UAAU,KAAK,IAAI,IAAI,MAC3B,CAAC;EACD,OAAO,KAAK,mBAAmB;CACnC,CAAC;CAED,IAAI,IAAI,OAAO,GAAG,SAAS;EACvB,CAAA,GAAA,YAAA,SAAY,KAAK,GAAG,SAAS;GACzB,OAAO,KAAK,KAAK,GAAG,IAAI;EAC5B,CAAC;EACD,MAAM,KAAK;CACf,CAAC;CACD,IAAIC,kBAAAA,iBAAiB,GACjB,IAAI,KAAA,GAAA,iBAAA,YAAe,CAAC;CAGxB,IA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