@rytejs/core 0.2.0 → 0.4.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.cjs +294 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +100 -8
- package/dist/index.d.ts +100 -8
- package/dist/index.js +288 -44
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ZodType, z } from 'zod';
|
|
2
2
|
|
|
3
3
|
interface WorkflowConfig {
|
|
4
|
+
modelVersion?: number;
|
|
4
5
|
states: Record<string, ZodType>;
|
|
5
6
|
commands: Record<string, ZodType>;
|
|
6
7
|
events: Record<string, ZodType>;
|
|
@@ -29,7 +30,7 @@ type Workflow<TConfig extends WorkflowConfig = WorkflowConfig> = {
|
|
|
29
30
|
}[StateNames<TConfig>];
|
|
30
31
|
type PipelineError<TConfig extends WorkflowConfig = WorkflowConfig> = {
|
|
31
32
|
category: "validation";
|
|
32
|
-
source: "command" | "state" | "event" | "transition";
|
|
33
|
+
source: "command" | "state" | "event" | "transition" | "restore";
|
|
33
34
|
issues: z.core.$ZodIssue[];
|
|
34
35
|
message: string;
|
|
35
36
|
} | {
|
|
@@ -54,9 +55,9 @@ type DispatchResult<TConfig extends WorkflowConfig = WorkflowConfig> = {
|
|
|
54
55
|
};
|
|
55
56
|
/** Thrown internally when Zod validation fails during dispatch. */
|
|
56
57
|
declare class ValidationError extends Error {
|
|
57
|
-
readonly source: "command" | "state" | "event" | "transition";
|
|
58
|
+
readonly source: "command" | "state" | "event" | "transition" | "restore";
|
|
58
59
|
readonly issues: z.core.$ZodIssue[];
|
|
59
|
-
constructor(source: "command" | "state" | "event" | "transition", issues: z.core.$ZodIssue[]);
|
|
60
|
+
constructor(source: "command" | "state" | "event" | "transition" | "restore", issues: z.core.$ZodIssue[]);
|
|
60
61
|
}
|
|
61
62
|
/** Thrown internally when a handler calls ctx.error(). Caught by the router. */
|
|
62
63
|
declare class DomainErrorSignal extends Error {
|
|
@@ -65,6 +66,17 @@ declare class DomainErrorSignal extends Error {
|
|
|
65
66
|
constructor(code: string, data: unknown);
|
|
66
67
|
}
|
|
67
68
|
|
|
69
|
+
/** A plain, JSON-safe representation of a workflow's state. */
|
|
70
|
+
interface WorkflowSnapshot<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly definitionName: string;
|
|
73
|
+
readonly state: StateNames<TConfig>;
|
|
74
|
+
readonly data: unknown;
|
|
75
|
+
readonly createdAt: string;
|
|
76
|
+
readonly updatedAt: string;
|
|
77
|
+
readonly modelVersion: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
68
80
|
/** The result of defineWorkflow() — holds schemas and creates workflow instances. */
|
|
69
81
|
interface WorkflowDefinition<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
70
82
|
readonly config: TConfig;
|
|
@@ -78,6 +90,14 @@ interface WorkflowDefinition<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
|
78
90
|
getEventSchema(eventName: string): ZodType;
|
|
79
91
|
getErrorSchema(errorCode: string): ZodType;
|
|
80
92
|
hasState(stateName: string): boolean;
|
|
93
|
+
snapshot(workflow: Workflow<TConfig>): WorkflowSnapshot<TConfig>;
|
|
94
|
+
restore(snapshot: WorkflowSnapshot<TConfig>): {
|
|
95
|
+
ok: true;
|
|
96
|
+
workflow: Workflow<TConfig>;
|
|
97
|
+
} | {
|
|
98
|
+
ok: false;
|
|
99
|
+
error: ValidationError;
|
|
100
|
+
};
|
|
81
101
|
}
|
|
82
102
|
/**
|
|
83
103
|
* Creates a workflow definition from a name and Zod schema configuration.
|
|
@@ -125,6 +145,9 @@ interface Context<TConfig extends WorkflowConfig, TDeps, TState extends StateNam
|
|
|
125
145
|
/** Terminal handler function — receives fully typed context with state and command narrowing. */
|
|
126
146
|
type Handler<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig>, TCommand extends CommandNames<TConfig>> = (ctx: Context<TConfig, TDeps, TState, TCommand>) => void | Promise<void>;
|
|
127
147
|
|
|
148
|
+
/** The lifecycle hook event names. */
|
|
149
|
+
type HookEvent = "dispatch:start" | "dispatch:end" | "transition" | "error" | "event";
|
|
150
|
+
|
|
128
151
|
/**
|
|
129
152
|
* Koa-style middleware function with full context narrowing via defaults.
|
|
130
153
|
*
|
|
@@ -134,11 +157,59 @@ type Handler<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TC
|
|
|
134
157
|
*/
|
|
135
158
|
type Middleware<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig> = StateNames<TConfig>, TCommand extends CommandNames<TConfig> = CommandNames<TConfig>> = (ctx: Context<TConfig, TDeps, TState, TCommand>, next: () => Promise<void>) => Promise<void>;
|
|
136
159
|
|
|
160
|
+
/** A function that transforms a snapshot's data from one version to the next. */
|
|
161
|
+
type MigrationFn = (snapshot: WorkflowSnapshot) => WorkflowSnapshot;
|
|
162
|
+
/** A validated migration pipeline ready to transform snapshots. */
|
|
163
|
+
interface MigrationPipeline<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
164
|
+
readonly definition: WorkflowDefinition<TConfig>;
|
|
165
|
+
readonly targetVersion: number;
|
|
166
|
+
readonly migrations: ReadonlyMap<number, MigrationFn>;
|
|
167
|
+
}
|
|
168
|
+
/** Result of migrate(). */
|
|
169
|
+
type MigrateResult = {
|
|
170
|
+
ok: true;
|
|
171
|
+
snapshot: WorkflowSnapshot;
|
|
172
|
+
} | {
|
|
173
|
+
ok: false;
|
|
174
|
+
error: MigrationError;
|
|
175
|
+
};
|
|
176
|
+
/** Options for migrate(). */
|
|
177
|
+
interface MigrateOptions {
|
|
178
|
+
onStep?: (fromVersion: number, toVersion: number, snapshot: WorkflowSnapshot) => void;
|
|
179
|
+
onError?: (error: MigrationError) => void;
|
|
180
|
+
}
|
|
181
|
+
/** Error thrown when a migration step fails. */
|
|
182
|
+
declare class MigrationError extends Error {
|
|
183
|
+
readonly fromVersion: number;
|
|
184
|
+
readonly toVersion: number;
|
|
185
|
+
readonly cause: unknown;
|
|
186
|
+
constructor(fromVersion: number, toVersion: number, cause: unknown);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Creates a validated migration pipeline from a definition and version-keyed transform functions.
|
|
190
|
+
* Each key is the target version — the function transforms from (key - 1) to key.
|
|
191
|
+
*/
|
|
192
|
+
declare function defineMigrations<TConfig extends WorkflowConfig>(definition: WorkflowDefinition<TConfig>, migrationMap: Record<number, MigrationFn>): MigrationPipeline<TConfig>;
|
|
193
|
+
/**
|
|
194
|
+
* Runs the migration chain from the snapshot's modelVersion to the pipeline's targetVersion.
|
|
195
|
+
* Returns a Result. Auto-stamps modelVersion after each step.
|
|
196
|
+
*/
|
|
197
|
+
declare function migrate<TConfig extends WorkflowConfig>(pipeline: MigrationPipeline<TConfig>, snapshot: WorkflowSnapshot, options?: MigrateOptions): MigrateResult;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Read-only subset of Context for hook callbacks.
|
|
201
|
+
* Includes context-key access (set/get) but excludes dispatch mutation methods.
|
|
202
|
+
*/
|
|
203
|
+
type ReadonlyContext<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig> = StateNames<TConfig>, TCommand extends CommandNames<TConfig> = CommandNames<TConfig>> = Omit<Context<TConfig, TDeps, TState, TCommand>, "update" | "transition" | "emit" | "error" | "getWorkflowSnapshot">;
|
|
204
|
+
|
|
137
205
|
type AnyMiddleware = (ctx: any, next: () => Promise<void>) => Promise<void>;
|
|
138
206
|
type HandlerEntry = {
|
|
139
207
|
inlineMiddleware: AnyMiddleware[];
|
|
140
208
|
handler: AnyMiddleware;
|
|
141
209
|
};
|
|
210
|
+
interface RouterOptions {
|
|
211
|
+
onHookError?: (error: unknown) => void;
|
|
212
|
+
}
|
|
142
213
|
declare class StateBuilder<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig>> {
|
|
143
214
|
/** @internal */ readonly middleware: AnyMiddleware[];
|
|
144
215
|
/** @internal */ readonly handlers: Map<string, HandlerEntry>;
|
|
@@ -158,15 +229,26 @@ declare class WorkflowRouter<TConfig extends WorkflowConfig, TDeps = {}> {
|
|
|
158
229
|
private singleStateBuilders;
|
|
159
230
|
private multiStateBuilders;
|
|
160
231
|
private wildcardHandlers;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
232
|
+
private hookRegistry;
|
|
233
|
+
private readonly onHookError;
|
|
234
|
+
constructor(definition: WorkflowDefinition<TConfig>, deps?: TDeps, options?: RouterOptions);
|
|
235
|
+
/** Adds global middleware, merges another router, or applies a plugin. */
|
|
236
|
+
use(arg: ((ctx: Context<TConfig, TDeps>, next: () => Promise<void>) => Promise<void>) | WorkflowRouter<TConfig, TDeps> | Plugin<TConfig, TDeps>): this;
|
|
164
237
|
private merge;
|
|
165
238
|
private mergeStateBuilders;
|
|
166
239
|
/** Registers handlers for one or more states. */
|
|
167
240
|
state<P extends StateNames<TConfig> | readonly StateNames<TConfig>[]>(name: P, setup: (state: StateBuilder<TConfig, TDeps, P extends readonly (infer S)[] ? S & StateNames<TConfig> : P & StateNames<TConfig>>) => void): this;
|
|
241
|
+
/** Registers a lifecycle hook callback. */
|
|
242
|
+
on(event: "dispatch:start", callback: (ctx: ReadonlyContext<TConfig, TDeps>) => void | Promise<void>): this;
|
|
243
|
+
on(event: "dispatch:end", callback: (ctx: ReadonlyContext<TConfig, TDeps>, result: DispatchResult<TConfig>) => void | Promise<void>): this;
|
|
244
|
+
on(event: "transition", callback: (from: StateNames<TConfig>, to: StateNames<TConfig>, workflow: Workflow<TConfig>) => void | Promise<void>): this;
|
|
245
|
+
on(event: "error", callback: (error: PipelineError<TConfig>, ctx: ReadonlyContext<TConfig, TDeps>) => void | Promise<void>): this;
|
|
246
|
+
on(event: "event", callback: (event: {
|
|
247
|
+
type: EventNames<TConfig>;
|
|
248
|
+
data: unknown;
|
|
249
|
+
}, workflow: Workflow<TConfig>) => void | Promise<void>): this;
|
|
168
250
|
/** Registers a wildcard handler that matches any state. */
|
|
169
|
-
on<C extends CommandNames<TConfig>>(
|
|
251
|
+
on<C extends CommandNames<TConfig>>(state: "*", command: C, ...fns: [
|
|
170
252
|
...AnyMiddleware[],
|
|
171
253
|
(ctx: Context<TConfig, TDeps, StateNames<TConfig>, C>) => void | Promise<void>
|
|
172
254
|
]): this;
|
|
@@ -177,4 +259,14 @@ declare class WorkflowRouter<TConfig extends WorkflowConfig, TDeps = {}> {
|
|
|
177
259
|
}): Promise<DispatchResult<TConfig>>;
|
|
178
260
|
}
|
|
179
261
|
|
|
180
|
-
|
|
262
|
+
declare const PLUGIN_SYMBOL: unique symbol;
|
|
263
|
+
/** A branded plugin function that can be passed to router.use(). */
|
|
264
|
+
type Plugin<TConfig extends WorkflowConfig, TDeps> = ((router: WorkflowRouter<TConfig, TDeps>) => void) & {
|
|
265
|
+
readonly [PLUGIN_SYMBOL]: true;
|
|
266
|
+
};
|
|
267
|
+
/** Brands a function as a Ryte plugin for use with router.use(). */
|
|
268
|
+
declare function definePlugin<TConfig extends WorkflowConfig, TDeps>(fn: (router: WorkflowRouter<TConfig, TDeps>) => void): Plugin<TConfig, TDeps>;
|
|
269
|
+
/** Checks whether a value is a branded Ryte plugin. */
|
|
270
|
+
declare function isPlugin(value: unknown): value is Plugin<WorkflowConfig, unknown>;
|
|
271
|
+
|
|
272
|
+
export { type CommandNames, type CommandPayload, type Context, type ContextKey, type DispatchResult, DomainErrorSignal, type ErrorCodes, type ErrorData, type EventData, type EventNames, type Handler, type HookEvent, type Middleware, type MigrateOptions, type MigrateResult, MigrationError, type MigrationFn, type MigrationPipeline, type PipelineError, type Plugin, type ReadonlyContext, type RouterOptions, type StateData, type StateNames, ValidationError, type Workflow, type WorkflowConfig, type WorkflowDefinition, type WorkflowOf, WorkflowRouter, type WorkflowSnapshot, createKey, defineMigrations, definePlugin, defineWorkflow, isPlugin, migrate };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ZodType, z } from 'zod';
|
|
2
2
|
|
|
3
3
|
interface WorkflowConfig {
|
|
4
|
+
modelVersion?: number;
|
|
4
5
|
states: Record<string, ZodType>;
|
|
5
6
|
commands: Record<string, ZodType>;
|
|
6
7
|
events: Record<string, ZodType>;
|
|
@@ -29,7 +30,7 @@ type Workflow<TConfig extends WorkflowConfig = WorkflowConfig> = {
|
|
|
29
30
|
}[StateNames<TConfig>];
|
|
30
31
|
type PipelineError<TConfig extends WorkflowConfig = WorkflowConfig> = {
|
|
31
32
|
category: "validation";
|
|
32
|
-
source: "command" | "state" | "event" | "transition";
|
|
33
|
+
source: "command" | "state" | "event" | "transition" | "restore";
|
|
33
34
|
issues: z.core.$ZodIssue[];
|
|
34
35
|
message: string;
|
|
35
36
|
} | {
|
|
@@ -54,9 +55,9 @@ type DispatchResult<TConfig extends WorkflowConfig = WorkflowConfig> = {
|
|
|
54
55
|
};
|
|
55
56
|
/** Thrown internally when Zod validation fails during dispatch. */
|
|
56
57
|
declare class ValidationError extends Error {
|
|
57
|
-
readonly source: "command" | "state" | "event" | "transition";
|
|
58
|
+
readonly source: "command" | "state" | "event" | "transition" | "restore";
|
|
58
59
|
readonly issues: z.core.$ZodIssue[];
|
|
59
|
-
constructor(source: "command" | "state" | "event" | "transition", issues: z.core.$ZodIssue[]);
|
|
60
|
+
constructor(source: "command" | "state" | "event" | "transition" | "restore", issues: z.core.$ZodIssue[]);
|
|
60
61
|
}
|
|
61
62
|
/** Thrown internally when a handler calls ctx.error(). Caught by the router. */
|
|
62
63
|
declare class DomainErrorSignal extends Error {
|
|
@@ -65,6 +66,17 @@ declare class DomainErrorSignal extends Error {
|
|
|
65
66
|
constructor(code: string, data: unknown);
|
|
66
67
|
}
|
|
67
68
|
|
|
69
|
+
/** A plain, JSON-safe representation of a workflow's state. */
|
|
70
|
+
interface WorkflowSnapshot<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly definitionName: string;
|
|
73
|
+
readonly state: StateNames<TConfig>;
|
|
74
|
+
readonly data: unknown;
|
|
75
|
+
readonly createdAt: string;
|
|
76
|
+
readonly updatedAt: string;
|
|
77
|
+
readonly modelVersion: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
68
80
|
/** The result of defineWorkflow() — holds schemas and creates workflow instances. */
|
|
69
81
|
interface WorkflowDefinition<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
70
82
|
readonly config: TConfig;
|
|
@@ -78,6 +90,14 @@ interface WorkflowDefinition<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
|
78
90
|
getEventSchema(eventName: string): ZodType;
|
|
79
91
|
getErrorSchema(errorCode: string): ZodType;
|
|
80
92
|
hasState(stateName: string): boolean;
|
|
93
|
+
snapshot(workflow: Workflow<TConfig>): WorkflowSnapshot<TConfig>;
|
|
94
|
+
restore(snapshot: WorkflowSnapshot<TConfig>): {
|
|
95
|
+
ok: true;
|
|
96
|
+
workflow: Workflow<TConfig>;
|
|
97
|
+
} | {
|
|
98
|
+
ok: false;
|
|
99
|
+
error: ValidationError;
|
|
100
|
+
};
|
|
81
101
|
}
|
|
82
102
|
/**
|
|
83
103
|
* Creates a workflow definition from a name and Zod schema configuration.
|
|
@@ -125,6 +145,9 @@ interface Context<TConfig extends WorkflowConfig, TDeps, TState extends StateNam
|
|
|
125
145
|
/** Terminal handler function — receives fully typed context with state and command narrowing. */
|
|
126
146
|
type Handler<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig>, TCommand extends CommandNames<TConfig>> = (ctx: Context<TConfig, TDeps, TState, TCommand>) => void | Promise<void>;
|
|
127
147
|
|
|
148
|
+
/** The lifecycle hook event names. */
|
|
149
|
+
type HookEvent = "dispatch:start" | "dispatch:end" | "transition" | "error" | "event";
|
|
150
|
+
|
|
128
151
|
/**
|
|
129
152
|
* Koa-style middleware function with full context narrowing via defaults.
|
|
130
153
|
*
|
|
@@ -134,11 +157,59 @@ type Handler<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TC
|
|
|
134
157
|
*/
|
|
135
158
|
type Middleware<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig> = StateNames<TConfig>, TCommand extends CommandNames<TConfig> = CommandNames<TConfig>> = (ctx: Context<TConfig, TDeps, TState, TCommand>, next: () => Promise<void>) => Promise<void>;
|
|
136
159
|
|
|
160
|
+
/** A function that transforms a snapshot's data from one version to the next. */
|
|
161
|
+
type MigrationFn = (snapshot: WorkflowSnapshot) => WorkflowSnapshot;
|
|
162
|
+
/** A validated migration pipeline ready to transform snapshots. */
|
|
163
|
+
interface MigrationPipeline<TConfig extends WorkflowConfig = WorkflowConfig> {
|
|
164
|
+
readonly definition: WorkflowDefinition<TConfig>;
|
|
165
|
+
readonly targetVersion: number;
|
|
166
|
+
readonly migrations: ReadonlyMap<number, MigrationFn>;
|
|
167
|
+
}
|
|
168
|
+
/** Result of migrate(). */
|
|
169
|
+
type MigrateResult = {
|
|
170
|
+
ok: true;
|
|
171
|
+
snapshot: WorkflowSnapshot;
|
|
172
|
+
} | {
|
|
173
|
+
ok: false;
|
|
174
|
+
error: MigrationError;
|
|
175
|
+
};
|
|
176
|
+
/** Options for migrate(). */
|
|
177
|
+
interface MigrateOptions {
|
|
178
|
+
onStep?: (fromVersion: number, toVersion: number, snapshot: WorkflowSnapshot) => void;
|
|
179
|
+
onError?: (error: MigrationError) => void;
|
|
180
|
+
}
|
|
181
|
+
/** Error thrown when a migration step fails. */
|
|
182
|
+
declare class MigrationError extends Error {
|
|
183
|
+
readonly fromVersion: number;
|
|
184
|
+
readonly toVersion: number;
|
|
185
|
+
readonly cause: unknown;
|
|
186
|
+
constructor(fromVersion: number, toVersion: number, cause: unknown);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Creates a validated migration pipeline from a definition and version-keyed transform functions.
|
|
190
|
+
* Each key is the target version — the function transforms from (key - 1) to key.
|
|
191
|
+
*/
|
|
192
|
+
declare function defineMigrations<TConfig extends WorkflowConfig>(definition: WorkflowDefinition<TConfig>, migrationMap: Record<number, MigrationFn>): MigrationPipeline<TConfig>;
|
|
193
|
+
/**
|
|
194
|
+
* Runs the migration chain from the snapshot's modelVersion to the pipeline's targetVersion.
|
|
195
|
+
* Returns a Result. Auto-stamps modelVersion after each step.
|
|
196
|
+
*/
|
|
197
|
+
declare function migrate<TConfig extends WorkflowConfig>(pipeline: MigrationPipeline<TConfig>, snapshot: WorkflowSnapshot, options?: MigrateOptions): MigrateResult;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Read-only subset of Context for hook callbacks.
|
|
201
|
+
* Includes context-key access (set/get) but excludes dispatch mutation methods.
|
|
202
|
+
*/
|
|
203
|
+
type ReadonlyContext<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig> = StateNames<TConfig>, TCommand extends CommandNames<TConfig> = CommandNames<TConfig>> = Omit<Context<TConfig, TDeps, TState, TCommand>, "update" | "transition" | "emit" | "error" | "getWorkflowSnapshot">;
|
|
204
|
+
|
|
137
205
|
type AnyMiddleware = (ctx: any, next: () => Promise<void>) => Promise<void>;
|
|
138
206
|
type HandlerEntry = {
|
|
139
207
|
inlineMiddleware: AnyMiddleware[];
|
|
140
208
|
handler: AnyMiddleware;
|
|
141
209
|
};
|
|
210
|
+
interface RouterOptions {
|
|
211
|
+
onHookError?: (error: unknown) => void;
|
|
212
|
+
}
|
|
142
213
|
declare class StateBuilder<TConfig extends WorkflowConfig, TDeps, TState extends StateNames<TConfig>> {
|
|
143
214
|
/** @internal */ readonly middleware: AnyMiddleware[];
|
|
144
215
|
/** @internal */ readonly handlers: Map<string, HandlerEntry>;
|
|
@@ -158,15 +229,26 @@ declare class WorkflowRouter<TConfig extends WorkflowConfig, TDeps = {}> {
|
|
|
158
229
|
private singleStateBuilders;
|
|
159
230
|
private multiStateBuilders;
|
|
160
231
|
private wildcardHandlers;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
232
|
+
private hookRegistry;
|
|
233
|
+
private readonly onHookError;
|
|
234
|
+
constructor(definition: WorkflowDefinition<TConfig>, deps?: TDeps, options?: RouterOptions);
|
|
235
|
+
/** Adds global middleware, merges another router, or applies a plugin. */
|
|
236
|
+
use(arg: ((ctx: Context<TConfig, TDeps>, next: () => Promise<void>) => Promise<void>) | WorkflowRouter<TConfig, TDeps> | Plugin<TConfig, TDeps>): this;
|
|
164
237
|
private merge;
|
|
165
238
|
private mergeStateBuilders;
|
|
166
239
|
/** Registers handlers for one or more states. */
|
|
167
240
|
state<P extends StateNames<TConfig> | readonly StateNames<TConfig>[]>(name: P, setup: (state: StateBuilder<TConfig, TDeps, P extends readonly (infer S)[] ? S & StateNames<TConfig> : P & StateNames<TConfig>>) => void): this;
|
|
241
|
+
/** Registers a lifecycle hook callback. */
|
|
242
|
+
on(event: "dispatch:start", callback: (ctx: ReadonlyContext<TConfig, TDeps>) => void | Promise<void>): this;
|
|
243
|
+
on(event: "dispatch:end", callback: (ctx: ReadonlyContext<TConfig, TDeps>, result: DispatchResult<TConfig>) => void | Promise<void>): this;
|
|
244
|
+
on(event: "transition", callback: (from: StateNames<TConfig>, to: StateNames<TConfig>, workflow: Workflow<TConfig>) => void | Promise<void>): this;
|
|
245
|
+
on(event: "error", callback: (error: PipelineError<TConfig>, ctx: ReadonlyContext<TConfig, TDeps>) => void | Promise<void>): this;
|
|
246
|
+
on(event: "event", callback: (event: {
|
|
247
|
+
type: EventNames<TConfig>;
|
|
248
|
+
data: unknown;
|
|
249
|
+
}, workflow: Workflow<TConfig>) => void | Promise<void>): this;
|
|
168
250
|
/** Registers a wildcard handler that matches any state. */
|
|
169
|
-
on<C extends CommandNames<TConfig>>(
|
|
251
|
+
on<C extends CommandNames<TConfig>>(state: "*", command: C, ...fns: [
|
|
170
252
|
...AnyMiddleware[],
|
|
171
253
|
(ctx: Context<TConfig, TDeps, StateNames<TConfig>, C>) => void | Promise<void>
|
|
172
254
|
]): this;
|
|
@@ -177,4 +259,14 @@ declare class WorkflowRouter<TConfig extends WorkflowConfig, TDeps = {}> {
|
|
|
177
259
|
}): Promise<DispatchResult<TConfig>>;
|
|
178
260
|
}
|
|
179
261
|
|
|
180
|
-
|
|
262
|
+
declare const PLUGIN_SYMBOL: unique symbol;
|
|
263
|
+
/** A branded plugin function that can be passed to router.use(). */
|
|
264
|
+
type Plugin<TConfig extends WorkflowConfig, TDeps> = ((router: WorkflowRouter<TConfig, TDeps>) => void) & {
|
|
265
|
+
readonly [PLUGIN_SYMBOL]: true;
|
|
266
|
+
};
|
|
267
|
+
/** Brands a function as a Ryte plugin for use with router.use(). */
|
|
268
|
+
declare function definePlugin<TConfig extends WorkflowConfig, TDeps>(fn: (router: WorkflowRouter<TConfig, TDeps>) => void): Plugin<TConfig, TDeps>;
|
|
269
|
+
/** Checks whether a value is a branded Ryte plugin. */
|
|
270
|
+
declare function isPlugin(value: unknown): value is Plugin<WorkflowConfig, unknown>;
|
|
271
|
+
|
|
272
|
+
export { type CommandNames, type CommandPayload, type Context, type ContextKey, type DispatchResult, DomainErrorSignal, type ErrorCodes, type ErrorData, type EventData, type EventNames, type Handler, type HookEvent, type Middleware, type MigrateOptions, type MigrateResult, MigrationError, type MigrationFn, type MigrationPipeline, type PipelineError, type Plugin, type ReadonlyContext, type RouterOptions, type StateData, type StateNames, ValidationError, type Workflow, type WorkflowConfig, type WorkflowDefinition, type WorkflowOf, WorkflowRouter, type WorkflowSnapshot, createKey, defineMigrations, definePlugin, defineWorkflow, isPlugin, migrate };
|