@sanity/blueprints 0.23.0 → 0.23.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.
Files changed (47) hide show
  1. package/dist/definers/functions/base-event.d.ts +2 -0
  2. package/dist/definers/functions/base-event.js +2 -0
  3. package/dist/definers/functions/base-event.js.map +1 -0
  4. package/dist/definers/functions/document.d.ts +26 -0
  5. package/dist/definers/functions/document.js +43 -0
  6. package/dist/definers/functions/document.js.map +1 -0
  7. package/dist/definers/functions/event.d.ts +20 -0
  8. package/dist/definers/functions/event.js +30 -0
  9. package/dist/definers/functions/event.js.map +1 -0
  10. package/dist/definers/functions/index.d.ts +15 -0
  11. package/dist/definers/functions/index.js +34 -0
  12. package/dist/definers/functions/index.js.map +1 -0
  13. package/dist/definers/functions/media-library.d.ts +23 -0
  14. package/dist/definers/functions/media-library.js +83 -0
  15. package/dist/definers/functions/media-library.js.map +1 -0
  16. package/dist/definers/functions/pipeline.d.ts +26 -0
  17. package/dist/definers/functions/pipeline.js +41 -0
  18. package/dist/definers/functions/pipeline.js.map +1 -0
  19. package/dist/definers/functions/queue.d.ts +30 -0
  20. package/dist/definers/functions/queue.js +47 -0
  21. package/dist/definers/functions/queue.js.map +1 -0
  22. package/dist/definers/functions/scheduled.d.ts +46 -0
  23. package/dist/definers/functions/scheduled.js +91 -0
  24. package/dist/definers/functions/scheduled.js.map +1 -0
  25. package/dist/definers/functions/sync-tag-invalidate.d.ts +31 -0
  26. package/dist/definers/functions/sync-tag-invalidate.js +43 -0
  27. package/dist/definers/functions/sync-tag-invalidate.js.map +1 -0
  28. package/dist/index.d.ts +8 -1
  29. package/dist/index.js +8 -1
  30. package/dist/index.js.map +1 -1
  31. package/dist/types/functions/index.d.ts +51 -31
  32. package/package.json +1 -1
  33. package/src/definers/functions/base-event.ts +4 -0
  34. package/src/definers/functions/document.ts +129 -0
  35. package/src/definers/functions/event.ts +32 -0
  36. package/src/definers/functions/index.ts +39 -0
  37. package/src/definers/functions/media-library.ts +98 -0
  38. package/src/definers/functions/pipeline.ts +42 -0
  39. package/src/definers/functions/queue.ts +49 -0
  40. package/src/definers/functions/scheduled.ts +112 -0
  41. package/src/definers/functions/sync-tag-invalidate.ts +51 -0
  42. package/src/index.ts +8 -1
  43. package/src/types/functions/index.ts +64 -43
  44. package/dist/definers/functions.d.ts +0 -210
  45. package/dist/definers/functions.js +0 -381
  46. package/dist/definers/functions.js.map +0 -1
  47. package/src/definers/functions.ts +0 -540
