import type { StandardSchemaV1 } from '@standard-schema/spec'; import type { ParseError } from '../errors.js'; import type { ArrayEnvelope, DynamoDBArrayEnvelope, DynamoDBStreamEnvelopeResponse, Envelope } from './envelope.js'; /** * A callback invoked when parsing fails with a {@link ParseError}, receiving the error and the original, * unparsed event. Returning a value short-circuits the parse failure with that value; returning `undefined` * rethrows the error. The callback must be synchronous - returning a `Promise` is a type error. */ type ErrorHandler = (error: ParseError, event: unknown) => TErrorHandlerReturn extends Promise ? never : TErrorHandlerReturn; type ParserOptionsBase = { schema: TSchema; envelope?: TEnvelope; }; /** * Options for the parser used in middy middleware and decorator. * * `safeParse` and `errorHandler` are mutually exclusive: with `safeParse` the parser never throws, * so an `errorHandler` would never be invoked. */ type ParserOptions = ParserOptionsBase & (TSafeParse extends true ? { safeParse: TSafeParse; errorHandler?: never; } : { safeParse?: TSafeParse; errorHandler?: ErrorHandler; }); /** * A successful parsing result with the parsed data when using safeParse */ type ParsedResultSuccess = { success: true; data: Output; }; /** * A failed parsing result with the error when using safeParse, contains the original event and the error. */ type ParsedResultError = { success: false; error: Error; originalEvent?: Input; }; /** * The result of parsing an event using the safeParse, can either be a success or an error */ type ParsedResult = ParsedResultSuccess | ParsedResultError; /** * The inferred result of the schema, can be either an array or a single object depending on the envelope */ type ZodInferredResult = undefined extends TEnvelope ? InferOutput : TEnvelope extends DynamoDBArrayEnvelope ? DynamoDBStreamEnvelopeResponse>[] : TEnvelope extends ArrayEnvelope ? InferOutput[] : InferOutput; type ZodInferredSafeParseResult = undefined extends TEnvelope ? ParsedResult, InferOutput> : TEnvelope extends DynamoDBArrayEnvelope ? ParsedResult>[]> : TEnvelope extends ArrayEnvelope ? ParsedResult[]> : ParsedResult>; /** * The output of the parser function, can be either schema inferred type or a ParsedResult */ type ParserOutput = TSafeParse extends true ? ZodInferredSafeParseResult : ZodInferredResult; /** * The parser function that can parse the data using the provided schema and envelope * we use function overloads to provide the correct return type based on the provided envelope */ type ParseFunction = { (data: unknown, envelope: undefined, schema: T, safeParse?: false): InferOutput; (data: unknown, envelope: undefined, schema: T, safeParse: true): ParsedResult>; (data: unknown, envelope: E, schema: T, safeParse?: false): E extends DynamoDBArrayEnvelope ? DynamoDBStreamEnvelopeResponse>[] : E extends ArrayEnvelope ? InferOutput[] : InferOutput; (data: unknown, envelope: E, schema: T, safeParse: true): E extends DynamoDBArrayEnvelope ? ParsedResult>[]> : E extends ArrayEnvelope ? ParsedResult[]> : ParsedResult>; }; type InferOutput = NonNullable['output']; export type { ErrorHandler, InferOutput, ParsedResult, ParsedResultError, ParsedResultSuccess, ParseFunction, ParserOptions, ParserOutput, }; //# sourceMappingURL=parser.d.ts.map