UNPKG

@aws-lambda-powertools/parser

Version:
76 lines (75 loc) 2.61 kB
import { parse } from './parser.js'; /** * A decorator to parse your event. * * @example * ```typescript * import { z } from 'zod'; * import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; * import type { SqSEvent } from '@aws-lambda-powertools/parser/types; * import { parser } from '@aws-lambda-powertools/parser'; * import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes'; * * const Order = z.object({ * orderId: z.string(), * description: z.string(), * }); * * class Lambda implements LambdaInterface { * * @parser({ envelope: SqsEnvelope, schema: OrderSchema }) * public async handler(event: Order, _context: Context): Promise<unknown> { * // sqs event is parsed and the payload is extracted and parsed * // apply business logic to your Order event * const res = processOrder(event); * return res; * } * } * * ``` * * In case you want to parse the event and handle the error, you can use the safeParse option. * The safeParse option will return an object with the parsed event and an error object if the parsing fails. * * @example * ```typescript * * import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; * import type { SqSEvent, ParsedResult } from '@aws-lambda-powertools/parser/types; * import { z } from 'zod'; * import { parser } from '@aws-lambda-powertools/parser'; * import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes'; * * * const Order = z.object({ * orderId: z.string(), * description: z.string(), * } * * class Lambda implements LambdaInterface { * * @parser({ envelope: SqsEnvelope, schema: OrderSchema, safeParse: true }) * public async handler(event: ParsedResult<Order>, _context: unknown): Promise<unknown> { * if (event.success) { * // event.data is the parsed event object of type Order * } else { * // event.error is the error object, you can inspect and recover * // event.originalEvent is the original event that failed to parse * } * } * } * ``` * * @param options Configure the parser with the `schema`, `envelope` and whether to `safeParse` or not */ export const parser = (options) => { return (_target, _propertyKey, descriptor) => { const original = descriptor.value; const { schema, envelope, safeParse } = options; descriptor.value = function (...args) { const parsedEvent = parse(args[0], envelope, schema, safeParse); return original.apply(this, [parsedEvent, ...args.slice(1)]); }; return descriptor; }; };