@sanity/functions 1.5.0 → 1.6.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/src/types.ts CHANGED
@@ -23,10 +23,8 @@ export interface FunctionContext {
23
23
  functionResourceType: string
24
24
  /** The resource ID of the function container; resource ID that houses the function. */
25
25
  functionResourceId: string
26
- /**
27
- * uniqueId for a pipeline run
28
- */
29
- runId?: string
26
+ /** Sanity lineage token */
27
+ lineage?: string | undefined
30
28
  /**
31
29
  * `local` is set to `true` when testing your function locally.
32
30
  * i.e. `sanity function test func-name`
@@ -62,6 +60,8 @@ export interface FunctionContext {
62
60
  * The context object passed to the schedule function handler.
63
61
  */
64
62
  export interface ScheduledFunctionContext {
63
+ /** Sanity lineage token */
64
+ lineage?: string | undefined
65
65
  /**
66
66
  * `local` is set to `true` when testing your function locally.
67
67
  * i.e. `sanity function test func-name`
@@ -234,14 +234,76 @@ export type FunctionResourceEnvelope = {
234
234
  [K in FunctionResourceKey]-?: {[P in K]: FunctionResource} & {[P in Exclude<FunctionResourceKey, K>]?: FunctionResource}
235
235
  }[FunctionResourceKey]
236
236
 
237
+ type DurableLogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'
238
+
239
+ /**
240
+ * Context logger interface for durables.
241
+ * Exposes console.log through the durable context.
242
+ * @alpha Using durables is considered experimental and may change in the future.
243
+ * @hidden
244
+ */
245
+ interface DurableLogger {
246
+ log?(level: DurableLogLevel, ...params: unknown[]): void
247
+ debug(...params: unknown[]): void
248
+ info(...params: unknown[]): void
249
+ warn(...params: unknown[]): void
250
+ error(...params: unknown[]): void
251
+ }
252
+
253
+ /**
254
+ * @alpha Using durables is considered experimental and may change in the future.
255
+ * @hidden
256
+ */
257
+ export type DurableContext = FunctionContext & {
258
+ /**
259
+ * Durable logger
260
+ */
261
+ log: DurableLogger
262
+ /**
263
+ * Current attempt of the active durable operation.
264
+ * Only available in methods that expose the attempt like `waitForCondition`
265
+ */
266
+ attempt?: number
267
+ /**
268
+ * Unique ID for a durable execution
269
+ */
270
+ runId?: string
271
+ }
272
+
273
+ /**
274
+ * The signature of the duration object passed to the `wait` method of a durable function.
275
+ * It can be specified in days, hours, minutes, or seconds.
276
+ * @alpha Using durables is considered experimental and may change in the future.
277
+ * @hidden
278
+ */
279
+ export type DurableDuration =
280
+ | {days: number; hours?: number; minutes?: number; seconds?: number}
281
+ | {hours: number; minutes?: number; seconds?: number}
282
+ | {minutes: number; seconds?: number}
283
+ | {seconds: number}
284
+
285
+ /**
286
+ * @alpha Using durables is considered experimental and may change in the future.
287
+ * @hidden
288
+ */
289
+ export type DurableWaitForConditionDecision = {shouldContinue: true; delay: DurableDuration} | {shouldContinue: false}
290
+
237
291
  /**
238
- * The interface defining the operations available to a pipeline function.
292
+ * @alpha Using durables is considered experimental and may change in the future.
293
+ * @hidden
294
+ */
295
+ export interface DurableWaitForConditionOptions<T> {
296
+ initial: T
297
+ next: (state: T, context: DurableContext) => DurableWaitForConditionDecision
298
+ }
299
+
300
+ /**
301
+ * The interface defining the operations available to a durable function.
239
302
  * These operations are steps that delegate to other functions, and wait for external callbacks.
240
- * @alpha Using pipeline functions is considered experimental and may change in the future.
241
- * @public
303
+ * @alpha Using durables is considered experimental and may change in the future.
242
304
  * @hidden
243
305
  */
244
- export interface PipelineOperations {
306
+ export interface DurableOperations {
245
307
  /**
246
308
  * Runs a named step. Its result is recorded, so if the workflow re-runs the step
247
309
  * is not executed again. Failures are retried on their own.
@@ -251,7 +313,7 @@ export interface PipelineOperations {
251
313
  * @param fn - function being executed
252
314
  * @returns The value returned by the executed function
253
315
  */
254
- run<T>(name: string, fn: () => T | Promise<T>): Promise<T>
316
+ run<T>(name: string, fn: (ctx: DurableContext) => T | Promise<T>): Promise<T>
255
317
  /**
256
318
  * Calls another function and awaits its result.
257
319
  * similar to ctx.invoke()
@@ -267,19 +329,153 @@ export interface PipelineOperations {
267
329
  * similar to ctx.waitForCallback()
268
330
  * @remarks cannot be nested inside another step
269
331
  * @param name - Step name
270
- * @param fn -Receives the generated callbackId and returns a delivered promise
332
+ * @param fn - Receives the generated callbackId and returns a delivered promise
333
+ * @returns The value returned by the executed function
334
+ */
335
+ waitForCallback<T>(name: string, fn: (callbackId: string, ctx: DurableContext) => Promise<void>): Promise<T>
336
+
337
+ /**
338
+ * Waits for a specified duration
339
+ * similar to ctx.wait()
340
+ * @remarks cannot be nested inside another step
341
+ * @example
342
+ * Wait for 30 seconds
343
+ * ```ts
344
+ * await step.wait('wait30Sec', { seconds: 30 });
345
+ * ```
346
+ * Wait for 2 days and 3 hours
347
+ * ```ts
348
+ * await step.wait('waitALongTime', { days: 2, hours: 3 });
349
+ * ```
350
+ * @param name - Step name
351
+ * @param duration - Amount of time the function will wait before continuing.
352
+ * Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
353
+ * @returns Resolves after the duration
354
+ */
355
+ wait(name: string, duration: DurableDuration): Promise<void>
356
+
357
+ /**
358
+ * Waits for a specified duration
359
+ * similar to ctx.wait()
360
+ * @remarks cannot be nested inside another step
361
+ * @example
362
+ * Wait for 30 seconds
363
+ * ```ts
364
+ * await step.wait({ seconds: 30 });
365
+ * ```
366
+ * @param duration - Amount of time the function will wait before continuing.
367
+ * Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
368
+ * @returns Resolves after the duration
369
+ */
370
+ wait(duration: DurableDuration): Promise<void>
371
+
372
+ /**
373
+ * Waits for a specified condition to be met
374
+ * similar to ctx.waitForCondition()
375
+ * @remarks cannot be nested inside another step
376
+ * @example
377
+ * ```ts
378
+ * await step.waitForCondition(
379
+ * 'waitForCondition',
380
+ * async (state, context) => {
381
+ * context.log.info('Checking for article', {
382
+ * runId: context.runId,
383
+ * attempt: context.attempt,
384
+ * })
385
+ *
386
+ * const client = createClient({
387
+ * apiVersion: '2026-08-05',
388
+ * ...context.clientOptions,
389
+ * })
390
+ * const article = await client.fetch<Article | null>(
391
+ * '*[_type == "article"][0]',
392
+ * )
393
+ * return {...state, article}
394
+ * },
395
+ * {
396
+ * initial: {
397
+ * article: null as Article | null,
398
+ * },
399
+ * next: (state, context) => {
400
+ * if (state.article) return {shouldContinue: false}
401
+ *
402
+ * return {
403
+ * shouldContinue: true,
404
+ * delay: {
405
+ * seconds: Math.min((context.attempt ?? 1) * 2, 60),
406
+ * },
407
+ * }
408
+ * },
409
+ * },
410
+ * )
411
+ * ```
412
+ * @param name - Step name
413
+ * @param fn - Called on each attempt with the current state and durable context.
414
+ * Returns a promise resolving to the updated state,
415
+ * which is passed to next to determine whether the condition has been met or another check should be scheduled.
416
+ * @param options - Configuration options for the condition
417
+ * @returns The value returned by the executed function
418
+ */
419
+ waitForCondition<T>(
420
+ name: string,
421
+ fn: (state: T, context: DurableContext) => Promise<T>,
422
+ options: DurableWaitForConditionOptions<T>,
423
+ ): Promise<T>
424
+
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, context) => {
433
+ * context.log.info('Checking for article', {
434
+ * runId: context.runId,
435
+ * attempt: context.attempt,
436
+ * })
437
+ *
438
+ * const client = createClient({
439
+ * apiVersion: '2026-08-05',
440
+ * ...context.clientOptions,
441
+ * })
442
+ * const article = await client.fetch<Article | null>(
443
+ * '*[_type == "article"][0]',
444
+ * )
445
+ * return {...state, article}
446
+ * },
447
+ * {
448
+ * initial: {
449
+ * article: null as Article | null,
450
+ * },
451
+ * next: (state, context) => {
452
+ * if (state.article) return {shouldContinue: false}
453
+ *
454
+ * return {
455
+ * shouldContinue: true,
456
+ * delay: {
457
+ * seconds: Math.min((context.attempt ?? 1) * 2, 60),
458
+ * },
459
+ * }
460
+ * },
461
+ * },
462
+ * )
463
+ * ```
464
+ * @param fn - Called on each attempt with the current state and durable context.
465
+ * Returns a promise resolving to the updated state,
466
+ * which is passed to next to determine whether the condition has been met or another check should be scheduled.
467
+ * @param options - Configuration options for the condition
271
468
  * @returns The value returned by the executed function
272
469
  */
273
- waitForCallback<T>(name: string, fn: (callbackId: string) => Promise<void>): Promise<T>
274
- // @todo: other methods wanted for durables
470
+ waitForCondition<T>(fn: (state: T, context: DurableContext) => Promise<T>, options: DurableWaitForConditionOptions<T>): Promise<T>
275
471
  }
276
472
 
277
473
  /**
278
- * @alpha Using pipeline functions is considered experimental and may change in the future.
474
+ * @alpha Using durables is considered experimental and may change in the future.
279
475
  * @hidden
280
476
  */
281
- export type PipelineHandler = (envelope: {
282
- context: FunctionContext
477
+ export type DurableHandler = (envelope: {
478
+ context: DurableContext
283
479
  event?: GenericEvent
284
- step: PipelineOperations
480
+ step: DurableOperations
285
481
  }) => unknown | Promise<unknown>
@@ -1,32 +0,0 @@
1
- import type { PipelineHandler } from './types';
2
- /**
3
- * Pipeline creation function that can be called with or without a config object.
4
- * @alpha Pipelines are an experimental feature and may change in the future.
5
- * @hidden
6
- * @param handler
7
- * @returns The handler function, unmodified.
8
- */
9
- export declare function createPipeline(handler: PipelineHandler): PipelineHandler & {
10
- config?: undefined;
11
- };
12
- /**
13
- * Pipeline creation function that can be called with or without a config object.
14
- * @alpha Pipelines are an experimental feature and may change in the future.
15
- * @hidden
16
- * @param config
17
- * @param handler
18
- * @returns The handler function, unmodified and the provided config object.
19
- */
20
- export declare function createPipeline<TConfig extends {
21
- name: string;
22
- }>(config: TConfig, handler: PipelineHandler): PipelineHandler & {
23
- config: TConfig;
24
- };
25
- /**
26
- * @alpha Pipelines are an experimental feature and may change in the future.
27
- * @hidden
28
- * @public
29
- */
30
- export declare const pipeline: {
31
- createPipeline: typeof createPipeline;
32
- };
@@ -1 +0,0 @@
1
- {"version":3,"file":"pipelines.js","sourceRoot":"","sources":["../src/pipelines.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,eAAe,GAAG,CACtB,eAA0C,EAC1C,YAA8B,EACwC,EAAE;IACxE,OAAO,OAAO,e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