@@ -0,0 +1,43 @@
1
+ import { validateSyncTagInvalidateFunction, } from '../../index.js';
2
+ import { runValidation } from '../../utils/validation.js';
3
+ import { defineFunction } from './index.js';
4
+ /**
5
+ * Defines a function that is triggered on a sync tag invalidate event.
6
+ *
7
+ * @remarks
8
+ * Using implicit event scoping to all datasets in the function project:
9
+ * ```ts
10
+ * defineSyncTagInvalidateFunction({
11
+ * name: 'bustin-caches',
12
+ * })
13
+ * ```
14
+ *
15
+ * Using explicit event scoping to a particular dataset in the function project:
16
+ * ```ts
17
+ * defineSyncTagInvalidateFunction({
18
+ * name: 'bustin-caches',
19
+ * event: {
20
+ * resource: {
21
+ * type: 'dataset',
22
+ * id: 'myProj.myDataset',
23
+ * },
24
+ * },
25
+ * })
26
+ * ```
27
+ * @public
28
+ * @category Definers
29
+ * @expandType BlueprintSyncTagInvalidateFunctionConfig
30
+ * @param functionConfig The configuration for the sync tag invalidate function
31
+ * @returns The validated sync tag invalidate function resource
32
+ */
33
+ export function defineSyncTagInvalidateFunction(functionConfig) {
34
+ const { event } = functionConfig;
35
+ const functionResource = {
36
+ ...defineFunction(functionConfig, { skipValidation: true }),
37
+ type: 'sanity.function.sync-tag-invalidate',
38
+ ...(event?.resource ? { event } : {}),
39
+ };
40
+ runValidation(() => validateSyncTagInvalidateFunction(functionResource));
41
+ return functionResource;
42
+ }
43
+ //# sourceMappingURL=sync-tag-invalidate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-tag-invalidate.js","sourceRoot":"","sources":["../../../src/definers/functions/sync-tag-invalidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,iCAAiC,GAClC,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAA;AACzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,+BAA+B,CAC7C,cAAwD;IAExD,MAAM,EAAC,KAAK,EAAC,GAAG,cAAc,CAAA;IAE9B,MAAM,gBAAgB,GAA+C;QACnE,GAAG,cAAc,CAAC,cAAc,EAAE,EAAC,cAAc,EAAE,IAAI,EAAC,CAAC;QACzD,IAAI,EAAE,qCAAqC;QAC3C,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAC,KAAK,EAAC,CAAC,CAAC,CAAC,EAAE,CAAC;KACpC,CAAA;IAED,aAAa,CAAC,GAAG,EAAE,CAAC,iCAAiC,CAAC,gBAAgB,CAAC,CAAC,CAAA;IAExE,OAAO,gBAAgB,CAAA;AACzB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -9,7 +9,14 @@
9
9
  export * from './definers/blueprints.js';
10
10
  export * from './definers/cors.js';
11
11
  export * from './definers/datasets.js';
12
- export * from './definers/functions.js';
12
+ export * from './definers/functions/document.js';
13
+ export * from './definers/functions/event.js';
14
+ export * from './definers/functions/index.js';
15
+ export * from './definers/functions/media-library.js';
16
+ export * from './definers/functions/pipeline.js';
17
+ export * from './definers/functions/queue.js';
18
+ export * from './definers/functions/scheduled.js';
19
+ export * from './definers/functions/sync-tag-invalidate.js';
13
20
  export * from './definers/projects.js';
14
21
  export * from './definers/resources.js';
15
22
  export * from './definers/robots.js';
package/dist/index.js CHANGED
@@ -10,7 +10,14 @@
10
10
  export * from './definers/blueprints.js';
11
11
  export * from './definers/cors.js';
12
12
  export * from './definers/datasets.js';
13
- export * from './definers/functions.js';
13
+ export * from './definers/functions/document.js';
14
+ export * from './definers/functions/event.js';
15
+ export * from './definers/functions/index.js';
16
+ export * from './definers/functions/media-library.js';
17
+ export * from './definers/functions/pipeline.js';
18
+ export * from './definers/functions/queue.js';
19
+ export * from './definers/functions/scheduled.js';
20
+ export * from './definers/functions/sync-tag-invalidate.js';
14
21
  export * from './definers/projects.js';
15
22
  export * from './definers/resources.js';
