@temporalio/workflow 0.23.0 → 1.0.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.
Files changed (44) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -2
  3. package/lib/cancellation-scope.js +6 -2
  4. package/lib/cancellation-scope.js.map +1 -1
  5. package/lib/errors.js +9 -5
  6. package/lib/errors.js.map +1 -1
  7. package/lib/index.d.ts +18 -12
  8. package/lib/index.js +16 -13
  9. package/lib/index.js.map +1 -1
  10. package/lib/interceptors.d.ts +3 -1
  11. package/lib/interfaces.d.ts +147 -12
  12. package/lib/interfaces.js +44 -0
  13. package/lib/interfaces.js.map +1 -1
  14. package/lib/internals.d.ts +11 -4
  15. package/lib/internals.js +47 -15
  16. package/lib/internals.js.map +1 -1
  17. package/lib/sinks.d.ts +2 -1
  18. package/lib/stack-helpers.d.ts +4 -0
  19. package/lib/stack-helpers.js +15 -0
  20. package/lib/stack-helpers.js.map +1 -0
  21. package/lib/trigger.d.ts +2 -1
  22. package/lib/trigger.js +7 -4
  23. package/lib/trigger.js.map +1 -1
  24. package/lib/worker-interface.d.ts +2 -1
  25. package/lib/worker-interface.js +16 -12
  26. package/lib/worker-interface.js.map +1 -1
  27. package/lib/workflow-handle.d.ts +1 -1
  28. package/lib/workflow.d.ts +115 -43
  29. package/lib/workflow.js +151 -74
  30. package/lib/workflow.js.map +1 -1
  31. package/package.json +10 -8
  32. package/src/alea.ts +88 -0
  33. package/src/cancellation-scope.ts +205 -0
  34. package/src/errors.ts +30 -0
  35. package/src/index.ts +91 -0
  36. package/src/interceptors.ts +239 -0
  37. package/src/interfaces.ts +294 -0
  38. package/src/internals.ts +611 -0
  39. package/src/sinks.ts +41 -0
  40. package/src/stack-helpers.ts +11 -0
  41. package/src/trigger.ts +49 -0
  42. package/src/worker-interface.ts +273 -0
  43. package/src/workflow-handle.ts +63 -0
  44. package/src/workflow.ts +1247 -0
package/lib/workflow.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ActivityFunction, ActivityInterface, ActivityOptions, LocalActivityOptions, QueryDefinition, SignalDefinition, WithWorkflowArgs, Workflow, WorkflowResultType, WorkflowReturnType } from '@temporalio/internal-workflow-common';
1
+ import { ActivityFunction, ActivityOptions, LocalActivityOptions, QueryDefinition, SearchAttributes, SignalDefinition, UntypedActivities, WithWorkflowArgs, Workflow, WorkflowResultType, WorkflowReturnType } from '@temporalio/internal-workflow-common';
2
2
  import { ChildWorkflowOptions, ChildWorkflowOptionsWithDefaults, ContinueAsNewOptions, WorkflowInfo } from './interfaces';
3
3
  import { Sinks } from './sinks';
4
4
  import { ChildWorkflowHandle, ExternalWorkflowHandle } from './workflow-handle';
