@aws-lambda-powertools/parser
Version:
The parser package for the Powertools for AWS Lambda (TypeScript) library.
92 lines (91 loc) • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KinesisFirehoseEnvelope = void 0;
const zod_1 = require("zod");
const errors_js_1 = require("../errors.js");
const index_js_1 = require("../schemas/index.js");
const envelope_js_1 = require("./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
*/
exports.KinesisFirehoseEnvelope = {
/**
* 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 = index_js_1.KinesisFirehoseSchema.parse(data);
}
catch (error) {
throw new errors_js_1.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 errors_js_1.ParseError(`Failed to parse Kinesis Firehose record at index ${recordIndex}`, {
cause: new zod_1.ZodError(error.issues.map((issue) => ({
...issue,
path: ['records', recordIndex, 'data', ...issue.path],
}))),
});
}
return parsedRecord;
});
},
safeParse(data, schema) {
const parsedEnvelope = index_js_1.KinesisFirehoseSchema.safeParse(data);
if (!parsedEnvelope.success) {
return {
success: false,
error: new errors_js_1.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 errors_js_1.ParseError(errorMessage, {
cause: new zod_1.ZodError(Object.values(result.errors).flatMap((error) => error.issues)),
}),
originalEvent: data,
};
},
};