1
- import { DataConverter , LoadedDataConverter } from '@temporalio/common' ;
2
- import { filterNullAndUndefined , loadDataConverter } from '@temporalio/common/lib/internal-non-workflow' ;
3
- import { Replace } from '@temporalio/common/lib/type-helpers' ;
1
+ import { filterNullAndUndefined } from '@temporalio/common/lib/internal-non-workflow' ;
4
2
import { temporal } from '@temporalio/proto' ;
5
- import os from 'os' ;
6
3
import { AsyncCompletionClient } from './async-completion-client' ;
7
- import { Connection } from './connection ' ;
4
+ import { BaseClient , BaseClientOptions , defaultBaseClientOptions , LoadedWithDefaults } from './base-client ' ;
8
5
import { ClientInterceptors } from './interceptors' ;
9
6
import { ScheduleClient } from './schedule-client' ;
10
- import { ConnectionLike , Metadata , WorkflowService } from './types' ;
7
+ import { WorkflowService } from './types' ;
11
8
import { WorkflowClient } from './workflow-client' ;
12
9
13
- export interface ClientOptions {
14
- /**
15
- * {@link DataConverter } to use for serializing and deserializing payloads
16
- */
17
- dataConverter ?: DataConverter ;
18
-
10
+ export interface ClientOptions extends BaseClientOptions {
19
11
/**
20
12
* Used to override and extend default Connection functionality
21
13
*
22
14
* Useful for injecting auth headers and tracing Workflow executions
23
15
*/
24
16
interceptors ?: ClientInterceptors ;
25
17
26
- /**
27
- * Identity to report to the server
28
- *
29
- * @default `${process.pid}@${os.hostname()}`
30
- */
31
- identity ?: string ;
32
-
33
- /**
34
- * Connection to use to communicate with the server.
35
- *
36
- * By default `WorkflowClient` connects to localhost.
37
- *
38
- * Connections are expensive to construct and should be reused.
39
- */
40
- connection ?: ConnectionLike ;
41
-
42
- /**
43
- * Server namespace
44
- *
45
- * @default default
46
- */
47
- namespace ?: string ;
48
-
49
18
workflow ?: {
50
19
/**
51
20
* Should a query be rejected by closed and failed workflows
@@ -56,37 +25,12 @@ export interface ClientOptions {
56
25
} ;
57
26
}
58
27
59
- export type ClientOptionsWithDefaults = Replace <
60
- Required < ClientOptions > ,
61
- {
62
- connection ?: ConnectionLike ;
63
- }
64
- > ;
65
-
66
- export type LoadedClientOptions = ClientOptionsWithDefaults & {
67
- loadedDataConverter : LoadedDataConverter ;
68
- } ;
69
-
70
- export function defaultClientOptions ( ) : ClientOptionsWithDefaults {
71
- return {
72
- dataConverter : { } ,
73
- identity : `${ process . pid } @${ os . hostname ( ) } ` ,
74
- interceptors : { } ,
75
- namespace : 'default' ,
76
- workflow : {
77
- queryRejectCondition : temporal . api . enums . v1 . QueryRejectCondition . QUERY_REJECT_CONDITION_UNSPECIFIED ,
78
- } ,
79
- } ;
80
- }
28
+ export type LoadedClientOptions = LoadedWithDefaults < ClientOptions > ;
81
29
82
30
/**
83
31
* High level SDK client.
84
32
*/
85
- export class Client {
86
- /**
87
- * Underlying gRPC connection to the Temporal service
88
- */
89
- public readonly connection : ConnectionLike ;
33
+ export class Client extends BaseClient {
90
34
public readonly options : LoadedClientOptions ;
91
35
/**
92
36
* Workflow sub-client - use to start and interact with Workflows
@@ -104,35 +48,43 @@ export class Client {
104
48
public readonly schedule : ScheduleClient ;
105
49
106
50
constructor ( options ?: ClientOptions ) {
107
- this . connection = options ?. connection ?? Connection . lazy ( ) ;
108
- this . options = {
109
- ...defaultClientOptions ( ) ,
110
- ...filterNullAndUndefined ( options ?? { } ) ,
111
- loadedDataConverter : loadDataConverter ( options ?. dataConverter ) ,
112
- } ;
51
+ super ( options ) ;
113
52
114
- const { workflow , loadedDataConverter , interceptors , ...base } = this . options ;
53
+ const { interceptors , workflow , ...commonOptions } = options ?? { } ;
115
54
116
55
this . workflow = new WorkflowClient ( {
117
- ...base ,
118
- ...workflow ,
56
+ ...commonOptions ,
57
+ ...( workflow ?? { } ) ,
119
58
connection : this . connection ,
120
- dataConverter : loadedDataConverter ,
121
- interceptors : interceptors . workflow ,
59
+ dataConverter : this . dataConverter ,
60
+ interceptors : interceptors ? .workflow ,
122
61
} ) ;
123
62
124
63
this . activity = new AsyncCompletionClient ( {
125
- ...base ,
64
+ ...commonOptions ,
126
65
connection : this . connection ,
127
- dataConverter : loadedDataConverter ,
66
+ dataConverter : this . dataConverter ,
128
67
} ) ;
129
68
130
69
this . schedule = new ScheduleClient ( {
131
- ...base ,
70
+ ...commonOptions ,
132
71
connection : this . connection ,
133
- dataConverter : loadedDataConverter ,
134
- interceptors : interceptors . schedule ,
72
+ dataConverter : this . dataConverter ,
73
+ interceptors : interceptors ? .schedule ,
135
74
} ) ;
75
+
76
+ this . options = {
77
+ ...defaultBaseClientOptions ( ) ,
78
+ ...filterNullAndUndefined ( commonOptions ) ,
79
+ loadedDataConverter : this . dataConverter ,
80
+ interceptors : {
81
+ workflow : this . workflow . options . interceptors ,
82
+ schedule : this . schedule . options . interceptors ,
83
+ } ,
84
+ workflow : {
85
+ queryRejectCondition : this . workflow . options . queryRejectCondition ,
86
+ } ,
87
+ } ;
136
88
}
137
89
138
90
/**
@@ -144,22 +96,4 @@ export class Client {
144
96
get workflowService ( ) : WorkflowService {
145
97
return this . connection . workflowService ;
146
98
}
147
-
148
- /**
149
- * Set the deadline for any service requests executed in `fn`'s scope.
150
- */
151
- async withDeadline < R > ( deadline : number | Date , fn : ( ) => Promise < R > ) : Promise < R > {
152
- return await this . connection . withDeadline ( deadline , fn ) ;
153
- }
154
-
155
- /**
156
- * Set metadata for any service requests executed in `fn`'s scope.
157
- *
158
- * @returns returned value of `fn`
159
- *
160
- * @see {@link Connection.withMetadata }
161
- */
162
- async withMetadata < R > ( metadata : Metadata , fn : ( ) => Promise < R > ) : Promise < R > {
163
- return await this . connection . withMetadata ( metadata , fn ) ;
164
- }
165
99
}
0 commit comments