UNPKG

@aws-lambda-powertools/parser

Version:
89 lines (88 loc) 3.52 kB
import { ZodError } from 'zod'; import { ParseError } from '../errors.js'; import { KinesisFirehoseSchema } from '../schemas/index.js'; import { envelopeDiscriminator } from './envelope.js'; /** * Kinesis Firehose Envelope to extract array of Records * * The record's data parameter is a base64 encoded string which is parsed into a bytes array, * though it can also be a JSON encoded string. * Regardless of its type it'll be parsed into a BaseModel object. * * Note: Records will be parsed the same way so if model is str, * all items in the list will be parsed as str and not as JSON (and vice versa) * * https://docs.aws.amazon.com/lambda/latest/dg/services-kinesisfirehose.html */ export const KinesisFirehoseEnvelope = { /** * This is a discriminator to differentiate whether an envelope returns an array or an object * @hidden */ [envelopeDiscriminator]: 'array', parse(data, schema) { let parsedEnvelope; try { parsedEnvelope = KinesisFirehoseSchema.parse(data); } catch (error) { throw new ParseError('Failed to parse Kinesis Firehose envelope', { cause: error, }); } return parsedEnvelope.records.map((record, recordIndex) => { let parsedRecord; try { parsedRecord = schema.parse(record.data); } catch (error) { throw new ParseError(`Failed to parse Kinesis Firehose record at index ${recordIndex}`, { cause: new ZodError(error.issues.map((issue) => ({ ...issue, path: ['records', recordIndex, 'data', ...issue.path], }))), }); } return parsedRecord; }); }, safeParse(data, schema) { const parsedEnvelope = KinesisFirehoseSchema.safeParse(data); if (!parsedEnvelope.success) { return { success: false, error: new ParseError('Failed to parse Kinesis Firehose envelope', { cause: parsedEnvelope.error, }), originalEvent: data, }; } const result = parsedEnvelope.data.records.reduce((acc, record, index) => { const parsedRecord = schema.safeParse(record.data); if (!parsedRecord.success) { const issues = parsedRecord.error.issues.map((issue) => ({ ...issue, path: ['records', index, 'data', ...issue.path], })); acc.success = false; 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 Kinesis Firehose records at indexes ${Object.keys(result.errors).join(', ')}` : `Failed to parse Kinesis Firehose record at index ${Object.keys(result.errors)[0]}`; return { success: false, error: new ParseError(errorMessage, { cause: new ZodError(Object.values(result.errors).flatMap((error) => error.issues)), }), originalEvent: data, }; }, };