UNPKG

@aws-lambda-powertools/parser

Version:
44 lines (43 loc) 1.28 kB
import { ParseError } from '../errors.js'; import { VpcLatticeV2Schema } from '../schemas/index.js'; import { envelopeDiscriminator } from './envelope.js'; /** * Amazon VPC Lattice envelope to extract data within body key */ export const VpcLatticeV2Envelope = { /** * This is a discriminator to differentiate whether an envelope returns an array or an object * @hidden */ [envelopeDiscriminator]: 'object', parse(data, schema) { try { return VpcLatticeV2Schema.extend({ body: schema, }).parse(data).body; } catch (error) { throw new ParseError('Failed to parse VPC Lattice v2 body', { cause: error, }); } }, safeParse(data, schema) { const result = VpcLatticeV2Schema.extend({ body: schema, }).safeParse(data); if (!result.success) { return { success: false, error: new ParseError('Failed to parse VPC Lattice v2 body', { cause: result.error, }), originalEvent: data, }; } return { success: true, data: result.data.body, }; }, };