@wooksjs/event-wf 0.7.7 → 0.7.9

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.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  import * as _wooksjs_event_core from '@wooksjs/event-core';
2
2
  import { EventContextOptions, EventKindSeeds, EventContext } from '@wooksjs/event-core';
3
3
  export { EventContext, EventContextOptions, useLogger, useRouteParams } from '@wooksjs/event-core';
4
+ import * as _prostojs_wf_outlets from '@prostojs/wf/outlets';
5
+ import { WfStateStrategy, WfOutlet, WfOutletRequest } from '@prostojs/wf/outlets';
6
+ export { EncapsulatedStateStrategy, HandleStateStrategy, WfOutlet, WfOutletRequest, WfOutletResult, WfState, WfStateStore, WfStateStoreMemory, WfStateStrategy, outlet, outletEmail, outletHttp } from '@prostojs/wf/outlets';
7
+ import { TFlowOutput, Workflow, Step, TWorkflowSpy, TStepHandler, TWorkflowSchema } from '@prostojs/wf';
8
+ export { StepRetriableError, TStepHandler, TWorkflowSchema } from '@prostojs/wf';
4
9
  import * as wooks from 'wooks';
5
10
  import { Wooks, TWooksHandler, TWooksOptions, WooksAdapterBase } from 'wooks';
6
11
  import { TConsoleBase } from '@prostojs/logger';
7
- import { Workflow, Step, TWorkflowSpy, TStepHandler, TWorkflowSchema, TFlowOutput } from '@prostojs/wf';
8
- export { StepRetriableError, TStepHandler, TWorkflowSchema } from '@prostojs/wf';
9
12
 
10
13
  /**
11
14
  * Composable that provides access to the current workflow execution state.
@@ -16,14 +19,14 @@ export { StepRetriableError, TStepHandler, TWorkflowSchema } from '@prostojs/wf'
16
19
  * const stepInput = input<MyInput>()
17
20
  * ```
18
21
  */
19
- declare function useWfState(): {
22
+ declare const useWfState: _wooksjs_event_core.WookComposable<{
20
23
  ctx: <T>() => T;
21
24
  input: <I>() => I | undefined;
22
25
  schemaId: string;
23
26
  stepId: () => string | null;
24
27
  indexes: () => number[] | undefined;
25
28
  resume: boolean;
26
- };
29
+ }>;
27
30
 
