@@ -15,19 +15,13 @@ import {
15
15
SignalDefinition ,
16
16
WorkflowResultType ,
17
17
WithWorkflowArgs ,
18
- WorkflowReturnType ,
19
18
CancelledFailure ,
20
19
TerminatedFailure ,
21
20
RetryState ,
22
21
TimeoutFailure ,
23
22
TimeoutType ,
24
23
} from '@temporalio/common' ;
25
- import {
26
- WorkflowOptions ,
27
- addDefaults ,
28
- compileWorkflowOptions ,
29
- WorkflowSignalWithStartOptions ,
30
- } from './workflow-options' ;
24
+ import { WorkflowOptions , compileWorkflowOptions , WorkflowSignalWithStartOptions } from './workflow-options' ;
31
25
import {
32
26
WorkflowCancelInput ,
33
27
WorkflowClientCallsInterceptor ,
@@ -152,14 +146,6 @@ export interface WorkflowClientOptions {
152
146
* @default QUERY_REJECT_CONDITION_UNSPECIFIED which means that closed and failed workflows are still queryable
153
147
*/
154
148
queryRejectCondition ?: temporal . api . enums . v1 . QueryRejectCondition ;
155
-
156
- /**
157
- * Apply default options for starting new Workflows.
158
- *
159
- * These defaults are **shallowly** merged with options provided to methods that start Workflows
160
- * e.g. {@link WorkflowClient.start}.
161
- */
162
- workflowDefaults ?: Partial < WorkflowOptions > ;
163
149
}
164
150
165
151
export type WorkflowClientOptionsWithDefaults = Required < WorkflowClientOptions > ;
@@ -172,14 +158,23 @@ export function defaultWorkflowClientOptions(): WorkflowClientOptionsWithDefault
172
158
interceptors : { } ,
173
159
namespace : 'default' ,
174
160
queryRejectCondition : temporal . api . enums . v1 . QueryRejectCondition . QUERY_REJECT_CONDITION_UNSPECIFIED ,
175
- workflowDefaults : { } ,
176
161
} ;
177
162
}
178
163
179
- function assertRequiredWorkflowOptions ( opts : Partial < WorkflowOptions > ) : asserts opts is WorkflowOptions {
164
+ function assertRequiredWorkflowOptions ( opts : WorkflowOptions ) : void {
180
165
if ( ! opts . taskQueue ) {
181
166
throw new TypeError ( 'Missing WorkflowOptions.taskQueue' ) ;
182
167
}
168
+ if ( ! opts . workflowId ) {
169
+ throw new TypeError ( 'Missing WorkflowOptions.workflowId' ) ;
170
+ }
171
+ }
172
+
173
+ function ensureArgs < W extends Workflow , T extends WorkflowStartOptions < W > > (
174
+ opts : T
175
+ ) : Omit < T , 'args' > & { args : unknown [ ] } {
176
+ const { args, ...rest } = opts ;
177
+ return { args : args ?? [ ] , ...rest } ;
183
178
}
184
179
185
180
/**
@@ -200,7 +195,7 @@ export interface WorkflowResultOptions {
200
195
/**
201
196
* Options for starting a Workflow
202
197
*/
203
- export type WorkflowStartOptions < T extends Workflow > = WithWorkflowArgs < T , Partial < WorkflowOptions > > ;
198
+ export type WorkflowStartOptions < T extends Workflow > = WithWorkflowArgs < T , WorkflowOptions > ;
204
199
205
200
/**
206
201
* Client for starting Workflow executions and creating Workflow handles
@@ -219,12 +214,12 @@ export class WorkflowClient {
219
214
*/
220
215
protected async _start < T extends Workflow > (
221
216
workflowTypeOrFunc : string | T ,
222
- options : WithWorkflowArgs < T , Partial < WorkflowOptions > > ,
217
+ options : WithWorkflowArgs < T , WorkflowOptions > ,
223
218
interceptors : WorkflowClientCallsInterceptor [ ]
224
219
) : Promise < string > {
225
220
const workflowType = typeof workflowTypeOrFunc === 'string' ? workflowTypeOrFunc : workflowTypeOrFunc . name ;
226
221
assertRequiredWorkflowOptions ( options ) ;
227
- const compiledOptions = compileWorkflowOptions ( addDefaults ( options ) ) ;
222
+ const compiledOptions = compileWorkflowOptions ( ensureArgs ( options ) ) ;
228
223
229
224
const start = composeInterceptors ( interceptors , 'start' , this . _startWorkflowHandler . bind ( this ) ) ;
230
225
@@ -249,7 +244,7 @@ export class WorkflowClient {
249
244
const workflowType = typeof workflowTypeOrFunc === 'string' ? workflowTypeOrFunc : workflowTypeOrFunc . name ;
250
245
const { signal, signalArgs, ...rest } = options ;
251
246
assertRequiredWorkflowOptions ( rest ) ;
252
- const compiledOptions = compileWorkflowOptions ( addDefaults ( rest ) ) ;
247
+ const compiledOptions = compileWorkflowOptions ( ensureArgs ( rest ) ) ;
253
248
254
249
const signalWithStart = composeInterceptors (
255
250
interceptors ,
@@ -266,17 +261,6 @@ export class WorkflowClient {
266
261
} ) ;
267
262
}
268
263
269
- /**
270
- * Start a new Workflow execution.
271
- *
272
- * **Override for Workflows that accept no arguments**.
273
- *
274
- * @returns a WorkflowHandle to the started Workflow
275
- */
276
- public async start < T extends ( ) => WorkflowReturnType > (
277
- workflowTypeOrFunc : string | T
278
- ) : Promise < WorkflowHandleWithRunId < T > > ;
279
-
280
264
/**
281
265
* Start a new Workflow execution.
282
266
*
@@ -285,15 +269,9 @@ export class WorkflowClient {
285
269
public async start < T extends Workflow > (
286
270
workflowTypeOrFunc : string | T ,
287
271
options : WorkflowStartOptions < T >
288
- ) : Promise < WorkflowHandleWithRunId < T > > ;
289
-
290
- public async start < T extends Workflow > (
291
- workflowTypeOrFunc : string | T ,
292
- maybeOptions ?: WorkflowStartOptions < T >
293
272
) : Promise < WorkflowHandleWithRunId < T > > {
273
+ const { workflowId } = options ;
294
274
// Cast is needed because it's impossible to deduce the type in this situation
295
- const options = { ...this . options . workflowDefaults , ...maybeOptions } as WorkflowStartOptions < T > ;
296
- const workflowId = options . workflowId ?? uuid4 ( ) ;
297
275
const interceptors = ( this . options . interceptors . calls ?? [ ] ) . map ( ( ctor ) => ctor ( { workflowId } ) ) ;
298
276
const runId = await this . _start ( workflowTypeOrFunc , { ...options , workflowId } , interceptors ) ;
299
277
const handle = this . _createWorkflowHandle ( workflowId , runId , interceptors , {
@@ -313,10 +291,9 @@ export class WorkflowClient {
313
291
workflowTypeOrFunc : string | T ,
314
292
options : WithWorkflowArgs < T , WorkflowSignalWithStartOptions < SA > >
315
293
) : Promise < WorkflowHandleWithRunId < T > > {
316
- options = { ...this . options . workflowDefaults , ...options } ;
317
- const workflowId = options . workflowId ?? uuid4 ( ) ;
294
+ const { workflowId } = options ;
318
295
const interceptors = ( this . options . interceptors . calls ?? [ ] ) . map ( ( ctor ) => ctor ( { workflowId } ) ) ;
319
- const runId = await this . _signalWithStart ( workflowTypeOrFunc , { ... options , workflowId } , interceptors ) ;
296
+ const runId = await this . _signalWithStart ( workflowTypeOrFunc , options , interceptors ) ;
320
297
const handle = this . _createWorkflowHandle ( workflowId , runId , interceptors , {
321
298
followRuns : options . followRuns ?? true ,
322
299
} ) as WorkflowHandleWithRunId < T > ; // Cast is safe because we know we add the originalRunId below
@@ -332,28 +309,10 @@ export class WorkflowClient {
332
309
public async execute < T extends Workflow > (
333
310
workflowTypeOrFunc : string | T ,
334
311
options : WorkflowStartOptions < T >
335
- ) : Promise < WorkflowResultType < T > > ;
336
-
337
- /**
338
- * Starts a new Workflow execution and awaits its completion.
339
- *
340
- * **Override for Workflows that accept no arguments**.
341
- *
342
- * @returns the result of the Workflow execution
343
- */
344
- public async execute < T extends ( ) => WorkflowReturnType > (
345
- workflowTypeOrFunc : string | T
346
- ) : Promise < WorkflowResultType < T > > ;
347
-
348
- public async execute < T extends Workflow > (
349
- workflowTypeOrFunc : string | T ,
350
- maybeOptions ?: WorkflowStartOptions < T >
351
312
) : Promise < WorkflowResultType < T > > {
352
- // Cast is needed because it's impossible to deduce the type in this situation
353
- const options = { ...this . options . workflowDefaults , ...maybeOptions } as WorkflowStartOptions < T > ;
354
- const workflowId = options . workflowId ?? uuid4 ( ) ;
313
+ const { workflowId } = options ;
355
314
const interceptors = ( this . options . interceptors . calls ?? [ ] ) . map ( ( ctor ) => ctor ( { workflowId } ) ) ;
356
- await this . _start ( workflowTypeOrFunc , { ... options , workflowId } , interceptors ) ;
315
+ await this . _start ( workflowTypeOrFunc , options , interceptors ) ;
357
316
return await this . result ( workflowId , undefined , {
358
317
followRuns : options . followRuns ?? true ,
359
318
} ) ;
@@ -695,11 +654,15 @@ export class WorkflowClient {
695
654
/**
696
655
* Creates a Workflow handle for existing Workflow using `workflowId` and optional `runId`
697
656
*/
698
- public getHandle < T extends Workflow > ( workflowId : string , runId ?: string ) : WorkflowHandle < T > {
657
+ public getHandle < T extends Workflow > (
658
+ workflowId : string ,
659
+ runId ?: string ,
660
+ options ?: WorkflowResultOptions
661
+ ) : WorkflowHandle < T > {
699
662
const interceptors = ( this . options . interceptors . calls ?? [ ] ) . map ( ( ctor ) => ctor ( { workflowId, runId } ) ) ;
700
663
701
664
return this . _createWorkflowHandle ( workflowId , runId , interceptors , {
702
- followRuns : this . options . workflowDefaults . followRuns ?? true ,
665
+ followRuns : options ? .followRuns ?? true ,
703
666
} ) ;
704
667
}
705
668
}
0 commit comments