@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.
1 lines • 3.46 kB
Source Map (JSON)
{"version":3,"file":"sqsHandler.cjs","names":["ServerLogger","ApiError","HumanReadableSchemaError","ZodError","z"],"sources":["../../src/api/sqsHandler.ts"],"sourcesContent":["import { ApiError } from '@/api/ApiError';\nimport { HumanReadableSchemaError } from '@/validation/standardSchema';\nimport type { Context, SQSBatchItemFailure, SQSBatchResponse, SQSEvent, SQSRecord } from 'aws-lambda';\nimport { z, ZodError } from 'zod';\nimport ServerLogger from '@smooai/logger/AwsServerLogger';\n\nconst logger = new ServerLogger();\n\nexport type JsonSQSRecord = Omit<SQSRecord, 'body'> & { body: Record<string, unknown> };\n\nexport function sqsHandler(\n handler: (event: JsonSQSRecord, context: Context) => Promise<void>,\n): (event: SQSEvent, context: Context) => Promise<SQSBatchResponse> {\n return async (event: SQSEvent, context: Context) => {\n try {\n const batchItemFailures: SQSBatchItemFailure[] = [];\n logger.addLambdaContext(undefined, context);\n\n for (const record of event.Records) {\n try {\n logger.addSQSRecordContext(record);\n const jsonRecord: JsonSQSRecord = {\n ...record,\n body: record.body ? JSON.parse(record.body) : {},\n };\n await handler(jsonRecord, context);\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 batchItemFailures.push({ itemIdentifier: record.messageId });\n }\n }\n\n return { batchItemFailures };\n } catch (error) {\n logger.error(error, `An unexpected error occurred: ${error}`);\n throw error;\n }\n };\n}\n"],"mappings":";;;;;;;;AAMA,MAAM,SAAS,IAAIA,+BAAAA,QAAa;AAIhC,SAAgB,WACZ,SACgE;CAChE,OAAO,OAAO,OAAiB,YAAqB;EAChD,IAAI;GACA,MAAM,oBAA2C,CAAC;GAClD,OAAO,iBAAiB,KAAA,GAAW,OAAO;GAE1C,KAAK,MAAM,UAAU,MAAM,SACvB,IAAI;IACA,OAAO,oBAAoB,MAAM;IAKjC,MAAM,QAAQ;KAHV,GAAG;KACH,MAAM,OAAO,OAAO,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC;IAE5B,GAAG,OAAO;GACrC,SAAS,OAAO;IACZ,IAAI,iBAAiBC,qBAAAA,UACjB,OAAO,MAAM,OAAO,kCAAkC,MAAM,OAAO,IAAI,MAAM,WAAW,cAAc,MAAM,SAAS;SAClH,IAAI,iBAAiBC,kCAAAA,0BACxB,OAAO,MAAM,OAAO,uCAAuC,MAAM,SAAS;SACvE,IAAI,iBAAiBC,IAAAA,UAAU;KAClC,MAAM,cAAcC,IAAAA,EAAE,cAAc,KAAK;KACzC,OAAO,MAAM,OAAO,gCAAgC,aAAa;IACrE,OAAO,IAAI,iBAAiB,OACxB,OAAO,MAAM,OAAO,iCAAiC,MAAM,SAAS;SAEpE,OAAO,MAAM,OAAO,iCAAiC,OAAO;IAGhE,kBAAkB,KAAK,EAAE,gBAAgB,OAAO,UAAU,CAAC;GAC/D;GAGJ,OAAO,EAAE,kBAAkB;EAC/B,SAAS,OAAO;GACZ,OAAO,MAAM,OAAO,iCAAiC,OAAO;GAC5D,MAAM;EACV;CACJ;AACJ"}