@sanity/functions 1.5.0 → 1.6.1

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
@@ -1,10 +1,14 @@
1
1
  /**
2
2
  * Callback provided by Sanity CLI when doing local development
3
+ * @alpha
4
+ * @hidden
3
5
  */
4
6
  export type InvokeCallback = <T = unknown>(name: string, payload: FunctionPayload, options?: InvokeOptions) => Promise<T>
5
7
 
6
8
  /**
7
9
  * Options provided to invoke
10
+ * @alpha
11
+ * @hidden
8
12
  */
9
13
  export type InvokeOptions = {
10
14
  // Whether the function should be invoked synchronously
@@ -23,10 +27,8 @@ export interface FunctionContext {
23
27
  functionResourceType: string
24
28
  /** The resource ID of the function container; resource ID that houses the function. */
25
29
  functionResourceId: string
26
- /**
27
- * uniqueId for a pipeline run
28
- */
29
- runId?: string
30
+ /** Sanity lineage token */
31
+ lineage?: string | undefined
30
32
  /**
31
33
  * `local` is set to `true` when testing your function locally.
32
34
  * i.e. `sanity function test func-name`
@@ -62,6 +64,8 @@ export interface FunctionContext {
62
64
  * The context object passed to the schedule function handler.
63
65
  */
64
66
  export interface ScheduledFunctionContext {
67
+ /** Sanity lineage token */
68
+ lineage?: string | undefined
65
69
  /**
66
70
  * `local` is set to `true` when testing your function locally.
67
71
  * i.e. `sanity function test func-name`
@@ -234,14 +238,76 @@ export type FunctionResourceEnvelope = {
234
238
  [K in FunctionResourceKey]-?: {[P in K]: FunctionResource} & {[P in Exclude<FunctionResourceKey, K>]?: FunctionResource}
235
239
  }[FunctionResourceKey]
236
240
 
241
+ type DurableLogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'
242
+
237
243
  /**
238
- * The interface defining the operations available to a pipeline function.
244
+ * Context logger interface for durables.
245
+ * Exposes console.log through the durable context.
246
+ * @alpha Using durables is considered experimental and may change in the future.
247
+ * @hidden
248
+ */
249
+ interface DurableLogger {
250
+ log?(level: DurableLogLevel, ...params: unknown[]): void
251
+ debug(...params: unknown[]): void
252
+ info(...params: unknown[]): void
253
+ warn(...params: unknown[]): void
254
+ error(...params: unknown[]): void
255
+ }
256
+
257
+ /**
258
+ * @alpha Using durables is considered experimental and may change in the future.
259
+ * @hidden
260
+ */
261
+ export type DurableContext = FunctionContext & {
262
+ /**
263
+ * Durable logger
264
+ */
265
+ log: DurableLogger
266
+ /**
267
+ * Current attempt of the active durable operation.
268
+ * Only available in methods that expose the attempt like `waitForCondition`
269
+ */
270
+ attempt?: number
271
+ /**
272
+ * Unique ID for a durable execution
273
+ */
274
+ runId?: string
275
+ }
276
+
277
+ /**
278
+ * The signature of the duration object passed to the `wait` method of a durable function.
279
+ * It can be specified in days, hours, minutes, or seconds.
280
+ * @alpha Using durables is considered experimental and may change in the future.
281
+ * @hidden
282
+ */
283
+ export type DurableDuration =
284
+ | {days: number; hours?: number; minutes?: number; seconds?: number}
285
+ | {hours: number; minutes?: number; seconds?: number}
286
+ | {minutes: number; seconds?: number}
287
+ | {seconds: number}
288
+
289
+ /**
290
+ * @alpha Using durables is considered experimental and may change in the future.
291
+ * @hidden
292
+ */
293
+ export type DurableWaitForConditionDecision = {shouldContinue: true; delay: DurableDuration} | {shouldContinue: false}
294
+
295
+ /**
296
+ * @alpha Using durables is considered experimental and may change in the future.
297
+ * @hidden
298
+ */
299
+ export interface DurableWaitForConditionOptions<T> {
300
+ initial: T
301
+ next: (state: T, context: DurableContext) => DurableWaitForConditionDecision
302
+ }
303
+
304
+ /**
305
+ * The interface defining the operations available to a durable function.
239
306
  * 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
307
+ * @alpha Using durables is considered experimental and may change in the future.
242
308
  * @hidden
243
309
  */
244
- export interface PipelineOperations {
310
+ export interface DurableOperations {
245
311
  /**
246
312
  * Runs a named step. Its result is recorded, so if the workflow re-runs the step
247
313
  * is not executed again. Failures are retried on their own.
@@ -251,7 +317,7 @@ export interface PipelineOperations {
251
317
  * @param fn - function being executed
252
318
  * @returns The value returned by the executed function
253
319
  */
254
- run<T>(name: string, fn: () => T | Promise<T>): Promise<T>
320
+ run<T>(name: string, fn: (ctx: DurableContext) => T | Promise<T>): Promise<T>
255
321
  /**
256
322
  * Calls another function and awaits its result.
257
323
  * similar to ctx.invoke()
@@ -267,19 +333,153 @@ export interface PipelineOperations {
267
333
  * similar to ctx.waitForCallback()
268
334
  * @remarks cannot be nested inside another step
269
335
  * @param name - Step name
270
- * @param fn -Receives the generated callbackId and returns a delivered promise
336
+ * @param fn - Receives the generated callbackId and returns a delivered promise
337
+ * @returns The value returned by the executed function
338
+ */
339
+ waitForCallback<T>(name: string, fn: (callbackId: string, ctx: DurableContext) => Promise<void>): Promise<T>
340
+
341
+ /**
342
+ * Waits for a specified duration
343
+ * similar to ctx.wait()
344
+ * @remarks cannot be nested inside another step
345
+ * @example
346
+ * Wait for 30 seconds
347
+ * ```ts
348
+ * await step.wait('wait30Sec', { seconds: 30 });
349
+ * ```
350
+ * Wait for 2 days and 3 hours
351
+ * ```ts
352
+ * await step.wait('waitALongTime', { days: 2, hours: 3 });
353
+ * ```
354
+ * @param name - Step name
355
+ * @param duration - Amount of time the function will wait before continuing.
356
+ * Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
357
+ * @returns Resolves after the duration
358
+ */
359
+ wait(name: string, duration: DurableDuration): Promise<void>
360
+
361
+ /**
362
+ * Waits for a specified duration
363
+ * similar to ctx.wait()
364
+ * @remarks cannot be nested inside another step
365
+ * @example
366
+ * Wait for 30 seconds
367
+ * ```ts
368
+ * await step.wait({ seconds: 30 });
369
+ * ```
370
+ * @param duration - Amount of time the function will wait before continuing.
371
+ * Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
372
+ * @returns Resolves after the duration
373
+ */
374
+ wait(duration: DurableDuration): Promise<void>
375
+
376
+ /**
377
+ * Waits for a specified condition to be met
378
+ * similar to ctx.waitForCondition()
379
+ * @remarks cannot be nested inside another step
380
+ * @example
381
+ * ```ts
382
+ * await step.waitForCondition(
383
+ * 'waitForCondition',
384
+ * async (state, context) => {
385
+ * context.log.info('Checking for article', {
386
+ * runId: context.runId,
387
+ * attempt: context.attempt,
388
+ * })
389
+ *
390
+ * const client = createClient({
391
+ * apiVersion: '2026-08-05',
392
+ * ...context.clientOptions,
393
+ * })
394
+ * const article = await client.fetch<Article | null>(
395
+ * '*[_type == "article"][0]',
396
+ * )
397
+ * return {...state, article}
398
+ * },
399
+ * {
400
+ * initial: {
401
+ * article: null as Article | null,
402
+ * },
403
+ * next: (state, context) => {
404
+ * if (state.article) return {shouldContinue: false}
405
+ *
406
+ * return {
407
+ * shouldContinue: true,
408
+ * delay: {
409
+ * seconds: Math.min((context.attempt ?? 1) * 2, 60),
410
+ * },
411
+ * }
412
+ * },
413
+ * },
414
+ * )
415
+ * ```
416
+ * @param name - Step name
417
+ * @param fn - Called on each attempt with the current state and durable context.
418
+ * Returns a promise resolving to the updated state,
419
+ * which is passed to next to determine whether the condition has been met or another check should be scheduled.
420
+ * @param options - Configuration options for the condition
421
+ * @returns The value returned by the executed function
422
+ */
423
+ waitForCondition<T>(
424
+ name: string,
425
+ fn: (state: T, context: DurableContext) => Promise<T>,
426
+ options: DurableWaitForConditionOptions<T>,
427
+ ): Promise<T>
428
+
429
+ /**
430
+ * Waits for a specified condition to be met
431
+ * similar to ctx.waitForCondition()
432
+ * @remarks cannot be nested inside another step
433
+ * @example
434
+ * ```ts
435
+ * await step.waitForCondition(
436
+ * async (state, context) => {
437
+ * context.log.info('Checking for article', {
438
+ * runId: context.runId,
439
+ * attempt: context.attempt,
440
+ * })
441
+ *
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
+ * {
452
+ * initial: {
453
+ * article: null as Article | null,
454
+ * },
455
+ * next: (state, context) => {
456
+ * if (state.article) return {shouldContinue: false}
457
+ *
458
+ * return {
459
+ * shouldContinue: true,
460
+ * delay: {
461
+ * seconds: Math.min((context.attempt ?? 1) * 2, 60),
462
+ * },
463
+ * }
464
+ * },
465
+ * },
466
+ * )
467
+ * ```
468
+ * @param fn - Called on each attempt with the current state and durable context.
469
+ * Returns a promise resolving to the updated state,
470
+ * which is passed to next to determine whether the condition has been met or another check should be scheduled.
471
+ * @param options - Configuration options for the condition
271
472
  * @returns The value returned by the executed function
272
473
  */
273
- waitForCallback<T>(name: string, fn: (callbackId: string) => Promise<void>): Promise<T>
274
- // @todo: other methods wanted for durables
474
+ waitForCondition<T>(fn: (state: T, context: DurableContext) => Promise<T>, options: DurableWaitForConditionOptions<T>): Promise<T>
275
475
  }
276
476
 
277
477
  /**
278
- * @alpha Using pipeline functions is considered experimental and may change in the future.
478
+ * @alpha Using durables is considered experimental and may change in the future.
279
479
  * @hidden
280
480
  */
281
- export type PipelineHandler = (envelope: {
282
- context: FunctionContext
481
+ export type DurableHandler = (envelope: {
482
+ context: DurableContext
283
483
  event?: GenericEvent
284
- step: PipelineOperations
484
+ step: DurableOperations
285
485
  }) => 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,eAAe,KAAK,UAAU;QAC1C,CAAC,CAAC,EAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,KAAK,EAAC;QACpE,CAAC,CAAC,EAAC,MAAM,EAAE,eAAe,EAAE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,IAAI,EAAC,CAAA;AAC1E,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,cAAc,GAAG,CAAC,MAAe,EAAE,EAAE;IACzC,MAAM,WAAW,GAAG,CAAC,UAAU,EAAE,eAAe,EAAE,MAAM,EAAE,qBAAqB,CAAC,CAAA;IAChF,MAAM,MAAM,GAAa,EAAE,CAAA;IAE3B,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC5C,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;QACvC,OAAO,MAAM,CAAA;IACf,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAA;QACzC,OAAO,MAAM,CAAA;IACf,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;IACvD,MAAM,KAAK,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IAC1D,IAAI,OAAO,IAAI,KAAK,QAA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