@sanity/functions 1.8.1 → 1.10.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.
- package/dist/types/durables.d.ts +203 -14
- package/package.json +5 -5
- package/src/types/durables.ts +227 -13
package/dist/types/durables.d.ts
CHANGED
|
@@ -26,6 +26,33 @@ export interface DurableLogger {
|
|
|
26
26
|
export interface DurableStepCallbackContext {
|
|
27
27
|
logger: DurableLogger;
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Context parameter passed to a branch method of a `step.fanout` execution.
|
|
31
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
32
|
+
* @hidden
|
|
33
|
+
*/
|
|
34
|
+
export interface DurableStepContext extends DurableStepCallbackContext {
|
|
35
|
+
step: DurableOperations;
|
|
36
|
+
}
|
|
37
|
+
export interface DurableFanoutBranchContext extends DurableStepContext {
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Context parameter passed to the top-level `durableEventHandler` method.
|
|
41
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
42
|
+
* @hidden
|
|
43
|
+
*/
|
|
44
|
+
export interface DurableTopLevelContext extends DurableStepContext {
|
|
45
|
+
context: DurableContext;
|
|
46
|
+
event?: GenericEvent;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Context parameter passed to the `waitForCallback` submitter method.
|
|
50
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
51
|
+
* @hidden
|
|
52
|
+
*/
|
|
53
|
+
export interface DurableCallbackSubmitterContext extends DurableStepCallbackContext {
|
|
54
|
+
callbackId: string;
|
|
55
|
+
}
|
|
29
56
|
/**
|
|
30
57
|
* Arguments passed to callbacks that execute a durable attempt.
|
|
31
58
|
* step.run & step.waitForCondition
|
|
@@ -89,7 +116,12 @@ export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: Du
|
|
|
89
116
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
90
117
|
* @hidden
|
|
91
118
|
*/
|
|
92
|
-
export type
|
|
119
|
+
export type DurableStepFanoutHandler<T = unknown> = DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>;
|
|
120
|
+
/**
|
|
121
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
122
|
+
* @hidden
|
|
123
|
+
*/
|
|
124
|
+
export type DurableStepWaitForCallbackSubmitter = DurableStepHandler<[context: DurableCallbackSubmitterContext], Promise<void>>;
|
|
93
125
|
/**
|
|
94
126
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
95
127
|
* @hidden
|
|
@@ -103,6 +135,109 @@ export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<[
|
|
|
103
135
|
state: T,
|
|
104
136
|
context: Pick<DurableStepAttemptContext, 'attempt'>
|
|
105
137
|
], DurableWaitForConditionDecision>;
|
|
138
|
+
/**
|
|
139
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
140
|
+
* @hidden
|
|
141
|
+
*/
|
|
142
|
+
export type BatchItemSucceededStatus = 'SUCCEEDED';
|
|
143
|
+
/**
|
|
144
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
145
|
+
* @hidden
|
|
146
|
+
*/
|
|
147
|
+
export type BatchItemFailedStatus = 'FAILED';
|
|
148
|
+
/**
|
|
149
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
150
|
+
* @hidden
|
|
151
|
+
*/
|
|
152
|
+
export type BatchItemStartedStatus = 'STARTED';
|
|
153
|
+
/**
|
|
154
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
155
|
+
* @hidden
|
|
156
|
+
*/
|
|
157
|
+
export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus | BatchItemStartedStatus;
|
|
158
|
+
/**
|
|
159
|
+
* Reason why a batch operation (map, parallel, or concurrent execution)
|
|
160
|
+
* completed.
|
|
161
|
+
*
|
|
162
|
+
* - `ALL_COMPLETED`: every item finished.
|
|
163
|
+
* - `MIN_SUCCESSFUL_REACHED`: CompletionConfig.minSuccessful was reached.
|
|
164
|
+
* - `FAILURE_TOLERANCE_EXCEEDED`: a failure threshold was exceeded.
|
|
165
|
+
* - `CUSTOM_COMPLETION_SUCCEEDED`: a custom CompletionConfig.shouldComplete
|
|
166
|
+
* predicate signalled completion with a `SUCCEEDED` outcome before all items
|
|
167
|
+
* finished.
|
|
168
|
+
* - `CUSTOM_COMPLETION_FAILED`: a custom CompletionConfig.shouldComplete
|
|
169
|
+
* predicate signalled completion with a `FAILED` outcome before all items
|
|
170
|
+
* finished.
|
|
171
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
172
|
+
* @hidden
|
|
173
|
+
* @remarks copied from AWS Durable Execution SDK
|
|
174
|
+
*/
|
|
175
|
+
export type CompletionReason = 'ALL_COMPLETED' | 'MIN_SUCCESSFUL_REACHED' | 'FAILURE_TOLERANCE_EXCEEDED' | 'CUSTOM_COMPLETION_SUCCEEDED' | 'CUSTOM_COMPLETION_FAILED';
|
|
176
|
+
/**
|
|
177
|
+
* The result of a `step.fanout` branch.
|
|
178
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
179
|
+
* @hidden
|
|
180
|
+
* @remarks copied from AWS Durable Execution SDK
|
|
181
|
+
*/
|
|
182
|
+
export interface BatchItem<TResult> {
|
|
183
|
+
/** The result value if the item succeeded */
|
|
184
|
+
result?: TResult;
|
|
185
|
+
/** The error if the item failed */
|
|
186
|
+
error?: Error;
|
|
187
|
+
/** Index of the item in the original array */
|
|
188
|
+
index: number;
|
|
189
|
+
/** Status of the item execution */
|
|
190
|
+
status: BatchItemStatus;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
194
|
+
* @hidden
|
|
195
|
+
* @remarks copied from AWS Durable Execution SDK
|
|
196
|
+
*/
|
|
197
|
+
export interface BatchResult<TResult> {
|
|
198
|
+
/**
|
|
199
|
+
* All items in the batch with their results/errors.
|
|
200
|
+
*/
|
|
201
|
+
all: Array<BatchItem<TResult>>;
|
|
202
|
+
/** Returns only the items that succeeded */
|
|
203
|
+
succeeded(): Array<BatchItem<TResult> & {
|
|
204
|
+
result: TResult;
|
|
205
|
+
}>;
|
|
206
|
+
/** Returns only the items that failed */
|
|
207
|
+
failed(): Array<BatchItem<TResult> & {
|
|
208
|
+
error: Error;
|
|
209
|
+
}>;
|
|
210
|
+
/**
|
|
211
|
+
* Returns only the items that are still in progress (STARTED) — items that
|
|
212
|
+
* were in flight when the batch completed early (e.g. via CompletionConfig.minSuccessful
|
|
213
|
+
* or a custom CompletionConfig.shouldComplete).
|
|
214
|
+
*/
|
|
215
|
+
started(): Array<BatchItem<TResult> & {
|
|
216
|
+
status: BatchItemStartedStatus;
|
|
217
|
+
}>;
|
|
218
|
+
/** Overall status of the batch (SUCCEEDED if no failures, FAILED otherwise) */
|
|
219
|
+
status: BatchItemSucceededStatus | BatchItemFailedStatus;
|
|
220
|
+
/** Reason why the batch completed */
|
|
221
|
+
completionReason: CompletionReason;
|
|
222
|
+
/** Whether any item in the batch failed */
|
|
223
|
+
hasFailure: boolean;
|
|
224
|
+
/** Throws the first error if any item failed */
|
|
225
|
+
throwIfError(): void;
|
|
226
|
+
/** Returns array of all successful results */
|
|
227
|
+
getResults(): Array<TResult>;
|
|
228
|
+
/** Returns array of all errors */
|
|
229
|
+
getErrors(): Array<Error>;
|
|
230
|
+
/** Number of successful items */
|
|
231
|
+
successCount: number;
|
|
232
|
+
/** Number of failed items */
|
|
233
|
+
failureCount: number;
|
|
234
|
+
/**
|
|
235
|
+
* Number of started but not completed items. Not guaranteed to be stable across suspend/resume
|
|
236
|
+
*/
|
|
237
|
+
startedCount: number;
|
|
238
|
+
/** Total number of items */
|
|
239
|
+
totalCount: number;
|
|
240
|
+
}
|
|
106
241
|
/**
|
|
107
242
|
* The interface defining the operations available to a durable function.
|
|
108
243
|
* These operations are steps that delegate to other functions, and wait for external callbacks.
|
|
@@ -143,6 +278,55 @@ export type DurableOperations = {
|
|
|
143
278
|
handler: DurableStepRunHandler<T>;
|
|
144
279
|
retry?: DurableRetryStrategy;
|
|
145
280
|
}): Promise<T>;
|
|
281
|
+
/**
|
|
282
|
+
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
283
|
+
* @remarks By using the generic, you can set a single, homogeneous return type for all branches.
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* step.fanout<string>({
|
|
287
|
+
* name: 'parallel-runs',
|
|
288
|
+
* branches: [async ({step, logger}) => {
|
|
289
|
+
* logger.log('Running first function in parallel')
|
|
290
|
+
* return 'first'
|
|
291
|
+
* }, async ({step, logger}) => {
|
|
292
|
+
* logger.log('Running second function in parallel')
|
|
293
|
+
* return 'second'
|
|
294
|
+
* }],
|
|
295
|
+
* })
|
|
296
|
+
* ```
|
|
297
|
+
* @param name - Step name
|
|
298
|
+
* @param branches - functions being executed in parallel
|
|
299
|
+
* @returns A batch result of values returned or errors thrown by all branches.
|
|
300
|
+
*/
|
|
301
|
+
fanout<T>({ name, branches }: {
|
|
302
|
+
name?: string;
|
|
303
|
+
branches: DurableStepFanoutHandler<T>[];
|
|
304
|
+
}): Promise<BatchResult<T>>;
|
|
305
|
+
/**
|
|
306
|
+
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
307
|
+
* @remarks This overload provides automatic type inference for heterogeneous branch return types. When branches return different types, the result will be `BatchResult<T1 | T2 | ...>`.
|
|
308
|
+
* Use the explicit type parameter overloads above for strict homogeneous type checking.
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* step.fanout<string>({
|
|
312
|
+
* name: 'parallel-runs',
|
|
313
|
+
* branches: [async ({step, logger}) => {
|
|
314
|
+
* logger.log('Running first function in parallel')
|
|
315
|
+
* return 'first'
|
|
316
|
+
* }, async ({step, logger}) => {
|
|
317
|
+
* logger.log('Running second function in parallel')
|
|
318
|
+
* return 'second'
|
|
319
|
+
* }],
|
|
320
|
+
* })
|
|
321
|
+
* ```
|
|
322
|
+
* @param name - Step name
|
|
323
|
+
* @param branches - functions being executed in parallel
|
|
324
|
+
* @returns A batch result of values returned or errors thrown by all branches.
|
|
325
|
+
*/
|
|
326
|
+
fanout<Branches extends readonly unknown[]>({ name, branches, }: {
|
|
327
|
+
name?: string;
|
|
328
|
+
branches: Branches;
|
|
329
|
+
}): Promise<BatchResult<Branches[number] extends DurableStepFanoutHandler<infer ReturnType> ? ReturnType : never>>;
|
|
146
330
|
/**
|
|
147
331
|
* Calls another function and awaits its result.
|
|
148
332
|
* similar to ctx.invoke()
|
|
@@ -179,16 +363,26 @@ export type DurableOperations = {
|
|
|
179
363
|
retry?: DurableRetryStrategy;
|
|
180
364
|
}): Promise<T>;
|
|
181
365
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
366
|
+
* Generates a unique callback ID, runs a handler to pass the ID to an external system and waits for some external system to POST JSON data to a Sanity Durable callback API.
|
|
367
|
+
* @example
|
|
368
|
+
* Wait for 30 seconds
|
|
369
|
+
* ```ts
|
|
370
|
+
* await step.waitForCallback({name: 'ask-for-permission', submitter: async ({callbackId}) => {
|
|
371
|
+
* await sendForApproval(callbackId)
|
|
372
|
+
* }});
|
|
373
|
+
* ```
|
|
374
|
+
* ```
|
|
185
375
|
* @param name - Step name
|
|
186
|
-
* @param
|
|
187
|
-
* @
|
|
376
|
+
* @param submitter - Function that submits the provided callbackId parameter to an external system
|
|
377
|
+
* @param timeout - Maximum time to wait for the callback result
|
|
378
|
+
* @param retry - retry configuration for the submitter function
|
|
379
|
+
* @returns The value returned to the Sanity Durable callback HTTP API
|
|
188
380
|
*/
|
|
189
|
-
waitForCallback<T>({ name,
|
|
381
|
+
waitForCallback<T>({ name, submitter, timeout, retry, }: {
|
|
190
382
|
name?: string;
|
|
191
|
-
|
|
383
|
+
submitter: DurableStepWaitForCallbackSubmitter;
|
|
384
|
+
timeout?: DurableDuration;
|
|
385
|
+
retry?: DurableRetryStrategy;
|
|
192
386
|
}): Promise<T>;
|
|
193
387
|
/**
|
|
194
388
|
* Waits for a specified duration
|
|
@@ -265,9 +459,4 @@ export type DurableOperations = {
|
|
|
265
459
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
266
460
|
* @hidden
|
|
267
461
|
*/
|
|
268
|
-
export type DurableHandler = (envelope:
|
|
269
|
-
context: DurableContext;
|
|
270
|
-
event?: GenericEvent;
|
|
271
|
-
step: DurableOperations;
|
|
272
|
-
logger: DurableLogger;
|
|
273
|
-
}) => unknown | Promise<unknown>;
|
|
462
|
+
export type DurableHandler = (envelope: DurableTopLevelContext) => unknown | Promise<unknown>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/functions",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Helper methods and type definitions for Sanity Functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"typecheck": "tsc --noEmit --project tsconfig.test.json"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@aws-lite/client": "^0.
|
|
50
|
+
"@aws-lite/client": "^0.24.1",
|
|
51
51
|
"@aws-lite/dynamodb": "^0.3.9",
|
|
52
52
|
"@aws-lite/dynamodb-types": "^0.3.12",
|
|
53
53
|
"@aws-lite/lambda": "^0.1.5",
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
"@aws-lite/sns-types": "^0.0.11",
|
|
57
57
|
"@aws-lite/sqs": "^0.2.4",
|
|
58
58
|
"@aws-lite/sqs-types": "^0.2.7",
|
|
59
|
-
"@biomejs/biome": "2.5.
|
|
59
|
+
"@biomejs/biome": "2.5.15",
|
|
60
60
|
"@types/node": "^22",
|
|
61
|
-
"@vitest/coverage-v8": "
|
|
61
|
+
"@vitest/coverage-v8": "5.0.4",
|
|
62
62
|
"rimraf": "^6.1.2",
|
|
63
63
|
"typescript": "^7.0.2",
|
|
64
|
-
"vitest": "
|
|
64
|
+
"vitest": "5.0.4"
|
|
65
65
|
},
|
|
66
66
|
"engines": {
|
|
67
67
|
"node": ">=22"
|
package/src/types/durables.ts
CHANGED
|
@@ -30,6 +30,35 @@ export interface DurableStepCallbackContext {
|
|
|
30
30
|
logger: DurableLogger
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Context parameter passed to a branch method of a `step.fanout` execution.
|
|
35
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
36
|
+
* @hidden
|
|
37
|
+
*/
|
|
38
|
+
export interface DurableStepContext extends DurableStepCallbackContext {
|
|
39
|
+
step: DurableOperations
|
|
40
|
+
}
|
|
41
|
+
export interface DurableFanoutBranchContext extends DurableStepContext {}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Context parameter passed to the top-level `durableEventHandler` method.
|
|
45
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
46
|
+
* @hidden
|
|
47
|
+
*/
|
|
48
|
+
export interface DurableTopLevelContext extends DurableStepContext {
|
|
49
|
+
context: DurableContext
|
|
50
|
+
event?: GenericEvent
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Context parameter passed to the `waitForCallback` submitter method.
|
|
55
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
56
|
+
* @hidden
|
|
57
|
+
*/
|
|
58
|
+
export interface DurableCallbackSubmitterContext extends DurableStepCallbackContext {
|
|
59
|
+
callbackId: string
|
|
60
|
+
}
|
|
61
|
+
|
|
33
62
|
/**
|
|
34
63
|
* Arguments passed to callbacks that execute a durable attempt.
|
|
35
64
|
* step.run & step.waitForCondition
|
|
@@ -85,7 +114,13 @@ export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: Du
|
|
|
85
114
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
86
115
|
* @hidden
|
|
87
116
|
*/
|
|
88
|
-
export type
|
|
117
|
+
export type DurableStepFanoutHandler<T = unknown> = DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
121
|
+
* @hidden
|
|
122
|
+
*/
|
|
123
|
+
export type DurableStepWaitForCallbackSubmitter = DurableStepHandler<[context: DurableCallbackSubmitterContext], Promise<void>>
|
|
89
124
|
|
|
90
125
|
/**
|
|
91
126
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
@@ -102,6 +137,121 @@ export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<
|
|
|
102
137
|
DurableWaitForConditionDecision
|
|
103
138
|
>
|
|
104
139
|
|
|
140
|
+
/**
|
|
141
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
142
|
+
* @hidden
|
|
143
|
+
*/
|
|
144
|
+
export type BatchItemSucceededStatus = 'SUCCEEDED'
|
|
145
|
+
/**
|
|
146
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
147
|
+
* @hidden
|
|
148
|
+
*/
|
|
149
|
+
export type BatchItemFailedStatus = 'FAILED'
|
|
150
|
+
/**
|
|
151
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
152
|
+
* @hidden
|
|
153
|
+
*/
|
|
154
|
+
export type BatchItemStartedStatus = 'STARTED'
|
|
155
|
+
/**
|
|
156
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
157
|
+
* @hidden
|
|
158
|
+
*/
|
|
159
|
+
export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus | BatchItemStartedStatus
|
|
160
|
+
/**
|
|
161
|
+
* Reason why a batch operation (map, parallel, or concurrent execution)
|
|
162
|
+
* completed.
|
|
163
|
+
*
|
|
164
|
+
* - `ALL_COMPLETED`: every item finished.
|
|
165
|
+
* - `MIN_SUCCESSFUL_REACHED`: CompletionConfig.minSuccessful was reached.
|
|
166
|
+
* - `FAILURE_TOLERANCE_EXCEEDED`: a failure threshold was exceeded.
|
|
167
|
+
* - `CUSTOM_COMPLETION_SUCCEEDED`: a custom CompletionConfig.shouldComplete
|
|
168
|
+
* predicate signalled completion with a `SUCCEEDED` outcome before all items
|
|
169
|
+
* finished.
|
|
170
|
+
* - `CUSTOM_COMPLETION_FAILED`: a custom CompletionConfig.shouldComplete
|
|
171
|
+
* predicate signalled completion with a `FAILED` outcome before all items
|
|
172
|
+
* finished.
|
|
173
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
174
|
+
* @hidden
|
|
175
|
+
* @remarks copied from AWS Durable Execution SDK
|
|
176
|
+
*/
|
|
177
|
+
export type CompletionReason =
|
|
178
|
+
| 'ALL_COMPLETED'
|
|
179
|
+
| 'MIN_SUCCESSFUL_REACHED'
|
|
180
|
+
| 'FAILURE_TOLERANCE_EXCEEDED'
|
|
181
|
+
| 'CUSTOM_COMPLETION_SUCCEEDED'
|
|
182
|
+
| 'CUSTOM_COMPLETION_FAILED'
|
|
183
|
+
/**
|
|
184
|
+
* The result of a `step.fanout` branch.
|
|
185
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
186
|
+
* @hidden
|
|
187
|
+
* @remarks copied from AWS Durable Execution SDK
|
|
188
|
+
*/
|
|
189
|
+
export interface BatchItem<TResult> {
|
|
190
|
+
/** The result value if the item succeeded */
|
|
191
|
+
result?: TResult
|
|
192
|
+
/** The error if the item failed */
|
|
193
|
+
error?: Error
|
|
194
|
+
/** Index of the item in the original array */
|
|
195
|
+
index: number
|
|
196
|
+
/** Status of the item execution */
|
|
197
|
+
status: BatchItemStatus
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
201
|
+
* @hidden
|
|
202
|
+
* @remarks copied from AWS Durable Execution SDK
|
|
203
|
+
*/
|
|
204
|
+
export interface BatchResult<TResult> {
|
|
205
|
+
/**
|
|
206
|
+
* All items in the batch with their results/errors.
|
|
207
|
+
*/
|
|
208
|
+
all: Array<BatchItem<TResult>>
|
|
209
|
+
/** Returns only the items that succeeded */
|
|
210
|
+
succeeded(): Array<
|
|
211
|
+
BatchItem<TResult> & {
|
|
212
|
+
result: TResult
|
|
213
|
+
}
|
|
214
|
+
>
|
|
215
|
+
/** Returns only the items that failed */
|
|
216
|
+
failed(): Array<
|
|
217
|
+
BatchItem<TResult> & {
|
|
218
|
+
error: Error
|
|
219
|
+
}
|
|
220
|
+
>
|
|
221
|
+
/**
|
|
222
|
+
* Returns only the items that are still in progress (STARTED) — items that
|
|
223
|
+
* were in flight when the batch completed early (e.g. via CompletionConfig.minSuccessful
|
|
224
|
+
* or a custom CompletionConfig.shouldComplete).
|
|
225
|
+
*/
|
|
226
|
+
started(): Array<
|
|
227
|
+
BatchItem<TResult> & {
|
|
228
|
+
status: BatchItemStartedStatus
|
|
229
|
+
}
|
|
230
|
+
>
|
|
231
|
+
/** Overall status of the batch (SUCCEEDED if no failures, FAILED otherwise) */
|
|
232
|
+
status: BatchItemSucceededStatus | BatchItemFailedStatus
|
|
233
|
+
/** Reason why the batch completed */
|
|
234
|
+
completionReason: CompletionReason
|
|
235
|
+
/** Whether any item in the batch failed */
|
|
236
|
+
hasFailure: boolean
|
|
237
|
+
/** Throws the first error if any item failed */
|
|
238
|
+
throwIfError(): void
|
|
239
|
+
/** Returns array of all successful results */
|
|
240
|
+
getResults(): Array<TResult>
|
|
241
|
+
/** Returns array of all errors */
|
|
242
|
+
getErrors(): Array<Error>
|
|
243
|
+
/** Number of successful items */
|
|
244
|
+
successCount: number
|
|
245
|
+
/** Number of failed items */
|
|
246
|
+
failureCount: number
|
|
247
|
+
/**
|
|
248
|
+
* Number of started but not completed items. Not guaranteed to be stable across suspend/resume
|
|
249
|
+
*/
|
|
250
|
+
startedCount: number
|
|
251
|
+
/** Total number of items */
|
|
252
|
+
totalCount: number
|
|
253
|
+
}
|
|
254
|
+
|
|
105
255
|
/**
|
|
106
256
|
* The interface defining the operations available to a durable function.
|
|
107
257
|
* These operations are steps that delegate to other functions, and wait for external callbacks.
|
|
@@ -138,6 +288,57 @@ export type DurableOperations = {
|
|
|
138
288
|
* @returns The value returned by the executed function
|
|
139
289
|
*/
|
|
140
290
|
run<T>({name, handler, retry}: {name?: string; handler: DurableStepRunHandler<T>; retry?: DurableRetryStrategy}): Promise<T>
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
294
|
+
* @remarks By using the generic, you can set a single, homogeneous return type for all branches.
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* step.fanout<string>({
|
|
298
|
+
* name: 'parallel-runs',
|
|
299
|
+
* branches: [async ({step, logger}) => {
|
|
300
|
+
* logger.log('Running first function in parallel')
|
|
301
|
+
* return 'first'
|
|
302
|
+
* }, async ({step, logger}) => {
|
|
303
|
+
* logger.log('Running second function in parallel')
|
|
304
|
+
* return 'second'
|
|
305
|
+
* }],
|
|
306
|
+
* })
|
|
307
|
+
* ```
|
|
308
|
+
* @param name - Step name
|
|
309
|
+
* @param branches - functions being executed in parallel
|
|
310
|
+
* @returns A batch result of values returned or errors thrown by all branches.
|
|
311
|
+
*/
|
|
312
|
+
fanout<T>({name, branches}: {name?: string; branches: DurableStepFanoutHandler<T>[]}): Promise<BatchResult<T>>
|
|
313
|
+
/**
|
|
314
|
+
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
315
|
+
* @remarks This overload provides automatic type inference for heterogeneous branch return types. When branches return different types, the result will be `BatchResult<T1 | T2 | ...>`.
|
|
316
|
+
* Use the explicit type parameter overloads above for strict homogeneous type checking.
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* step.fanout<string>({
|
|
320
|
+
* name: 'parallel-runs',
|
|
321
|
+
* branches: [async ({step, logger}) => {
|
|
322
|
+
* logger.log('Running first function in parallel')
|
|
323
|
+
* return 'first'
|
|
324
|
+
* }, async ({step, logger}) => {
|
|
325
|
+
* logger.log('Running second function in parallel')
|
|
326
|
+
* return 'second'
|
|
327
|
+
* }],
|
|
328
|
+
* })
|
|
329
|
+
* ```
|
|
330
|
+
* @param name - Step name
|
|
331
|
+
* @param branches - functions being executed in parallel
|
|
332
|
+
* @returns A batch result of values returned or errors thrown by all branches.
|
|
333
|
+
*/
|
|
334
|
+
fanout<Branches extends readonly unknown[]>({
|
|
335
|
+
name,
|
|
336
|
+
branches,
|
|
337
|
+
}: {
|
|
338
|
+
name?: string
|
|
339
|
+
branches: Branches
|
|
340
|
+
}): Promise<BatchResult<Branches[number] extends DurableStepFanoutHandler<infer ReturnType> ? ReturnType : never>>
|
|
341
|
+
|
|
141
342
|
/**
|
|
142
343
|
* Calls another function and awaits its result.
|
|
143
344
|
* similar to ctx.invoke()
|
|
@@ -179,14 +380,32 @@ export type DurableOperations = {
|
|
|
179
380
|
}): Promise<T>
|
|
180
381
|
|
|
181
382
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
383
|
+
* Generates a unique callback ID, runs a handler to pass the ID to an external system and waits for some external system to POST JSON data to a Sanity Durable callback API.
|
|
384
|
+
* @example
|
|
385
|
+
* Wait for 30 seconds
|
|
386
|
+
* ```ts
|
|
387
|
+
* await step.waitForCallback({name: 'ask-for-permission', submitter: async ({callbackId}) => {
|
|
388
|
+
* await sendForApproval(callbackId)
|
|
389
|
+
* }});
|
|
390
|
+
* ```
|
|
391
|
+
* ```
|
|
185
392
|
* @param name - Step name
|
|
186
|
-
* @param
|
|
187
|
-
* @
|
|
393
|
+
* @param submitter - Function that submits the provided callbackId parameter to an external system
|
|
394
|
+
* @param timeout - Maximum time to wait for the callback result
|
|
395
|
+
* @param retry - retry configuration for the submitter function
|
|
396
|
+
* @returns The value returned to the Sanity Durable callback HTTP API
|
|
188
397
|
*/
|
|
189
|
-
waitForCallback<T>({
|
|
398
|
+
waitForCallback<T>({
|
|
399
|
+
name,
|
|
400
|
+
submitter,
|
|
401
|
+
timeout,
|
|
402
|
+
retry,
|
|
403
|
+
}: {
|
|
404
|
+
name?: string
|
|
405
|
+
submitter: DurableStepWaitForCallbackSubmitter
|
|
406
|
+
timeout?: DurableDuration
|
|
407
|
+
retry?: DurableRetryStrategy
|
|
408
|
+
}): Promise<T>
|
|
190
409
|
|
|
191
410
|
/**
|
|
192
411
|
* Waits for a specified duration
|
|
@@ -267,9 +486,4 @@ export type DurableOperations = {
|
|
|
267
486
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
268
487
|
* @hidden
|
|
269
488
|
*/
|
|
270
|
-
export type DurableHandler = (envelope:
|
|
271
|
-
context: DurableContext
|
|
272
|
-
event?: GenericEvent
|
|
273
|
-
step: DurableOperations
|
|
274
|
-
logger: DurableLogger
|
|
275
|
-
}) => unknown | Promise<unknown>
|
|
489
|
+
export type DurableHandler = (envelope: DurableTopLevelContext) => unknown | Promise<unknown>
|