@@ -11,19 +11,10 @@ export declare function addDefaultWorkflowOptions<T extends Workflow>(opts: With
11
11
  *
12
12
  * Schedules a timer on the Temporal service.
13
13
  *
14
- * @param ms sleep duration - {@link https://www.npmjs.com/package/ms | ms} formatted string or number of milliseconds. If given a negative number, value will be set to 1.
15
- *
14
+ * @param ms sleep duration - {@link https://www.npmjs.com/package/ms | ms} formatted string or number of milliseconds.
15
+ * If given a negative number or 0, value will be set to 1.
16
16
  */
17
17
  export declare function sleep(ms: number | string): Promise<void>;
18
- export interface ActivityInfo {
19
- name: string;
20
- type: string;
21
- }
22
- export declare type InternalActivityFunction<P extends any[], R> = ActivityFunction<P, R> & ActivityInfo;
23
- /**
24
- * @hidden
25
- */
26
- export declare function validateActivityOptions(options: ActivityOptions): void;
27
18
  /**
28
19
  * Schedule an activity and run outbound interceptors
29
20
  * @hidden
@@ -34,19 +25,52 @@ export declare function scheduleActivity<R>(activityType: string, args: any[], o
34
25
  * @hidden
35
26
  */
36
27
  export declare function scheduleLocalActivity<R>(activityType: string, args: any[], options: LocalActivityOptions): Promise<R>;
28
+ /**
29
+ * Symbol used in the return type of proxy methods to mark that an attribute on the source type is not a method.
30
+ *
31
+ * @see {@link ActivityInterfaceFor}
32
+ * @see {@link proxyActivities}
33
+ * @see {@link proxyLocalActivities}
34
+ */
35
+ export declare const NotAnActivityMethod: unique symbol;
36
+ /**
37
+ * Type helper that takes a type `T` and transforms attributes that are not {@link ActivityFunction} to
38
+ * {@link NotAnActivityMethod}.
39
+ *
40
+ * @example
41
+ *
42
+ * Used by {@link proxyActivities} to get this compile-time error:
43
+ *
44
+ * ```ts
45
+ * interface MyActivities {
46
+ * valid(input: number): Promise<number>;
47
+ * invalid(input: number): number;
48
+ * }
49
+ *
50
+ * const act = proxyActivities<MyActivities>({ startToCloseTimeout: '5m' });
51
+ *
52
+ * await act.valid(true);
53
+ * await act.invalid();
54
+ * // ^ TS complains with:
55
+ * // (property) invalidDefinition: typeof NotAnActivityMethod
56
+ * // This expression is not callable.
57
+ * // Type 'Symbol' has no call signatures.(2349)
58
+ * ```
59
+ */
60
+ export declare type ActivityInterfaceFor<T> = {
61
+ [K in keyof T]: T[K] extends ActivityFunction ? T[K] : typeof NotAnActivityMethod;
62
+ };
37
63
  /**
38
64
  * Configure Activity functions with given {@link ActivityOptions}.
39
65
  *
40
66
  * This method may be called multiple times to setup Activities with different options.
41
67
  *
42
- * @return a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
43
- * for which each attribute is a callable Activity function
44
- *
45
- * @typeparam A An {@link ActivityInterface} - mapping of name to function
68
+ * @return a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy | Proxy} for
69
+ * which each attribute is a callable Activity function
46
70
  *
47
71
  * @example
48
72
  * ```ts
49
- * import { proxyActivities, ActivityInterface } from '@temporalio/workflow';
73
+ * import { proxyActivities } from '@temporalio/workflow';
50
74
  * import * as activities from '../activities';
51
75
  *
52
76
  * // Setup Activities from module exports
@@ -55,7 +79,7 @@ export declare function scheduleLocalActivity<R>(activityType: string, args: any
55
79
  * });
56
80
  *
57
81
  * // Setup Activities from an explicit interface (e.g. when defined by another SDK)
58
- * interface JavaActivities extends ActivityInterface {
82
+ * interface JavaActivities {
59
83
  * httpGetFromJava(url: string): Promise<string>
60
84
  * someOtherJavaActivity(arg1: number, arg2: string): Promise<string>;
61
85
  * }
@@ -74,22 +98,20 @@ export declare function scheduleLocalActivity<R>(activityType: string, args: any
74
98
  * }
75
99
  * ```
76
100
  */
77
- export declare function proxyActivities<A extends ActivityInterface>(options: ActivityOptions): A;
101
+ export declare function proxyActivities<A = UntypedActivities>(options: ActivityOptions): ActivityInterfaceFor<A>;
78
102
  /**
79
103
  * Configure Local Activity functions with given {@link LocalActivityOptions}.
80
104
  *
81
105
  * This method may be called multiple times to setup Activities with different options.
82
106
  *
83
- * @return a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
107
+ * @return a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy | Proxy}
84
108
  * for which each attribute is a callable Activity function
85
109
  *
86
- * @typeparam A An {@link ActivityInterface} - mapping of name to function
87
- *
88
110
  * @experimental
89
111
  *
90
- * See {@link proxyActivities} for examples
112
+ * @see {@link proxyActivities} for examples
91
113
  */
92
- export declare function proxyLocalActivities<A extends ActivityInterface>(options: LocalActivityOptions): A;
114
+ export declare function proxyLocalActivities<A = UntypedActivities>(options: LocalActivityOptions): ActivityInterfaceFor<A>;
93
115
  /**
94
116
  * Returns a client-side handle that can be used to signal and cancel an existing Workflow execution.
95
117
  * It takes a Workflow ID and optional run ID.
@@ -182,9 +204,31 @@ export declare function executeChild<T extends () => WorkflowReturnType>(workflo
182
204
  *
183
205
  * @return The result of the child Workflow.
184
206
  */
185
- export declare function executeChild<T extends () => WorkflowReturnType>(workflowFunc: T): Promise<ChildWorkflowHandle<T>>;
207
+ export declare function executeChild<T extends () => WorkflowReturnType>(workflowFunc: T): Promise<WorkflowResultType<T>>;
186
208
  /**
187
- * Get information about the current Workflow
209
+ * Get information about the current Workflow.
210
+ *
211
+ * ⚠️ We recommend calling `workflowInfo()` whenever accessing {@link WorkflowInfo} fields. Some WorkflowInfo fields
212
+ * change during the lifetime of an Execution—like {@link WorkflowInfo.historyLength} and
213
+ * {@link WorkflowInfo.searchAttributes}—and some may be changeable in the future—like {@link WorkflowInfo.taskQueue}.
214
+ *
215
+ * ```ts
216
+ * // GOOD
217
+ * function myWorkflow() {
218
+ * doSomething(workflowInfo().searchAttributes)
219
+ * ...
220
+ * doSomethingElse(workflowInfo().searchAttributes)
221
+ * }
222
+ * ```
223
+ *
224
+ * ```ts
225
+ * // BAD
226
+ * function myWorkflow() {
227
+ * const attributes = workflowInfo().searchAttributes
228
+ * doSomething(attributes)
229
+ * ...
230
+ * doSomethingElse(attributes)
231
+ * }
188
232
  */
189
233
  export declare function workflowInfo(): WorkflowInfo;
190
234
  /**
@@ -225,30 +269,27 @@ export declare function proxySinks<T extends Sinks>(): T;
225
269
  /**
226
270
  * Returns a function `f` that will cause the current Workflow to ContinueAsNew when called.
227
271
  *
228
- * `f` takes the same arguments as the Workflow execute function supplied to typeparam `F`.
272
+ * `f` takes the same arguments as the Workflow function supplied to typeparam `F`.
229
273
  *
230
- * Once `f` is called, Workflow execution immediately completes.
274
+ * Once `f` is called, Workflow Execution immediately completes.
231
275
  */
232
276
  export declare function makeContinueAsNewFunc<F extends Workflow>(options?: ContinueAsNewOptions): (...args: Parameters<F>) => Promise<never>;
233
277
  /**
234
- * Continues current Workflow execution as new with default options.
278
+ * {@link https://docs.temporal.io/concepts/what-is-continue-as-new/ | Continues-As-New} the current Workflow Execution
279
+ * with default options.
235
280
  *
236
- * Shorthand for `makeContinueAsNewFunc<F>()(...args)`.
281
+ * Shorthand for `makeContinueAsNewFunc<F>()(...args)`. (See: {@link makeContinueAsNewFunc}.)
237
282
  *
238
283
  * @example
239
284
  *
240
- * ```ts
241
- * import { continueAsNew } from '@temporalio/workflow';
285
+ *```ts
286
+ *import { continueAsNew } from '@temporalio/workflow';
242
287
  *
243
- * export function myWorkflow(n: number) {
244
- * return {
245
- * async execute() {
246
- * // ... Workflow logic
247
- * await continueAsNew<typeof myWorkflow>(n + 1);
248
- * }
249
- * };
250
- * }
251
- * ```
288
+ *export async function myWorkflow(n: number): Promise<void> {
289
+ * // ... Workflow logic
290
+ * await continueAsNew<typeof myWorkflow>(n + 1);
291
+ *}
292
+ *```
252
293
  */
253
294
  export declare function continueAsNew<F extends Workflow>(...args: Parameters<F>): Promise<never>;
254
295
  /**
@@ -261,7 +302,7 @@ export declare function uuid4(): string;
261
302
  /**
262
303
  * Patch or upgrade workflow code by checking or stating that this workflow has a certain patch.
263
304
  *
264
- * See [docs page](https://docs.temporal.io/docs/typescript/versioning) for info.
305
+ * See {@link https://docs.temporal.io/typescript/versioning | docs page} for info.
265
306
  *
266
307
  * If the workflow is replaying an existing history, then this function returns true if that
267
308
  * history was produced by a worker which also had a `patched` call with the same `patchId`.
@@ -279,7 +320,7 @@ export declare function patched(patchId: string): boolean;
279
320
  /**
280
321
  * Indicate that a patch is being phased out.
281
322
  *
282
- * See [docs page](https://docs.temporal.io/docs/typescript/versioning) for info.
323
+ * See {@link https://docs.temporal.io/typescript/versioning | docs page} for info.
283
324
  *
284
325
  * Workflows with this call may be deployed alongside workflows with a {@link patched} call, but
285
326
  * they must *not* be deployed while any workers still exist running old code without a
@@ -333,3 +374,34 @@ export declare type Handler<Ret, Args extends any[], T extends SignalDefinition<
333
374
  * @param handler a compatible handler function for the given definition or `undefined` to unset the handler.
334
375
  */
335
376
  export declare function setHandler<Ret, Args extends any[], T extends SignalDefinition<Args> | QueryDefinition<Ret, Args>>(def: T, handler: Handler<Ret, Args, T> | undefined): void;
377
+ /**
378
+ * Updates this Workflow's Search Attributes by merging the provided `searchAttributes` with the existing Search
379
+ * Attributes, `workflowInfo().searchAttributes`.
380
+ *
381
+ * For example, this Workflow code:
382
+ *
383
+ * ```ts
384
+ * upsertSearchAttributes({
385
+ * CustomIntField: [1, 2, 3],
386
+ * CustomBoolField: [true]
387
+ * });
388
+ * upsertSearchAttributes({
389
+ * CustomIntField: [42],
390
+ * CustomKeywordField: ['durable code', 'is great']
391
+ * });
392
+ * ```
393
+ *
394
+ * would result in the Workflow having these Search Attributes:
395
+ *
396
+ * ```ts
397
+ * {
398
+ * CustomIntField: [42],
399
+ * CustomBoolField: [true],
400
+ * CustomKeywordField: ['durable code', 'is great']
401
+ * }
402
+ * ```
403
+ *
404
+ * @param searchAttributes The Record to merge. Use a value of `[]` to clear a Search Attribute.
405
+ */
406
+ export declare function upsertSearchAttributes(searchAttributes: SearchAttributes): void;
407
+ export declare const stackTraceQuery: QueryDefinition<string, []>;