@temporal-contract/client 0.0.3 → 0.0.5

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.mts CHANGED
@@ -1,71 +1,196 @@
1
1
  import { ClientOptions, WorkflowHandle, WorkflowOptions, WorkflowStartOptions } from "@temporalio/client";
2
- import { ClientInferInput, ClientInferOutput, ClientInferWorkflowQueries, ClientInferWorkflowSignals, ClientInferWorkflowUpdates, ContractDefinition, WorkflowDefinition } from "@temporal-contract/contract";
2
+ import { Future, Result } from "@temporal-contract/boxed";
3
+ import { ActivityDefinition, AnySchema, ContractDefinition, QueryDefinition, SignalDefinition, UpdateDefinition, WorkflowDefinition } from "@temporal-contract/contract";
3
4
  import { StandardSchemaV1 } from "@standard-schema/spec";
4
5
 
5
- //#region src/client.d.ts
6
+ //#region src/types.d.ts
6
7
 
8
+ /**
9
+ * Infer input type from a definition (client perspective)
10
+ * Client sends the input type (before input schema parsing/transformation)
11
+ */
12
+ type ClientInferInput<T extends {
13
+ input: AnySchema;
14
+ }> = StandardSchemaV1.InferInput<T["input"]>;
15
+ /**
16
+ * Infer output type from a definition (client perspective)
17
+ * Client receives the output type (after output schema parsing/transformation)
18
+ */
19
+ type ClientInferOutput<T extends {
20
+ output: AnySchema;
21
+ }> = StandardSchemaV1.InferOutput<T["output"]>;
22
+ /**
23
+ * CLIENT PERSPECTIVE
24
+ * Client sends z.output and receives z.input
25
+ */
26
+ /**
27
+ * Infer workflow function signature from client perspective
28
+ * Client sends z.output and receives z.input
29
+ */
30
+ type ClientInferWorkflow<TWorkflow extends WorkflowDefinition> = (args: ClientInferInput<TWorkflow>) => Promise<ClientInferOutput<TWorkflow>>;
31
+ /**
32
+ * Infer activity function signature from client perspective
33
+ * Client sends z.output and receives z.input
34
+ */
35
+ type ClientInferActivity<TActivity extends ActivityDefinition> = (args: ClientInferInput<TActivity>) => Promise<ClientInferOutput<TActivity>>;
36
+ /**
37
+ * Infer signal handler signature from client perspective
38
+ * Client sends z.output and returns Future<Result<void, Error>>
39
+ */
40
+ type ClientInferSignal<TSignal extends SignalDefinition> = (args: ClientInferInput<TSignal>) => Future<Result<void, Error>>;
41
+ /**
42
+ * Infer query handler signature from client perspective
43
+ * Client sends z.output and receives z.input wrapped in Future<Result<T, Error>>
44
+ */
45
+ type ClientInferQuery<TQuery extends QueryDefinition> = (args: ClientInferInput<TQuery>) => Future<Result<ClientInferOutput<TQuery>, Error>>;
46
+ /**
47
+ * Infer update handler signature from client perspective
48
+ * Client sends z.output and receives z.input wrapped in Future<Result<T, Error>>
49
+ */
50
+ type ClientInferUpdate<TUpdate extends UpdateDefinition> = (args: ClientInferInput<TUpdate>) => Future<Result<ClientInferOutput<TUpdate>, Error>>;
51
+ /**
52
+ * CLIENT PERSPECTIVE - Contract-level types
53
+ */
54
+ /**
55
+ * Infer all workflows from a contract (client perspective)
56
+ */
57
+ type ClientInferWorkflows<TContract extends ContractDefinition> = { [K in keyof TContract["workflows"]]: ClientInferWorkflow<TContract["workflows"][K]> };
58
+ /**
59
+ * Infer all activities from a contract (client perspective)
60
+ */
61
+ type ClientInferActivities<TContract extends ContractDefinition> = TContract["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof TContract["activities"]]: ClientInferActivity<TContract["activities"][K]> } : {};
62
+ /**
63
+ * Infer activities from a workflow definition (client perspective)
64
+ */
65
+ type ClientInferWorkflowActivities<T extends WorkflowDefinition> = T["activities"] extends Record<string, ActivityDefinition> ? { [K in keyof T["activities"]]: ClientInferActivity<T["activities"][K]> } : {};
66
+ /**
67
+ * Infer signals from a workflow definition (client perspective)
68
+ */
69
+ type ClientInferWorkflowSignals<T extends WorkflowDefinition> = T["signals"] extends Record<string, SignalDefinition> ? { [K in keyof T["signals"]]: ClientInferSignal<T["signals"][K]> } : {};
70
+ /**
71
+ * Infer queries from a workflow definition (client perspective)
72
+ */
73
+ type ClientInferWorkflowQueries<T extends WorkflowDefinition> = T["queries"] extends Record<string, QueryDefinition> ? { [K in keyof T["queries"]]: ClientInferQuery<T["queries"][K]> } : {};
74
+ /**
75
+ * Infer updates from a workflow definition (client perspective)
76
+ */
77
+ type ClientInferWorkflowUpdates<T extends WorkflowDefinition> = T["updates"] extends Record<string, UpdateDefinition> ? { [K in keyof T["updates"]]: ClientInferUpdate<T["updates"][K]> } : {};
78
+ /**
79
+ * Infer all activities available in a workflow context (client perspective)
80
+ * Combines workflow-specific activities with global activities
81
+ */
82
+ type ClientInferWorkflowContextActivities<TContract extends ContractDefinition, TWorkflowName extends keyof TContract["workflows"]> = ClientInferWorkflowActivities<TContract["workflows"][TWorkflowName]> & ClientInferActivities<TContract>;
83
+ //#endregion
84
+ //#region src/errors.d.ts
85
+ /**
86
+ * Base class for all typed client errors with boxed pattern
87
+ */
88
+ declare class TypedClientError extends Error {
89
+ constructor(message: string);
90
+ }
91
+ /**
92
+ * Thrown when a workflow is not found in the contract
93
+ */
94
+ declare class WorkflowNotFoundError extends TypedClientError {
95
+ readonly workflowName: string;
96
+ readonly availableWorkflows: string[];
97
+ constructor(workflowName: string, availableWorkflows: string[]);
98
+ }
99
+ /**
100
+ * Thrown when workflow input or output validation fails
101
+ */
102
+ declare class WorkflowValidationError extends TypedClientError {
103
+ readonly workflowName: string;
104
+ readonly direction: "input" | "output";
105
+ readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
106
+ constructor(workflowName: string, direction: "input" | "output", issues: ReadonlyArray<StandardSchemaV1.Issue>);
107
+ }
108
+ /**
109
+ * Thrown when query input or output validation fails
110
+ */
111
+ declare class QueryValidationError extends TypedClientError {
112
+ readonly queryName: string;
113
+ readonly direction: "input" | "output";
114
+ readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
115
+ constructor(queryName: string, direction: "input" | "output", issues: ReadonlyArray<StandardSchemaV1.Issue>);
116
+ }
117
+ /**
118
+ * Thrown when signal input validation fails
119
+ */
120
+ declare class SignalValidationError extends TypedClientError {
121
+ readonly signalName: string;
122
+ readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
123
+ constructor(signalName: string, issues: ReadonlyArray<StandardSchemaV1.Issue>);
124
+ }
125
+ /**
126
+ * Thrown when update input or output validation fails
127
+ */
128
+ declare class UpdateValidationError extends TypedClientError {
129
+ readonly updateName: string;
130
+ readonly direction: "input" | "output";
131
+ readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
132
+ constructor(updateName: string, direction: "input" | "output", issues: ReadonlyArray<StandardSchemaV1.Issue>);
133
+ }
134
+ //#endregion
135
+ //#region src/client.d.ts
7
136
  /**
8
137
  * Extended options for starting workflows with Temporal-specific features
9
138
  * Combines required workflowId with optional Temporal workflow options
10
139
  */
11
140
  type TypedWorkflowStartOptions = Pick<WorkflowStartOptions, "workflowId" | "workflowIdReusePolicy" | "workflowExecutionTimeout" | "workflowRunTimeout" | "workflowTaskTimeout" | "retry" | "memo" | "searchAttributes" | "cronSchedule"> & Pick<WorkflowOptions, "workflowId">;
12
141
  /**
13
- * Typed workflow handle with validated results, queries, signals and updates
142
+ * Typed workflow handle with validated results using Result/Future pattern
14
143
  */
15
144
  interface TypedWorkflowHandle<TWorkflow extends WorkflowDefinition> {
16
145
  workflowId: string;
17
146
  /**
18
- * Type-safe queries based on workflow definition
147
+ * Type-safe queries based on workflow definition with Result pattern
148
+ * Each query returns Future<Result<T, Error>> instead of Promise<T>
19
149
  */
20
- queries: ClientInferWorkflowQueries<TWorkflow>;
150
+ queries: { [K in keyof ClientInferWorkflowQueries<TWorkflow>]: ClientInferWorkflowQueries<TWorkflow>[K] extends ((...args: infer Args) => Future<Result<infer R, Error>>) ? (...args: Args) => Future<Result<R, TypedClientError>> : never };
21
151
  /**
22
- * Type-safe signals based on workflow definition
152
+ * Type-safe signals based on workflow definition with Result pattern
153
+ * Each signal returns Future<Result<void, Error>> instead of Promise<void>
23
154
  */
24
- signals: ClientInferWorkflowSignals<TWorkflow>;
155
+ signals: { [K in keyof ClientInferWorkflowSignals<TWorkflow>]: ClientInferWorkflowSignals<TWorkflow>[K] extends ((...args: infer Args) => Future<Result<void, Error>>) ? (...args: Args) => Future<Result<void, TypedClientError>> : never };
25
156
  /**
26
- * Type-safe updates based on workflow definition
157
+ * Type-safe updates based on workflow definition with Result pattern
158
+ * Each update returns Future<Result<T, Error>> instead of Promise<T>
27
159
  */
28
- updates: ClientInferWorkflowUpdates<TWorkflow>;
29
- result: () => Promise<ClientInferOutput<TWorkflow>>;
30
- terminate: (reason?: string) => Promise<void>;
31
- cancel: () => Promise<void>;
160
+ updates: { [K in keyof ClientInferWorkflowUpdates<TWorkflow>]: ClientInferWorkflowUpdates<TWorkflow>[K] extends ((...args: infer Args) => Future<Result<infer R, Error>>) ? (...args: Args) => Future<Result<R, TypedClientError>> : never };
161
+ /**
162
+ * Get workflow result with Result pattern
163
+ */
164
+ result: () => Future<Result<ClientInferOutput<TWorkflow>, TypedClientError>>;
165
+ /**
166
+ * Terminate workflow with Result pattern
167
+ */
168
+ terminate: (reason?: string) => Future<Result<void, TypedClientError>>;
169
+ /**
170
+ * Cancel workflow with Result pattern
171
+ */
172
+ cancel: () => Future<Result<void, TypedClientError>>;
32
173
  /**
33
174
  * Get workflow execution description including status and metadata
34
- *
35
- * @example
36
- * ```ts
37
- * const handle = await client.getHandle('processOrder', 'order-123');
38
- * const description = await handle.describe();
39
- * console.log(description.workflowExecutionInfo.status); // RUNNING, COMPLETED, etc.
40
- * ```
41
175
  */
42
- describe: () => ReturnType<WorkflowHandle["describe"]>;
176
+ describe: () => Future<Result<Awaited<ReturnType<WorkflowHandle["describe"]>>, TypedClientError>>;
43
177
  /**
44
178
  * Fetch the workflow execution history
45
- *
46
- * @example
47
- * ```ts
48
- * const handle = await client.getHandle('processOrder', 'order-123');
49
- * const history = handle.fetchHistory();
50
- * for await (const event of history) {
51
- * console.log(event);
52
- * }
53
- * ```
54
179
  */
55
180
  fetchHistory: () => ReturnType<WorkflowHandle["fetchHistory"]>;
56
181
  }
57
182
  /**
58
- * Typed Temporal client based on a contract
183
+ * Typed Temporal client with Result/Future pattern based on a contract
59
184
  *
60
185
  * Provides type-safe methods to start and execute workflows
61
- * defined in the contract.
186
+ * defined in the contract, with explicit error handling using Result pattern.
62
187
  */
63
188
  declare class TypedClient<TContract extends ContractDefinition> {
64
189
  private readonly contract;
65
190
  private readonly client;
66
191
  private constructor();
67
192
  /**
68
- * Create a typed Temporal client from a contract
193
+ * Create a typed Temporal client with boxed pattern from a contract
69
194
  *
70
195
  * @example
71
196
  * ```ts
@@ -77,24 +202,35 @@ declare class TypedClient<TContract extends ContractDefinition> {
77
202
  *
78
203
  * const result = await client.executeWorkflow('processOrder', {
79
204
  * workflowId: 'order-123',
80
- * args: [...],
205
+ * args: { ... },
206
+ * });
207
+ *
208
+ * result.match({
209
+ * Ok: (output) => console.log('Success:', output),
210
+ * Error: (error) => console.error('Failed:', error),
81
211
  * });
82
212
  * ```
83
213
  */
84
214
  static create<TContract extends ContractDefinition>(contract: TContract, options: ClientOptions): TypedClient<TContract>;
85
215
  /**
86
- * Start a workflow and return a typed handle
216
+ * Start a workflow and return a typed handle with Future pattern
87
217
  *
88
218
  * @example
89
219
  * ```ts
90
- * const handle = await client.startWorkflow('processOrder', {
220
+ * const handleResult = await client.startWorkflow('processOrder', {
91
221
  * workflowId: 'order-123',
92
- * args: ['ORD-123', 'CUST-456', [{ productId: 'PROD-1', quantity: 2 }]],
222
+ * args: { orderId: 'ORD-123' },
93
223
  * workflowExecutionTimeout: '1 day',
94
224
  * retry: { maximumAttempts: 3 },
95
225
  * });
96
226
  *
97
- * const result = await handle.result();
227
+ * handleResult.match({
228
+ * Ok: async (handle) => {
229
+ * const result = await handle.result();
230
+ * // ... handle result
231
+ * },
232
+ * Error: (error) => console.error('Failed to start:', error),
233
+ * });
98
234
  * ```
99
235
  */
100
236
  startWorkflow<TWorkflowName extends keyof TContract["workflows"]>(workflowName: TWorkflowName, {
@@ -102,20 +238,23 @@ declare class TypedClient<TContract extends ContractDefinition> {
102
238
  ...temporalOptions
103
239
  }: TypedWorkflowStartOptions & {
104
240
  args: ClientInferInput<TContract["workflows"][TWorkflowName]>;
105
- }): Promise<TypedWorkflowHandle<TContract["workflows"][TWorkflowName]>>;
241
+ }): Future<Result<TypedWorkflowHandle<TContract["workflows"][TWorkflowName]>, TypedClientError>>;
106
242
  /**
107
- * Execute a workflow (start and wait for result)
243
+ * Execute a workflow (start and wait for result) with Future/Result pattern
108
244
  *
109
245
  * @example
110
246
  * ```ts
111
247
  * const result = await client.executeWorkflow('processOrder', {
112
248
  * workflowId: 'order-123',
113
- * args: ['ORD-123', 'CUST-456', [{ productId: 'PROD-1', quantity: 2 }]],
249
+ * args: { orderId: 'ORD-123' },
114
250
  * workflowExecutionTimeout: '1 day',
115
251
  * retry: { maximumAttempts: 3 },
116
252
  * });
117
253
  *
118
- * console.log(result.status); // fully typed!
254
+ * result.match({
255
+ * Ok: (output) => console.log('Order processed:', output.status),
256
+ * Error: (error) => console.error('Processing failed:', error),
257
+ * });
119
258
  * ```
120
259
  */
121
260
  executeWorkflow<TWorkflowName extends keyof TContract["workflows"]>(workflowName: TWorkflowName, {
@@ -123,69 +262,24 @@ declare class TypedClient<TContract extends ContractDefinition> {
123
262
  ...temporalOptions
124
263
  }: TypedWorkflowStartOptions & {
125
264
  args: ClientInferInput<TContract["workflows"][TWorkflowName]>;
126
- }): Promise<ClientInferOutput<TContract["workflows"][TWorkflowName]>>;
265
+ }): Future<Result<ClientInferOutput<TContract["workflows"][TWorkflowName]>, TypedClientError>>;
127
266
  /**
128
- * Get a handle to an existing workflow
267
+ * Get a handle to an existing workflow with Future/Result pattern
129
268
  *
130
269
  * @example
131
270
  * ```ts
132
- * const handle = await client.getHandle('processOrder', 'order-123');
133
- * const result = await handle.result();
271
+ * const handleResult = await client.getHandle('processOrder', 'order-123');
272
+ * handleResult.match({
273
+ * Ok: async (handle) => {
274
+ * const result = await handle.result();
275
+ * // ... handle result
276
+ * },
277
+ * Error: (error) => console.error('Failed to get handle:', error),
278
+ * });
134
279
  * ```
135
280
  */
