@sanity/functions 1.4.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/dist/durables.d.ts +32 -0
- package/dist/durables.js +74 -0
- package/dist/durables.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/invoke.d.ts +38 -2
- package/dist/invoke.js +106 -20
- package/dist/invoke.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +255 -1
- package/package.json +1 -1
- package/src/durables.ts +104 -0
- package/src/index.ts +1 -0
- package/src/invoke.ts +125 -21
- package/src/types.ts +259 -1
package/src/types.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Callback provided by Sanity CLI when doing local development
|
|
3
3
|
*/
|
|
4
|
-
export type InvokeCallback = (name: string, payload: FunctionPayload) => Promise<
|
|
4
|
+
export type InvokeCallback = <T = unknown>(name: string, payload: FunctionPayload, options?: InvokeOptions) => Promise<T>
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Options provided to invoke
|
|
8
|
+
*/
|
|
9
|
+
export type InvokeOptions = {
|
|
10
|
+
// Whether the function should be invoked synchronously
|
|
11
|
+
sync?: boolean
|
|
12
|
+
}
|
|
5
13
|
|
|
6
14
|
/**
|
|
7
15
|
* The context object passed to the function handler.
|
|
@@ -15,6 +23,8 @@ export interface FunctionContext {
|
|
|
15
23
|
functionResourceType: string
|
|
16
24
|
/** The resource ID of the function container; resource ID that houses the function. */
|
|
17
25
|
functionResourceId: string
|
|
26
|
+
/** Sanity lineage token */
|
|
27
|
+
lineage?: string | undefined
|
|
18
28
|
/**
|
|
19
29
|
* `local` is set to `true` when testing your function locally.
|
|
20
30
|
* i.e. `sanity function test func-name`
|
|
@@ -50,6 +60,8 @@ export interface FunctionContext {
|
|
|
50
60
|
* The context object passed to the schedule function handler.
|
|
51
61
|
*/
|
|
52
62
|
export interface ScheduledFunctionContext {
|
|
63
|
+
/** Sanity lineage token */
|
|
64
|
+
lineage?: string | undefined
|
|
53
65
|
/**
|
|
54
66
|
* `local` is set to `true` when testing your function locally.
|
|
55
67
|
* i.e. `sanity function test func-name`
|
|
@@ -221,3 +233,249 @@ type FunctionResourceKey = 'eventsourcemapping' | 'function' | 'parameter' | 'qu
|
|
|
221
233
|
export type FunctionResourceEnvelope = {
|
|
222
234
|
[K in FunctionResourceKey]-?: {[P in K]: FunctionResource} & {[P in Exclude<FunctionResourceKey, K>]?: FunctionResource}
|
|
223
235
|
}[FunctionResourceKey]
|
|
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
|
+
|
|
291
|
+
/**
|
|
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.
|
|
302
|
+
* These operations are steps that delegate to other functions, and wait for external callbacks.
|
|
303
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
304
|
+
* @hidden
|
|
305
|
+
*/
|
|
306
|
+
export interface DurableOperations {
|
|
307
|
+
/**
|
|
308
|
+
* Runs a named step. Its result is recorded, so if the workflow re-runs the step
|
|
309
|
+
* is not executed again. Failures are retried on their own.
|
|
310
|
+
* similar to ctx.step()
|
|
311
|
+
* @remarks cannot be nested inside another step
|
|
312
|
+
* @param name - Step name
|
|
313
|
+
* @param fn - function being executed
|
|
314
|
+
* @returns The value returned by the executed function
|
|
315
|
+
*/
|
|
316
|
+
run<T>(name: string, fn: (ctx: DurableContext) => T | Promise<T>): Promise<T>
|
|
317
|
+
/**
|
|
318
|
+
* Calls another function and awaits its result.
|
|
319
|
+
* similar to ctx.invoke()
|
|
320
|
+
* @remarks cannot be nested inside another step
|
|
321
|
+
* @param name - Step name
|
|
322
|
+
* @param input - Data passed to the called function
|
|
323
|
+
* @returns The value returned by the called function
|
|
324
|
+
*/
|
|
325
|
+
delegate<T>(name: string, input?: unknown): Promise<T>
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Waits for an external callback
|
|
329
|
+
* similar to ctx.waitForCallback()
|
|
330
|
+
* @remarks cannot be nested inside another step
|
|
331
|
+
* @param name - Step name
|
|
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
|
|
468
|
+
* @returns The value returned by the executed function
|
|
469
|
+
*/
|
|
470
|
+
waitForCondition<T>(fn: (state: T, context: DurableContext) => Promise<T>, options: DurableWaitForConditionOptions<T>): Promise<T>
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* @alpha Using durables is considered experimental and may change in the future.
|
|
475
|
+
* @hidden
|
|
476
|
+
*/
|
|
477
|
+
export type DurableHandler = (envelope: {
|
|
478
|
+
context: DurableContext
|
|
479
|
+
event?: GenericEvent
|
|
480
|
+
step: DurableOperations
|
|
481
|
+
}) => unknown | Promise<unknown>
|