@sanity/functions 1.6.5 → 1.6.6
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.d.ts +101 -116
- package/package.json +1 -1
- package/src/types.ts +97 -119
package/dist/types.d.ts
CHANGED
|
@@ -234,42 +234,41 @@ export type FunctionResourceEnvelope = {
|
|
|
234
234
|
[P in Exclude<FunctionResourceKey, K>]?: FunctionResource;
|
|
235
235
|
};
|
|
236
236
|
}[FunctionResourceKey];
|
|
237
|
+
/**
|
|
238
|
+
* Durable alias of FunctionsContext
|
|
239
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
240
|
+
* @hidden
|
|
241
|
+
*/
|
|
242
|
+
export type DurableContext = FunctionContext;
|
|
237
243
|
/**
|
|
238
244
|
* Logger interface for durables.
|
|
239
245
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
240
246
|
* @hidden
|
|
241
247
|
*/
|
|
242
248
|
export interface DurableLogger {
|
|
243
|
-
debug(
|
|
244
|
-
info(
|
|
245
|
-
warn(
|
|
246
|
-
error(
|
|
247
|
-
log(
|
|
249
|
+
debug(...params: unknown[]): void;
|
|
250
|
+
info(...params: unknown[]): void;
|
|
251
|
+
warn(...params: unknown[]): void;
|
|
252
|
+
error(...params: unknown[]): void;
|
|
253
|
+
log(...params: unknown[]): void;
|
|
248
254
|
}
|
|
249
255
|
/**
|
|
250
|
-
* // @todo: follow-up to better define separated context
|
|
251
256
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
252
257
|
* @hidden
|
|
253
258
|
*/
|
|
254
|
-
export interface
|
|
259
|
+
export interface DurableStepCallbackContext {
|
|
255
260
|
logger: DurableLogger;
|
|
256
261
|
}
|
|
257
262
|
/**
|
|
258
|
-
*
|
|
263
|
+
* Arguments passed to callbacks that execute a durable attempt.
|
|
264
|
+
* step.run & step.waitForCondition
|
|
259
265
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
260
266
|
* @hidden
|
|
261
267
|
*/
|
|
262
|
-
export
|
|
263
|
-
/**
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
*/
|
|
267
|
-
attempt?: number;
|
|
268
|
-
/**
|
|
269
|
-
* Unique ID for a durable execution
|
|
270
|
-
*/
|
|
271
|
-
runId?: string;
|
|
272
|
-
};
|
|
268
|
+
export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
269
|
+
/** Current attempt number, starting at 1. */
|
|
270
|
+
attempt: number;
|
|
271
|
+
}
|
|
273
272
|
/**
|
|
274
273
|
* The signature of the duration object passed to the `wait` method of a durable function.
|
|
275
274
|
* It can be specified in days, hours, minutes, or seconds.
|
|
@@ -296,36 +295,67 @@ export type DurableDuration = {
|
|
|
296
295
|
* @hidden
|
|
297
296
|
*/
|
|
298
297
|
export type DurableWaitForConditionDecision = {
|
|
299
|
-
|
|
298
|
+
shouldRetry: true;
|
|
300
299
|
delay: DurableDuration;
|
|
301
300
|
} | {
|
|
302
|
-
|
|
301
|
+
shouldRetry: false;
|
|
303
302
|
};
|
|
304
303
|
/**
|
|
305
304
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
306
305
|
* @hidden
|
|
307
306
|
*/
|
|
308
|
-
export
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
307
|
+
export type DurableStepHandler<TArgs extends unknown[], TReturn> = (...args: TArgs) => TReturn;
|
|
308
|
+
/**
|
|
309
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
310
|
+
* @hidden
|
|
311
|
+
*/
|
|
312
|
+
export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: DurableStepAttemptContext], T | Promise<T>>;
|
|
313
|
+
/**
|
|
314
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
315
|
+
* @hidden
|
|
316
|
+
*/
|
|
317
|
+
export type DurableStepWaitForCallbackHandler = DurableStepHandler<[callbackId: string, context: DurableStepCallbackContext], Promise<void>>;
|
|
318
|
+
/**
|
|
319
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
320
|
+
* @hidden
|
|
321
|
+
*/
|
|
322
|
+
export type DurableWaitForConditionPoller<T = unknown> = DurableStepHandler<[state: T, context: DurableStepAttemptContext], Promise<T>>;
|
|
323
|
+
/**
|
|
324
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
325
|
+
* @hidden
|
|
326
|
+
*/
|
|
327
|
+
export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<[
|
|
328
|
+
state: T,
|
|
329
|
+
context: Pick<DurableStepAttemptContext, 'attempt'>
|
|
330
|
+
], DurableWaitForConditionDecision>;
|
|
312
331
|
/**
|
|
313
332
|
* The interface defining the operations available to a durable function.
|
|
314
333
|
* These operations are steps that delegate to other functions, and wait for external callbacks.
|
|
315
334
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
316
335
|
* @hidden
|
|
317
336
|
*/
|
|
318
|
-
export
|
|
337
|
+
export type DurableOperations = {
|
|
319
338
|
/**
|
|
320
339
|
* Runs a named step. Its result is recorded, so if the workflow re-runs the step
|
|
321
340
|
* is not executed again. Failures are retried on their own.
|
|
322
341
|
* similar to ctx.step()
|
|
323
342
|
* @remarks cannot be nested inside another step
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* step.run({
|
|
346
|
+
* name: 'run-step',
|
|
347
|
+
* handler: async ({logger}) => {
|
|
348
|
+
* logger.log('Running step')
|
|
349
|
+
* }})
|
|
350
|
+
* ```
|
|
324
351
|
* @param name - Step name
|
|
325
|
-
* @param
|
|
352
|
+
* @param handler - function being executed
|
|
326
353
|
* @returns The value returned by the executed function
|
|
327
354
|
*/
|
|
328
|
-
run<T>(name
|
|
355
|
+
run<T>({ name, handler }: {
|
|
356
|
+
name?: string;
|
|
357
|
+
handler: DurableStepRunHandler<T>;
|
|
358
|
+
}): Promise<T>;
|
|
329
359
|
/**
|
|
330
360
|
* Calls another function and awaits its result.
|
|
331
361
|
* similar to ctx.invoke()
|
|
@@ -334,16 +364,22 @@ export interface DurableOperations {
|
|
|
334
364
|
* @param input - Data passed to the called function
|
|
335
365
|
* @returns The value returned by the called function
|
|
336
366
|
*/
|
|
337
|
-
delegate<T>(name
|
|
367
|
+
delegate<T>({ name, input }: {
|
|
368
|
+
name?: string;
|
|
369
|
+
input?: unknown;
|
|
370
|
+
}): Promise<T>;
|
|
338
371
|
/**
|
|
339
372
|
* Waits for an external callback
|
|
340
373
|
* similar to ctx.waitForCallback()
|
|
341
374
|
* @remarks cannot be nested inside another step
|
|
342
375
|
* @param name - Step name
|
|
343
|
-
* @param
|
|
376
|
+
* @param handler - Receives the generated callbackId and returns a delivered promise
|
|
344
377
|
* @returns The value returned by the executed function
|
|
345
378
|
*/
|
|
346
|
-
waitForCallback<T>(name
|
|
379
|
+
waitForCallback<T>({ name, handler }: {
|
|
380
|
+
name?: string;
|
|
381
|
+
handler: DurableStepWaitForCallbackHandler;
|
|
382
|
+
}): Promise<T>;
|
|
347
383
|
/**
|
|
348
384
|
* Waits for a specified duration
|
|
349
385
|
* similar to ctx.wait()
|
|
@@ -351,63 +387,49 @@ export interface DurableOperations {
|
|
|
351
387
|
* @example
|
|
352
388
|
* Wait for 30 seconds
|
|
353
389
|
* ```ts
|
|
354
|
-
* await step.wait('wait30Sec', { seconds: 30 });
|
|
390
|
+
* await step.wait({name: 'wait30Sec', duration: { seconds: 30 }});
|
|
355
391
|
* ```
|
|
356
392
|
* Wait for 2 days and 3 hours
|
|
357
393
|
* ```ts
|
|
358
|
-
* await step.wait('waitALongTime', { days: 2, hours: 3 });
|
|
394
|
+
* await step.wait({name: 'waitALongTime', duration: { days: 2, hours: 3 }});
|
|
359
395
|
* ```
|
|
360
396
|
* @param name - Step name
|
|
361
397
|
* @param duration - Amount of time the function will wait before continuing.
|
|
362
398
|
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
363
399
|
* @returns Resolves after the duration
|
|
364
400
|
*/
|
|
365
|
-
wait(name
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
* @remarks cannot be nested inside another step
|
|
370
|
-
* @example
|
|
371
|
-
* Wait for 30 seconds
|
|
372
|
-
* ```ts
|
|
373
|
-
* await step.wait({ seconds: 30 });
|
|
374
|
-
* ```
|
|
375
|
-
* @param duration - Amount of time the function will wait before continuing.
|
|
376
|
-
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
377
|
-
* @returns Resolves after the duration
|
|
378
|
-
*/
|
|
379
|
-
wait(duration: DurableDuration): Promise<void>;
|
|
401
|
+
wait({ name, duration }: {
|
|
402
|
+
name?: string;
|
|
403
|
+
duration: DurableDuration;
|
|
404
|
+
}): Promise<void>;
|
|
380
405
|
/**
|
|
381
406
|
* Waits for a specified condition to be met
|
|
382
407
|
* similar to ctx.waitForCondition()
|
|
383
408
|
* @remarks cannot be nested inside another step
|
|
384
409
|
* @example
|
|
385
410
|
* ```ts
|
|
386
|
-
* await step.waitForCondition(
|
|
387
|
-
*
|
|
388
|
-
* async (state, {logger}) => {
|
|
389
|
-
* logger.log('Checking for article')
|
|
390
|
-
*
|
|
391
|
-
* const client = createClient({
|
|
392
|
-
* apiVersion: '2026-08-05',
|
|
393
|
-
* ...context.clientOptions,
|
|
394
|
-
* })
|
|
395
|
-
* const article = await client.fetch<Article | null>(
|
|
396
|
-
* '*[_type == "article"][0]',
|
|
397
|
-
* )
|
|
398
|
-
* return {...state, article}
|
|
399
|
-
* },
|
|
400
|
-
* {
|
|
411
|
+
* await step.waitForCondition({{
|
|
412
|
+
* name: 'waitForCondition',
|
|
401
413
|
* initial: {
|
|
402
|
-
* article: null
|
|
414
|
+
* article: null,
|
|
403
415
|
* },
|
|
404
|
-
*
|
|
405
|
-
*
|
|
416
|
+
* poller: async (state, {attempt}) => {
|
|
417
|
+
* const client = createClient({
|
|
418
|
+
* apiVersion: '2026-08-05',
|
|
419
|
+
* ...context.clientOptions,
|
|
420
|
+
* })
|
|
421
|
+
* const article = await client.fetch<Article | null>(
|
|
422
|
+
* '*[_type == "article"][0]',
|
|
423
|
+
* )
|
|
424
|
+
* return {...state, article}
|
|
425
|
+
* }
|
|
426
|
+
* next: (state, {attempt}) => {
|
|
427
|
+
* if (state.article) return {shouldResume: false}
|
|
406
428
|
*
|
|
407
429
|
* return {
|
|
408
|
-
*
|
|
430
|
+
* shouldResume: true,
|
|
409
431
|
* delay: {
|
|
410
|
-
* seconds: Math.min(
|
|
432
|
+
* seconds: Math.min(attempt * 2, 60),
|
|
411
433
|
* },
|
|
412
434
|
* }
|
|
413
435
|
* },
|
|
@@ -415,57 +437,20 @@ export interface DurableOperations {
|
|
|
415
437
|
* )
|
|
416
438
|
* ```
|
|
417
439
|
* @param name - Step name
|
|
418
|
-
* @param
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
* @param options - Configuration options for the condition
|
|
422
|
-
* @returns The value returned by the executed function
|
|
423
|
-
*/
|
|
424
|
-
waitForCondition<T>(name: string, fn: (state: T, args: BaseDurableOperationArgs) => Promise<T>, options: DurableWaitForConditionOptions<T>): Promise<T>;
|
|
425
|
-
/**
|
|
426
|
-
* Waits for a specified condition to be met
|
|
427
|
-
* similar to ctx.waitForCondition()
|
|
428
|
-
* @remarks cannot be nested inside another step
|
|
429
|
-
* @example
|
|
430
|
-
* ```ts
|
|
431
|
-
* await step.waitForCondition(
|
|
432
|
-
* async (state, {logger}) => {
|
|
433
|
-
* logger.log('Checking for article')
|
|
434
|
-
*
|
|
435
|
-
* const client = createClient({
|
|
436
|
-
* apiVersion: '2026-08-05',
|
|
437
|
-
* ...context.clientOptions,
|
|
438
|
-
* })
|
|
439
|
-
* const article = await client.fetch<Article | null>(
|
|
440
|
-
* '*[_type == "article"][0]',
|
|
441
|
-
* )
|
|
442
|
-
* return {...state, article}
|
|
443
|
-
* },
|
|
444
|
-
* {
|
|
445
|
-
* initial: {
|
|
446
|
-
* article: null as Article | null,
|
|
447
|
-
* },
|
|
448
|
-
* next: (state, context) => {
|
|
449
|
-
* if (state.article) return {shouldContinue: false}
|
|
450
|
-
*
|
|
451
|
-
* return {
|
|
452
|
-
* shouldContinue: true,
|
|
453
|
-
* delay: {
|
|
454
|
-
* seconds: Math.min((context.attempt ?? 1) * 2, 60),
|
|
455
|
-
* },
|
|
456
|
-
* }
|
|
457
|
-
* },
|
|
458
|
-
* },
|
|
459
|
-
* )
|
|
460
|
-
* ```
|
|
461
|
-
* @param fn - Called on each attempt with the current state and durable context.
|
|
440
|
+
* @param initial - Initial state passed to the first poller call.
|
|
441
|
+
* @param poller - Called on each attempt with the current state and condition context.
|
|
442
|
+
* @param next - A function that determines whether the condition has been met or another check should be run.
|
|
462
443
|
* Returns a promise resolving to the updated state,
|
|
463
|
-
* which is passed to next to determine whether the condition has been met or another check should be
|
|
464
|
-
* @param options - Configuration options for the condition
|
|
444
|
+
* which is passed to next to determine whether the condition has been met or another check should be run.
|
|
465
445
|
* @returns The value returned by the executed function
|
|
466
446
|
*/
|
|
467
|
-
waitForCondition<T>(
|
|
468
|
-
|
|
447
|
+
waitForCondition<T>({ name, initial, next, poller, }: {
|
|
448
|
+
name?: string;
|
|
449
|
+
initial: T;
|
|
450
|
+
poller: DurableWaitForConditionPoller<T>;
|
|
451
|
+
next: DurableWaitForConditionNext<T>;
|
|
452
|
+
}): Promise<T>;
|
|
453
|
+
};
|
|
469
454
|
/**
|
|
470
455
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
471
456
|
* @hidden
|
package/package.json
CHANGED
package/src/types.ts
CHANGED
|
@@ -245,43 +245,43 @@ export type FunctionResourceEnvelope = {
|
|
|
245
245
|
[K in FunctionResourceKey]-?: {[P in K]: FunctionResource} & {[P in Exclude<FunctionResourceKey, K>]?: FunctionResource}
|
|
246
246
|
}[FunctionResourceKey]
|
|
247
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Durable alias of FunctionsContext
|
|
250
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
251
|
+
* @hidden
|
|
252
|
+
*/
|
|
253
|
+
export type DurableContext = FunctionContext
|
|
254
|
+
|
|
248
255
|
/**
|
|
249
256
|
* Logger interface for durables.
|
|
250
257
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
251
258
|
* @hidden
|
|
252
259
|
*/
|
|
253
260
|
export interface DurableLogger {
|
|
254
|
-
debug(
|
|
255
|
-
info(
|
|
256
|
-
warn(
|
|
257
|
-
error(
|
|
258
|
-
log(
|
|
261
|
+
debug(...params: unknown[]): void
|
|
262
|
+
info(...params: unknown[]): void
|
|
263
|
+
warn(...params: unknown[]): void
|
|
264
|
+
error(...params: unknown[]): void
|
|
265
|
+
log(...params: unknown[]): void
|
|
259
266
|
}
|
|
260
267
|
|
|
261
268
|
/**
|
|
262
|
-
* // @todo: follow-up to better define separated context
|
|
263
269
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
264
270
|
* @hidden
|
|
265
271
|
*/
|
|
266
|
-
export interface
|
|
272
|
+
export interface DurableStepCallbackContext {
|
|
267
273
|
logger: DurableLogger
|
|
268
274
|
}
|
|
269
275
|
|
|
270
276
|
/**
|
|
271
|
-
*
|
|
277
|
+
* Arguments passed to callbacks that execute a durable attempt.
|
|
278
|
+
* step.run & step.waitForCondition
|
|
272
279
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
273
280
|
* @hidden
|
|
274
281
|
*/
|
|
275
|
-
export
|
|
276
|
-
/**
|
|
277
|
-
|
|
278
|
-
* Only available in methods that expose the attempt like `waitForCondition`
|
|
279
|
-
*/
|
|
280
|
-
attempt?: number
|
|
281
|
-
/**
|
|
282
|
-
* Unique ID for a durable execution
|
|
283
|
-
*/
|
|
284
|
-
runId?: string
|
|
282
|
+
export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
283
|
+
/** Current attempt number, starting at 1. */
|
|
284
|
+
attempt: number
|
|
285
285
|
}
|
|
286
286
|
|
|
287
287
|
/**
|
|
@@ -300,16 +300,40 @@ export type DurableDuration =
|
|
|
300
300
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
301
301
|
* @hidden
|
|
302
302
|
*/
|
|
303
|
-
export type DurableWaitForConditionDecision = {
|
|
303
|
+
export type DurableWaitForConditionDecision = {shouldRetry: true; delay: DurableDuration} | {shouldRetry: false}
|
|
304
304
|
|
|
305
305
|
/**
|
|
306
306
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
307
307
|
* @hidden
|
|
308
308
|
*/
|
|
309
|
-
export
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
309
|
+
export type DurableStepHandler<TArgs extends unknown[], TReturn> = (...args: TArgs) => TReturn
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
313
|
+
* @hidden
|
|
314
|
+
*/
|
|
315
|
+
export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: DurableStepAttemptContext], T | Promise<T>>
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
319
|
+
* @hidden
|
|
320
|
+
*/
|
|
321
|
+
export type DurableStepWaitForCallbackHandler = DurableStepHandler<[callbackId: string, context: DurableStepCallbackContext], Promise<void>>
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
325
|
+
* @hidden
|
|
326
|
+
*/
|
|
327
|
+
export type DurableWaitForConditionPoller<T = unknown> = DurableStepHandler<[state: T, context: DurableStepAttemptContext], Promise<T>>
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
331
|
+
* @hidden
|
|
332
|
+
*/
|
|
333
|
+
export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<
|
|
334
|
+
[state: T, context: Pick<DurableStepAttemptContext, 'attempt'>],
|
|
335
|
+
DurableWaitForConditionDecision
|
|
336
|
+
>
|
|
313
337
|
|
|
314
338
|
/**
|
|
315
339
|
* The interface defining the operations available to a durable function.
|
|
@@ -317,17 +341,26 @@ export interface DurableWaitForConditionOptions<T> {
|
|
|
317
341
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
318
342
|
* @hidden
|
|
319
343
|
*/
|
|
320
|
-
export
|
|
344
|
+
export type DurableOperations = {
|
|
321
345
|
/**
|
|
322
346
|
* Runs a named step. Its result is recorded, so if the workflow re-runs the step
|
|
323
347
|
* is not executed again. Failures are retried on their own.
|
|
324
348
|
* similar to ctx.step()
|
|
325
349
|
* @remarks cannot be nested inside another step
|
|
350
|
+
* @example
|
|
351
|
+
* ```ts
|
|
352
|
+
* step.run({
|
|
353
|
+
* name: 'run-step',
|
|
354
|
+
* handler: async ({logger}) => {
|
|
355
|
+
* logger.log('Running step')
|
|
356
|
+
* }})
|
|
357
|
+
* ```
|
|
326
358
|
* @param name - Step name
|
|
327
|
-
* @param
|
|
359
|
+
* @param handler - function being executed
|
|
328
360
|
* @returns The value returned by the executed function
|
|
329
361
|
*/
|
|
330
|
-
run<T>(name
|
|
362
|
+
run<T>({name, handler}: {name?: string; handler: DurableStepRunHandler<T>}): Promise<T>
|
|
363
|
+
|
|
331
364
|
/**
|
|
332
365
|
* Calls another function and awaits its result.
|
|
333
366
|
* similar to ctx.invoke()
|
|
@@ -336,17 +369,17 @@ export interface DurableOperations {
|
|
|
336
369
|
* @param input - Data passed to the called function
|
|
337
370
|
* @returns The value returned by the called function
|
|
338
371
|
*/
|
|
339
|
-
delegate<T>(name: string
|
|
372
|
+
delegate<T>({name, input}: {name?: string; input?: unknown}): Promise<T>
|
|
340
373
|
|
|
341
374
|
/**
|
|
342
375
|
* Waits for an external callback
|
|
343
376
|
* similar to ctx.waitForCallback()
|
|
344
377
|
* @remarks cannot be nested inside another step
|
|
345
378
|
* @param name - Step name
|
|
346
|
-
* @param
|
|
379
|
+
* @param handler - Receives the generated callbackId and returns a delivered promise
|
|
347
380
|
* @returns The value returned by the executed function
|
|
348
381
|
*/
|
|
349
|
-
waitForCallback<T>(name
|
|
382
|
+
waitForCallback<T>({name, handler}: {name?: string; handler: DurableStepWaitForCallbackHandler}): Promise<T>
|
|
350
383
|
|
|
351
384
|
/**
|
|
352
385
|
* Waits for a specified duration
|
|
@@ -355,33 +388,18 @@ export interface DurableOperations {
|
|
|
355
388
|
* @example
|
|
356
389
|
* Wait for 30 seconds
|
|
357
390
|
* ```ts
|
|
358
|
-
* await step.wait('wait30Sec', { seconds: 30 });
|
|
391
|
+
* await step.wait({name: 'wait30Sec', duration: { seconds: 30 }});
|
|
359
392
|
* ```
|
|
360
393
|
* Wait for 2 days and 3 hours
|
|
361
394
|
* ```ts
|
|
362
|
-
* await step.wait('waitALongTime', { days: 2, hours: 3 });
|
|
395
|
+
* await step.wait({name: 'waitALongTime', duration: { days: 2, hours: 3 }});
|
|
363
396
|
* ```
|
|
364
397
|
* @param name - Step name
|
|
365
398
|
* @param duration - Amount of time the function will wait before continuing.
|
|
366
399
|
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
367
400
|
* @returns Resolves after the duration
|
|
368
401
|
*/
|
|
369
|
-
wait(name: string
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Waits for a specified duration
|
|
373
|
-
* similar to ctx.wait()
|
|
374
|
-
* @remarks cannot be nested inside another step
|
|
375
|
-
* @example
|
|
376
|
-
* Wait for 30 seconds
|
|
377
|
-
* ```ts
|
|
378
|
-
* await step.wait({ seconds: 30 });
|
|
379
|
-
* ```
|
|
380
|
-
* @param duration - Amount of time the function will wait before continuing.
|
|
381
|
-
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
382
|
-
* @returns Resolves after the duration
|
|
383
|
-
*/
|
|
384
|
-
wait(duration: DurableDuration): Promise<void>
|
|
402
|
+
wait({name, duration}: {name?: string; duration: DurableDuration}): Promise<void>
|
|
385
403
|
|
|
386
404
|
/**
|
|
387
405
|
* Waits for a specified condition to be met
|
|
@@ -389,31 +407,28 @@ export interface DurableOperations {
|
|
|
389
407
|
* @remarks cannot be nested inside another step
|
|
390
408
|
* @example
|
|
391
409
|
* ```ts
|
|
392
|
-
* await step.waitForCondition(
|
|
393
|
-
*
|
|
394
|
-
* async (state, {logger}) => {
|
|
395
|
-
* logger.log('Checking for article')
|
|
396
|
-
*
|
|
397
|
-
* const client = createClient({
|
|
398
|
-
* apiVersion: '2026-08-05',
|
|
399
|
-
* ...context.clientOptions,
|
|
400
|
-
* })
|
|
401
|
-
* const article = await client.fetch<Article | null>(
|
|
402
|
-
* '*[_type == "article"][0]',
|
|
403
|
-
* )
|
|
404
|
-
* return {...state, article}
|
|
405
|
-
* },
|
|
406
|
-
* {
|
|
410
|
+
* await step.waitForCondition({{
|
|
411
|
+
* name: 'waitForCondition',
|
|
407
412
|
* initial: {
|
|
408
|
-
* article: null
|
|
413
|
+
* article: null,
|
|
409
414
|
* },
|
|
410
|
-
*
|
|
411
|
-
*
|
|
415
|
+
* poller: async (state, {attempt}) => {
|
|
416
|
+
* const client = createClient({
|
|
417
|
+
* apiVersion: '2026-08-05',
|
|
418
|
+
* ...context.clientOptions,
|
|
419
|
+
* })
|
|
420
|
+
* const article = await client.fetch<Article | null>(
|
|
421
|
+
* '*[_type == "article"][0]',
|
|
422
|
+
* )
|
|
423
|
+
* return {...state, article}
|
|
424
|
+
* }
|
|
425
|
+
* next: (state, {attempt}) => {
|
|
426
|
+
* if (state.article) return {shouldResume: false}
|
|
412
427
|
*
|
|
413
428
|
* return {
|
|
414
|
-
*
|
|
429
|
+
* shouldResume: true,
|
|
415
430
|
* delay: {
|
|
416
|
-
* seconds: Math.min(
|
|
431
|
+
* seconds: Math.min(attempt * 2, 60),
|
|
417
432
|
* },
|
|
418
433
|
* }
|
|
419
434
|
* },
|
|
@@ -421,61 +436,24 @@ export interface DurableOperations {
|
|
|
421
436
|
* )
|
|
422
437
|
* ```
|
|
423
438
|
* @param name - Step name
|
|
424
|
-
* @param
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @param options - Configuration options for the condition
|
|
428
|
-
* @returns The value returned by the executed function
|
|
429
|
-
*/
|
|
430
|
-
waitForCondition<T>(
|
|
431
|
-
name: string,
|
|
432
|
-
fn: (state: T, args: BaseDurableOperationArgs) => Promise<T>,
|
|
433
|
-
options: DurableWaitForConditionOptions<T>,
|
|
434
|
-
): Promise<T>
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Waits for a specified condition to be met
|
|
438
|
-
* similar to ctx.waitForCondition()
|
|
439
|
-
* @remarks cannot be nested inside another step
|
|
440
|
-
* @example
|
|
441
|
-
* ```ts
|
|
442
|
-
* await step.waitForCondition(
|
|
443
|
-
* async (state, {logger}) => {
|
|
444
|
-
* logger.log('Checking for article')
|
|
445
|
-
*
|
|
446
|
-
* const client = createClient({
|
|
447
|
-
* apiVersion: '2026-08-05',
|
|
448
|
-
* ...context.clientOptions,
|
|
449
|
-
* })
|
|
450
|
-
* const article = await client.fetch<Article | null>(
|
|
451
|
-
* '*[_type == "article"][0]',
|
|
452
|
-
* )
|
|
453
|
-
* return {...state, article}
|
|
454
|
-
* },
|
|
455
|
-
* {
|
|
456
|
-
* initial: {
|
|
457
|
-
* article: null as Article | null,
|
|
458
|
-
* },
|
|
459
|
-
* next: (state, context) => {
|
|
460
|
-
* if (state.article) return {shouldContinue: false}
|
|
461
|
-
*
|
|
462
|
-
* return {
|
|
463
|
-
* shouldContinue: true,
|
|
464
|
-
* delay: {
|
|
465
|
-
* seconds: Math.min((context.attempt ?? 1) * 2, 60),
|
|
466
|
-
* },
|
|
467
|
-
* }
|
|
468
|
-
* },
|
|
469
|
-
* },
|
|
470
|
-
* )
|
|
471
|
-
* ```
|
|
472
|
-
* @param fn - Called on each attempt with the current state and durable context.
|
|
439
|
+
* @param initial - Initial state passed to the first poller call.
|
|
440
|
+
* @param poller - Called on each attempt with the current state and condition context.
|
|
441
|
+
* @param next - A function that determines whether the condition has been met or another check should be run.
|
|
473
442
|
* Returns a promise resolving to the updated state,
|
|
474
|
-
* which is passed to next to determine whether the condition has been met or another check should be
|
|
475
|
-
* @param options - Configuration options for the condition
|
|
443
|
+
* which is passed to next to determine whether the condition has been met or another check should be run.
|
|
476
444
|
* @returns The value returned by the executed function
|
|
477
445
|
*/
|
|
478
|
-
waitForCondition<T>(
|
|
446
|
+
waitForCondition<T>({
|
|
447
|
+
name,
|
|
448
|
+
initial,
|
|
449
|
+
next,
|
|
450
|
+
poller,
|
|
451
|
+
}: {
|
|
452
|
+
name?: string
|
|
453
|
+
initial: T
|
|
454
|
+
poller: DurableWaitForConditionPoller<T>
|
|
455
|
+
next: DurableWaitForConditionNext<T>
|
|
456
|
+
}): Promise<T>
|
|
479
457
|
}
|
|
480
458
|
|
|
481
459
|
/**
|