UNPKG

@smooai/utils

Version:

A collection of shared utilities and tools used across SmooAI projects. This package provides common functionality to standardize and simplify development across all SmooAI repositories.

63 lines (62 loc) 1.86 kB
import { isRunningLocally } from "../chunk-PXQ26ZJA.mjs"; import { HumanReadableSchemaError } from "../chunk-WKR62SAR.mjs"; import "../chunk-LZOMFHX3.mjs"; // src/api/hono.ts import AwsLambdaLogger from "@smooai/logger/AwsLambdaLogger"; import { Hono } from "hono"; import { handle } from "hono/aws-lambda"; import { HTTPException } from "hono/http-exception"; import { logger as honoLogger } from "hono/logger"; import { prettyJSON } from "hono/pretty-json"; import { requestId } from "hono/request-id"; import { ZodError } from "zod"; import { fromZodError } from "zod-validation-error"; var logger = new AwsLambdaLogger(); function createAwsLambdaHonoApp(appFunction) { const app = new Hono(); app.use(requestId()); app.use( honoLogger((str, ...rest) => { logger.info(str, ...rest); }) ); app.use(async (c, next) => { logger.addContext({ honoRequestId: c.get("requestId") }); await next(); }); if (isRunningLocally()) { app.use(prettyJSON()); } app.onError((error) => { if (error instanceof HumanReadableSchemaError) { logger.error(error, `A schema validation error occurred: ${error.message}`); throw new HTTPException(400, { cause: error, message: error.message }); } else if (error instanceof ZodError) { const validationError = fromZodError(error); logger.error(error, `A validation error occurred: ${validationError.toString()}`); throw new HTTPException(400, { cause: error, message: validationError.toString() }); } throw error; }); const appWithRoutes = appFunction(app); return (event, lambdaContext) => { logger.addLambdaContext(event, lambdaContext); return handle(appWithRoutes)(event, lambdaContext); }; } export { createAwsLambdaHonoApp }; //# sourceMappingURL=hono.mjs.map