@sanity/functions 1.10.1 → 1.11.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/durables.d.ts +14 -42
- package/dist/durables.js +15 -37
- package/dist/durables.js.map +1 -1
- package/dist/invoke.d.ts +3 -2
- package/dist/invoke.js +22 -10
- package/dist/invoke.js.map +1 -1
- package/dist/types/durables.d.ts +97 -117
- package/package.json +1 -1
- package/src/durables.ts +17 -70
- package/src/invoke.ts +27 -12
- package/src/types/durables.ts +117 -116
package/dist/types/durables.d.ts
CHANGED
|
@@ -3,14 +3,12 @@ import type { GenericEvent } from './functions.js';
|
|
|
3
3
|
import type { BlueprintResource } from './resources.js';
|
|
4
4
|
/**
|
|
5
5
|
* Durable alias of FunctionsContext
|
|
6
|
-
* @
|
|
7
|
-
* @hidden
|
|
6
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
8
7
|
*/
|
|
9
8
|
export type DurableContext = FunctionContext;
|
|
10
9
|
/**
|
|
11
10
|
* Logger interface for durables.
|
|
12
|
-
* @
|
|
13
|
-
* @hidden
|
|
11
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
14
12
|
*/
|
|
15
13
|
export interface DurableLogger {
|
|
16
14
|
debug(...params: unknown[]): void;
|
|
@@ -20,26 +18,26 @@ export interface DurableLogger {
|
|
|
20
18
|
log(...params: unknown[]): void;
|
|
21
19
|
}
|
|
22
20
|
/**
|
|
23
|
-
* @
|
|
24
|
-
* @hidden
|
|
21
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
25
22
|
*/
|
|
26
23
|
export interface DurableStepCallbackContext {
|
|
27
24
|
logger: DurableLogger;
|
|
28
25
|
}
|
|
29
26
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
32
|
-
* @hidden
|
|
27
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
33
28
|
*/
|
|
34
29
|
export interface DurableStepContext extends DurableStepCallbackContext {
|
|
35
30
|
step: DurableOperations;
|
|
36
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Context parameter passed to a branch method of a `step.fanout` execution.
|
|
34
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
35
|
+
*/
|
|
37
36
|
export interface DurableFanoutBranchContext extends DurableStepContext {
|
|
38
37
|
}
|
|
39
38
|
/**
|
|
40
39
|
* Context parameter passed to the top-level `durableEventHandler` method.
|
|
41
|
-
* @
|
|
42
|
-
* @hidden
|
|
40
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
43
41
|
*/
|
|
44
42
|
export interface DurableTopLevelContext extends DurableStepContext {
|
|
45
43
|
context: DurableContext;
|
|
@@ -47,27 +45,23 @@ export interface DurableTopLevelContext extends DurableStepContext {
|
|
|
47
45
|
}
|
|
48
46
|
/**
|
|
49
47
|
* Context parameter passed to the `waitForCallback` submitter method.
|
|
50
|
-
* @
|
|
51
|
-
* @hidden
|
|
48
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
52
49
|
*/
|
|
53
50
|
export interface DurableCallbackSubmitterContext extends DurableStepCallbackContext {
|
|
54
51
|
callbackId: string;
|
|
55
52
|
}
|
|
56
53
|
/**
|
|
57
|
-
* Arguments passed to callbacks that execute a durable attempt.
|
|
58
|
-
*
|
|
59
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
60
|
-
* @hidden
|
|
54
|
+
* Arguments passed to `step.run` & `step.waitForCondition` callbacks that execute a durable attempt.
|
|
55
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
61
56
|
*/
|
|
62
57
|
export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
63
58
|
/** Current attempt number, starting at 1. */
|
|
64
59
|
attempt: number;
|
|
65
60
|
}
|
|
66
61
|
/**
|
|
67
|
-
* The signature of the duration object passed to the `
|
|
62
|
+
* The signature of the duration object passed to `wait`, `waitForCondition` and `waitForCallback` methods. It is also used when specifying the `retry.delay` of durable steps.
|
|
68
63
|
* It can be specified in days, hours, minutes, or seconds.
|
|
69
|
-
* @
|
|
70
|
-
* @hidden
|
|
64
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
71
65
|
*/
|
|
72
66
|
export type DurableDuration = {
|
|
73
67
|
days: number;
|
|
@@ -85,16 +79,14 @@ export type DurableDuration = {
|
|
|
85
79
|
seconds: number;
|
|
86
80
|
};
|
|
87
81
|
/**
|
|
88
|
-
* @
|
|
89
|
-
* @hidden
|
|
82
|
+
* @beta: Using durables is considered experimental and may change in the future.
|
|
90
83
|
*/
|
|
91
84
|
export interface DurableRetryStrategy {
|
|
92
85
|
maxAttempts?: number;
|
|
93
86
|
delay?: DurableDuration;
|
|
94
87
|
}
|
|
95
88
|
/**
|
|
96
|
-
* @
|
|
97
|
-
* @hidden
|
|
89
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
98
90
|
*/
|
|
99
91
|
export type DurableWaitForConditionDecision = {
|
|
100
92
|
shouldRetry: true;
|
|
@@ -103,62 +95,59 @@ export type DurableWaitForConditionDecision = {
|
|
|
103
95
|
shouldRetry: false;
|
|
104
96
|
};
|
|
105
97
|
/**
|
|
106
|
-
* @
|
|
107
|
-
* @hidden
|
|
98
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
108
99
|
*/
|
|
109
100
|
export type DurableStepHandler<TArgs extends unknown[], TReturn> = (...args: TArgs) => TReturn;
|
|
110
101
|
/**
|
|
111
|
-
* @
|
|
112
|
-
* @hidden
|
|
102
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
113
103
|
*/
|
|
114
104
|
export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: DurableStepAttemptContext], T | Promise<T>>;
|
|
115
105
|
/**
|
|
116
|
-
* @
|
|
117
|
-
* @hidden
|
|
106
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
118
107
|
*/
|
|
119
108
|
export type DurableStepFanoutHandler<T = unknown> = DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>;
|
|
120
109
|
/**
|
|
121
|
-
* @
|
|
122
|
-
|
|
110
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
111
|
+
*/
|
|
112
|
+
export type DurableStepFanoutNamedHandler<T = unknown> = {
|
|
113
|
+
func: DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>;
|
|
114
|
+
name?: string;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
123
118
|
*/
|
|
124
119
|
export type DurableStepWaitForCallbackSubmitter = DurableStepHandler<[context: DurableCallbackSubmitterContext], Promise<void>>;
|
|
125
120
|
/**
|
|
126
|
-
* @
|
|
127
|
-
* @hidden
|
|
121
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
128
122
|
*/
|
|
129
123
|
export type DurableWaitForConditionPoller<T = unknown> = DurableStepHandler<[state: T, context: DurableStepAttemptContext], Promise<T>>;
|
|
130
124
|
/**
|
|
131
|
-
* @
|
|
132
|
-
* @hidden
|
|
125
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
133
126
|
*/
|
|
134
127
|
export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<[
|
|
135
128
|
state: T,
|
|
136
129
|
context: Pick<DurableStepAttemptContext, 'attempt'>
|
|
137
130
|
], DurableWaitForConditionDecision>;
|
|
138
131
|
/**
|
|
139
|
-
* @
|
|
140
|
-
* @hidden
|
|
132
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
141
133
|
*/
|
|
142
134
|
export type BatchItemSucceededStatus = 'SUCCEEDED';
|
|
143
135
|
/**
|
|
144
|
-
* @
|
|
136
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
145
137
|
* @hidden
|
|
146
138
|
*/
|
|
147
139
|
export type BatchItemFailedStatus = 'FAILED';
|
|
148
140
|
/**
|
|
149
|
-
* @
|
|
141
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
150
142
|
* @hidden
|
|
151
143
|
*/
|
|
152
144
|
export type BatchItemStartedStatus = 'STARTED';
|
|
153
145
|
/**
|
|
154
|
-
* @
|
|
155
|
-
* @hidden
|
|
146
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
156
147
|
*/
|
|
157
148
|
export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus | BatchItemStartedStatus;
|
|
158
149
|
/**
|
|
159
|
-
* Reason why a
|
|
160
|
-
* completed.
|
|
161
|
-
*
|
|
150
|
+
* Reason why a `step.fanout` operation completed.
|
|
162
151
|
* - `ALL_COMPLETED`: every item finished.
|
|
163
152
|
* - `MIN_SUCCESSFUL_REACHED`: CompletionConfig.minSuccessful was reached.
|
|
164
153
|
* - `FAILURE_TOLERANCE_EXCEEDED`: a failure threshold was exceeded.
|
|
@@ -168,16 +157,12 @@ export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus |
|
|
|
168
157
|
* - `CUSTOM_COMPLETION_FAILED`: a custom CompletionConfig.shouldComplete
|
|
169
158
|
* predicate signalled completion with a `FAILED` outcome before all items
|
|
170
159
|
* finished.
|
|
171
|
-
* @
|
|
172
|
-
* @hidden
|
|
173
|
-
* @remarks copied from AWS Durable Execution SDK
|
|
160
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
174
161
|
*/
|
|
175
162
|
export type CompletionReason = 'ALL_COMPLETED' | 'MIN_SUCCESSFUL_REACHED' | 'FAILURE_TOLERANCE_EXCEEDED' | 'CUSTOM_COMPLETION_SUCCEEDED' | 'CUSTOM_COMPLETION_FAILED';
|
|
176
163
|
/**
|
|
177
164
|
* The result of a `step.fanout` branch.
|
|
178
|
-
* @
|
|
179
|
-
* @hidden
|
|
180
|
-
* @remarks copied from AWS Durable Execution SDK
|
|
165
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
181
166
|
*/
|
|
182
167
|
export interface BatchItem<TResult> {
|
|
183
168
|
/** The result value if the item succeeded */
|
|
@@ -190,9 +175,7 @@ export interface BatchItem<TResult> {
|
|
|
190
175
|
status: BatchItemStatus;
|
|
191
176
|
}
|
|
192
177
|
/**
|
|
193
|
-
* @
|
|
194
|
-
* @hidden
|
|
195
|
-
* @remarks copied from AWS Durable Execution SDK
|
|
178
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
196
179
|
*/
|
|
197
180
|
export interface BatchResult<TResult> {
|
|
198
181
|
/**
|
|
@@ -239,39 +222,39 @@ export interface BatchResult<TResult> {
|
|
|
239
222
|
totalCount: number;
|
|
240
223
|
}
|
|
241
224
|
/**
|
|
242
|
-
* The interface defining the operations available to a durable function.
|
|
243
|
-
*
|
|
244
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
245
|
-
* @hidden
|
|
225
|
+
* The interface defining the durable operations available to a durable function.
|
|
226
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
246
227
|
*/
|
|
247
228
|
export type DurableOperations = {
|
|
248
229
|
/**
|
|
249
|
-
* Runs a named step. Its result is recorded, so if the
|
|
230
|
+
* Runs a named step. Its result is recorded, so if the durable re-runs the step
|
|
250
231
|
* is not executed again. Failures are retried on their own.
|
|
251
|
-
* similar to ctx.step()
|
|
252
|
-
* @remarks cannot be nested inside another step
|
|
253
232
|
* @example
|
|
254
233
|
* ```ts
|
|
255
|
-
* step.run({
|
|
234
|
+
* const hi = await step.run({
|
|
256
235
|
* name: 'run-step',
|
|
257
236
|
* handler: async ({logger}) => {
|
|
258
237
|
* logger.log('Running step')
|
|
238
|
+
* return 'hi'
|
|
259
239
|
* }})
|
|
260
240
|
* ```
|
|
261
241
|
*
|
|
262
242
|
* @example
|
|
263
243
|
* ```ts
|
|
264
244
|
* // With a retry config
|
|
265
|
-
* step.run({
|
|
245
|
+
* const hello = await step.run({
|
|
266
246
|
* name: 'run-with-retry'
|
|
267
|
-
* handler: () => {
|
|
247
|
+
* handler: () => {
|
|
248
|
+
* if (Math.random() > 0.5) throw new Error('random error!')
|
|
249
|
+
* return 'hello'
|
|
250
|
+
* }
|
|
268
251
|
* retry: { maxAttempts: 2, delay: { seconds: 5 } }
|
|
269
252
|
* })
|
|
270
253
|
* ```
|
|
271
254
|
* @param name - Step name
|
|
272
255
|
* @param handler - function being executed
|
|
273
256
|
* @param retry - retry configuration
|
|
274
|
-
* @returns The value returned by the
|
|
257
|
+
* @returns The value returned by the provided handler
|
|
275
258
|
*/
|
|
276
259
|
run<T>({ name, handler, retry }: {
|
|
277
260
|
name?: string;
|
|
@@ -279,18 +262,21 @@ export type DurableOperations = {
|
|
|
279
262
|
retry?: DurableRetryStrategy;
|
|
280
263
|
}): Promise<T>;
|
|
281
264
|
/**
|
|
282
|
-
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
265
|
+
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available. Branches may be named.
|
|
283
266
|
* @remarks By using the generic, you can set a single, homogeneous return type for all branches.
|
|
284
267
|
* @example
|
|
285
268
|
* ```ts
|
|
286
|
-
* step.fanout<string>({
|
|
269
|
+
* const results: BatchResult<string> = await step.fanout<string>({
|
|
287
270
|
* name: 'parallel-runs',
|
|
288
271
|
* branches: [async ({step, logger}) => {
|
|
289
272
|
* logger.log('Running first function in parallel')
|
|
290
273
|
* return 'first'
|
|
291
|
-
* },
|
|
292
|
-
*
|
|
293
|
-
*
|
|
274
|
+
* }, {
|
|
275
|
+
* func: async ({step, logger}) => {
|
|
276
|
+
* logger.log('Running second function in parallel')
|
|
277
|
+
* return 'second'
|
|
278
|
+
* },
|
|
279
|
+
* name: 'second-branch',
|
|
294
280
|
* }],
|
|
295
281
|
* })
|
|
296
282
|
* ```
|
|
@@ -298,9 +284,9 @@ export type DurableOperations = {
|
|
|
298
284
|
* @param branches - functions being executed in parallel
|
|
299
285
|
* @returns A batch result of values returned or errors thrown by all branches.
|
|
300
286
|
*/
|
|
301
|
-
fanout<T>({ name, branches }: {
|
|
287
|
+
fanout<T>({ name, branches, }: {
|
|
302
288
|
name?: string;
|
|
303
|
-
branches: DurableStepFanoutHandler<T>[];
|
|
289
|
+
branches: (DurableStepFanoutHandler<T> | DurableStepFanoutNamedHandler<T>)[];
|
|
304
290
|
}): Promise<BatchResult<T>>;
|
|
305
291
|
/**
|
|
306
292
|
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
@@ -308,14 +294,14 @@ export type DurableOperations = {
|
|
|
308
294
|
* Use the explicit type parameter overloads above for strict homogeneous type checking.
|
|
309
295
|
* @example
|
|
310
296
|
* ```ts
|
|
311
|
-
* step.fanout
|
|
297
|
+
* const results: BatchResult<string | number> = await step.fanout({
|
|
312
298
|
* name: 'parallel-runs',
|
|
313
299
|
* branches: [async ({step, logger}) => {
|
|
314
300
|
* logger.log('Running first function in parallel')
|
|
315
301
|
* return 'first'
|
|
316
302
|
* }, async ({step, logger}) => {
|
|
317
303
|
* logger.log('Running second function in parallel')
|
|
318
|
-
* return
|
|
304
|
+
* return 2
|
|
319
305
|
* }],
|
|
320
306
|
* })
|
|
321
307
|
* ```
|
|
@@ -326,29 +312,27 @@ export type DurableOperations = {
|
|
|
326
312
|
fanout<Branches extends readonly unknown[]>({ name, branches, }: {
|
|
327
313
|
name?: string;
|
|
328
314
|
branches: Branches;
|
|
329
|
-
}): Promise<BatchResult<Branches[number] extends DurableStepFanoutHandler<infer ReturnType> ? ReturnType : never>>;
|
|
315
|
+
}): Promise<BatchResult<Branches[number] extends DurableStepFanoutHandler<infer ReturnType> ? ReturnType : Branches[number] extends DurableStepFanoutNamedHandler<infer ReturnType> ? ReturnType : never>>;
|
|
330
316
|
/**
|
|
331
|
-
*
|
|
332
|
-
* similar to ctx.invoke()
|
|
333
|
-
* @remarks cannot be nested inside another step
|
|
317
|
+
* Invokes another function, of type durable or pubsub, and awaits its result. While awaiting, the durable function shuts down, so you are not charged for compute.
|
|
334
318
|
* @example
|
|
335
319
|
* ```ts
|
|
336
320
|
* // my-delegated-function/index.ts
|
|
337
|
-
* export const handler = pubSubEventHandler({event}) => {
|
|
338
|
-
*
|
|
339
|
-
* }
|
|
321
|
+
* export const handler = pubSubEventHandler(({event}) => {
|
|
322
|
+
* return 'yo'
|
|
323
|
+
* })
|
|
340
324
|
*
|
|
341
325
|
* // my-resource/index.ts
|
|
342
|
-
* export const handler = pubSubEventHandler({event}) => {
|
|
343
|
-
*
|
|
344
|
-
* }
|
|
326
|
+
* export const handler = pubSubEventHandler(({event}) => {
|
|
327
|
+
* return 'hey'
|
|
328
|
+
* })
|
|
345
329
|
*
|
|
346
330
|
* // my-durable-function/index.ts
|
|
347
|
-
* durableEventHandler(({step, context}) => {
|
|
331
|
+
* export const handler = durableEventHandler(({step, context}) => {
|
|
348
332
|
* // With a function name
|
|
349
|
-
* step.delegate({name: '
|
|
333
|
+
* const firstResult = await step.delegate({name: 'first-delegation', handler: 'my-delegated-function', event: {data: {hello: 'world'}}})
|
|
350
334
|
* // With a resource object
|
|
351
|
-
* step.delegate({name: '
|
|
335
|
+
* const secondResult = await step.delegate({name: 'second-delegation', handler: context.resource.function('my-resource'), event: {data: {hello: 'world'}}})
|
|
352
336
|
* })
|
|
353
337
|
* ```
|
|
354
338
|
* @param name - Step name
|
|
@@ -363,13 +347,16 @@ export type DurableOperations = {
|
|
|
363
347
|
retry?: DurableRetryStrategy;
|
|
364
348
|
}): Promise<T>;
|
|
365
349
|
/**
|
|
366
|
-
* Generates a unique callback ID, runs a
|
|
350
|
+
* Generates a unique callback ID, runs a submitter function to pass the ID to an external system and waits for some external system to POST JSON data to a Sanity Durable callback API using the callback ID.
|
|
367
351
|
* @example
|
|
368
|
-
* Wait for 30 seconds
|
|
369
352
|
* ```ts
|
|
370
|
-
*
|
|
371
|
-
* await
|
|
372
|
-
*
|
|
353
|
+
* try {
|
|
354
|
+
* const successfulResult = await step.waitForCallback({name: 'ask-for-permission', submitter: async ({callbackId}) => {
|
|
355
|
+
* await sendForApproval(callbackId)
|
|
356
|
+
* }})
|
|
357
|
+
* } catch (e) {
|
|
358
|
+
* logger.log('oh no we received a callback failure', e)
|
|
359
|
+
* }
|
|
373
360
|
* ```
|
|
374
361
|
* ```
|
|
375
362
|
* @param name - Step name
|
|
@@ -386,8 +373,6 @@ export type DurableOperations = {
|
|
|
386
373
|
}): Promise<T>;
|
|
387
374
|
/**
|
|
388
375
|
* Waits for a specified duration
|
|
389
|
-
* similar to ctx.wait()
|
|
390
|
-
* @remarks cannot be nested inside another step
|
|
391
376
|
* @example
|
|
392
377
|
* Wait for 30 seconds
|
|
393
378
|
* ```ts
|
|
@@ -399,7 +384,6 @@ export type DurableOperations = {
|
|
|
399
384
|
* ```
|
|
400
385
|
* @param name - Step name
|
|
401
386
|
* @param duration - Amount of time the function will wait before continuing.
|
|
402
|
-
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
403
387
|
* @returns Resolves after the duration
|
|
404
388
|
*/
|
|
405
389
|
wait({ name, duration }: {
|
|
@@ -407,29 +391,26 @@ export type DurableOperations = {
|
|
|
407
391
|
duration: DurableDuration;
|
|
408
392
|
}): Promise<void>;
|
|
409
393
|
/**
|
|
410
|
-
* Waits for a specified condition to be met
|
|
411
|
-
* similar to ctx.waitForCondition()
|
|
412
|
-
* @remarks cannot be nested inside another step
|
|
394
|
+
* Waits for a specified condition to be met by executing a `poller` method to retrieve state and then inspecting the retrieved state in a `next` method to determine whether to retry and after what delay to do so.
|
|
413
395
|
* @example
|
|
414
396
|
* ```ts
|
|
415
397
|
* await step.waitForCondition({{
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
398
|
+
* name: 'waitForCondition',
|
|
399
|
+
* initial: {
|
|
400
|
+
* article: null as Article | null,
|
|
401
|
+
* },
|
|
402
|
+
* poller: async (state, {attempt}) => {
|
|
403
|
+
* const client = createClient({
|
|
404
|
+
* apiVersion: '2026-08-05',
|
|
405
|
+
* ...context.clientOptions,
|
|
406
|
+
* })
|
|
407
|
+
* const article = await client.fetch<Article | null>(
|
|
408
|
+
* '*[_type == "article"][0]',
|
|
409
|
+
* )
|
|
410
|
+
* return {...state, article}
|
|
411
|
+
* },
|
|
412
|
+
* next: (state, {attempt}) => {
|
|
413
|
+
* if (state.article) return {shouldRetry: false}
|
|
433
414
|
* return {
|
|
434
415
|
* shouldRetry: true,
|
|
435
416
|
* delay: {
|
|
@@ -456,7 +437,6 @@ export type DurableOperations = {
|
|
|
456
437
|
}): Promise<T>;
|
|
457
438
|
};
|
|
458
439
|
/**
|
|
459
|
-
* @
|
|
460
|
-
* @hidden
|
|
440
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
461
441
|
*/
|
|
462
442
|
export type DurableHandler = (envelope: DurableTopLevelContext) => unknown | Promise<unknown>;
|
package/package.json
CHANGED
package/src/durables.ts
CHANGED
|
@@ -2,19 +2,21 @@ import type {DurableHandler} from './types/durables.js'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Error type indicating an unrecoverable error in a durable function.
|
|
5
|
-
* Used to
|
|
6
|
-
* @
|
|
7
|
-
* @hidden
|
|
5
|
+
* Used to abort any remaining retry behavior in a durable step operation.
|
|
6
|
+
* @beta Durable functions are an experimental feature and may change in the future.
|
|
8
7
|
* @example
|
|
9
8
|
* ```ts
|
|
10
9
|
* const handler = durableEventHandler(async ({step}) => {
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* const user = await step.run({name: 'get-user', handler: async ({attempt}) => {
|
|
11
|
+
* try {
|
|
12
|
+
* return await getUser('some-user')
|
|
13
|
+
* } catch (err) {
|
|
14
|
+
* if (err instanceof UserNotFound) {
|
|
15
|
+
* throw new UnrecoverableError(err)
|
|
16
|
+
* }
|
|
17
|
+
* throw err
|
|
18
|
+
* }
|
|
13
19
|
* }, retry: {maxAttempts: 3})
|
|
14
|
-
* }).catch((err) => {
|
|
15
|
-
* if (err instanceof UserNotFound) {
|
|
16
|
-
* throw new UnrecoverableError(err)
|
|
17
|
-
* }
|
|
18
20
|
* })
|
|
19
21
|
* ```
|
|
20
22
|
*/
|
|
@@ -43,7 +45,7 @@ const resolveFuncArgs = <TConfig extends {name: string; durableTimeout?: number}
|
|
|
43
45
|
* @returns Errors while validating a config object
|
|
44
46
|
*/
|
|
45
47
|
const validateConfig = (config: unknown) => {
|
|
46
|
-
const EVENT_TYPES = ['document', 'media-library', '
|
|
48
|
+
const EVENT_TYPES = ['document', 'media-library', 'sync-tag-invalidate']
|
|
47
49
|
const errors: string[] = []
|
|
48
50
|
|
|
49
51
|
if (config === undefined || config === null) {
|
|
@@ -87,59 +89,7 @@ const validateConfig = (config: unknown) => {
|
|
|
87
89
|
|
|
88
90
|
/**
|
|
89
91
|
* Durables creation function that can be called with or without a config object.
|
|
90
|
-
* @
|
|
91
|
-
* @hidden
|
|
92
|
-
* @param handler
|
|
93
|
-
* @deprecated Use `durableEventHandler` instead
|
|
94
|
-
* @returns The handler function, unmodified.
|
|
95
|
-
*/
|
|
96
|
-
export function createDurable(handler: DurableHandler): DurableHandler & {config?: undefined}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Durables creation function that can be called with or without a config object.
|
|
100
|
-
* @alpha Durables are an experimental feature and may change in the future.
|
|
101
|
-
* @hidden
|
|
102
|
-
* @param config
|
|
103
|
-
* @param handler
|
|
104
|
-
* @deprecated Use `durableEventHandler` instead
|
|
105
|
-
* @returns The handler function, unmodified and the provided config object.
|
|
106
|
-
*/
|
|
107
|
-
export function createDurable<TConfig extends {name: string; durableTimeout?: number}>(
|
|
108
|
-
config: TConfig,
|
|
109
|
-
handler: DurableHandler,
|
|
110
|
-
): DurableHandler & {config: TConfig}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Durables creation function that can be called with or without a config object.
|
|
114
|
-
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
115
|
-
* @hidden
|
|
116
|
-
* @public
|
|
117
|
-
* @param configOrHandler
|
|
118
|
-
* @param maybeHandler
|
|
119
|
-
* @deprecated Use `durableEventHandler` instead
|
|
120
|
-
* @returns The handler function, unmodified and the config object if provided
|
|
121
|
-
*/
|
|
122
|
-
export function createDurable<TConfig extends {name: string; durableTimeout?: number}>(
|
|
123
|
-
configOrHandler: TConfig | DurableHandler,
|
|
124
|
-
maybeHandler?: DurableHandler,
|
|
125
|
-
): DurableHandler {
|
|
126
|
-
const {config, handler, hadConfigArg} = resolveFuncArgs(configOrHandler, maybeHandler)
|
|
127
|
-
const errors = [
|
|
128
|
-
...(hadConfigArg ? validateConfig(config) : []),
|
|
129
|
-
...(typeof handler !== 'function' ? ['`handler` must be a function'] : []),
|
|
130
|
-
]
|
|
131
|
-
|
|
132
|
-
if (errors.length > 0) {
|
|
133
|
-
throw new TypeError(errors.join(', '))
|
|
134
|
-
}
|
|
135
|
-
// Separate config from the handler during build
|
|
136
|
-
return Object.assign(handler as DurableHandler, {config})
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Durables creation function that can be called with or without a config object.
|
|
141
|
-
* @alpha Durables are an experimental feature and may change in the future.
|
|
142
|
-
* @hidden
|
|
92
|
+
* @beta Durables are an experimental feature and may change in the future.
|
|
143
93
|
* @param handler
|
|
144
94
|
* @returns The handler function, unmodified.
|
|
145
95
|
*/
|
|
@@ -147,8 +97,7 @@ export function durableEventHandler(handler: DurableHandler): DurableHandler & {
|
|
|
147
97
|
|
|
148
98
|
/**
|
|
149
99
|
* Durables creation function that can be called with or without a config object.
|
|
150
|
-
* @
|
|
151
|
-
* @hidden
|
|
100
|
+
* @beta Durables are an experimental feature and may change in the future.
|
|
152
101
|
* @param config
|
|
153
102
|
* @param handler
|
|
154
103
|
* @returns The handler function, unmodified and the provided config object.
|
|
@@ -160,8 +109,7 @@ export function durableEventHandler<TConfig extends {name: string; durableTimeou
|
|
|
160
109
|
|
|
161
110
|
/**
|
|
162
111
|
* Durables creation function that can be called with or without a config object.
|
|
163
|
-
* @
|
|
164
|
-
* @hidden
|
|
112
|
+
* @beta Durable functions are an experimental feature and may change in the future.
|
|
165
113
|
* @public
|
|
166
114
|
* @param configOrHandler
|
|
167
115
|
* @param maybeHandler
|
|
@@ -185,8 +133,7 @@ export function durableEventHandler<TConfig extends {name: string; durableTimeou
|
|
|
185
133
|
}
|
|
186
134
|
|
|
187
135
|
/**
|
|
188
|
-
* @
|
|
189
|
-
* @hidden
|
|
136
|
+
* @beta Durable functions are an experimental feature and may change in the future.
|
|
190
137
|
* @public
|
|
191
138
|
*/
|
|
192
|
-
export const durable = {durableEventHandler
|
|
139
|
+
export const durable = {durableEventHandler}
|
package/src/invoke.ts
CHANGED
|
@@ -11,6 +11,13 @@ const PARTITION_KEY = 'arc-app-res'
|
|
|
11
11
|
/** @internal */
|
|
12
12
|
export const MAX_RECURSION_COUNT = 16
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
* Function types that can be invoked asynchronously.
|
|
17
|
+
* TODO: RUN-1849 a durable or queue function with a Content Lake `event` should not be invokeable
|
|
18
|
+
*/
|
|
19
|
+
const ASYNC_INVOKEABLE_TYPES = ['sanity.function.pubsub', 'sanity.function.queue', 'sanity.function.durable']
|
|
20
|
+
|
|
14
21
|
const INVOKE_DOCS_HINT = 'See docs for more information: https://www.sanity.io/docs/functions/function-to-function-invocation#ddd7727d1418'
|
|
15
22
|
|
|
16
23
|
/**
|
|
@@ -112,8 +119,9 @@ export function buildLineageToken(name: string, lineage: string | undefined) {
|
|
|
112
119
|
* Invokes another Sanity Function.
|
|
113
120
|
*
|
|
114
121
|
* By default the invocation is async: the payload is handed off to the function's
|
|
115
|
-
* event source
|
|
116
|
-
* wait for its
|
|
122
|
+
* event source (or, for durable functions, a new durable execution is started) and
|
|
123
|
+
* nothing is returned. Pass `{sync: true}` to invoke the function and wait for its
|
|
124
|
+
* return value; only pubsub functions support this.
|
|
117
125
|
*
|
|
118
126
|
* @param name - Name of the function to invoke
|
|
119
127
|
* @param payload - The `{event, context}` envelope to deliver
|
|
@@ -137,16 +145,17 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
137
145
|
|
|
138
146
|
// Make sure we can invoke this function type
|
|
139
147
|
const contextResource = payload?.context?.resources?.(name)
|
|
140
|
-
|
|
148
|
+
if (contextResource?.type === undefined) {
|
|
149
|
+
throw new Error(`Cannot find or determine function ${name} type!`)
|
|
150
|
+
}
|
|
141
151
|
if (sync === true) {
|
|
142
|
-
if (
|
|
143
|
-
throw new Error(`Function ${name} cannot be invoked synchronously.
|
|
152
|
+
if (contextResource.type !== 'sanity.function.pubsub') {
|
|
153
|
+
throw new Error(`Function ${name} of type ${contextResource.type} cannot be invoked synchronously. ${INVOKE_DOCS_HINT}`)
|
|
144
154
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
throw new Error(`No invokeable resource for function: ${name}`)
|
|
155
|
+
} else if (!ASYNC_INVOKEABLE_TYPES.includes(contextResource.type)) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`Function ${name} of type ${contextResource.type} cannot be invoked asynchronously; only the following function types can be invoked asynchronously: ${ASYNC_INVOKEABLE_TYPES.join(', ')}`,
|
|
158
|
+
)
|
|
150
159
|
}
|
|
151
160
|
|
|
152
161
|
// Local invoke path for Sanity CLI
|
|
@@ -182,12 +191,12 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
182
191
|
}
|
|
183
192
|
|
|
184
193
|
// Async invocation by type
|
|
185
|
-
if (resource.topic &&
|
|
194
|
+
if (resource.topic && contextResource.type === 'sanity.function.pubsub') {
|
|
186
195
|
await aws.SNS.Publish({
|
|
187
196
|
TopicArn: resource.topic.physicalResourceId,
|
|
188
197
|
Message: stringPayload,
|
|
189
198
|
})
|
|
190
|
-
} else if (resource.queue &&
|
|
199
|
+
} else if (resource.queue && contextResource.type === 'sanity.function.queue') {
|
|
191
200
|
const queueUrl = resource.queue.physicalResourceId
|
|
192
201
|
const message: {MessageBody: string; MessageGroupId?: string; QueueUrl: string} = {
|
|
193
202
|
MessageBody: stringPayload,
|
|
@@ -196,6 +205,12 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
196
205
|
// A FIFO queue rejects a message without a `MessageGroupId`
|
|
197
206
|
if (queueUrl.endsWith('.fifo')) message.MessageGroupId = name
|
|
198
207
|
await aws.SQS.SendMessage(message)
|
|
208
|
+
} else if (resource.function && contextResource.type === 'sanity.function.durable') {
|
|
209
|
+
await aws.Lambda.Invoke({
|
|
210
|
+
FunctionName: `${resource.function.physicalResourceId}:live`,
|
|
211
|
+
Payload: outgoingPayload,
|
|
212
|
+
InvocationType: 'Event',
|
|
213
|
+
})
|
|
199
214
|
} else {
|
|
200
215
|
throw new Error(`No invokeable resource for function: ${name}`)
|
|
201
216
|
}
|