28
31
  declare const wfKind: _wooksjs_event_core.EventKind<{
29
32
  schemaId: _wooksjs_event_core.SlotMarker<string>;
@@ -39,6 +42,189 @@ declare function createWfContext<R>(options: EventContextOptions, seeds: EventKi
39
42
  /** Creates a WF event context for resuming a paused workflow and runs `fn` inside it. */
40
43
  declare function resumeWfContext<R>(options: EventContextOptions, seeds: EventKindSeeds<typeof wfKind>, fn: () => R): R;
41
44
 
45
+ /**
46
+ * Composable for accessing outlet infrastructure from within workflow steps.
47
+ *
48
+ * Most steps don't need this — they just return `outletHttp(form)` or
49
+ * `outletEmail(to, template)`. This composable is for advanced cases
50
+ * where steps need to inspect or modify outlet state directly.
51
+ */
52
+ declare const useWfOutlet: _wooksjs_event_core.WookComposable<{
53
+ getStateStrategy: () => _prostojs_wf_outlets.WfStateStrategy;
54
+ getOutlets: () => Map<string, _prostojs_wf_outlets.WfOutlet>;
55
+ getOutlet: (name: string) => _prostojs_wf_outlets.WfOutlet | null;
56
+ }>;
57
+
58
+ interface WfFinishedResponse {
59
+ type: 'redirect' | 'data';
60
+ /** Redirect URL or response body */
61
+ value: unknown;
62
+ /** HTTP status code (default 200 for data, 302 for redirect) */
63
+ status?: number;
64
+ /** Cookies to set */
65
+ cookies?: Record<string, {
66
+ value: string;
67
+ options?: Record<string, unknown>;
68
+ }>;
69
+ }
70
+
71
+ /**
72
+ * Composable to set the completion response for a finished workflow.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * // Redirect after login
77
+ * useWfFinished().set({ type: 'redirect', value: '/dashboard' })
78
+ *
79
+ * // Return data
80
+ * useWfFinished().set({ type: 'data', value: { success: true } })
81
+ * ```
82
+ */
83
+ declare const useWfFinished: _wooksjs_event_core.WookComposable<{
84
+ set: (response: WfFinishedResponse) => void;
85
+ get: () => WfFinishedResponse | undefined;
86
+ }>;
87
+
88
+ interface WfOutletTokenConfig {
89
+ /** Where to read state token from incoming request (default: `['body', 'query', 'cookie']`) */
90
+ read?: Array<'body' | 'query' | 'cookie'>;
91
+ /** Where to write state token in response (default: `'body'`) */
92
+ write?: 'body' | 'cookie';
93
+ /** Parameter name for state token (default: `'wfs'`) */
94
+ name?: string;
95
+ /**
96
+ * Token consumption mode per outlet. When `true`, the trigger calls
97
+ * `strategy.consume()` (single-use token) instead of `strategy.retrieve()`
98
+ * on resume. Defaults to `{ email: true }` — email magic links are consumed
99
+ * on first use, HTTP tokens are reusable.
100
+ *
101
+ * Can be a boolean (applies to all outlets) or a per-outlet-name map.
102
+ */
103
+ consume?: boolean | Record<string, boolean>;
104
+ }
105
+ interface WfOutletTriggerConfig {
106
+ /** Whitelist of allowed workflow IDs. If empty, all are allowed. */
107
+ allow?: string[];
108
+ /** Blacklist of workflow IDs. Checked after allow. */
109
+ block?: string[];
110
+ /** State persistence strategy */
111
+ state: WfStateStrategy | ((wfid: string) => WfStateStrategy);
112
+ /** Registered outlets */
113
+ outlets: WfOutlet[];
114
+ /** Token configuration (reading, writing, naming, consumption) */
115
+ token?: WfOutletTokenConfig;
116
+ /** Parameter name for workflow ID (default: `'wfid'`) */
117
+ wfidName?: string;
118
+ /**
119
+ * Initial workflow context factory. Called when starting a new workflow.
120
+ * Receives the parsed request body so you can seed context from the request.
121
+ * Default: `() => ({})` (empty context).
122
+ */
123
+ initialContext?: (body: Record<string, unknown> | undefined, wfid: string) => unknown;
124
+ /**
125
+ * Called when a workflow finishes. If provided, its return value becomes the
126
+ * HTTP response — overriding `useWfFinished()`. This keeps steps transport-agnostic
127
+ * when the completion response is always the same shape.
128
+ *
129
+ * If not provided, falls back to `useWfFinished()` or `{ finished: true }`.
130
+ */
131
+ onFinished?: (ctx: {
132
+ context: unknown;
133
+ schemaId: string;
134
+ }) => unknown;
135
+ }
136
+ interface WfOutletTriggerDeps {
137
+ /** Start a workflow. Provided by WooksWf or MoostWf. */
138
+ start: (schemaId: string, context: unknown, opts?: {
139
+ input?: unknown;
140
+ eventContext?: unknown;
141
+ }) => Promise<TFlowOutput<unknown, unknown, WfOutletRequest>>;
142
+ /** Resume a workflow. Provided by WooksWf or MoostWf. */
143
+ resume: (state: {
144
+ schemaId: string;
145
+ indexes: number[];
146
+ context: unknown;
147
+ }, opts?: {
148
+ input?: unknown;
149
+ eventContext?: unknown;
150
+ }) => Promise<TFlowOutput<unknown, unknown, WfOutletRequest>>;
151
+ }
152
+
153
+ /**
154
+ * Handle an HTTP request that starts or resumes a workflow.
155
+ *
156
+ * Reads wfs (state token) and wfid (workflow ID) from request body, query params,
157
+ * or cookies — configurable via `config.token`. On workflow pause, persists state
158
+ * and dispatches to the named outlet. On finish, returns the finished response.
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * // In a wooks HTTP handler:
163
+ * app.post('/workflow', () => handleWfOutletRequest(config, deps))
164
+ *
165
+ * // Better — use createOutletHandler():
166
+ * const handle = createOutletHandler(wfApp)
167
+ * app.post('/workflow', () => handle(config))
168
+ * ```
169
+ */
170
+ declare function handleWfOutletRequest(config: WfOutletTriggerConfig, deps: WfOutletTriggerDeps): Promise<unknown>;
171
+
172
+ /**
173
+ * Creates an HTTP outlet that passes through the outlet payload as
174
+ * the HTTP response body. This is the most common outlet — it returns
175
+ * forms, prompts, or data to the client.
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * const httpOutlet = createHttpOutlet()
180
+ * // Step does: return outletHttp({ fields: ['email', 'password'] })
181
+ * // Client receives: { fields: ['email', 'password'] }
182
+ * ```
183
+ */
184
+ declare function createHttpOutlet(opts?: {
185
+ /** Transform the payload before returning to client */
186
+ transform?: (payload: unknown, context?: Record<string, unknown>) => unknown;
187
+ }): WfOutlet;
188
+ /**
189
+ * Creates an email outlet that delegates to a user-provided send function.
190
+ * The send function receives the target, template, context, and the state
191
+ * token (for embedding in magic links / verification URLs).
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * const emailOutlet = createEmailOutlet(async (opts) => {
196
+ * await mailer.send({
197
+ * to: opts.target,
198
+ * template: opts.template,
199
+ * data: { ...opts.context, verifyUrl: `/verify?wfs=${opts.token}` },
200
+ * })
201
+ * })
202
+ * ```
203
+ */
204
+ declare function createEmailOutlet(send: (opts: {
205
+ target: string;
206
+ template: string;
207
+ context: Record<string, unknown>;
208
+ token: string;
209
+ }) => Promise<void>): WfOutlet;
210
+
211
+ /**
212
+ * Creates a pre-wired outlet handler from a workflow app instance.
213
+ * Eliminates the need to manually construct `WfOutletTriggerDeps`.
214
+ *
215
+ * Accepts any object with `start` and `resume` methods (WooksWf, MoostWf, etc.).
216
+ *
217
+ * @example
218
+ * ```ts
219
+ * const handle = createOutletHandler(wfApp)
220
+ * httpApp.post('/workflow', () => handle(config))
221
+ * ```
222
+ */
223
+ declare function createOutletHandler(wfApp: {
224
+ start: WfOutletTriggerDeps['start'];
225
+ resume: WfOutletTriggerDeps['resume'];
226
+ }): (config: WfOutletTriggerConfig) => Promise<unknown>;
227
+
42
228
  /** Input data for creating a workflow event context. */
43
229
  interface TWFEventInput {
44
230
  schemaId: string;
@@ -153,5 +339,5 @@ declare class WooksWf<T = any, IR = any> extends WooksAdapterBase {
153
339
  */
154
340
  declare function createWfApp<T>(opts?: TWooksWfOptions, wooks?: Wooks | WooksAdapterBase): WooksWf<T, any>;
155
341
 
156
- export { WooksWf, createWfApp, createWfContext, resumeKey, resumeWfContext, useWfState, wfKind, wfShortcuts };
157
- export type { TWFEventInput, TWfRunOptions, TWooksWfOptions };
342
+ export { WooksWf, createEmailOutlet, createHttpOutlet, createOutletHandler, createWfApp, createWfContext, handleWfOutletRequest, resumeKey, resumeWfContext, useWfFinished, useWfOutlet, useWfState, wfKind, wfShortcuts };
343
+ export type { TWFEventInput, TWfRunOptions, TWooksWfOptions, WfFinishedResponse, WfOutletTokenConfig, WfOutletTriggerConfig, WfOutletTriggerDeps };