import type { StandardSchemaV1 } from '@standard-schema/spec'; import type { ArrayEnvelope, DynamoDBArrayEnvelope, DynamoDBStreamEnvelopeResponse, Envelope } from './envelope.js'; /** * Options for the parser used in middy middleware and decorator */ type ParserOptions = { schema: TSchema; envelope?: TEnvelope; safeParse?: TSafeParse; }; /** * 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 { InferOutput, ParsedResult, ParsedResultError, ParsedResultSuccess, ParseFunction, ParserOptions, ParserOutput, }; //# sourceMappingURL=parser.d.ts.map