@sanity/functions 1.3.1 → 1.5.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
@@ -1,7 +1,18 @@
1
+ /**
2
+ * Callback provided by Sanity CLI when doing local development
3
+ */
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
+ }
13
+
1
14
  /**
2
15
  * The context object passed to the function handler.
3
- *
4
- * @beta
5
16
  */
6
17
  export interface FunctionContext {
7
18
  /** The resource type of the event source; resource type that invoked the function. */
@@ -12,12 +23,17 @@ export interface FunctionContext {
12
23
  functionResourceType: string
13
24
  /** The resource ID of the function container; resource ID that houses the function. */
14
25
  functionResourceId: string
26
+ /**
27
+ * uniqueId for a pipeline run
28
+ */
29
+ runId?: string
15
30
  /**
16
31
  * `local` is set to `true` when testing your function locally.
17
32
  * i.e. `sanity function test func-name`
18
33
  * Otherwise, the property is not set.
19
34
  */
20
35
  local?: boolean
36
+ invoke?: InvokeCallback
21
37
  /**
22
38
  * Options that can be passed to a `@sanity/client` constructor to configure it
23
39
  * against the project and dataset which triggered the event. Note that you should
@@ -31,7 +47,6 @@ export interface FunctionContext {
31
47
  * ...context.clientOptions,
32
48
  * })
33
49
  * ```
34
- * @beta
35
50
  */
36
51
  clientOptions: {
37
52
  apiHost?: string
@@ -39,12 +54,12 @@ export interface FunctionContext {
39
54
  projectId: string
40
55
  token: string
41
56
  }
57
+ /** Resource interface that allows access to Blueprint Resources */
58
+ resources: ResourcesApi
42
59
  }
43
60
 
44
61
  /**
45
62
  * The context object passed to the schedule function handler.
46
- *
47
- * @beta
48
63
  */
49
64
  export interface ScheduledFunctionContext {
50
65
  /**
@@ -53,6 +68,7 @@ export interface ScheduledFunctionContext {
53
68
  * Otherwise, the property is not set.
54
69
  */
55
70
  local?: boolean
71
+ invoke?: InvokeCallback
56
72
  /**
57
73
  * Options that can be passed to a `@sanity/client` constructor to configure it
58
74
  * against the project and dataset which triggered the event. Note that you should
@@ -66,7 +82,6 @@ export interface ScheduledFunctionContext {
66
82
  * ...context.clientOptions,
67
83
  * })
68
84
  * ```
69
- * @beta
70
85
  */
71
86
  clientOptions?: {
72
87
  apiHost?: string
@@ -74,35 +89,29 @@ export interface ScheduledFunctionContext {
74
89
  projectId?: string
75
90
  token?: string
76
91
  }
92
+ /** Resource interface that allows access to Blueprint Resources */
93
+ resources: ResourcesApi
77
94
  }
78
95
 
79
96
  /**
80
97
  * The event object received by the function handler in the case of a document event,
81
98
  * such as a publish, unpublish, delete or mutation event and similar.
82
- *
83
- * @beta
84
99
  */
85
100
  export interface DocumentEvent<IData = any> {
86
101
  /**
87
102
  * The data delivered to the function. This is the result of applying any configured
88
103
  * GROQ-projection to the changed document. If no projection is configured, this is
89
104
  * the document itself.
90
- *
91
- * @beta
92
105
  */
93
106
  data: IData
94
107
  }
95
108
 
96
109
  /**
97
110
  * The event object received by the function handler in the case of a sync-tag-invalidate event.
98
- *
99
- * @beta
100
111
  */
101
112
  export interface SyncTagInvalidateEvent {
102
113
  /**
103
114
  * The sync tags for use with cache invalidation and notifying the callback endpoint once tags are invalidated.
104
- *
105
- * @beta
106
115
  */
107
116
  data: {
108
117
  /** Array of sync tags to be invalidated. */
@@ -112,29 +121,45 @@ export interface SyncTagInvalidateEvent {
112
121
 
113
122
  /**
114
123
  * A function handler for a document event.
115
- *
116
- * @beta
117
124
  */
118
125
  export type DocumentEventHandler<IData = any> = (envelope: {context: FunctionContext; event: DocumentEvent<IData>}) => void | Promise<void>
119
126
 
120
127
  /**
121
128
  * A function handler for a schedule event.
122
- *
123
- * @beta
124
129
  */
125
130
  export type ScheduledEventHandler = (envelope: {context: ScheduledFunctionContext}) => void | Promise<void>
126
131
 
127
132
  /**
128
- * A callback function to invoke once a sync-tag-invalidate event has been processed. Signals to Sanity that sync tag invalidation has completed.
133
+ * A generic function context type which supports all function types
134
+ */
135
+ export type GenericContext = FunctionContext | ScheduledFunctionContext | SyncTagInvalidateContext
136
+
137
+ /**
138
+ * A generic function event type which supports all function types
139
+ */
140
+ export type GenericEvent = DocumentEvent | SyncTagInvalidateEvent
141
+
142
+ /**
143
+ * A generic function handler that can receive the payload of any function type.
129
144
  *
130
- * @beta
145
+ * The envelope is intentionally permissive so a single handler can be registered
146
+ * regardless of the event source without narrowing: `context` is any supported
147
+ * context, `event` is any supported event, and `done` is optional (present only
148
+ * for sync-tag-invalidate events).
149
+ */
150
+ export type EventHandler<IData = any> = (envelope: {
151
+ context: GenericContext
152
+ event: DocumentEvent<IData> | SyncTagInvalidateEvent
153
+ done?: SyncTagInvalidateCallback
154
+ }) => void | Promise<void>
155
+
156
+ /**
157
+ * A callback function to invoke once a sync-tag-invalidate event has been processed. Signals to Sanity that sync tag invalidation has completed.
131
158
  */
132
159
  export type SyncTagInvalidateCallback = (syncTags: string[]) => Promise<Response>
133
160
 
134
161
  /**
135
162
  * The context object passed to the sync tag invalidate event handler.
136
- *
137
- * @beta
138
163
  */
139
164
  export interface SyncTagInvalidateContext extends Omit<FunctionContext, 'clientOptions'> {
140
165
  /**
@@ -151,14 +176,110 @@ export interface SyncTagInvalidateContext extends Omit<FunctionContext, 'clientO
151
176
  */
152
177
  token?: string
153
178
  }
179
+ /** Resource interface that allows access to Blueprint Resources */
180
+ resources: ResourcesApi
154
181
  }
155
182
  /**
156
183
  * A function handler for a sync-tag-invalidate event.
157
- *
158
- * @beta
159
184
  */
160
185
  export type SyncTagInvalidateEventHandler = (envelope: {
161
186
  context: SyncTagInvalidateContext
162
187
  event: SyncTagInvalidateEvent
163
188
  done: SyncTagInvalidateCallback
164
189
  }) => void | Promise<void>
190
+
191
+ /**
192
+ * An interface to describe resources found in a Blueprint
193
+ */
194
+ export interface BlueprintResource {
195
+ id: string
196
+ name: string
197
+ type: string
198
+ }
199
+ export interface ResourcesApi {
200
+ /** Cross-type lookup by name: `context.resources('my-proj')`. */
201
+ (name: string): BlueprintResource | undefined
202
+ /** Flat array of every resource across types. */
203
+ all(): BlueprintResource[]
204
+ /** Iterate every resource: `for (const r of context.resources)`. */
205
+ [Symbol.iterator](): IterableIterator<BlueprintResource>
206
+ /** Type specific lookup by name: `context.cors('my-cors')` */
207
+ cors(name: string): BlueprintResource | undefined
208
+ dataset(name: string): BlueprintResource | undefined
209
+ function(name: string): BlueprintResource | undefined
210
+ project(name: string): BlueprintResource | undefined
211
+ role(name: string): BlueprintResource | undefined
212
+ webhook(name: string): BlueprintResource | undefined
213
+ }
214
+
215
+ /**
216
+ * The payload for the `invoke` method
217
+ */
218
+ export type FunctionPayload = {
219
+ event: GenericEvent
220
+ context: GenericContext
221
+ }
222
+
223
+ /**
224
+ * The definition of a function resource in our resource discovery table
225
+ */
226
+ export type FunctionResource = {
227
+ logicalResourceId: string
228
+ physicalResourceId: string
229
+ }
230
+
231
+ type FunctionResourceKey = 'eventsourcemapping' | 'function' | 'parameter' | 'queue' | 'schedule' | 'subscription' | 'topic'
232
+
233
+ export type FunctionResourceEnvelope = {
234
+ [K in FunctionResourceKey]-?: {[P in K]: FunctionResource} & {[P in Exclude<FunctionResourceKey, K>]?: FunctionResource}
235
+ }[FunctionResourceKey]
236
+
237
+ /**
238
+ * The interface defining the operations available to a pipeline function.
239
+ * 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
242
+ * @hidden
243
+ */
244
+ export interface PipelineOperations {
245
+ /**
246
+ * Runs a named step. Its result is recorded, so if the workflow re-runs the step
247
+ * is not executed again. Failures are retried on their own.
248
+ * similar to ctx.step()
249
+ * @remarks cannot be nested inside another step
250
+ * @param name - Step name
251
+ * @param fn - function being executed
252
+ * @returns The value returned by the executed function
253
+ */
254
+ run<T>(name: string, fn: () => T | Promise<T>): Promise<T>
255
+ /**
256
+ * Calls another function and awaits its result.
257
+ * similar to ctx.invoke()
258
+ * @remarks cannot be nested inside another step
259
+ * @param name - Step name
260
+ * @param input - Data passed to the called function
261
+ * @returns The value returned by the called function
262
+ */
263
+ delegate<T>(name: string, input?: unknown): Promise<T>
264
+
265
+ /**
266
+ * Waits for an external callback
267
+ * similar to ctx.waitForCallback()
268
+ * @remarks cannot be nested inside another step
269
+ * @param name - Step name
270
+ * @param fn -Receives the generated callbackId and returns a delivered promise
271
+ * @returns The value returned by the executed function
272
+ */
273
+ waitForCallback<T>(name: string, fn: (callbackId: string) => Promise<void>): Promise<T>
274
+ // @todo: other methods wanted for durables
275
+ }
276
+
277
+ /**
278
+ * @alpha Using pipeline functions is considered experimental and may change in the future.
279
+ * @hidden
280
+ */
281
+ export type PipelineHandler = (envelope: {
282
+ context: FunctionContext
283
+ event?: GenericEvent
284
+ step: PipelineOperations
285
+ }) => unknown | Promise<unknown>