@sanity/functions 1.10.1 → 1.11.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/dist/durables.d.ts +14 -42
- package/dist/durables.js +15 -37
- package/dist/durables.js.map +1 -1
- package/dist/invoke.d.ts +3 -2
- package/dist/invoke.js +22 -10
- package/dist/invoke.js.map +1 -1
- package/dist/types/context.d.ts +3 -1
- package/dist/types/durables.d.ts +97 -117
- package/dist/types/invocation.d.ts +6 -0
- package/package.json +1 -1
- package/src/durables.ts +17 -70
- package/src/invoke.ts +27 -12
- package/src/types/context.ts +3 -1
- package/src/types/durables.ts +117 -116
- package/src/types/invocation.ts +7 -0
package/src/invoke.ts
CHANGED
|
@@ -11,6 +11,13 @@ const PARTITION_KEY = 'arc-app-res'
|
|
|
11
11
|
/** @internal */
|
|
12
12
|
export const MAX_RECURSION_COUNT = 16
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
* Function types that can be invoked asynchronously.
|
|
17
|
+
* TODO: RUN-1849 a durable or queue function with a Content Lake `event` should not be invokeable
|
|
18
|
+
*/
|
|
19
|
+
const ASYNC_INVOKEABLE_TYPES = ['sanity.function.pubsub', 'sanity.function.queue', 'sanity.function.durable']
|
|
20
|
+
|
|
14
21
|
const INVOKE_DOCS_HINT = 'See docs for more information: https://www.sanity.io/docs/functions/function-to-function-invocation#ddd7727d1418'
|
|
15
22
|
|
|
16
23
|
/**
|
|
@@ -112,8 +119,9 @@ export function buildLineageToken(name: string, lineage: string | undefined) {
|
|
|
112
119
|
* Invokes another Sanity Function.
|
|
113
120
|
*
|
|
114
121
|
* By default the invocation is async: the payload is handed off to the function's
|
|
115
|
-
* event source
|
|
116
|
-
* wait for its
|
|
122
|
+
* event source (or, for durable functions, a new durable execution is started) and
|
|
123
|
+
* nothing is returned. Pass `{sync: true}` to invoke the function and wait for its
|
|
124
|
+
* return value; only pubsub functions support this.
|
|
117
125
|
*
|
|
118
126
|
* @param name - Name of the function to invoke
|
|
119
127
|
* @param payload - The `{event, context}` envelope to deliver
|
|
@@ -137,16 +145,17 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
137
145
|
|
|
138
146
|
// Make sure we can invoke this function type
|
|
139
147
|
const contextResource = payload?.context?.resources?.(name)
|
|
140
|
-
|
|
148
|
+
if (contextResource?.type === undefined) {
|
|
149
|
+
throw new Error(`Cannot find or determine function ${name} type!`)
|
|
150
|
+
}
|
|
141
151
|
if (sync === true) {
|
|
142
|
-
if (
|
|
143
|
-
throw new Error(`Function ${name} cannot be invoked synchronously.
|
|
152
|
+
if (contextResource.type !== 'sanity.function.pubsub') {
|
|
153
|
+
throw new Error(`Function ${name} of type ${contextResource.type} cannot be invoked synchronously. ${INVOKE_DOCS_HINT}`)
|
|
144
154
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
throw new Error(`No invokeable resource for function: ${name}`)
|
|
155
|
+
} else if (!ASYNC_INVOKEABLE_TYPES.includes(contextResource.type)) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`Function ${name} of type ${contextResource.type} cannot be invoked asynchronously; only the following function types can be invoked asynchronously: ${ASYNC_INVOKEABLE_TYPES.join(', ')}`,
|
|
158
|
+
)
|
|
150
159
|
}
|
|
151
160
|
|
|
152
161
|
// Local invoke path for Sanity CLI
|
|
@@ -182,12 +191,12 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
182
191
|
}
|
|
183
192
|
|
|
184
193
|
// Async invocation by type
|
|
185
|
-
if (resource.topic &&
|
|
194
|
+
if (resource.topic && contextResource.type === 'sanity.function.pubsub') {
|
|
186
195
|
await aws.SNS.Publish({
|
|
187
196
|
TopicArn: resource.topic.physicalResourceId,
|
|
188
197
|
Message: stringPayload,
|
|
189
198
|
})
|
|
190
|
-
} else if (resource.queue &&
|
|
199
|
+
} else if (resource.queue && contextResource.type === 'sanity.function.queue') {
|
|
191
200
|
const queueUrl = resource.queue.physicalResourceId
|
|
192
201
|
const message: {MessageBody: string; MessageGroupId?: string; QueueUrl: string} = {
|
|
193
202
|
MessageBody: stringPayload,
|
|
@@ -196,6 +205,12 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
196
205
|
// A FIFO queue rejects a message without a `MessageGroupId`
|
|
197
206
|
if (queueUrl.endsWith('.fifo')) message.MessageGroupId = name
|
|
198
207
|
await aws.SQS.SendMessage(message)
|
|
208
|
+
} else if (resource.function && contextResource.type === 'sanity.function.durable') {
|
|
209
|
+
await aws.Lambda.Invoke({
|
|
210
|
+
FunctionName: `${resource.function.physicalResourceId}:live`,
|
|
211
|
+
Payload: outgoingPayload,
|
|
212
|
+
InvocationType: 'Event',
|
|
213
|
+
})
|
|
199
214
|
} else {
|
|
200
215
|
throw new Error(`No invokeable resource for function: ${name}`)
|
|
201
216
|
}
|
package/src/types/context.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {InvokeCallback} from './invocation.js'
|
|
1
|
+
import type {InvokeCallback, InvokedBy} from './invocation.js'
|
|
2
2
|
import type {ResourcesApi} from './resources.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -13,6 +13,8 @@ export interface FunctionContext {
|
|
|
13
13
|
functionResourceType: string
|
|
14
14
|
/** The resource ID of the function container; resource ID that houses the function. */
|
|
15
15
|
functionResourceId: string
|
|
16
|
+
/** Optional info on the invoker of the function */
|
|
17
|
+
invokedBy?: InvokedBy
|
|
16
18
|
/** Sanity lineage token */
|
|
17
19
|
lineage?: string | undefined
|
|
18
20
|
/**
|
package/src/types/durables.ts
CHANGED
|
@@ -4,15 +4,13 @@ import type {BlueprintResource} from './resources.js'
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Durable alias of FunctionsContext
|
|
7
|
-
* @
|
|
8
|
-
* @hidden
|
|
7
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
9
8
|
*/
|
|
10
9
|
export type DurableContext = FunctionContext
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* Logger interface for durables.
|
|
14
|
-
* @
|
|
15
|
-
* @hidden
|
|
13
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
16
14
|
*/
|
|
17
15
|
export interface DurableLogger {
|
|
18
16
|
debug(...params: unknown[]): void
|
|
@@ -23,27 +21,27 @@ export interface DurableLogger {
|
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
/**
|
|
26
|
-
* @
|
|
27
|
-
* @hidden
|
|
24
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
28
25
|
*/
|
|
29
26
|
export interface DurableStepCallbackContext {
|
|
30
27
|
logger: DurableLogger
|
|
31
28
|
}
|
|
32
29
|
|
|
33
30
|
/**
|
|
34
|
-
*
|
|
35
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
36
|
-
* @hidden
|
|
31
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
37
32
|
*/
|
|
38
33
|
export interface DurableStepContext extends DurableStepCallbackContext {
|
|
39
34
|
step: DurableOperations
|
|
40
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Context parameter passed to a branch method of a `step.fanout` execution.
|
|
38
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
39
|
+
*/
|
|
41
40
|
export interface DurableFanoutBranchContext extends DurableStepContext {}
|
|
42
41
|
|
|
43
42
|
/**
|
|
44
43
|
* Context parameter passed to the top-level `durableEventHandler` method.
|
|
45
|
-
* @
|
|
46
|
-
* @hidden
|
|
44
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
47
45
|
*/
|
|
48
46
|
export interface DurableTopLevelContext extends DurableStepContext {
|
|
49
47
|
context: DurableContext
|
|
@@ -52,18 +50,15 @@ export interface DurableTopLevelContext extends DurableStepContext {
|
|
|
52
50
|
|
|
53
51
|
/**
|
|
54
52
|
* Context parameter passed to the `waitForCallback` submitter method.
|
|
55
|
-
* @
|
|
56
|
-
* @hidden
|
|
53
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
57
54
|
*/
|
|
58
55
|
export interface DurableCallbackSubmitterContext extends DurableStepCallbackContext {
|
|
59
56
|
callbackId: string
|
|
60
57
|
}
|
|
61
58
|
|
|
62
59
|
/**
|
|
63
|
-
* Arguments passed to callbacks that execute a durable attempt.
|
|
64
|
-
*
|
|
65
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
66
|
-
* @hidden
|
|
60
|
+
* Arguments passed to `step.run` & `step.waitForCondition` callbacks that execute a durable attempt.
|
|
61
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
67
62
|
*/
|
|
68
63
|
export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
69
64
|
/** Current attempt number, starting at 1. */
|
|
@@ -71,10 +66,9 @@ export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
|
71
66
|
}
|
|
72
67
|
|
|
73
68
|
/**
|
|
74
|
-
* The signature of the duration object passed to the `
|
|
69
|
+
* The signature of the duration object passed to `wait`, `waitForCondition` and `waitForCallback` methods. It is also used when specifying the `retry.delay` of durable steps.
|
|
75
70
|
* It can be specified in days, hours, minutes, or seconds.
|
|
76
|
-
* @
|
|
77
|
-
* @hidden
|
|
71
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
78
72
|
*/
|
|
79
73
|
export type DurableDuration =
|
|
80
74
|
| {days: number; hours?: number; minutes?: number; seconds?: number}
|
|
@@ -83,8 +77,7 @@ export type DurableDuration =
|
|
|
83
77
|
| {seconds: number}
|
|
84
78
|
|
|
85
79
|
/**
|
|
86
|
-
* @
|
|
87
|
-
* @hidden
|
|
80
|
+
* @beta: Using durables is considered experimental and may change in the future.
|
|
88
81
|
*/
|
|
89
82
|
export interface DurableRetryStrategy {
|
|
90
83
|
maxAttempts?: number
|
|
@@ -93,44 +86,45 @@ export interface DurableRetryStrategy {
|
|
|
93
86
|
}
|
|
94
87
|
|
|
95
88
|
/**
|
|
96
|
-
* @
|
|
97
|
-
* @hidden
|
|
89
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
98
90
|
*/
|
|
99
91
|
export type DurableWaitForConditionDecision = {shouldRetry: true; delay: DurableDuration} | {shouldRetry: false}
|
|
100
92
|
|
|
101
93
|
/**
|
|
102
|
-
* @
|
|
103
|
-
* @hidden
|
|
94
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
104
95
|
*/
|
|
105
96
|
export type DurableStepHandler<TArgs extends unknown[], TReturn> = (...args: TArgs) => TReturn
|
|
106
97
|
|
|
107
98
|
/**
|
|
108
|
-
* @
|
|
109
|
-
* @hidden
|
|
99
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
110
100
|
*/
|
|
111
101
|
export type DurableStepRunHandler<T = unknown> = DurableStepHandler<[context: DurableStepAttemptContext], T | Promise<T>>
|
|
112
102
|
|
|
113
103
|
/**
|
|
114
|
-
* @
|
|
115
|
-
* @hidden
|
|
104
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
116
105
|
*/
|
|
117
106
|
export type DurableStepFanoutHandler<T = unknown> = DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>
|
|
118
107
|
|
|
119
108
|
/**
|
|
120
|
-
* @
|
|
121
|
-
|
|
109
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
110
|
+
*/
|
|
111
|
+
export type DurableStepFanoutNamedHandler<T = unknown> = {
|
|
112
|
+
func: DurableStepHandler<[context: DurableFanoutBranchContext], T | Promise<T>>
|
|
113
|
+
name?: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
122
118
|
*/
|
|
123
119
|
export type DurableStepWaitForCallbackSubmitter = DurableStepHandler<[context: DurableCallbackSubmitterContext], Promise<void>>
|
|
124
120
|
|
|
125
121
|
/**
|
|
126
|
-
* @
|
|
127
|
-
* @hidden
|
|
122
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
128
123
|
*/
|
|
129
124
|
export type DurableWaitForConditionPoller<T = unknown> = DurableStepHandler<[state: T, context: DurableStepAttemptContext], Promise<T>>
|
|
130
125
|
|
|
131
126
|
/**
|
|
132
|
-
* @
|
|
133
|
-
* @hidden
|
|
127
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
134
128
|
*/
|
|
135
129
|
export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<
|
|
136
130
|
[state: T, context: Pick<DurableStepAttemptContext, 'attempt'>],
|
|
@@ -138,29 +132,29 @@ export type DurableWaitForConditionNext<T = unknown> = DurableStepHandler<
|
|
|
138
132
|
>
|
|
139
133
|
|
|
140
134
|
/**
|
|
141
|
-
* @
|
|
142
|
-
* @hidden
|
|
135
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
143
136
|
*/
|
|
144
137
|
export type BatchItemSucceededStatus = 'SUCCEEDED'
|
|
138
|
+
|
|
145
139
|
/**
|
|
146
|
-
* @
|
|
140
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
147
141
|
* @hidden
|
|
148
142
|
*/
|
|
149
143
|
export type BatchItemFailedStatus = 'FAILED'
|
|
144
|
+
|
|
150
145
|
/**
|
|
151
|
-
* @
|
|
146
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
152
147
|
* @hidden
|
|
153
148
|
*/
|
|
154
149
|
export type BatchItemStartedStatus = 'STARTED'
|
|
150
|
+
|
|
155
151
|
/**
|
|
156
|
-
* @
|
|
157
|
-
* @hidden
|
|
152
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
158
153
|
*/
|
|
159
154
|
export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus | BatchItemStartedStatus
|
|
155
|
+
|
|
160
156
|
/**
|
|
161
|
-
* Reason why a
|
|
162
|
-
* completed.
|
|
163
|
-
*
|
|
157
|
+
* Reason why a `step.fanout` operation completed.
|
|
164
158
|
* - `ALL_COMPLETED`: every item finished.
|
|
165
159
|
* - `MIN_SUCCESSFUL_REACHED`: CompletionConfig.minSuccessful was reached.
|
|
166
160
|
* - `FAILURE_TOLERANCE_EXCEEDED`: a failure threshold was exceeded.
|
|
@@ -170,9 +164,7 @@ export type BatchItemStatus = BatchItemSucceededStatus | BatchItemFailedStatus |
|
|
|
170
164
|
* - `CUSTOM_COMPLETION_FAILED`: a custom CompletionConfig.shouldComplete
|
|
171
165
|
* predicate signalled completion with a `FAILED` outcome before all items
|
|
172
166
|
* finished.
|
|
173
|
-
* @
|
|
174
|
-
* @hidden
|
|
175
|
-
* @remarks copied from AWS Durable Execution SDK
|
|
167
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
176
168
|
*/
|
|
177
169
|
export type CompletionReason =
|
|
178
170
|
| 'ALL_COMPLETED'
|
|
@@ -180,11 +172,10 @@ export type CompletionReason =
|
|
|
180
172
|
| 'FAILURE_TOLERANCE_EXCEEDED'
|
|
181
173
|
| 'CUSTOM_COMPLETION_SUCCEEDED'
|
|
182
174
|
| 'CUSTOM_COMPLETION_FAILED'
|
|
175
|
+
|
|
183
176
|
/**
|
|
184
177
|
* The result of a `step.fanout` branch.
|
|
185
|
-
* @
|
|
186
|
-
* @hidden
|
|
187
|
-
* @remarks copied from AWS Durable Execution SDK
|
|
178
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
188
179
|
*/
|
|
189
180
|
export interface BatchItem<TResult> {
|
|
190
181
|
/** The result value if the item succeeded */
|
|
@@ -196,10 +187,9 @@ export interface BatchItem<TResult> {
|
|
|
196
187
|
/** Status of the item execution */
|
|
197
188
|
status: BatchItemStatus
|
|
198
189
|
}
|
|
190
|
+
|
|
199
191
|
/**
|
|
200
|
-
* @
|
|
201
|
-
* @hidden
|
|
202
|
-
* @remarks copied from AWS Durable Execution SDK
|
|
192
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
203
193
|
*/
|
|
204
194
|
export interface BatchResult<TResult> {
|
|
205
195
|
/**
|
|
@@ -253,55 +243,58 @@ export interface BatchResult<TResult> {
|
|
|
253
243
|
}
|
|
254
244
|
|
|
255
245
|
/**
|
|
256
|
-
* The interface defining the operations available to a durable function.
|
|
257
|
-
*
|
|
258
|
-
* @alpha Using durables is considered experimental and may change in the future.
|
|
259
|
-
* @hidden
|
|
246
|
+
* The interface defining the durable operations available to a durable function.
|
|
247
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
260
248
|
*/
|
|
261
249
|
export type DurableOperations = {
|
|
262
250
|
/**
|
|
263
|
-
* Runs a named step. Its result is recorded, so if the
|
|
251
|
+
* Runs a named step. Its result is recorded, so if the durable re-runs the step
|
|
264
252
|
* is not executed again. Failures are retried on their own.
|
|
265
|
-
* similar to ctx.step()
|
|
266
|
-
* @remarks cannot be nested inside another step
|
|
267
253
|
* @example
|
|
268
254
|
* ```ts
|
|
269
|
-
* step.run({
|
|
255
|
+
* const hi = await step.run({
|
|
270
256
|
* name: 'run-step',
|
|
271
257
|
* handler: async ({logger}) => {
|
|
272
258
|
* logger.log('Running step')
|
|
259
|
+
* return 'hi'
|
|
273
260
|
* }})
|
|
274
261
|
* ```
|
|
275
262
|
*
|
|
276
263
|
* @example
|
|
277
264
|
* ```ts
|
|
278
265
|
* // With a retry config
|
|
279
|
-
* step.run({
|
|
266
|
+
* const hello = await step.run({
|
|
280
267
|
* name: 'run-with-retry'
|
|
281
|
-
* handler: () => {
|
|
268
|
+
* handler: () => {
|
|
269
|
+
* if (Math.random() > 0.5) throw new Error('random error!')
|
|
270
|
+
* return 'hello'
|
|
271
|
+
* }
|
|
282
272
|
* retry: { maxAttempts: 2, delay: { seconds: 5 } }
|
|
283
273
|
* })
|
|
284
274
|
* ```
|
|
285
275
|
* @param name - Step name
|
|
286
276
|
* @param handler - function being executed
|
|
287
277
|
* @param retry - retry configuration
|
|
288
|
-
* @returns The value returned by the
|
|
278
|
+
* @returns The value returned by the provided handler
|
|
289
279
|
*/
|
|
290
280
|
run<T>({name, handler, retry}: {name?: string; handler: DurableStepRunHandler<T>; retry?: DurableRetryStrategy}): Promise<T>
|
|
291
281
|
|
|
292
282
|
/**
|
|
293
|
-
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
283
|
+
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available. Branches may be named.
|
|
294
284
|
* @remarks By using the generic, you can set a single, homogeneous return type for all branches.
|
|
295
285
|
* @example
|
|
296
286
|
* ```ts
|
|
297
|
-
* step.fanout<string>({
|
|
287
|
+
* const results: BatchResult<string> = await step.fanout<string>({
|
|
298
288
|
* name: 'parallel-runs',
|
|
299
289
|
* branches: [async ({step, logger}) => {
|
|
300
290
|
* logger.log('Running first function in parallel')
|
|
301
291
|
* return 'first'
|
|
302
|
-
* },
|
|
303
|
-
*
|
|
304
|
-
*
|
|
292
|
+
* }, {
|
|
293
|
+
* func: async ({step, logger}) => {
|
|
294
|
+
* logger.log('Running second function in parallel')
|
|
295
|
+
* return 'second'
|
|
296
|
+
* },
|
|
297
|
+
* name: 'second-branch',
|
|
305
298
|
* }],
|
|
306
299
|
* })
|
|
307
300
|
* ```
|
|
@@ -309,21 +302,27 @@ export type DurableOperations = {
|
|
|
309
302
|
* @param branches - functions being executed in parallel
|
|
310
303
|
* @returns A batch result of values returned or errors thrown by all branches.
|
|
311
304
|
*/
|
|
312
|
-
fanout<T>({
|
|
305
|
+
fanout<T>({
|
|
306
|
+
name,
|
|
307
|
+
branches,
|
|
308
|
+
}: {
|
|
309
|
+
name?: string
|
|
310
|
+
branches: (DurableStepFanoutHandler<T> | DurableStepFanoutNamedHandler<T>)[]
|
|
311
|
+
}): Promise<BatchResult<T>>
|
|
313
312
|
/**
|
|
314
313
|
* Runs a set of steps, or branches, in parallel. Each branch contains its own child durable context, with all usual `step` operations available.
|
|
315
314
|
* @remarks This overload provides automatic type inference for heterogeneous branch return types. When branches return different types, the result will be `BatchResult<T1 | T2 | ...>`.
|
|
316
315
|
* Use the explicit type parameter overloads above for strict homogeneous type checking.
|
|
317
316
|
* @example
|
|
318
317
|
* ```ts
|
|
319
|
-
* step.fanout
|
|
318
|
+
* const results: BatchResult<string | number> = await step.fanout({
|
|
320
319
|
* name: 'parallel-runs',
|
|
321
320
|
* branches: [async ({step, logger}) => {
|
|
322
321
|
* logger.log('Running first function in parallel')
|
|
323
322
|
* return 'first'
|
|
324
323
|
* }, async ({step, logger}) => {
|
|
325
324
|
* logger.log('Running second function in parallel')
|
|
326
|
-
* return
|
|
325
|
+
* return 2
|
|
327
326
|
* }],
|
|
328
327
|
* })
|
|
329
328
|
* ```
|
|
@@ -337,30 +336,36 @@ export type DurableOperations = {
|
|
|
337
336
|
}: {
|
|
338
337
|
name?: string
|
|
339
338
|
branches: Branches
|
|
340
|
-
}): Promise<
|
|
339
|
+
}): Promise<
|
|
340
|
+
BatchResult<
|
|
341
|
+
Branches[number] extends DurableStepFanoutHandler<infer ReturnType>
|
|
342
|
+
? ReturnType
|
|
343
|
+
: Branches[number] extends DurableStepFanoutNamedHandler<infer ReturnType>
|
|
344
|
+
? ReturnType
|
|
345
|
+
: never
|
|
346
|
+
>
|
|
347
|
+
>
|
|
341
348
|
|
|
342
349
|
/**
|
|
343
|
-
*
|
|
344
|
-
* similar to ctx.invoke()
|
|
345
|
-
* @remarks cannot be nested inside another step
|
|
350
|
+
* Invokes another function, of type durable or pubsub, and awaits its result. While awaiting, the durable function shuts down, so you are not charged for compute.
|
|
346
351
|
* @example
|
|
347
352
|
* ```ts
|
|
348
353
|
* // my-delegated-function/index.ts
|
|
349
|
-
* export const handler = pubSubEventHandler({event}) => {
|
|
350
|
-
*
|
|
351
|
-
* }
|
|
354
|
+
* export const handler = pubSubEventHandler(({event}) => {
|
|
355
|
+
* return 'yo'
|
|
356
|
+
* })
|
|
352
357
|
*
|
|
353
358
|
* // my-resource/index.ts
|
|
354
|
-
* export const handler = pubSubEventHandler({event}) => {
|
|
355
|
-
*
|
|
356
|
-
* }
|
|
359
|
+
* export const handler = pubSubEventHandler(({event}) => {
|
|
360
|
+
* return 'hey'
|
|
361
|
+
* })
|
|
357
362
|
*
|
|
358
363
|
* // my-durable-function/index.ts
|
|
359
|
-
* durableEventHandler(({step, context}) => {
|
|
364
|
+
* export const handler = durableEventHandler(({step, context}) => {
|
|
360
365
|
* // With a function name
|
|
361
|
-
* step.delegate({name: '
|
|
366
|
+
* const firstResult = await step.delegate({name: 'first-delegation', handler: 'my-delegated-function', event: {data: {hello: 'world'}}})
|
|
362
367
|
* // With a resource object
|
|
363
|
-
* step.delegate({name: '
|
|
368
|
+
* const secondResult = await step.delegate({name: 'second-delegation', handler: context.resource.function('my-resource'), event: {data: {hello: 'world'}}})
|
|
364
369
|
* })
|
|
365
370
|
* ```
|
|
366
371
|
* @param name - Step name
|
|
@@ -380,13 +385,16 @@ export type DurableOperations = {
|
|
|
380
385
|
}): Promise<T>
|
|
381
386
|
|
|
382
387
|
/**
|
|
383
|
-
* Generates a unique callback ID, runs a
|
|
388
|
+
* Generates a unique callback ID, runs a submitter function to pass the ID to an external system and waits for some external system to POST JSON data to a Sanity Durable callback API using the callback ID.
|
|
384
389
|
* @example
|
|
385
|
-
* Wait for 30 seconds
|
|
386
390
|
* ```ts
|
|
387
|
-
*
|
|
388
|
-
* await
|
|
389
|
-
*
|
|
391
|
+
* try {
|
|
392
|
+
* const successfulResult = await step.waitForCallback({name: 'ask-for-permission', submitter: async ({callbackId}) => {
|
|
393
|
+
* await sendForApproval(callbackId)
|
|
394
|
+
* }})
|
|
395
|
+
* } catch (e) {
|
|
396
|
+
* logger.log('oh no we received a callback failure', e)
|
|
397
|
+
* }
|
|
390
398
|
* ```
|
|
391
399
|
* ```
|
|
392
400
|
* @param name - Step name
|
|
@@ -409,8 +417,6 @@ export type DurableOperations = {
|
|
|
409
417
|
|
|
410
418
|
/**
|
|
411
419
|
* Waits for a specified duration
|
|
412
|
-
* similar to ctx.wait()
|
|
413
|
-
* @remarks cannot be nested inside another step
|
|
414
420
|
* @example
|
|
415
421
|
* Wait for 30 seconds
|
|
416
422
|
* ```ts
|
|
@@ -422,35 +428,31 @@ export type DurableOperations = {
|
|
|
422
428
|
* ```
|
|
423
429
|
* @param name - Step name
|
|
424
430
|
* @param duration - Amount of time the function will wait before continuing.
|
|
425
|
-
* Duration is an object with properties of `seconds`, `minutes`, `hours`, or `days`. Must be at least {seconds: 1}
|
|
426
431
|
* @returns Resolves after the duration
|
|
427
432
|
*/
|
|
428
433
|
wait({name, duration}: {name?: string; duration: DurableDuration}): Promise<void>
|
|
429
434
|
|
|
430
435
|
/**
|
|
431
|
-
* Waits for a specified condition to be met
|
|
432
|
-
* similar to ctx.waitForCondition()
|
|
433
|
-
* @remarks cannot be nested inside another step
|
|
436
|
+
* Waits for a specified condition to be met by executing a `poller` method to retrieve state and then inspecting the retrieved state in a `next` method to determine whether to retry and after what delay to do so.
|
|
434
437
|
* @example
|
|
435
438
|
* ```ts
|
|
436
439
|
* await step.waitForCondition({{
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
440
|
+
* name: 'waitForCondition',
|
|
441
|
+
* initial: {
|
|
442
|
+
* article: null as Article | null,
|
|
443
|
+
* },
|
|
444
|
+
* poller: async (state, {attempt}) => {
|
|
445
|
+
* const client = createClient({
|
|
446
|
+
* apiVersion: '2026-08-05',
|
|
447
|
+
* ...context.clientOptions,
|
|
448
|
+
* })
|
|
449
|
+
* const article = await client.fetch<Article | null>(
|
|
450
|
+
* '*[_type == "article"][0]',
|
|
451
|
+
* )
|
|
452
|
+
* return {...state, article}
|
|
453
|
+
* },
|
|
454
|
+
* next: (state, {attempt}) => {
|
|
455
|
+
* if (state.article) return {shouldRetry: false}
|
|
454
456
|
* return {
|
|
455
457
|
* shouldRetry: true,
|
|
456
458
|
* delay: {
|
|
@@ -483,7 +485,6 @@ export type DurableOperations = {
|
|
|
483
485
|
}
|
|
484
486
|
|
|
485
487
|
/**
|
|
486
|
-
* @
|
|
487
|
-
* @hidden
|
|
488
|
+
* @beta Using durables is considered experimental and may change in the future.
|
|
488
489
|
*/
|
|
489
490
|
export type DurableHandler = (envelope: DurableTopLevelContext) => unknown | Promise<unknown>
|
package/src/types/invocation.ts
CHANGED