pg-workflows 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -19,6 +19,15 @@ type WorkflowRun = {
19
19
  maxRetries: number;
20
20
  jobId: string | null;
21
21
  };
22
+ type DurationObject = {
23
+ weeks?: number;
24
+ days?: number;
25
+ hours?: number;
26
+ minutes?: number;
27
+ seconds?: number;
28
+ };
29
+ type Duration = string | DurationObject;
30
+ declare function parseDuration(duration: Duration): number;
22
31
  declare enum WorkflowStatus {
23
32
  PENDING = "pending",
24
33
  RUNNING = "running",
@@ -31,77 +40,132 @@ declare enum StepType {
31
40
  PAUSE = "pause",
32
41
  RUN = "run",
33
42
  WAIT_FOR = "waitFor",
34
- WAIT_UNTIL = "waitUntil"
43
+ WAIT_UNTIL = "waitUntil",
44
+ DELAY = "delay",
45
+ POLL = "poll"
35
46
  }
36
- type Parameters = z.ZodTypeAny;
37
- type inferParameters<P extends Parameters> = P extends z.ZodTypeAny ? z.infer<P> : never;
38
- type WorkflowOptions<I extends Parameters> = {
47
+ type InputParameters = z.ZodTypeAny;
48
+ type InferInputParameters<P extends InputParameters> = P extends z.ZodTypeAny ? z.infer<P> : never;
49
+ type WorkflowOptions<I extends InputParameters> = {
39
50
  timeout?: number;
40
51
  retries?: number;
41
52
  inputSchema?: I;
42
53
  };
43
- interface WorkflowLogger {
44
- log(message: string): void;
45
- error(message: string, ...args: unknown[]): void;
46
- }
47
- type InternalWorkflowLoggerContext = {
48
- runId?: string;
49
- workflowId?: string;
50
- };
51
- interface InternalWorkflowLogger {
52
- log(message: string, context?: InternalWorkflowLoggerContext): void;
53
- error(message: string, error: Error, context?: InternalWorkflowLoggerContext): void;
54
- }
55
- type StepContext = {
54
+ type StepBaseContext = {
56
55
  run: <T>(stepId: string, handler: () => Promise<T>) => Promise<T>;
57
- waitFor: <T extends Parameters>(stepId: string, { eventName, timeout, schema }: {
58
- eventName: string;
59
- timeout?: number;
60
- schema?: T;
61
- }) => Promise<inferParameters<T>>;
62
- waitUntil: (stepId: string, { date }: {
63
- date: Date;
64
- }) => Promise<void>;
56
+ waitFor: {
57
+ <T extends InputParameters>(stepId: string, options: {
58
+ eventName: string;
59
+ schema?: T;
60
+ }): Promise<InferInputParameters<T>>;
61
+ <T extends InputParameters>(stepId: string, options: {
62
+ eventName: string;
63
+ timeout: number;
64
+ schema?: T;
65
+ }): Promise<InferInputParameters<T> | undefined>;
66
+ };
67
+ waitUntil: {
68
+ (stepId: string, date: Date): Promise<void>;
69
+ (stepId: string, dateString: string): Promise<void>;
70
+ (stepId: string, options: {
71
+ date: Date | string;
72
+ }): Promise<void>;
73
+ };
74
+ /** Delay execution for a duration (sugar over waitUntil). Alias: sleep. */
75
+ delay: (stepId: string, duration: Duration) => Promise<void>;
76
+ /** Alias for delay. */
77
+ sleep: (stepId: string, duration: Duration) => Promise<void>;
65
78
  pause: (stepId: string) => Promise<void>;
79
+ poll: <T>(stepId: string, conditionFn: () => Promise<T | false>, options?: {
80
+ interval?: Duration;
81
+ timeout?: Duration;
82
+ }) => Promise<{
83
+ timedOut: false;
84
+ data: T;
85
+ } | {
86
+ timedOut: true;
87
+ }>;
66
88
  };
67
- type WorkflowContext<T extends Parameters = Parameters> = {
68
- input: T;
69
- step: StepContext;
89
+ /**
90
+ * Plugin that extends the workflow step API with extra methods.
91
+ * @template TStepBase - The step type this plugin receives (base + previous plugins).
92
+ * @template TStepExt - The extra methods this plugin adds to step.
93
+ */
94
+ interface WorkflowPlugin<
95
+ TStepBase = StepBaseContext,
96
+ TStepExt = object
97
+ > {
98
+ name: string;
99
+ methods: (step: TStepBase) => TStepExt;
100
+ }
101
+ type WorkflowContext<
102
+ TInput extends InputParameters = InputParameters,
103
+ TStep extends StepBaseContext = StepBaseContext
104
+ > = {
105
+ input: InferInputParameters<TInput>;
106
+ step: TStep;
70
107
  workflowId: string;
71
108
  runId: string;
72
109
  timeline: Record<string, unknown>;
73
110
  logger: WorkflowLogger;
74
111
  };
75
- type WorkflowDefinition<T extends Parameters = Parameters> = {
112
+ type WorkflowDefinition<
113
+ TInput extends InputParameters = InputParameters,
114
+ TStep extends StepBaseContext = StepBaseContext
115
+ > = {
76
116
  id: string;
77
- handler: (context: WorkflowContext<inferParameters<T>>) => Promise<unknown>;
78
- inputSchema?: T;
117
+ handler: (context: WorkflowContext<TInput, TStep>) => Promise<unknown>;
118
+ inputSchema?: TInput;
79
119
  timeout?: number;
80
120
  retries?: number;
121
+ plugins?: WorkflowPlugin[];
81
122
  };
82
- type InternalStepDefinition = {
123
+ type StepInternalDefinition = {
83
124
  id: string;
84
125
  type: StepType;
85
126
  conditional: boolean;
86
127
  loop: boolean;
87
128
  isDynamic: boolean;
88
129
  };
89
- type InternalWorkflowDefinition<T extends Parameters = Parameters> = WorkflowDefinition<T> & {
90
- steps: InternalStepDefinition[];
130
+ type WorkflowInternalDefinition<
131
+ TInput extends InputParameters = InputParameters,
132
+ TStep extends StepBaseContext = StepBaseContext
133
+ > = WorkflowDefinition<TInput, TStep> & {
134
+ steps: StepInternalDefinition[];
91
135
  };
136
+ /**
137
+ * Chainable workflow factory: call as (id, handler, options) and/or use .use(plugin).
138
+ * TStepExt is the accumulated step extension from all plugins (step = StepContext & TStepExt).
139
+ */
140
+ interface WorkflowFactory<TStepExt = object> {
141
+ (id: string, handler: (context: WorkflowContext<InputParameters, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<InputParameters>): WorkflowDefinition<InputParameters, StepBaseContext & TStepExt>;
142
+ use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
143
+ }
92
144
  type WorkflowRunProgress = WorkflowRun & {
93
145
  completionPercentage: number;
94
146
  totalSteps: number;
95
147
  completedSteps: number;
96
148
  };
97
- declare function workflow<I extends Parameters>(id: string, handler: (context: WorkflowContext<inferParameters<I>>) => Promise<unknown>, { inputSchema, timeout, retries }?: WorkflowOptions<I>): WorkflowDefinition<I>;
149
+ interface WorkflowLogger {
150
+ log(message: string): void;
151
+ error(message: string, ...args: unknown[]): void;
152
+ }
153
+ type WorkflowInternalLoggerContext = {
154
+ runId?: string;
155
+ workflowId?: string;
156
+ };
157
+ interface WorkflowInternalLogger {
158
+ log(message: string, context?: WorkflowInternalLoggerContext): void;
159
+ error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;
160
+ }
161
+ declare const workflow: WorkflowFactory;
98
162
  import { Db, PgBoss } from "pg-boss";
99
163
  declare class WorkflowEngine {
100
164
  private boss;
101
165
  private db;
102
166
  private unregisteredWorkflows;
103
167
  private _started;
104
- workflows: Map<string, InternalWorkflowDefinition>;
168
+ workflows: Map<string, WorkflowInternalDefinition>;
105
169
  private logger;
106
170
  constructor({ workflows, logger, boss }?: Partial<{
107
171
  workflows: WorkflowDefinition[];
@@ -112,7 +176,7 @@ declare class WorkflowEngine {
112
176
  batchSize?: number;
113
177
  }): Promise<void>;
114
178
  stop(): Promise<void>;
115
- registerWorkflow(definition: WorkflowDefinition): Promise<WorkflowEngine>;
179
+ registerWorkflow<TStep extends StepBaseContext>(definition: WorkflowDefinition<InputParameters, TStep>): Promise<WorkflowEngine>;
116
180
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
117
181
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
118
182
  startWorkflow({ resourceId, workflowId, input, options }: {
@@ -169,10 +233,10 @@ declare class WorkflowEngine {
169
233
  resourceId?: string;
170
234
  }): Promise<WorkflowRunProgress>;
171
235
  private handleWorkflowRun;
236
+ private getCachedStepEntry;
172
237
  private runStep;
173
- private waitForEvent;
174
- private pauseStep;
175
- private waitUntilDate;
238
+ private waitStep;
239
+ private pollStep;
176
240
  private checkIfHasStarted;
177
241
  private buildLogger;
178
242
  getRuns({ resourceId, startingAfter, endingBefore, limit, statuses, workflowId }: {
@@ -199,4 +263,4 @@ declare class WorkflowEngineError extends Error {
199
263
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
200
264
  constructor(runId?: string, workflowId?: string);
201
265
  }
202
- export { workflow, inferParameters, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowOptions, WorkflowLogger, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepContext, Parameters, InternalWorkflowLoggerContext, InternalWorkflowLogger, InternalWorkflowDefinition, InternalStepDefinition };
266
+ export { workflow, parseDuration, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepInternalDefinition, StepBaseContext, InputParameters, InferInputParameters, DurationObject, Duration };
package/dist/index.d.ts CHANGED
@@ -19,6 +19,15 @@ type WorkflowRun = {
19
19
  maxRetries: number;
20
20
  jobId: string | null;
21
21
  };
22
+ type DurationObject = {
23
+ weeks?: number;
24
+ days?: number;
25
+ hours?: number;
26
+ minutes?: number;
27
+ seconds?: number;
28
+ };
29
+ type Duration = string | DurationObject;
30
+ declare function parseDuration(duration: Duration): number;
22
31
  declare enum WorkflowStatus {
23
32
  PENDING = "pending",
24
33
  RUNNING = "running",
@@ -31,77 +40,132 @@ declare enum StepType {
31
40
  PAUSE = "pause",
32
41
  RUN = "run",
33
42
  WAIT_FOR = "waitFor",
34
- WAIT_UNTIL = "waitUntil"
43
+ WAIT_UNTIL = "waitUntil",
44
+ DELAY = "delay",
45
+ POLL = "poll"
35
46
  }
36
- type Parameters = z.ZodTypeAny;
37
- type inferParameters<P extends Parameters> = P extends z.ZodTypeAny ? z.infer<P> : never;
38
- type WorkflowOptions<I extends Parameters> = {
47
+ type InputParameters = z.ZodTypeAny;
48
+ type InferInputParameters<P extends InputParameters> = P extends z.ZodTypeAny ? z.infer<P> : never;
49
+ type WorkflowOptions<I extends InputParameters> = {
39
50
  timeout?: number;
40
51
  retries?: number;
41
52
  inputSchema?: I;
42
53
  };
43
- interface WorkflowLogger {
44
- log(message: string): void;
45
- error(message: string, ...args: unknown[]): void;
46
- }
47
- type InternalWorkflowLoggerContext = {
48
- runId?: string;
49
- workflowId?: string;
50
- };
51
- interface InternalWorkflowLogger {
52
- log(message: string, context?: InternalWorkflowLoggerContext): void;
53
- error(message: string, error: Error, context?: InternalWorkflowLoggerContext): void;
54
- }
55
- type StepContext = {
54
+ type StepBaseContext = {
56
55
  run: <T>(stepId: string, handler: () => Promise<T>) => Promise<T>;
57
- waitFor: <T extends Parameters>(stepId: string, { eventName, timeout, schema }: {
58
- eventName: string;
59
- timeout?: number;
60
- schema?: T;
61
- }) => Promise<inferParameters<T>>;
62
- waitUntil: (stepId: string, { date }: {
63
- date: Date;
64
- }) => Promise<void>;
56
+ waitFor: {
57
+ <T extends InputParameters>(stepId: string, options: {
58
+ eventName: string;
59
+ schema?: T;
60
+ }): Promise<InferInputParameters<T>>;
61
+ <T extends InputParameters>(stepId: string, options: {
62
+ eventName: string;
63
+ timeout: number;
64
+ schema?: T;
65
+ }): Promise<InferInputParameters<T> | undefined>;
66
+ };
67
+ waitUntil: {
68
+ (stepId: string, date: Date): Promise<void>;
69
+ (stepId: string, dateString: string): Promise<void>;
70
+ (stepId: string, options: {
71
+ date: Date | string;
72
+ }): Promise<void>;
73
+ };
74
+ /** Delay execution for a duration (sugar over waitUntil). Alias: sleep. */
75
+ delay: (stepId: string, duration: Duration) => Promise<void>;
76
+ /** Alias for delay. */
77
+ sleep: (stepId: string, duration: Duration) => Promise<void>;
65
78
  pause: (stepId: string) => Promise<void>;
79
+ poll: <T>(stepId: string, conditionFn: () => Promise<T | false>, options?: {
80
+ interval?: Duration;
81
+ timeout?: Duration;
82
+ }) => Promise<{
83
+ timedOut: false;
84
+ data: T;
85
+ } | {
86
+ timedOut: true;
87
+ }>;
66
88
  };
67
- type WorkflowContext<T extends Parameters = Parameters> = {
68
- input: T;
69
- step: StepContext;
89
+ /**
90
+ * Plugin that extends the workflow step API with extra methods.
91
+ * @template TStepBase - The step type this plugin receives (base + previous plugins).
92
+ * @template TStepExt - The extra methods this plugin adds to step.
93
+ */
94
+ interface WorkflowPlugin<
95
+ TStepBase = StepBaseContext,
96
+ TStepExt = object
97
+ > {
98
+ name: string;
99
+ methods: (step: TStepBase) => TStepExt;
100
+ }
101
+ type WorkflowContext<
102
+ TInput extends InputParameters = InputParameters,
103
+ TStep extends StepBaseContext = StepBaseContext
104
+ > = {
105
+ input: InferInputParameters<TInput>;
106
+ step: TStep;
70
107
  workflowId: string;
71
108
  runId: string;
72
109
  timeline: Record<string, unknown>;
73
110
  logger: WorkflowLogger;
74
111
  };
75
- type WorkflowDefinition<T extends Parameters = Parameters> = {
112
+ type WorkflowDefinition<
113
+ TInput extends InputParameters = InputParameters,
114
+ TStep extends StepBaseContext = StepBaseContext
115
+ > = {
76
116
  id: string;
77
- handler: (context: WorkflowContext<inferParameters<T>>) => Promise<unknown>;
78
- inputSchema?: T;
117
+ handler: (context: WorkflowContext<TInput, TStep>) => Promise<unknown>;
118
+ inputSchema?: TInput;
79
119
  timeout?: number;
80
120
  retries?: number;
121
+ plugins?: WorkflowPlugin[];
81
122
  };
82
- type InternalStepDefinition = {
123
+ type StepInternalDefinition = {
83
124
  id: string;
84
125
  type: StepType;
85
126
  conditional: boolean;
86
127
  loop: boolean;
87
128
  isDynamic: boolean;
88
129
  };
89
- type InternalWorkflowDefinition<T extends Parameters = Parameters> = WorkflowDefinition<T> & {
90
- steps: InternalStepDefinition[];
130
+ type WorkflowInternalDefinition<
131
+ TInput extends InputParameters = InputParameters,
132
+ TStep extends StepBaseContext = StepBaseContext
133
+ > = WorkflowDefinition<TInput, TStep> & {
134
+ steps: StepInternalDefinition[];
91
135
  };
136
+ /**
137
+ * Chainable workflow factory: call as (id, handler, options) and/or use .use(plugin).
138
+ * TStepExt is the accumulated step extension from all plugins (step = StepContext & TStepExt).
139
+ */
140
+ interface WorkflowFactory<TStepExt = object> {
141
+ (id: string, handler: (context: WorkflowContext<InputParameters, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<InputParameters>): WorkflowDefinition<InputParameters, StepBaseContext & TStepExt>;
142
+ use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
143
+ }
92
144
  type WorkflowRunProgress = WorkflowRun & {
93
145
  completionPercentage: number;
94
146
  totalSteps: number;
95
147
  completedSteps: number;
96
148
  };
97
- declare function workflow<I extends Parameters>(id: string, handler: (context: WorkflowContext<inferParameters<I>>) => Promise<unknown>, { inputSchema, timeout, retries }?: WorkflowOptions<I>): WorkflowDefinition<I>;
149
+ interface WorkflowLogger {
150
+ log(message: string): void;
151
+ error(message: string, ...args: unknown[]): void;
152
+ }
153
+ type WorkflowInternalLoggerContext = {
154
+ runId?: string;
155
+ workflowId?: string;
156
+ };
157
+ interface WorkflowInternalLogger {
158
+ log(message: string, context?: WorkflowInternalLoggerContext): void;
159
+ error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;
160
+ }
161
+ declare const workflow: WorkflowFactory;
98
162
  import { Db, PgBoss } from "pg-boss";
99
163
  declare class WorkflowEngine {
100
164
  private boss;
101
165
  private db;
102
166
  private unregisteredWorkflows;
103
167
  private _started;
104
- workflows: Map<string, InternalWorkflowDefinition>;
168
+ workflows: Map<string, WorkflowInternalDefinition>;
105
169
  private logger;
106
170
  constructor({ workflows, logger, boss }?: Partial<{
107
171
  workflows: WorkflowDefinition[];
@@ -112,7 +176,7 @@ declare class WorkflowEngine {
112
176
  batchSize?: number;
113
177
  }): Promise<void>;
114
178
  stop(): Promise<void>;
115
- registerWorkflow(definition: WorkflowDefinition): Promise<WorkflowEngine>;
179
+ registerWorkflow<TStep extends StepBaseContext>(definition: WorkflowDefinition<InputParameters, TStep>): Promise<WorkflowEngine>;
116
180
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
117
181
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
118
182
  startWorkflow({ resourceId, workflowId, input, options }: {
@@ -169,10 +233,10 @@ declare class WorkflowEngine {
169
233
  resourceId?: string;
170
234
  }): Promise<WorkflowRunProgress>;
171
235
  private handleWorkflowRun;
236
+ private getCachedStepEntry;
172
237
  private runStep;
173
- private waitForEvent;
174
- private pauseStep;
175
- private waitUntilDate;
238
+ private waitStep;
239
+ private pollStep;
176
240
  private checkIfHasStarted;
177
241
  private buildLogger;
178
242
  getRuns({ resourceId, startingAfter, endingBefore, limit, statuses, workflowId }: {
@@ -199,4 +263,4 @@ declare class WorkflowEngineError extends Error {
199
263
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
200
264
  constructor(runId?: string, workflowId?: string);
201
265
  }
202
- export { workflow, inferParameters, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowOptions, WorkflowLogger, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepContext, Parameters, InternalWorkflowLoggerContext, InternalWorkflowLogger, InternalWorkflowDefinition, InternalStepDefinition };
266
+ export { workflow, parseDuration, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepInternalDefinition, StepBaseContext, InputParameters, InferInputParameters, DurationObject, Duration };