UNPKG

@aws-lambda-powertools/parser

Version:
103 lines (102 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SqsEnvelope = void 0; const zod_1 = require("zod"); const errors_js_1 = require("../errors.js"); const sqs_js_1 = require("../schemas/sqs.js"); const envelope_js_1 = require("./envelope.js"); /** * SQS Envelope to extract array of Records * * The record's `body` parameter is a string and needs to be parsed against the provided schema. * * If you know that the `body` is a JSON string, you can use `JSONStringified` to parse it, * for example: * * ```ts * import { JSONStringified } from '@aws-lambda-powertools/helpers'; * import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes/sqs'; * * const schema = z.object({ * name: z.string(), * }); * * const parsed = SqsEnvelope.parse(event, JSONStringified(schema)); * ``` */ const SqsEnvelope = { /** * This is a discriminator to differentiate whether an envelope returns an array or an object * @hidden */ [envelope_js_1.envelopeDiscriminator]: 'array', parse(data, schema) { let parsedEnvelope; try { parsedEnvelope = sqs_js_1.SqsSchema.parse(data); } catch (error) { throw new errors_js_1.ParseError('Failed to parse SQS Envelope', { cause: error, }); } return parsedEnvelope.Records.map((record, recordIndex) => { let parsedRecord; try { parsedRecord = schema.parse(record.body); } catch (error) { throw new errors_js_1.ParseError(`Failed to parse SQS Record at index ${recordIndex}`, { cause: new zod_1.ZodError(error.issues.map((issue) => ({ ...issue, path: ['Records', recordIndex, 'body', ...issue.path], }))), }); } return parsedRecord; }); }, safeParse(data, schema) { const parsedEnvelope = sqs_js_1.SqsSchema.safeParse(data); if (!parsedEnvelope.success) { return { success: false, error: new errors_js_1.ParseError('Failed to parse SQS envelope', { cause: parsedEnvelope.error, }), originalEvent: data, }; } const result = parsedEnvelope.data.Records.reduce((acc, record, index) => { const parsedRecord = schema.safeParse(record.body); if (!parsedRecord.success) { const issues = parsedRecord.error.issues.map((issue) => ({ ...issue, path: ['Records', index, 'body', ...issue.path], })); acc.success = false; // @ts-expect-error - index is assigned acc.errors[index] = { issues }; return acc; } acc.records.push(parsedRecord.data); return acc; }, { success: true, records: [], errors: {} }); if (result.success) { return { success: true, data: result.records }; } const errorMessage = Object.keys(result.errors).length > 1 ? `Failed to parse SQS Records at indexes ${Object.keys(result.errors).join(', ')}` : `Failed to parse SQS Record at index ${Object.keys(result.errors)[0]}`; return { success: false, error: new errors_js_1.ParseError(errorMessage, { cause: new zod_1.ZodError( // @ts-expect-error - issues are assigned because success is false Object.values(result.errors).flatMap((error) => error.issues)), }), originalEvent: data, }; }, }; exports.SqsEnvelope = SqsEnvelope;