@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.
@@ -4,15 +4,13 @@ import type {BlueprintResource} from './resources.js'
4
4
 
5
5
  /**
6
6
  * Durable alias of FunctionsContext
7
- * @alpha Using durables is considered experimental and may change in the future.
8
- * @hidden
7
+ * @beta Using durables is considered experimental and may change in the future.
9
8
  */
10
9
  export type DurableContext = FunctionContext
11
10
 
12
11
  /**
13
12
  * Logger interface for durables.
14
- * @alpha Using durables is considered experimental and may change in the future.
15
- * @hidden
13
+ * @beta Using durables is considered experimental and may change in the future.
16
14
  */
17
15
  export interface DurableLogger {
18
16
  debug(...params: unknown[]): void
@@ -23,27 +21,27 @@ export interface DurableLogger {
23
21
  }
24
22
 
25
23
  /**
26
- * @alpha Using durables is considered experimental and may change in the future.
27
- * @hidden
24
+ * @beta Using durables is considered experimental and may change in the future.
28
25
  */
29
26
  export interface DurableStepCallbackContext {
30
27
  logger: DurableLogger
31
28
  }
32
29
 
33
30
  /**
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
31
+ * @beta Using durables is considered experimental and may change in the future.
37
32
  */
38
33
  export interface DurableStepContext extends DurableStepCallbackContext {
39
34
  step: DurableOperations
40
35
  }
36
+ /**
37
+ * Context parameter passed to a branch method of a `step.fanout` execution.
38
+ * @beta Using durables is considered experimental and may change in the future.
39
+ */
41
40
  export interface DurableFanoutBranchContext extends DurableStepContext {}
42
41
 
43
42
  /**
44
43
  * Context parameter passed to the top-level `durableEventHandler` method.
45
- * @alpha Using durables is considered experimental and may change in the future.
46
- * @hidden
44
+ * @beta Using durables is considered experimental and may change in the future.
47
45
  */
48
46
  export interface DurableTopLevelContext extends DurableStepContext {
49
47
  context: DurableContext
@@ -52,18 +50,15 @@ export interface DurableTopLevelContext extends DurableStepContext {
52
50
 
53
51
  /**
54
52
  * Context parameter passed to the `waitForCallback` submitter method.
55
- * @alpha Using durables is considered experimental and may change in the future.
56
- * @hidden
53
+ * @beta Using durables is considered experimental and may change in the future.
57
54
  */
58
55
  export interface DurableCallbackSubmitterContext extends DurableStepCallbackContext {
59
56
  callbackId: string
60
57
  }
61
58
 
62
59
  /**
63
- * Arguments passed to callbacks that execute a durable attempt.
64
- * step.run & step.waitForCondition
65
- * @alpha Using durables is considered experimental and may change in the future.
66
- * @hidden
60
+ * Arguments passed to `step.run` & `step.waitForCondition` callbacks that execute a durable attempt.
61
+ * @beta Using durables is considered experimental and may change in the future.
67
62
  */
68
63
  export interface DurableStepAttemptContext extends DurableStepCallbackContext {
69
64
  /** Current attempt number, starting at 1. */
@@ -71,10 +66,9 @@ export interface DurableStepAttemptContext extends DurableStepCallbackContext {
71
66
  }
72
67
 
73
68
  /**
74
- * The signature of the duration object passed to the `wait` method of a durable function.
69
+ * 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.
75
70
  * It can be specified in days, hours, minutes, or seconds.
76
- * @alpha Using durables is considered experimental and may change in the future.
77
- * @hidden
71
+ * @beta Using durables is considered experimental and may change in the future.
78
72
  */
79
73
  export type DurableDuration =
80
74
  | {days: number; hours?: number; minutes?: number; seconds?: number}
@@ -83,8 +77,7 @@ export type DurableDuration =
83
77
  | {seconds: number}
84
78
 
85
79
  /**
86
- * @alpha: Using durables is considered experimental and may change in the future.
87
- * @hidden
80
+ * @beta: Using durables is considered experimental and may change in the future.
88
81
  */
89
82
  export interface DurableRetryStrategy {
90
83
  maxAttempts?: number
@@ -93,44 +86,45 @@ export interface DurableRetryStrategy {
93
86
  }
94
87
 
95
88
  /**
96
- * @alpha Using durables is considered experimental and may change in the future.
97
- * @hidden
89
+ * @beta Using durables is considered experimental and may change in the future.
98
90
  */
99
91
  export type DurableWaitForConditionDecision = {shouldRetry: true; delay: DurableDuration} | {shouldRetry: false}
100
92
 
101
93
  /**
102
- * @alpha Using durables is considered experimental and may change in the future.
103
- * @hidden
94
+ * @beta Using durables is considered experimental and may change in the future.
104
95
  */
105
96
  export type DurableStepHandler<TArgs extends unknown[], TReturn> = (...args: TArgs) => TReturn
106
97
 
107
98
  /**
108
- * @alpha Using durables is considered experimental and may change in the future.
109
- * @hidden
99
+ * @beta Using durables is considered experimental and may change in the future.
110
100
  */
111
101
  export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: DurableStepAttemptContext], T | Promise<T>>
112
102
 
113
103
  /**
114
- * @alpha Using durables is considered experimental and may change in the future.
115
- * @hidden
104
+ * @beta Using durables is considered experimental and may change in the future.
116
105
  */
117
106
  export type DurableStepFanoutHandler<T = unknown> = DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>
118
107
 
119
108
  /**
120
- * @alpha Using durables is considered experimental and may change in the future.
121
- * @hidden
109
+ * @beta Using durables is considered experimental and may change in the future.
110
+ */
111
+ export type DurableStepFanoutNamedHandler<T = unknown> = {
112
+ func: DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>
113
+ name?: string
114
+ }
115
+
116
+ /**
117
+ * @beta Using durables is considered experimental and may change in the future.
122
118
  */
123
119
  export type DurableStepWaitForCallbackSubmitter = DurableStepHandler<[context: DurableCallbackSubmitterContext], Promise<void>>
124
120
 
125
121
  /**
126
- * @alpha Using durables is considered experimental and may change in the future.
127
- * @hidden
122
+ * @beta Using durables is considered experimental and may change in the future.
128
123
  */
129
124
  export type DurableWaitForConditionPoller<T = unknown> = DurableStepHandler<[state: T, context: DurableStepAttemptContext], Promise<T>>
130
125
 
131
126
  /**
132
- * @alpha Using durables is considered experimental and may change in the future.
133
- * @hidden
127
+ * @beta Using durables is considered experimental and may change in the future.
134
128
  */
135
129
  export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<
136
130
  [state: T, context: Pick<DurableStepAttemptContext, 'attempt'>],
@@ -138,29 +132,29 @@ export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<
138
132
  >
139
133
 
140
134
  /**
141
- * @alpha Using durables is considered experimental and may change in the future.
142
- * @hidden
135
+ * @beta Using durables is considered experimental and may change in the future.
143
136
  */
144
137
  export type BatchItemSucceededStatus = 'SUCCEEDED'
138
+
145
139
  /**
146
- * @alpha Using durables is considered experimental and may change in the future.
140
+ * @beta Using durables is considered experimental and may change in the future.
147
141
  * @hidden
148
142
  */
149
143
  export type BatchItemFailedStatus = 'FAILED'
144
+
150
145
  /**
151
- * @alpha Using durables is considered experimental and may change in the future.
146
+ * @beta Using durables is considered experimental and may change in the future.
152
147
  * @hidden
153
148
  */
154
149
  export type BatchItemStartedStatus = 'STARTED'
150
+
155
151
  /**
156
- * @alpha Using durables is considered experimental and may change in the future.
157
- * @hidden
152
+ * @beta Using durables is considered experimental and may change in the future.
158
153
  */
159
154
  export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus | BatchItemStartedStatus
155
+
160
156
  /**
161
- * Reason why a batch operation (map, parallel, or concurrent execution)
162
- * completed.
163
- *
157
+ * Reason why a `step.fanout` operation completed.
164
158
  * - `ALL_COMPLETED`: every item finished.
165
159
  * - `MIN_SUCCESSFUL_REACHED`: CompletionConfig.minSuccessful was reached.
166
160
  * - `FAILURE_TOLERANCE_EXCEEDED`: a failure threshold was exceeded.
@@ -170,9 +164,7 @@ export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus |
170
164
  * - `CUSTOM_COMPLETION_FAILED`: a custom CompletionConfig.shouldComplete
171
165
  * predicate signalled completion with a `FAILED` outcome before all items
172
166
  * finished.
173
- * @alpha Using durables is considered experimental and may change in the future.
174
- * @hidden
175
- * @remarks copied from AWS Durable Execution SDK
167
+ * @beta Using durables is considered experimental and may change in the future.
176
168
  */
177
169
  export type CompletionReason =
178
170
  | 'ALL_COMPLETED'
@@ -180,11 +172,10 @@ export type CompletionReason =
180
172
  | 'FAILURE_TOLERANCE_EXCEEDED'
181
173
  | 'CUSTOM_COMPLETION_SUCCEEDED'
182
174
  | 'CUSTOM_COMPLETION_FAILED'
175
+
183
176
  /**
184
177
  * 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
178
+ * @beta Using durables is considered experimental and may change in the future.
188
179
  */
189
180
  export interface BatchItem<TResult> {
190
181
  /** The result value if the item succeeded */
@@ -196,10 +187,9 @@ export interface BatchItem<TResult> {
196
187
  /** Status of the item execution */
197
188
  status: BatchItemStatus
198
189
  }
190
+
199
191
  /**
200
- * @alpha Using durables is considered experimental and may change in the future.
201
- * @hidden
202
- * @remarks copied from AWS Durable Execution SDK
192
+ * @beta Using durables is considered experimental and may change in the future.
203
193
  */
204
194
  export interface BatchResult<TResult> {
205
195
  /**
@@ -253,55 +243,58 @@ export interface BatchResult<TResult> {
253
243
  }
254
244
 
255
245
  /**
256
- * The interface defining the operations available to a durable function.
257
- * These operations are steps that delegate to other functions, and wait for external callbacks.
258
- * @alpha Using durables is considered experimental and may change in the future.
259
- * @hidden
246
+ * The interface defining the durable operations available to a durable function.
247
+ * @beta Using durables is considered experimental and may change in the future.
260
248
  */
261
249
  export type DurableOperations = {
262
250
  /**
263
- * Runs a named step. Its result is recorded, so if the workflow re-runs the step
251
+ * Runs a named step. Its result is recorded, so if the durable re-runs the step
264
252
  * is not executed again. Failures are retried on their own.
265
- * similar to ctx.step()
266
- * @remarks cannot be nested inside another step
267
253
  * @example
268
254
  * ```ts
269
- * step.run({
255
+ * const hi = await step.run({
270
256
  * name: 'run-step',
271
257
  * handler: async ({logger}) => {
272
258
  * logger.log('Running step')
259
+ * return 'hi'
273
260
  * }})
274
261
  * ```
275
262
  *
276
263
  * @example
277
264
  * ```ts
278
265
  * // With a retry config
279
- * step.run({
266
+ * const hello = await step.run({
280
267
  * name: 'run-with-retry'
281
- * handler: () => { // handler code }
268
+ * handler: () => {
269
+ * if (Math.random() > 0.5) throw new Error('random error!')
270
+ * return 'hello'
271
+ * }
282
272
  * retry: { maxAttempts: 2, delay: { seconds: 5 } }
283
273
  * })
284
274
  * ```
285
275
  * @param name - Step name
286
276
  * @param handler - function being executed
287
277
  * @param retry - retry configuration
288
- * @returns The value returned by the executed function
278
+ * @returns The value returned by the provided handler
289
279
  */
290
280
  run<T>({name, handler, retry}: {name?: string; handler: DurableStepRunHandler<T>; retry?: DurableRetryStrategy}): Promise<T>
291
281
 
292
282
  /**
293
- * Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
283
+ * 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.
294
284
  * @remarks By using the generic, you can set a single, homogeneous return type for all branches.
295
285
  * @example
296
286
  * ```ts
297
- * step.fanout<string>({
287
+ * const results: BatchResult<string> = await step.fanout<string>({
298
288
  * name: 'parallel-runs',
299
289
  * branches: [async ({step, logger}) => {
300
290
  * logger.log('Running first function in parallel')
301
291
  * return 'first'
302
- * }, async ({step, logger}) => {
303
- * logger.log('Running second function in parallel')
304
- * return 'second'
292
+ * }, {
293
+ * func: async ({step, logger}) => {
294
+ * logger.log('Running second function in parallel')
295
+ * return 'second'
296
+ * },
297
+ * name: 'second-branch',
305
298
  * }],
306
299
  * })
307
300
  * ```
@@ -309,21 +302,27 @@ export type DurableOperations = {
309
302
  * @param branches - functions being executed in parallel
310
303
  * @returns A batch result of values returned or errors thrown by all branches.
311
304
  */
312
- fanout<T>({name, branches}: {name?: string; branches: DurableStepFanoutHandler<T>[]}): Promise<BatchResult<T>>
305
+ fanout<T>({
306
+ name,
307
+ branches,
308
+ }: {
309
+ name?: string
310
+ branches: (DurableStepFanoutHandler<T> | DurableStepFanoutNamedHandler<T>)[]
311
+ }): Promise<BatchResult<T>>
313
312
  /**
314
313
  * Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
315
314
  * @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
315
  * Use the explicit type parameter overloads above for strict homogeneous type checking.
317
316
  * @example
318
317
  * ```ts
319
- * step.fanout<string>({
318
+ * const results: BatchResult<string | number> = await step.fanout({
320
319
  * name: 'parallel-runs',
321
320
  * branches: [async ({step, logger}) => {
322
321
  * logger.log('Running first function in parallel')
323
322
  * return 'first'
324
323
  * }, async ({step, logger}) => {
325
324
  * logger.log('Running second function in parallel')
326
- * return 'second'
325
+ * return 2
327
326
  * }],
328
327
  * })
329
328
  * ```
@@ -337,30 +336,36 @@ export type DurableOperations = {
337
336
  }: {
338
337
  name?: string
339
338
  branches: Branches
340
- }): Promise<BatchResult<Branches[number] extends DurableStepFanoutHandler<infer ReturnType> ? ReturnType : never>>
339
+ }): Promise<
340
+ BatchResult<
341
+ Branches[number] extends DurableStepFanoutHandler<infer ReturnType>
342
+ ? ReturnType
343
+ : Branches[number] extends DurableStepFanoutNamedHandler<infer ReturnType>
344
+ ? ReturnType
345
+ : never
346
+ >
347
+ >
341
348
 
342
349
  /**
343
- * Calls another function and awaits its result.
344
- * similar to ctx.invoke()
345
- * @remarks cannot be nested inside another step
350
+ * 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.
346
351
  * @example
347
352
  * ```ts
348
353
  * // my-delegated-function/index.ts
349
- * export const handler = pubSubEventHandler({event}) => {
350
- * ...
351
- * }
354
+ * export const handler = pubSubEventHandler(({event}) => {
355
+ * return 'yo'
356
+ * })
352
357
  *
353
358
  * // my-resource/index.ts
354
- * export const handler = pubSubEventHandler({event}) => {
355
- * ...
356
- * }
359
+ * export const handler = pubSubEventHandler(({event}) => {
360
+ * return 'hey'
361
+ * })
357
362
  *
358
363
  * // my-durable-function/index.ts
359
- * durableEventHandler(({step, context}) => {
364
+ * export const handler = durableEventHandler(({step, context}) => {
360
365
  * // With a function name
361
- * step.delegate({name: 'my-function', handler: 'my-delegated-function', event: {data: {hello: 'world'}}})
366
+ * const firstResult = await step.delegate({name: 'first-delegation', handler: 'my-delegated-function', event: {data: {hello: 'world'}}})
362
367
  * // With a resource object
363
- * step.delegate({name: 'another-function', handler: context.resource.function('my-resource'), event: {data: {hello: 'world'}}})
368
+ * const secondResult = await step.delegate({name: 'second-delegation', handler: context.resource.function('my-resource'), event: {data: {hello: 'world'}}})
364
369
  * })
365
370
  * ```
366
371
  * @param name - Step name
@@ -380,13 +385,16 @@ export type DurableOperations = {
380
385
  }): Promise<T>
381
386
 
382
387
  /**
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.
388
+ * 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.
384
389
  * @example
385
- * Wait for 30 seconds
386
390
  * ```ts
387
- * await step.waitForCallback({name: 'ask-for-permission', submitter: async ({callbackId}) => {
388
- * await sendForApproval(callbackId)
389
- * }});
391
+ * try {
392
+ * const successfulResult = await step.waitForCallback({name: 'ask-for-permission', submitter: async ({callbackId}) => {
393
+ * await sendForApproval(callbackId)
394
+ * }})
395
+ * } catch (e) {
396
+ * logger.log('oh no we received a callback failure', e)
397
+ * }
390
398
  * ```
391
399
  * ```
392
400
  * @param name - Step name
@@ -409,8 +417,6 @@ export type DurableOperations = {
409
417
 
410
418
  /**
411
419
  * Waits for a specified duration
412
- * similar to ctx.wait()
413
- * @remarks cannot be nested inside another step
414
420
  * @example
415
421
  * Wait for 30 seconds
416
422
  * ```ts
@@ -422,35 +428,31 @@ export type DurableOperations = {
422
428
  * ```
423
429
  * @param name - Step name
424
430
  * @param duration - Amount of time the function will wait before continuing.
425
- * Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
426
431
  * @returns Resolves after the duration
427
432
  */
428
433
  wait({name, duration}: {name?: string; duration: DurableDuration}): Promise<void>
429
434
 
430
435
  /**
431
- * Waits for a specified condition to be met
432
- * similar to ctx.waitForCondition()
433
- * @remarks cannot be nested inside another step
436
+ * 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.
434
437
  * @example
435
438
  * ```ts
436
439
  * await step.waitForCondition({{
437
- * name: 'waitForCondition',
438
- * initial: {
439
- * article: null,
440
- * },
441
- * poller: async (state, {attempt}) => {
442
- * const client = createClient({
443
- * apiVersion: '2026-08-05',
444
- * ...context.clientOptions,
445
- * })
446
- * const article = await client.fetch<Article | null>(
447
- * '*[_type == "article"][0]',
448
- * )
449
- * return {...state, article}
450
- * }
451
- * next: (state, {attempt}) => {
452
- * if (state.article) return {shouldRetry: false}
453
- *
440
+ * name: 'waitForCondition',
441
+ * initial: {
442
+ * article: null as Article | null,
443
+ * },
444
+ * poller: async (state, {attempt}) => {
445
+ * const client = createClient({
446
+ * apiVersion: '2026-08-05',
447
+ * ...context.clientOptions,
448
+ * })
449
+ * const article = await client.fetch<Article | null>(
450
+ * '*[_type == "article"][0]',
451
+ * )
452
+ * return {...state, article}
453
+ * },
454
+ * next: (state, {attempt}) => {
455
+ * if (state.article) return {shouldRetry: false}
454
456
  * return {
455
457
  * shouldRetry: true,
456
458
  * delay: {
@@ -483,7 +485,6 @@ export type DurableOperations = {
483
485
  }
484
486
 
485
487
  /**
486
- * @alpha Using durables is considered experimental and may change in the future.
487
- * @hidden
488
+ * @beta Using durables is considered experimental and may change in the future.
488
489
  */
489
490
  export type DurableHandler = (envelope: DurableTopLevelContext) => unknown | Promise<unknown>