import { type Result, type AsyncResult } from './result.ts'; /** * Step function signature - transforms value and returns Result * @param value - Current value * @param context - Current context * @returns Result or AsyncResult of new value with custom error type */ export type StepFunction = (value: V, context: C) => Result | AsyncResult; /** * Context function signature - transforms context * @param context - Current context * @param value - Current value * @returns New context (sync or async) */ export type ContextFunction = (context: C, value: V) => NC | Promise; /** * Assert function signature - validates value without transformation * @param value - Current value * @param context - Current context * @returns Boolean indicating if assertion passes (sync or async) */ export type AssertFunction = (value: V, context: C) => boolean | Promise; /** * Error factory signature - matches standard Error constructor * @param message - Error message * @param options - Error options including cause * @returns New error instance */ export type ErrorFactory = new (message: string, options?: { cause?: Error; }) => E; /** * Computes the updated result type when the value type changes from V to NV. * If R is Result, it becomes Result. * If R is a custom type (from useResult), it is preserved. */ type UpdatedResultType = R extends Result ? Result : R; /** * Updates the result type when the error type changes from E to NE. * If R is Result, it becomes Result. * If R is a custom type (from useResult), it is preserved. */ type UpdatedErrorType = R extends Result ? Result : R; /** * Conditionally excludes undefined from type V if undefined is part of the union. * If undefined extends V (meaning undefined is in the union), exclude it. * Otherwise, return V unchanged. */ type ExcludeUndefinedIfPresent = undefined extends V ? Exclude : V; type CompiledPipeline = IV extends undefined ? (v?: IV) => AsyncResult : (v: IV) => AsyncResult; /** * Type-safe Instruction interface - chainable pipeline builder * @param IV - Initial Value type (input type, preserved through chain) * @param IC - Initial Context type (input type, preserved through chain) * @param V - Current Value type (transforms through pipeline) * @param C - Current Context type (transforms through pipeline) * @param E - Error type (must extend Error) * @param R - Result type (defaults to Result, can be changed by useResult) */ export interface Instruction> { /** * Compile the pipeline with context override * @param overwriteContext - Context to bind (overrides initial context) * @returns Compiled function that only needs value */ compile(overwriteContext: IC): CompiledPipeline; /** * Compile the pipeline with initial context * @returns Compiled function that only needs value */ compile(): CompiledPipeline; /** * Run the pipeline with a value (uses initial context) * When IV is undefined, the value parameter is optional * @param v - Initial value to process (optional when IV is undefined) * @returns Result of the pipeline execution (Promise where R defaults to Result) */ run(...args: IV extends undefined ? [] : [IV]): Promise; /** * Set error transformation for the instruction with custom error class * @param errorClass - Error class constructor * @param message - Error message * @returns New instruction with updated error type */ failsWith(errorClass: ErrorFactory, message: string): Instruction>; /** * Set error transformation for the instruction with generic error * @param message - Error message string * @returns New instruction with updated error type */ failsWith(message: string): Instruction>; /** * Add a processing step to the pipeline * @param fn - Step function that transforms the value * @returns New instruction with updated value type * If R is the default Result, it becomes Result * If R is a custom type (from useResult), it is preserved */ step(fn: StepFunction): Instruction>; /** * Add an assertion/validation step that doesn't transform the value * @param predicate - Predicate function that returns true if assertion passes * @param message - Optional error message if assertion fails * @returns New instruction with value type narrowed to exclude undefined (if undefined was part of V) * R type is updated to reflect the narrowed value type (custom type from useResult is preserved) */ assert(predicate: AssertFunction, message?: string): Instruction, C, E, UpdatedResultType, E>>; /** * Add a context transformation step to the pipeline * @param fn - Context function that transforms the context * @returns New instruction with updated context type * R type is preserved (custom type from useResult or default Result) */ context(fn: ContextFunction): Instruction; /** * Run the instruction and transform the result * @param fn - Function that receives the result of .run() and returns a new value * @param value - Optional initial value (required if IV is not undefined) * @returns Promise of the callback's return value */ useResult(fn: (result: R) => RR | Promise, ...args: IV extends undefined ? [] : [value: IV]): Promise; } /** * Create a new instruction without initial context * @returns New instruction instance with undefined context * * @template IV - Initial Value type (defaults to undefined) * * Note: When IV is undefined, the initial value is optional when calling run() or compile() * When IV is specified, the first step receives IV as the value type */ export declare function instruction(): Instruction>; /** * Create a new instruction with initial context * @param initialContext - Initial context for the instruction pipeline * @returns New instruction instance * * @template IV - Initial Value type (defaults to undefined) * @template IC - Initial Context type (inferred from initialContext) * * Note: When IV is undefined, the initial value is optional when calling run() or compile() * When IV is specified, the first step receives IV as the value type */ export declare function instruction(initialContext: IC): Instruction>; export {}; //# sourceMappingURL=instruction.d.ts.map