136
- getHandle<TWorkflowName extends keyof TContract["workflows"]>(workflowName: TWorkflowName, workflowId: string): Promise<TypedWorkflowHandle<TContract["workflows"][TWorkflowName]>>;
281
+ getHandle<TWorkflowName extends keyof TContract["workflows"]>(workflowName: TWorkflowName, workflowId: string): Future<Result<TypedWorkflowHandle<TContract["workflows"][TWorkflowName]>, TypedClientError>>;
137
282
  private createTypedHandle;
138
283
  }
139
284
  //#endregion
140
- //#region src/errors.d.ts
141
- /**
142
- * Base error class for typed client errors
143
- */
144
- declare class TypedClientError extends Error {
145
- constructor(message: string);
146
- }
147
- /**
148
- * Error thrown when a workflow is not found in the contract
149
- */
150
- declare class WorkflowNotFoundError extends TypedClientError {
151
- readonly workflowName: string;
152
- readonly availableWorkflows: readonly string[];
153
- constructor(workflowName: string, availableWorkflows?: readonly string[]);
154
- }
155
- /**
156
- * Error thrown when workflow input or output validation fails
157
- */
158
- declare class WorkflowValidationError extends TypedClientError {
159
- readonly workflowName: string;
160
- readonly phase: "input" | "output";
161
- readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
162
- constructor(workflowName: string, phase: "input" | "output", issues: ReadonlyArray<StandardSchemaV1.Issue>);
163
- }
164
- /**
165
- * Error thrown when query input or output validation fails
166
- */
167
- declare class QueryValidationError extends TypedClientError {
168
- readonly queryName: string;
169
- readonly phase: "input" | "output";
170
- readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
171
- constructor(queryName: string, phase: "input" | "output", issues: ReadonlyArray<StandardSchemaV1.Issue>);
172
- }
173
- /**
174
- * Error thrown when signal input validation fails
175
- */
176
- declare class SignalValidationError extends TypedClientError {
177
- readonly signalName: string;
178
- readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
179
- constructor(signalName: string, issues: ReadonlyArray<StandardSchemaV1.Issue>);
180
- }
181
- /**
182
- * Error thrown when update input or output validation fails
183
- */
184
- declare class UpdateValidationError extends TypedClientError {
185
- readonly updateName: string;
186
- readonly phase: "input" | "output";
187
- readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
188
- constructor(updateName: string, phase: "input" | "output", issues: ReadonlyArray<StandardSchemaV1.Issue>);
189
- }
190
- //#endregion
191
- export { QueryValidationError, SignalValidationError, TypedClient, TypedClientError, type TypedWorkflowHandle, type TypedWorkflowStartOptions, UpdateValidationError, WorkflowNotFoundError, WorkflowValidationError };
285
+ export { type ClientInferActivities, type ClientInferActivity, type ClientInferInput, type ClientInferOutput, type ClientInferQuery, type ClientInferSignal, type ClientInferUpdate, type ClientInferWorkflow, type ClientInferWorkflowActivities, type ClientInferWorkflowContextActivities, type ClientInferWorkflowQueries, type ClientInferWorkflowSignals, type ClientInferWorkflowUpdates, type ClientInferWorkflows, QueryValidationError, SignalValidationError, TypedClient, TypedClientError, type TypedWorkflowHandle, type TypedWorkflowStartOptions, UpdateValidationError, WorkflowNotFoundError, WorkflowValidationError };