@prostojs/wf
Version:
Generic workflow framework
89 lines • 2.84 kB
text/typescript
//#region src/types.d.ts
type TStepOutput<IR> = void | {
inputRequired: IR;
expires?: number;
errorList?: unknown;
};
type TStepHandler<T, I, IR> = (ctx: T, input: I) => TStepOutput<IR> | StepRetriableError<IR> | Promise<TStepOutput<IR> | StepRetriableError<IR>>;
interface TFlowState<T> {
schemaId: string;
context: T;
indexes: number[];
meta?: Record<string, unknown>;
}
interface TFlowSpyData<T, IR> {
state: TFlowState<T>;
finished: boolean;
stepId: string;
inputRequired?: IR;
interrupt?: boolean;
error?: Error;
expires?: number;
errorList?: unknown;
}
interface TFlowFinished<T, _IR> {
finished: true;
state: TFlowState<T>;
stepId: string;
resume?: never;
retry?: never;
error?: never;
inputRequired?: never;
expires?: never;
errorList?: never;
}
interface TFlowPaused<T, I, IR> {
finished: false;
state: TFlowState<T>;
stepId: string;
inputRequired: IR;
resume: (input: I) => Promise<TFlowOutput<T, unknown, IR>>;
expires?: number;
errorList?: unknown;
error?: never;
retry?: never;
}
interface TFlowFailed<T, I, IR> {
finished: false;
state: TFlowState<T>;
stepId: string;
error: Error;
retry: (input?: I) => Promise<TFlowOutput<T, unknown, IR>>;
inputRequired?: IR;
expires?: number;
errorList?: unknown;
resume?: never;
}
type TFlowOutput<T, I, IR> = TFlowFinished<T, IR> | TFlowPaused<T, I, IR> | TFlowFailed<T, I, IR>;
type TWorkflowStepConditionFn<T> = (ctx: T) => boolean | Promise<boolean>;
interface TWorkflowStepSchemaObj<T, I> {
condition?: string | TWorkflowStepConditionFn<T>;
id: string;
input?: I;
steps?: never;
}
interface TSubWorkflowSchemaObj<T> {
condition?: string | TWorkflowStepConditionFn<T>;
while?: string | TWorkflowStepConditionFn<T>;
steps: TWorkflowSchema<T>;
id?: never;
}
type TWorkflowControl<T> = {
continue: string | TWorkflowStepConditionFn<T>;
break?: never;
} | {
break: string | TWorkflowStepConditionFn<T>;
continue?: never;
};
type TWorkflowItem<T> = TWorkflowStepSchemaObj<T, any> | TSubWorkflowSchemaObj<T> | TWorkflowControl<T> | string;
type TWorkflowSchema<T> = TWorkflowItem<T>[];
declare class StepRetriableError<IR> extends Error {
readonly originalError: Error;
errorList?: unknown | undefined;
readonly inputRequired?: IR | undefined;
expires?: number | undefined;
name: string;
constructor(originalError: Error, errorList?: unknown | undefined, inputRequired?: IR | undefined, expires?: number | undefined);
}
//#endregion
export { TFlowPaused as a, TStepHandler as c, TWorkflowControl as d, TWorkflowItem as f, TWorkflowStepSchemaObj as h, TFlowOutput as i, TStepOutput as l, TWorkflowStepConditionFn as m, TFlowFailed as n, TFlowSpyData as o, TWorkflowSchema as p, TFlowFinished as r, TFlowState as s, StepRetriableError as t, TSubWorkflowSchemaObj as u };