16
23
  export * from './definers/robots.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD;;;GAGG;AACH;;;GAGG;AACH,cAAc,0BAA0B,CAAA;AACxC,cAAc,oBAAoB,CAAA;AAClC,cAAc,wBAAwB,CAAA;AACtC,cAAc,yBAAyB,CAAA;AACvC,cAAc,wBAAwB,CAAA;AACtC,cAAc,yBAAyB,CAAA;AACvC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,wBAAwB,CAAA;AAEtC,uCAAuC;AACvC;;;GAGG;AACH;;;GAGG;AACH,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,qBAAqB,CAAA;AACnC,cAAc,sBAAsB,CAAA;AACpC,cAAc,mBAAmB,CAAA;AACjC,cAAc,wBAAwB,CAAA;AACtC,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AAEnC,4CAA4C;AAC5C;;;GAGG;AACH,cAAc,4BAA4B,CAAA;AAC1C,cAAc,sBAAsB,CAAA;AACpC,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA;AACzC,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AACtC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,uBAAuB,CAAA;AACrC,cAAc,yBAAyB,CAAA;AACvC,cAAc,0BAA0B,CAAA;AAExC,yEAAyE;AACzE;;;GAGG"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD;;;GAGG;AACH;;;GAGG;AACH,cAAc,0BAA0B,CAAA;AACxC,cAAc,oBAAoB,CAAA;AAClC,cAAc,wBAAwB,CAAA;AACtC,cAAc,kCAAkC,CAAA;AAChD,cAAc,+BAA+B,CAAA;AAC7C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,uCAAuC,CAAA;AACrD,cAAc,kCAAkC,CAAA;AAChD,cAAc,+BAA+B,CAAA;AAC7C,cAAc,mCAAmC,CAAA;AACjD,cAAc,6CAA6C,CAAA;AAC3D,cAAc,wBAAwB,CAAA;AACtC,cAAc,yBAAyB,CAAA;AACvC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,wBAAwB,CAAA;AAEtC,uCAAuC;AACvC;;;GAGG;AACH;;;GAGG;AACH,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,qBAAqB,CAAA;AACnC,cAAc,sBAAsB,CAAA;AACpC,cAAc,mBAAmB,CAAA;AACjC,cAAc,wBAAwB,CAAA;AACtC,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AAEnC,4CAA4C;AAC5C;;;GAGG;AACH,cAAc,4BAA4B,CAAA;AAC1C,cAAc,sBAAsB,CAAA;AACpC,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA;AACzC,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AACtC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,uBAAuB,CAAA;AACrC,cAAc,yBAAyB,CAAA;AACvC,cAAc,0BAA0B,CAAA;AAExC,yEAAyE;AACzE;;;GAGG"}
@@ -35,6 +35,43 @@ interface BlueprintCommonFunctionResource extends BlueprintResource {
35
35
  */
36
36
  runtime?: FunctionRuntimes;
37
37
  }
38
+ /**
39
+ * Configuration used for Queue or Workflow function types.
40
+ * @todo: not implemented
41
+ * @category Functions Types
42
+ * @alpha
43
+ * @hidden
44
+ */
45
+ interface QueueConfig {
46
+ /**
47
+ * Maximum number of concurrent invocations in progress from queue to Function
48
+ * @todo: not implemented, what is the default anyways?
49
+ */
50
+ concurrency?: number;
51
+ /**
52
+ * Debounce window in seconds
53
+ * @todo: not implemented - and should this always be provided with debounceKey?
54
+ */
55
+ debounce?: number;
56
+ /**
57
+ * Path used to group debounced events, e.g. 'document._id'
58
+ * @todo: not implemented - and should this always be provided with debounce window?
59
+ */
60
+ debounceKey?: string;
61
+ /**
62
+ * Whether to place messages that failed processing into a Dead Letter Queue.
63
+ * @todo: not implemented
64
+ * @default `false`
65
+ */
66
+ dlq?: boolean;
67
+ /**
68
+ * By default, queues employ at-least once delivery. When set to `true`, employs first-in-first-out ordering and exactly-once delivery.
69
+ * FIFO queues have a lower transaction-per-second limit.
70
+ * @todo: not implemented
71
+ * @default `false`
72
+ */
73
+ fifo?: boolean;
74
+ }
38
75
  /**
39
76
  * Base function resource with common properties for all function types that can belong to projects.
40
77
  * @category Functions Types
@@ -87,7 +124,7 @@ export interface BlueprintSyncTagInvalidateFunctionResource extends BlueprintBas
87
124
  * @alpha
88
125
  * @hidden
89
126
  */
