@sanity/functions 1.6.9 → 1.7.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/definers.d.ts +1 -1
- package/dist/durables.d.ts +1 -1
- package/dist/invoke.d.ts +2 -1
- package/dist/invoke.js.map +1 -1
- package/dist/types/context.d.ts +107 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/durables.d.ts +315 -0
- package/dist/types/durables.js +2 -0
- package/dist/types/durables.js.map +1 -0
- package/dist/types/functions.d.ts +100 -0
- package/dist/types/functions.js +2 -0
- package/dist/types/functions.js.map +1 -0
- package/dist/types/invocation.d.ts +13 -0
- package/dist/types/invocation.js +2 -0
- package/dist/types/invocation.js.map +1 -0
- package/dist/types/resources.d.ts +23 -0
- package/dist/types/resources.js +2 -0
- package/dist/types/resources.js.map +1 -0
- package/dist/types.d.ts +5 -469
- package/dist/types.js +5 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/definers.ts +1 -1
- package/src/durables.ts +1 -1
- package/src/invoke.ts +2 -1
- package/src/types/context.ts +111 -0
- package/src/types/durables.ts +319 -0
- package/src/types/functions.ts +103 -0
- package/src/types/invocation.ts +15 -0
- package/src/types/resources.ts +24 -0
- package/src/types.ts +5 -481
package/dist/types.d.ts
CHANGED
|
@@ -1,469 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*/
|
|
7
|
-
export type InvokeCallback = <T = unknown>(name: string, payload?: Record<string, unknown>, options?: InvokeOptions) => Promise<T>;
|
|
8
|
-
/**
|
|
9
|
-
* Options provided to invoke
|
|
10
|
-
*/
|
|
11
|
-
export type InvokeOptions = {
|
|
12
|
-
sync?: boolean;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* The context object passed to the function handler.
|
|
16
|
-
*/
|
|
17
|
-
export interface FunctionContext {
|
|
18
|
-
/** The resource type of the event source; resource type that invoked the function. */
|
|
19
|
-
eventResourceType: string;
|
|
20
|
-
/** The resource ID of the event source; resource ID that invoked the function. */
|
|
21
|
-
eventResourceId: string;
|
|
22
|
-
/** The resource type of the function container; resource type that houses the function. */
|
|
23
|
-
functionResourceType: string;
|
|
24
|
-
/** The resource ID of the function container; resource ID that houses the function. */
|
|
25
|
-
functionResourceId: string;
|
|
26
|
-
/** Sanity lineage token */
|
|
27
|
-
lineage?: string | undefined;
|
|
28
|
-
/**
|
|
29
|
-
* `local` is set to `true` when testing your function locally.
|
|
30
|
-
* i.e. `sanity function test func-name`
|
|
31
|
-
* Otherwise, the property is not set.
|
|
32
|
-
*/
|
|
33
|
-
local?: boolean;
|
|
34
|
-
invoke?: InvokeCallback;
|
|
35
|
-
/**
|
|
36
|
-
* Options that can be passed to a `@sanity/client` constructor to configure it
|
|
37
|
-
* against the project and dataset which triggered the event. Note that you should
|
|
38
|
-
* always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* Constructing a client with the options from the context:
|
|
42
|
-
* ```ts
|
|
43
|
-
* const client = createClient({
|
|
44
|
-
* apiVersion: '2025-05-01',
|
|
45
|
-
* ...context.clientOptions,
|
|
46
|
-
* })
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
clientOptions: {
|
|
50
|
-
apiHost?: string;
|
|
51
|
-
dataset: string;
|
|
52
|
-
projectId: string;
|
|
53
|
-
token: string;
|
|
54
|
-
};
|
|
55
|
-
/** Resource interface that allows access to Blueprint Resources */
|
|
56
|
-
resources: ResourcesApi;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* The context object passed to the schedule function handler.
|
|
60
|
-
*/
|
|
61
|
-
export interface ScheduledFunctionContext {
|
|
62
|
-
/** Sanity lineage token */
|
|
63
|
-
lineage?: string | undefined;
|
|
64
|
-
/**
|
|
65
|
-
* `local` is set to `true` when testing your function locally.
|
|
66
|
-
* i.e. `sanity function test func-name`
|
|
67
|
-
* Otherwise, the property is not set.
|
|
68
|
-
*/
|
|
69
|
-
local?: boolean;
|
|
70
|
-
invoke?: InvokeCallback;
|
|
71
|
-
/**
|
|
72
|
-
* Options that can be passed to a `@sanity/client` constructor to configure it
|
|
73
|
-
* against the project and dataset which triggered the event. Note that you should
|
|
74
|
-
* always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* Constructing a client with the options from the context:
|
|
78
|
-
* ```ts
|
|
79
|
-
* const client = createClient({
|
|
80
|
-
* apiVersion: '2025-05-01',
|
|
81
|
-
* ...context.clientOptions,
|
|
82
|
-
* })
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
clientOptions?: {
|
|
86
|
-
apiHost?: string;
|
|
87
|
-
dataset?: string;
|
|
88
|
-
projectId?: string;
|
|
89
|
-
token?: string;
|
|
90
|
-
};
|
|
91
|
-
/** Resource interface that allows access to Blueprint Resources */
|
|
92
|
-
resources: ResourcesApi;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* The event object received by the function handler in the case of a document event,
|
|
96
|
-
* such as a publish, unpublish, delete or mutation event and similar.
|
|
97
|
-
*/
|
|
98
|
-
export interface DocumentEvent<IData = any> {
|
|
99
|
-
/**
|
|
100
|
-
* The data delivered to the function. This is the result of applying any configured
|
|
101
|
-
* GROQ-projection to the changed document. If no projection is configured, this is
|
|
102
|
-
* the document itself.
|
|
103
|
-
*/
|
|
104
|
-
data: IData;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* The event object received by the function handler in the case of a sync-tag-invalidate event.
|
|
108
|
-
*/
|
|
109
|
-
export interface SyncTagInvalidateEvent {
|
|
110
|
-
/**
|
|
111
|
-
* The sync tags for use with cache invalidation and notifying the callback endpoint once tags are invalidated.
|
|
112
|
-
*/
|
|
113
|
-
data: {
|
|
114
|
-
/** Array of sync tags to be invalidated. */
|
|
115
|
-
syncTags: string[];
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* A function handler for a document event.
|
|
120
|
-
*/
|
|
121
|
-
export type DocumentEventHandler<IData = any> = (envelope: {
|
|
122
|
-
context: FunctionContext;
|
|
123
|
-
event: DocumentEvent<IData>;
|
|
124
|
-
}) => void | Promise<void>;
|
|
125
|
-
/**
|
|
126
|
-
* A function handler for a schedule event.
|
|
127
|
-
*/
|
|
128
|
-
export type ScheduledEventHandler = (envelope: {
|
|
129
|
-
context: ScheduledFunctionContext;
|
|
130
|
-
}) => void | Promise<void>;
|
|
131
|
-
/**
|
|
132
|
-
* A generic function context type which supports all function types
|
|
133
|
-
*/
|
|
134
|
-
export type GenericContext = FunctionContext | ScheduledFunctionContext | SyncTagInvalidateContext;
|
|
135
|
-
/**
|
|
136
|
-
* A generic function event type which supports all function types
|
|
137
|
-
*/
|
|
138
|
-
export type GenericEvent = DocumentEvent | SyncTagInvalidateEvent;
|
|
139
|
-
/**
|
|
140
|
-
* A function handler for a pubsub event.
|
|
141
|
-
*
|
|
142
|
-
* A pubsub function can be triggered by any event source, so the envelope is intentionally
|
|
143
|
-
* permissive and needs no narrowing: `context` is any supported context, `event` is any
|
|
144
|
-
* supported event, and `done` is optional (present only for sync-tag-invalidate events).
|
|
145
|
-
*
|
|
146
|
-
* `IResult` is the value the handler resolves to and defaults to `void`. Pubsub is the one
|
|
147
|
-
* function type that may be invoked with {@link invoke | `invoke(name, payload, {sync: true})`},
|
|
148
|
-
* which returns the handler's resolved value to the caller.
|
|
149
|
-
*/
|
|
150
|
-
export type PubSubEventHandler<IData = any, IResult = void> = (envelope: {
|
|
151
|
-
context: GenericContext;
|
|
152
|
-
event: DocumentEvent<IData> | SyncTagInvalidateEvent;
|
|
153
|
-
done?: SyncTagInvalidateCallback;
|
|
154
|
-
}) => IResult | Promise<IResult>;
|
|
155
|
-
/**
|
|
156
|
-
* A generic function handler that can receive the payload of any function type.
|
|
157
|
-
*
|
|
158
|
-
* @deprecated Renamed to {@link PubSubEventHandler}. This alias will be removed in the next
|
|
159
|
-
* major version.
|
|
160
|
-
*/
|
|
161
|
-
export type EventHandler<IData = any, IResult = void> = PubSubEventHandler<IData, IResult>;
|
|
162
|
-
/**
|
|
163
|
-
* A callback function to invoke once a sync-tag-invalidate event has been processed. Signals to Sanity that sync tag invalidation has completed.
|
|
164
|
-
*/
|
|
165
|
-
export type SyncTagInvalidateCallback = (syncTags: string[]) => Promise<Response>;
|
|
166
|
-
/**
|
|
167
|
-
* The context object passed to the sync tag invalidate event handler.
|
|
168
|
-
*/
|
|
169
|
-
export interface SyncTagInvalidateContext extends Omit<FunctionContext, 'clientOptions'> {
|
|
170
|
-
/**
|
|
171
|
-
* A short-lived token that should be used to notify Sanity of sync tag invalidation routine completion. Recommended to use the `done` helper argument provided to the sync tag invalidate event handler instead of this token directly.
|
|
172
|
-
*/
|
|
173
|
-
callbackToken: string;
|
|
174
|
-
clientOptions: {
|
|
175
|
-
apiHost: string;
|
|
176
|
-
dataset: string;
|
|
177
|
-
projectId: string;
|
|
178
|
-
/**
|
|
179
|
-
* An API token for use with the Sanity HTTP API. Note that it may be undefined if the user does not explicitly assign a Robot Token to their function definition.
|
|
180
|
-
* @see https://www.sanity.io/docs/blueprints/blueprints-robot-tokens#k8a2a6a24a5c0
|
|
181
|
-
*/
|
|
182
|
-
token?: string;
|
|
183
|
-
};
|
|
184
|
-
/** Resource interface that allows access to Blueprint Resources */
|
|
185
|
-
resources: ResourcesApi;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* A function handler for a sync-tag-invalidate event.
|
|
189
|
-
*/
|
|
190
|
-
export type SyncTagInvalidateEventHandler = (envelope: {
|
|
191
|
-
context: SyncTagInvalidateContext;
|
|
192
|
-
event: SyncTagInvalidateEvent;
|
|
193
|
-
done: SyncTagInvalidateCallback;
|
|
194
|
-
}) => void | Promise<void>;
|
|
195
|
-
/**
|
|
196
|
-
* An interface to describe resources found in a Blueprint
|
|
197
|
-
*/
|
|
198
|
-
export interface BlueprintResource<TType extends string = string> {
|
|
199
|
-
id: string;
|
|
200
|
-
name: string;
|
|
201
|
-
type: TType;
|
|
202
|
-
}
|
|
203
|
-
export interface ResourcesApi {
|
|
204
|
-
/** Cross-type lookup by name: `context.resources('my-proj')`. */
|
|
205
|
-
(name: string): BlueprintResource | undefined;
|
|
206
|
-
/** Flat array of every resource across types. */
|
|
207
|
-
all(): BlueprintResource[];
|
|
208
|
-
/** Iterate every resource: `for (const r of context.resources)`. */
|
|
209
|
-
[Symbol.iterator](): IterableIterator<BlueprintResource>;
|
|
210
|
-
/** Type specific lookup by name: `context.cors('my-cors')` */
|
|
211
|
-
cors(name: string): BlueprintResource | undefined;
|
|
212
|
-
dataset(name: string): BlueprintResource | undefined;
|
|
213
|
-
function(name: string): BlueprintResource<`sanity.function.${string}`> | undefined;
|
|
214
|
-
project(name: string): BlueprintResource | undefined;
|
|
215
|
-
role(name: string): BlueprintResource | undefined;
|
|
216
|
-
webhook(name: string): BlueprintResource | undefined;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* The payload for the `invoke` method
|
|
220
|
-
*/
|
|
221
|
-
export type FunctionPayload = {
|
|
222
|
-
event: GenericEvent;
|
|
223
|
-
context: GenericContext;
|
|
224
|
-
};
|
|
225
|
-
/**
|
|
226
|
-
* The definition of a function resource in our resource discovery table
|
|
227
|
-
*/
|
|
228
|
-
export type FunctionResource = {
|
|
229
|
-
logicalResourceId: string;
|
|
230
|
-
physicalResourceId: string;
|
|
231
|
-
};
|
|
232
|
-
type FunctionResourceKey = 'eventsourcemapping' | 'function' | 'parameter' | 'queue' | 'schedule' | 'subscription' | 'topic';
|
|
233
|
-
export type FunctionResourceEnvelope = {
|
|
234
|
-
[K in FunctionResourceKey]-?: {
|
|
235
|
-
[P in K]: FunctionResource;
|
|
236
|
-
} & {
|
|
237
|
-
[P in Exclude<FunctionResourceKey, K>]?: FunctionResource;
|
|
238
|
-
};
|
|
239
|
-
}[FunctionResourceKey];
|
|
240
|
-
/**
|
|
241
|
-
* Durable alias of FunctionsContext
|
|
242
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
243
|
-
* @hidden
|
|
244
|
-
*/
|
|
245
|
-
export type DurableContext = FunctionContext;
|
|
246
|
-
/**
|
|
247
|
-
* Logger interface for durables.
|
|
248
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
249
|
-
* @hidden
|
|
250
|
-
*/
|
|
251
|
-
export interface DurableLogger {
|
|
252
|
-
debug(...params: unknown[]): void;
|
|
253
|
-
info(...params: unknown[]): void;
|
|
254
|
-
warn(...params: unknown[]): void;
|
|
255
|
-
error(...params: unknown[]): void;
|
|
256
|
-
log(...params: unknown[]): void;
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
260
|
-
* @hidden
|
|
261
|
-
*/
|
|
262
|
-
export interface DurableStepCallbackContext {
|
|
263
|
-
logger: DurableLogger;
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Arguments passed to callbacks that execute a durable attempt.
|
|
267
|
-
* step.run & step.waitForCondition
|
|
268
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
269
|
-
* @hidden
|
|
270
|
-
*/
|
|
271
|
-
export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
272
|
-
/** Current attempt number, starting at 1. */
|
|
273
|
-
attempt: number;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* The signature of the duration object passed to the `wait` method of a durable function.
|
|
277
|
-
* It can be specified in days, hours, minutes, or seconds.
|
|
278
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
279
|
-
* @hidden
|
|
280
|
-
*/
|
|
281
|
-
export type DurableDuration = {
|
|
282
|
-
days: number;
|
|
283
|
-
hours?: number;
|
|
284
|
-
minutes?: number;
|
|
285
|
-
seconds?: number;
|
|
286
|
-
} | {
|
|
287
|
-
hours: number;
|
|
288
|
-
minutes?: number;
|
|
289
|
-
seconds?: number;
|
|
290
|
-
} | {
|
|
291
|
-
minutes: number;
|
|
292
|
-
seconds?: number;
|
|
293
|
-
} | {
|
|
294
|
-
seconds: number;
|
|
295
|
-
};
|
|
296
|
-
/**
|
|
297
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
298
|
-
* @hidden
|
|
299
|
-
*/
|
|
300
|
-
export type DurableWaitForConditionDecision = {
|
|
301
|
-
shouldRetry: true;
|
|
302
|
-
delay: DurableDuration;
|
|
303
|
-
} | {
|
|
304
|
-
shouldRetry: false;
|
|
305
|
-
};
|
|
306
|
-
/**
|
|
307
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
308
|
-
* @hidden
|
|
309
|
-
*/
|
|
310
|
-
export type DurableStepHandler<TArgs extends unknown[], TReturn> = (...args: TArgs) => TReturn;
|
|
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
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
318
|
-
* @hidden
|
|
319
|
-
*/
|
|
320
|
-
export type DurableStepWaitForCallbackHandler = DurableStepHandler<[callbackId: string, context: DurableStepCallbackContext], Promise<void>>;
|
|
321
|
-
/**
|
|
322
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
323
|
-
* @hidden
|
|
324
|
-
*/
|
|
325
|
-
export type DurableWaitForConditionPoller<T = unknown> = DurableStepHandler<[state: T, context: DurableStepAttemptContext], Promise<T>>;
|
|
326
|
-
/**
|
|
327
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
328
|
-
* @hidden
|
|
329
|
-
*/
|
|
330
|
-
export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<[
|
|
331
|
-
state: T,
|
|
332
|
-
context: Pick<DurableStepAttemptContext, 'attempt'>
|
|
333
|
-
], DurableWaitForConditionDecision>;
|
|
334
|
-
/**
|
|
335
|
-
* The interface defining the operations available to a durable function.
|
|
336
|
-
* These operations are steps that delegate to other functions, and wait for external callbacks.
|
|
337
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
338
|
-
* @hidden
|
|
339
|
-
*/
|
|
340
|
-
export type DurableOperations = {
|
|
341
|
-
/**
|
|
342
|
-
* Runs a named step. Its result is recorded, so if the workflow re-runs the step
|
|
343
|
-
* is not executed again. Failures are retried on their own.
|
|
344
|
-
* similar to ctx.step()
|
|
345
|
-
* @remarks cannot be nested inside another step
|
|
346
|
-
* @example
|
|
347
|
-
* ```ts
|
|
348
|
-
* step.run({
|
|
349
|
-
* name: 'run-step',
|
|
350
|
-
* handler: async ({logger}) => {
|
|
351
|
-
* logger.log('Running step')
|
|
352
|
-
* }})
|
|
353
|
-
* ```
|
|
354
|
-
* @param name - Step name
|
|
355
|
-
* @param handler - function being executed
|
|
356
|
-
* @returns The value returned by the executed function
|
|
357
|
-
*/
|
|
358
|
-
run<T>({ name, handler }: {
|
|
359
|
-
name?: string;
|
|
360
|
-
handler: DurableStepRunHandler<T>;
|
|
361
|
-
}): Promise<T>;
|
|
362
|
-
/**
|
|
363
|
-
* Calls another function and awaits its result.
|
|
364
|
-
* similar to ctx.invoke()
|
|
365
|
-
* @remarks cannot be nested inside another step
|
|
366
|
-
* @param name - Step name
|
|
367
|
-
* @param handler - Function to be invoked
|
|
368
|
-
* @param input - Data passed to the called function
|
|
369
|
-
* @returns The value returned by the called function
|
|
370
|
-
*/
|
|
371
|
-
delegate<T>({ name, handler, input, }: {
|
|
372
|
-
name?: string;
|
|
373
|
-
handler: string | BlueprintResource<`sanity.function.${string}`>;
|
|
374
|
-
input?: unknown;
|
|
375
|
-
}): Promise<T>;
|
|
376
|
-
/**
|
|
377
|
-
* Waits for an external callback
|
|
378
|
-
* similar to ctx.waitForCallback()
|
|
379
|
-
* @remarks cannot be nested inside another step
|
|
380
|
-
* @param name - Step name
|
|
381
|
-
* @param handler - Receives the generated callbackId and returns a delivered promise
|
|
382
|
-
* @returns The value returned by the executed function
|
|
383
|
-
*/
|
|
384
|
-
waitForCallback<T>({ name, handler }: {
|
|
385
|
-
name?: string;
|
|
386
|
-
handler: DurableStepWaitForCallbackHandler;
|
|
387
|
-
}): Promise<T>;
|
|
388
|
-
/**
|
|
389
|
-
* Waits for a specified duration
|
|
390
|
-
* similar to ctx.wait()
|
|
391
|
-
* @remarks cannot be nested inside another step
|
|
392
|
-
* @example
|
|
393
|
-
* Wait for 30 seconds
|
|
394
|
-
* ```ts
|
|
395
|
-
* await step.wait({name: 'wait30Sec', duration: { seconds: 30 }});
|
|
396
|
-
* ```
|
|
397
|
-
* Wait for 2 days and 3 hours
|
|
398
|
-
* ```ts
|
|
399
|
-
* await step.wait({name: 'waitALongTime', duration: { days: 2, hours: 3 }});
|
|
400
|
-
* ```
|
|
401
|
-
* @param name - Step name
|
|
402
|
-
* @param duration - Amount of time the function will wait before continuing.
|
|
403
|
-
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
404
|
-
* @returns Resolves after the duration
|
|
405
|
-
*/
|
|
406
|
-
wait({ name, duration }: {
|
|
407
|
-
name?: string;
|
|
408
|
-
duration: DurableDuration;
|
|
409
|
-
}): Promise<void>;
|
|
410
|
-
/**
|
|
411
|
-
* Waits for a specified condition to be met
|
|
412
|
-
* similar to ctx.waitForCondition()
|
|
413
|
-
* @remarks cannot be nested inside another step
|
|
414
|
-
* @example
|
|
415
|
-
* ```ts
|
|
416
|
-
* await step.waitForCondition({{
|
|
417
|
-
* name: 'waitForCondition',
|
|
418
|
-
* initial: {
|
|
419
|
-
* article: null,
|
|
420
|
-
* },
|
|
421
|
-
* poller: async (state, {attempt}) => {
|
|
422
|
-
* const client = createClient({
|
|
423
|
-
* apiVersion: '2026-08-05',
|
|
424
|
-
* ...context.clientOptions,
|
|
425
|
-
* })
|
|
426
|
-
* const article = await client.fetch<Article | null>(
|
|
427
|
-
* '*[_type == "article"][0]',
|
|
428
|
-
* )
|
|
429
|
-
* return {...state, article}
|
|
430
|
-
* }
|
|
431
|
-
* next: (state, {attempt}) => {
|
|
432
|
-
* if (state.article) return {shouldResume: false}
|
|
433
|
-
*
|
|
434
|
-
* return {
|
|
435
|
-
* shouldResume: true,
|
|
436
|
-
* delay: {
|
|
437
|
-
* seconds: Math.min(attempt * 2, 60),
|
|
438
|
-
* },
|
|
439
|
-
* }
|
|
440
|
-
* },
|
|
441
|
-
* },
|
|
442
|
-
* )
|
|
443
|
-
* ```
|
|
444
|
-
* @param name - Step name
|
|
445
|
-
* @param initial - Initial state passed to the first poller call.
|
|
446
|
-
* @param poller - Called on each attempt with the current state and condition context.
|
|
447
|
-
* @param next - A function that determines whether the condition has been met or another check should be run.
|
|
448
|
-
* Returns a promise resolving to the updated state,
|
|
449
|
-
* which is passed to next to determine whether the condition has been met or another check should be run.
|
|
450
|
-
* @returns The value returned by the executed function
|
|
451
|
-
*/
|
|
452
|
-
waitForCondition<T>({ name, initial, next, poller, }: {
|
|
453
|
-
name?: string;
|
|
454
|
-
initial: T;
|
|
455
|
-
poller: DurableWaitForConditionPoller<T>;
|
|
456
|
-
next: DurableWaitForConditionNext<T>;
|
|
457
|
-
}): Promise<T>;
|
|
458
|
-
};
|
|
459
|
-
/**
|
|
460
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
461
|
-
* @hidden
|
|
462
|
-
*/
|
|
463
|
-
export type DurableHandler = (envelope: {
|
|
464
|
-
context: DurableContext;
|
|
465
|
-
event?: GenericEvent;
|
|
466
|
-
step: DurableOperations;
|
|
467
|
-
logger: DurableLogger;
|
|
468
|
-
}) => unknown | Promise<unknown>;
|
|
469
|
-
export {};
|
|
1
|
+
export * from './types/context';
|
|
2
|
+
export * from './types/durables';
|
|
3
|
+
export * from './types/functions';
|
|
4
|
+
export * from './types/invocation';
|
|
5
|
+
export * from './types/resources';
|
package/dist/types.js
CHANGED
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA"}
|
package/package.json
CHANGED
package/src/definers.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {DocumentEventHandler, PubSubEventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler} from './types
|
|
1
|
+
import type {DocumentEventHandler, PubSubEventHandler, ScheduledEventHandler, SyncTagInvalidateEventHandler} from './types/functions'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Defines a "document event" function handler.
|
package/src/durables.ts
CHANGED
package/src/invoke.ts
CHANGED
|
@@ -2,7 +2,8 @@ import {Buffer} from 'node:buffer'
|
|
|
2
2
|
import crypto from 'node:crypto'
|
|
3
3
|
import {env} from 'node:process'
|
|
4
4
|
import type awsLite from '@aws-lite/client'
|
|
5
|
-
import type {FunctionPayload, FunctionResourceEnvelope
|
|
5
|
+
import type {FunctionPayload, FunctionResourceEnvelope} from './types/functions'
|
|
6
|
+
import type {InvokeOptions} from './types/invocation'
|
|
6
7
|
|
|
7
8
|
let awsPromise: Promise<awsLite.AwsLiteClient> | undefined
|
|
8
9
|
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type {InvokeCallback} from './invocation'
|
|
2
|
+
import type {ResourcesApi} from './resources'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The context object passed to the function handler.
|
|
6
|
+
*/
|
|
7
|
+
export interface FunctionContext {
|
|
8
|
+
/** The resource type of the event source; resource type that invoked the function. */
|
|
9
|
+
eventResourceType: string
|
|
10
|
+
/** The resource ID of the event source; resource ID that invoked the function. */
|
|
11
|
+
eventResourceId: string
|
|
12
|
+
/** The resource type of the function container; resource type that houses the function. */
|
|
13
|
+
functionResourceType: string
|
|
14
|
+
/** The resource ID of the function container; resource ID that houses the function. */
|
|
15
|
+
functionResourceId: string
|
|
16
|
+
/** Sanity lineage token */
|
|
17
|
+
lineage?: string | undefined
|
|
18
|
+
/**
|
|
19
|
+
* `local` is set to `true` when testing your function locally.
|
|
20
|
+
* i.e. `sanity function test func-name`
|
|
21
|
+
* Otherwise, the property is not set.
|
|
22
|
+
*/
|
|
23
|
+
local?: boolean
|
|
24
|
+
invoke?: InvokeCallback
|
|
25
|
+
/**
|
|
26
|
+
* Options that can be passed to a `@sanity/client` constructor to configure it
|
|
27
|
+
* against the project and dataset which triggered the event. Note that you should
|
|
28
|
+
* always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* Constructing a client with the options from the context:
|
|
32
|
+
* ```ts
|
|
33
|
+
* const client = createClient({
|
|
34
|
+
* apiVersion: '2025-05-01',
|
|
35
|
+
* ...context.clientOptions,
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
clientOptions: {
|
|
40
|
+
apiHost?: string
|
|
41
|
+
dataset: string
|
|
42
|
+
projectId: string
|
|
43
|
+
token: string
|
|
44
|
+
}
|
|
45
|
+
/** Resource interface that allows access to Blueprint Resources */
|
|
46
|
+
resources: ResourcesApi
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The context object passed to the schedule function handler.
|
|
51
|
+
*/
|
|
52
|
+
export interface ScheduledFunctionContext {
|
|
53
|
+
/** Sanity lineage token */
|
|
54
|
+
lineage?: string | undefined
|
|
55
|
+
/**
|
|
56
|
+
* `local` is set to `true` when testing your function locally.
|
|
57
|
+
* i.e. `sanity function test func-name`
|
|
58
|
+
* Otherwise, the property is not set.
|
|
59
|
+
*/
|
|
60
|
+
local?: boolean
|
|
61
|
+
invoke?: InvokeCallback
|
|
62
|
+
/**
|
|
63
|
+
* Options that can be passed to a `@sanity/client` constructor to configure it
|
|
64
|
+
* against the project and dataset which triggered the event. Note that you should
|
|
65
|
+
* always specify an explicit `apiVersion` in YYYY-MM-DD format (e.g. `2025-05-01`).
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* Constructing a client with the options from the context:
|
|
69
|
+
* ```ts
|
|
70
|
+
* const client = createClient({
|
|
71
|
+
* apiVersion: '2025-05-01',
|
|
72
|
+
* ...context.clientOptions,
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
clientOptions?: {
|
|
77
|
+
apiHost?: string
|
|
78
|
+
dataset?: string
|
|
79
|
+
projectId?: string
|
|
80
|
+
token?: string
|
|
81
|
+
}
|
|
82
|
+
/** Resource interface that allows access to Blueprint Resources */
|
|
83
|
+
resources: ResourcesApi
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The context object passed to the sync tag invalidate event handler.
|
|
88
|
+
*/
|
|
89
|
+
export interface SyncTagInvalidateContext extends Omit<FunctionContext, 'clientOptions'> {
|
|
90
|
+
/**
|
|
91
|
+
* A short-lived token that should be used to notify Sanity of sync tag invalidation routine completion. Recommended to use the `done` helper argument provided to the sync tag invalidate event handler instead of this token directly.
|
|
92
|
+
*/
|
|
93
|
+
callbackToken: string
|
|
94
|
+
clientOptions: {
|
|
95
|
+
apiHost: string
|
|
96
|
+
dataset: string
|
|
97
|
+
projectId: string
|
|
98
|
+
/**
|
|
99
|
+
* An API token for use with the Sanity HTTP API. Note that it may be undefined if the user does not explicitly assign a Robot Token to their function definition.
|
|
100
|
+
* @see https://www.sanity.io/docs/blueprints/blueprints-robot-tokens#k8a2a6a24a5c0
|
|
101
|
+
*/
|
|
102
|
+
token?: string
|
|
103
|
+
}
|
|
104
|
+
/** Resource interface that allows access to Blueprint Resources */
|
|
105
|
+
resources: ResourcesApi
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A generic function context type which supports all function types
|
|
110
|
+
*/
|
|
111
|
+
export type GenericContext = FunctionContext | ScheduledFunctionContext | SyncTagInvalidateContext
|