90
- export interface BlueprintQueueFunctionResource extends BlueprintBaseFunctionResource {
127
+ export interface BlueprintQueueFunctionResource extends BlueprintBaseFunctionResource, QueueConfig {
91
128
  type: 'sanity.function.queue';
92
129
  /** Optional event configuration that triggers the function */
93
130
  event?: BlueprintFunctionResourceEvent;
@@ -101,6 +138,18 @@ export interface BlueprintQueueFunctionResource extends BlueprintBaseFunctionRes
101
138
  export interface BlueprintEventFunctionResource extends BlueprintBaseFunctionResource {
102
139
  type: 'sanity.function.event';
103
140
  }
141
+ /**
142
+ * A durable, step-based pipeline function.
143
+ * @public
144
+ * @alpha Deploying Pipeline Functions via Blueprints is experimental. This feature is not available publicly yet.
145
+ * @hidden
146
+ * @category Functions Types
147
+ * @interface
148
+ */
149
+ export interface BlueprintPipelineResource extends BlueprintBaseFunctionResource, QueueConfig {
150
+ type: 'sanity.function.pipeline';
151
+ event?: BlueprintFunctionResourceEvent;
152
+ }
104
153
  /**
105
154
  * Configuration for defining a base function.
106
155
  * @internal
@@ -187,7 +236,7 @@ export type BlueprintSyncTagInvalidateFunctionConfig = Omit<BlueprintSyncTagInva
187
236
  * @category Functions Types
188
237
  * @interface
189
238
  */
190
- export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event'> & {
239
+ export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource, 'type' | 'src' | 'event'> & QueueConfig & {
191
240
  /**
192
241
  * Path to the function source code
193
242
  * @defaultValue `functions/${name}`
@@ -195,9 +244,6 @@ export type BlueprintQueueFunctionConfig = Omit<BlueprintQueueFunctionResource,
195
244
  src?: string;
196
245
  /** Optional event configuration that triggers the queue function */
197
246
  event?: BlueprintFunctionResourceEvent;
198
- concurrency?: number;
199
- fifo?: boolean;
200
- dlq?: boolean;
201
247
  };
202
248
  /**
203
249
  * Configuration for defining an event function.
@@ -214,32 +260,6 @@ export type BlueprintEventFunctionConfig = Omit<BlueprintEventFunctionResource,
214
260
  */
215
261
  src?: string;
216
262
  };
217
- /**
218
- * A durable, step-based pipeline function.
219
- * @public
220
- * @alpha Deploying Pipeline Functions via Blueprints is experimental. This feature is not available publicly yet.
221
- * @hidden
222
- * @category Functions Types
223
- * @interface
224
- */
225
- export interface BlueprintPipelineResource extends BlueprintBaseFunctionResource {
226
- type: 'sanity.function.pipeline';
227
- event?: BlueprintFunctionResourceEvent;
228
- /**
229
- * Concurrent executions
230
- * Min 1
231
- * Max 500
232
- */
233
- concurrency?: number;
234
- /**
235
- * Debounce window in seconds
236
- */
237
- debounce?: number;
238
- /**
239
- * Path used to group debounced events, e.g. 'document._id'
240
- */
241
- debounceKey?: string;
242
- }
243
263
  /**
244
264
  * Configuration for defining a pipeline function.
245
265
  * @public
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/blueprints",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
4
4
  "description": "Helper methods and type definitions for Sanity Blueprints",
5
5
  "keywords": [
6
6
  "sanity",
@@ -0,0 +1,4 @@
1
+ import type {BlueprintFunctionBaseResourceEvent} from '../../index.js'
2
+
3
+ type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
4
+ export const BASE_EVENT_KEYS = new Set<BaseFunctionEventKey>(['on', 'filter', 'projection', 'includeDrafts'])
@@ -0,0 +1,129 @@
1
+ import {
2
+ type BlueprintDocumentFunctionConfig,
3
+ type BlueprintDocumentFunctionResource,
4
+ type BlueprintDocumentFunctionResourceEvent,
5
+ validateDocumentFunction,
6
+ } from '../../index.js'
7
+ import {runValidation} from '../../utils/validation.js'
8
+ import {BASE_EVENT_KEYS} from './base-event.js'
9
+ import {defineFunction} from './index.js'
10
+
11
+ type DocumentFunctionEventKey = keyof BlueprintDocumentFunctionResourceEvent
12
+ const DOCUMENT_EVENT_KEYS = new Set<DocumentFunctionEventKey>(['includeAllVersions', 'resource', ...BASE_EVENT_KEYS.values()])
13
+
14
+ /*
15
+ * FUTURE example (move below @example when ready)
16
+ * @example With robot token reference
17
+ * ```ts
18
+ * defineRole({
19
+ * name: 'fn-role',
20
+ * title: 'Function Role',
21
+ * appliesToRobots: true,
22
+ * permissions: [{name: 'sanity-project-dataset', action: 'read'}],
23
+ * })
24
+ *
25
+ * defineRobotToken({
26
+ * name: 'fn-robot',
27
+ * memberships: [{
28
+ * roleNames: ['$.resources.fn-role'],
29
+ * }],
30
+ * })
31
+ *
32
+ * defineDocumentFunction({
33
+ * name: 'sync-to-external',
34
+ * src: 'functions/sync',
35
+ * memory: 3,
36
+ * timeout: 300,
37
+ * robotToken: '$.resources.fn-robot',
38
+ * event: {
39
+ * on: ['create', 'update'],
40
+ * filter: "_type == 'product'",
41
+ * projection: "{_id, title, slug}",
42
+ * includeDrafts: false,
43
+ * },
44
+ * env: {
45
+ * EXTERNAL_API_URL: 'https://api.example.com',
46
+ * SUPER_SECRET: process.env.SUPER_SECRET,
47
+ * },
48
+ * })
49
+ * ```
50
+ */
51
+ /**
52
+ * Defines a function that is triggered by document events in Sanity datasets.
53
+ *
54
+ * ```ts
55
+ * defineDocumentFunction({
56
+ * name: 'my-document-function',
57
+ * event: {
58
+ * on: ['create', 'update'],
59
+ * filter: "_type == 'post'",
60
+ * projection: "{_id, title, slug}",
61
+ * },
62
+ * })
63
+ * ```
64
+ * @param functionConfig The configuration for the document function
65
+ * @public
66
+ * @category Definers
67
+ * @expandType BlueprintDocumentFunctionConfig
68
+ * @returns The validated document function resource
69
+ */
70
+ export function defineDocumentFunction(functionConfig: BlueprintDocumentFunctionConfig): BlueprintDocumentFunctionResource
71
+ /**
72
+ * @deprecated Define event properties under the 'event' key instead of specifying them at the top level
73
+ * @hidden
74
+ */
75
+ export function defineDocumentFunction(
76
+ functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>,
77
+ ): BlueprintDocumentFunctionResource
78
+
79
+ export function defineDocumentFunction(
80
+ functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>,
81
+ ): BlueprintDocumentFunctionResource {
82
+ let {name, src, event, timeout, memory, env, robotToken, project, runtime, ...maybeEvent} = functionConfig
83
+
84
+ // event validation and normalization
85
+ if (event) {
86
+ // `event` was specified, but event keys (aggregated in `maybeEvent`) were also specified at the top level. ambiguous and deprecated usage.
87
+ const duplicateKeys = Array.from(DOCUMENT_EVENT_KEYS).filter((key) => key in maybeEvent)
88
+ if (duplicateKeys.length > 0) {
89
+ throw new Error(
90
+ `\`event\` properties should be specified under the \`event\` key - specifying them at the top level is deprecated. The following keys were specified at the top level: ${duplicateKeys.map((k) => `\`${k}\``).join(', ')}`,
91
+ )
92
+ }
93
+
94
+ event = buildDocumentFunctionEvent(event)
95
+ } else {
96
+ event = buildDocumentFunctionEvent(maybeEvent)
97
+ // deprecated usage of putting event properties at the top level, warn about this.
98
+ console.warn(
99
+ '⚠️ Deprecated usage of `defineDocumentFunction`: prefer to put `event` properties under the `event` key rather than at the top level.',
100
+ )
101
+ }
102
+
103
+ const functionResource: BlueprintDocumentFunctionResource = {
104
+ ...defineFunction(functionConfig, {skipValidation: true}),
105
+ type: 'sanity.function.document',
106
+ event,
107
+ }
108
+
109
+ runValidation(() => validateDocumentFunction(functionResource))
110
+
111
+ return functionResource
112
+ }
113
+
114
+ /**
115
+ * Builds a document function event configuration from partial event properties.
116
+ * Filters out non-event properties and applies defaults.
117
+ * @param event Partial document function event configuration
118
+ * @returns Complete document function event configuration
119
+ */
120
+ function buildDocumentFunctionEvent(event: Partial<BlueprintDocumentFunctionResourceEvent>): BlueprintDocumentFunctionResourceEvent {
121
+ const cleanEvent = Object.fromEntries(
122
+ Object.entries(event).filter(([key]) => DOCUMENT_EVENT_KEYS.has(key as DocumentFunctionEventKey)),
123
+ ) as Partial<BlueprintDocumentFunctionResourceEvent>
124
+
125
+ return {
126
+ on: cleanEvent.on || ['publish'],
127
+ ...cleanEvent,
128
+ }
129
+ }
@@ -0,0 +1,32 @@
1
+ import {type BlueprintEventFunctionConfig, type BlueprintEventFunctionResource, validateEventFunction} from '../../index.js'
2
+ import {runValidation} from '../../utils/validation.js'
3
+ import {defineFunction} from './index.js'
4
+
5
+ /**
6
+ * Defines a function that provide event behaviour.
7
+ *
8
+ * @remarks
9
+ * A barebones event function
10
+ * ```ts
11
+ * defineEventFunction({
12
+ * name: 'send-email',
13
+ * })
14
+ * ```
15
+ * @public
16
+ * @alpha Deploying Event Functions via Blueprints is experimental. This feature is not available publicly yet.
17
+ * @hidden
18
+ * @category Definers
19
+ * @expandType BlueprintEventFunctionConfig
20
+ * @param functionConfig The configuration for the event function
21
+ * @returns The validated event function resource
22
+ */
23
+ export function defineEventFunction(functionConfig: BlueprintEventFunctionConfig): BlueprintEventFunctionResource {
24
+ const functionResource: BlueprintEventFunctionResource = {
25
+ ...defineFunction(functionConfig, {skipValidation: true}),
26
+ type: 'sanity.function.event',
27
+ }
28
+
29
+ runValidation(() => validateEventFunction(functionResource))
30
+
31
+ return functionResource
32
+ }
@@ -0,0 +1,39 @@
1
+ import {type BlueprintBaseFunctionConfig, type BlueprintBaseFunctionResource, validateFunction} from '../../index.js'
2
+ import {runValidation} from '../../utils/validation.js'
3
+
4
+ /**
5
+ * Defines a base function resource with common properties.
6
+ *
7
+ * @param functionConfig The configuration for the function
8
+ * @param options Optional configuration including validation options
9
+ * @category Definers
10
+ * @internal
11
+ * @expandType BlueprintBaseFunctionConfig
12
+ * @returns The validated function resource
13
+ */
14
+ export function defineFunction(
15
+ functionConfig: BlueprintBaseFunctionConfig,
16
+ options?: {skipValidation?: boolean; scopeType?: 'organization'},
17
+ ): BlueprintBaseFunctionResource {
18
+ const {name, displayName, src, timeout, memory, env, robotToken, project, runtime, lifecycle} = functionConfig
19
+
20
+ const functionResource: BlueprintBaseFunctionResource = {
21
+ type: 'sanity.function.document',
22
+ name,
23
+ src: src ?? `functions/${name}`,
24
+ displayName,
25
+ timeout,
26
+ memory,
27
+ env,
28
+ robotToken,
29
+ runtime,
30
+ }
31
+
32
+ if (options?.scopeType !== 'organization' && project) functionResource.project = project
33
+
34
+ if (lifecycle) functionResource.lifecycle = lifecycle
35
+
36
+ if (options?.skipValidation !== true) runValidation(() => validateFunction(functionResource))
37
+
38
+ return functionResource
39
+ }
@@ -0,0 +1,98 @@
1
+ import {
2
+ type BlueprintMediaLibraryAssetFunctionConfig,
3
+ type BlueprintMediaLibraryAssetFunctionResource,
4
+ type BlueprintMediaLibraryFunctionResourceEvent,
5
+ validateMediaLibraryAssetFunction,
6
+ } from '../../index.js'
7
+ import {runValidation} from '../../utils/validation.js'
8
+ import {BASE_EVENT_KEYS} from './base-event.js'
9
+ import {defineFunction} from './index.js'
10
+
11
+ type MediaLibraryFunctionEventKey = keyof BlueprintMediaLibraryFunctionResourceEvent
12
+ const MEDIA_LIBRARY_EVENT_KEYS = new Set<MediaLibraryFunctionEventKey>(['resource', ...BASE_EVENT_KEYS.values()])
13
+ /*
14
+ * FUTURE example (move below @example when ready)
15
+ * @example With robot token reference
16
+ * ```ts
17
+ * defineRobotToken({
18
+ * name: 'media-robot',
19
+ * memberships: [{
20
+ * resourceType: 'project',
21
+ * resourceId: projectId,
22
+ * roleNames: ['editor'],
23
+ * }],
24
+ * })
25
+ *
26
+ * defineMediaLibraryAssetFunction({
27
+ * name: 'process-uploads',
28
+ * src: 'functions/process-uploads-v2',
29
+ * robotToken: '$.resources.media-robot',
30
+ * event: {
31
+ * on: ['create', 'update'],
32
+ * resource: {
33
+ * type: 'media-library',
34
+ * id: 'my-media-library-id',
35
+ * },
36
+ * filter: "type == 'image'",
37
+ * projection: "{_id}",
38
+ * },
39
+ * env: {
40
+ * CDN_BUCKET: 'my-cdn-bucket',
41
+ * },
42
+ * })
43
+ * ```
44
+ */
45
+ /**
46
+ * Defines a function that is triggered by media library events.
47
+ *
48
+ * ```ts
49
+ * defineMediaLibraryAssetFunction({
50
+ * name: 'my-media-library-function',
51
+ * event: {
52
+ * on: ['create'],
53
+ * resource: {
54
+ * type: 'media-library',
55
+ * id: 'my-media-library-id',
56
+ * },
57
+ * },
58
+ * })
59
+ * ```
60
+ * @param functionConfig The configuration for the media library asset function
61
+ * @public
62
+ * @category Definers
63
+ * @expandType BlueprintMediaLibraryAssetFunctionConfig
64
+ * @returns The validated media library asset function resource
65
+ */
66
+ export function defineMediaLibraryAssetFunction(
67
+ functionConfig: BlueprintMediaLibraryAssetFunctionConfig,
68
+ ): BlueprintMediaLibraryAssetFunctionResource {
69
+ const {event} = functionConfig
70
+
71
+ const functionResource: BlueprintMediaLibraryAssetFunctionResource = {
72
+ ...defineFunction(functionConfig, {skipValidation: true}),
73
+ type: 'sanity.function.media-library.asset',
74
+ event: buildMediaLibraryFunctionEvent(event),
75
+ }
76
+
77
+ runValidation(() => validateMediaLibraryAssetFunction(functionResource))
78
+
79
+ return functionResource
80
+ }
81
+
82
+ /**
83
+ * Builds a media library function event configuration.
84
+ * Filters out non-event properties and applies defaults.
85
+ * @param event Media library function event configuration
86
+ * @returns Complete media library function event configuration
87
+ */
88
+ function buildMediaLibraryFunctionEvent(event: BlueprintMediaLibraryFunctionResourceEvent): BlueprintMediaLibraryFunctionResourceEvent {
89
+ const cleanEvent = Object.fromEntries(
90
+ Object.entries(event).filter(([key]) => MEDIA_LIBRARY_EVENT_KEYS.has(key as MediaLibraryFunctionEventKey)),
91
+ ) as BlueprintMediaLibraryFunctionResourceEvent
92
+
93
+ const fullEvent = {
94
+ on: cleanEvent.on || ['publish'],
95
+ ...cleanEvent,
96
+ }
97
+ return fullEvent
98
+ }
@@ -0,0 +1,42 @@
1
+ import {type BlueprintPipelineConfig, type BlueprintPipelineResource, validatePipelineFunction} from '../../index.js'
2
+ import {runValidation} from '../../utils/validation.js'
3
+ import {defineFunction} from './index.js'
4
+
5
+ /**
6
+ * Defines a pipeline function resource.
7
+ *
8
+ * @remarks
9
+ *
10
+ * ```ts
11
+ * definePipeline({
12
+ * name: 'daily-cleanup',
13
+ * event: {type: 'document', on: ['create'], filter: "_type == 'post'"},
14
+ * concurrency: 5,
15
+ * debounce: 10,
16
+ * debounceKey: 'document._id',
17
+ * })
18
+ * ```
19
+ *
20
+ *
21
+ * @param functionConfig The configuration for the function
22
+ * @category Definers
23
+ * @alpha Deploying Pipeline Functions via Blueprints is experimental. This feature is not available publicly yet.
24
+ * @public
25
+ * @hidden
26
+ * @expandType BlueprintPipelineConfig
27
+ * @returns The validated pipeline function resource
28
+ */
29
+ export function definePipeline(functionConfig: BlueprintPipelineConfig): BlueprintPipelineResource {
30
+ const {name, event, concurrency, debounce, debounceKey, src} = functionConfig
31
+ const functionResource: BlueprintPipelineResource = {
32
+ ...defineFunction({...functionConfig, src: src ?? `functions/${name}`}, {skipValidation: true}),
33
+ type: 'sanity.function.pipeline',
34
+ ...(event !== undefined && {event}),
35
+ ...(concurrency !== undefined && {concurrency}),
36
+ ...(debounce !== undefined && {debounce}),
37
+ ...(debounceKey !== undefined && {debounceKey}),
38
+ }
39
+
40
+ runValidation(() => validatePipelineFunction(functionResource))
41
+ return functionResource
42
+ }
@@ -0,0 +1,49 @@
1
+ import {type BlueprintQueueFunctionConfig, type BlueprintQueueFunctionResource, validateQueueFunction} from '../../index.js'
2
+ import {runValidation} from '../../utils/validation.js'
3
+ import {defineFunction} from './index.js'
4
+ /**
5
+ * Defines a function that provide queueing behaviour.
6
+ *
7
+ * @remarks
8
+ * Using the reasonable defaults of concurrency: 1, fifo: true, and dlq: true
9
+ * ```ts
10
+ * defineQueueFunction({
11
+ * name: 'send-email',
12
+ * })
13
+ * ```
14
+ *
15
+ * Specifying a concurrency of 5
16
+ * ```ts
17
+ * defineQueueFunction({
18
+ * name: 'bustin-caches',
19
+ * concurrency: 5,
20
+ * fifo: true,
21
+ * dlq: true,
22
+ * })
23
+ * ```
24
+ * @public
25
+ * @alpha Deploying Queue Functions via Blueprints is experimental. This feature is not available publicly yet.
26
+ * @hidden
27
+ * @category Definers
28
+ * @expandType BlueprintQueueFunctionConfig
29
+ * @param functionConfig The configuration for the queue function
30
+ * @returns The validated queue function resource
31
+ */
32
+ export function defineQueueFunction(functionConfig: BlueprintQueueFunctionConfig): BlueprintQueueFunctionResource {
33
+ const {concurrency = 1, fifo = true, dlq = true, debounce, debounceKey, event} = functionConfig
34
+
35
+ const functionResource: BlueprintQueueFunctionResource = {
36
+ ...defineFunction(functionConfig, {skipValidation: true}),
37
+ type: 'sanity.function.queue',
38
+ ...(concurrency !== undefined && {concurrency}),
39
+ ...(debounce !== undefined && {debounce}),
40
+ ...(debounceKey !== undefined && {debounceKey}),
41
+ ...(dlq !== undefined && {dlq}),
42
+ ...(event !== undefined && {event}),
43
+ ...(fifo !== undefined && {fifo}),
44
+ }
45
+
46
+ runValidation(() => validateQueueFunction(functionResource))
47
+
48
+ return functionResource
49
+ }