@sanity/blueprints 0.22.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 (67) hide show
  1. package/README.md +1 -0
  2. package/dist/definers/functions/base-event.d.ts +2 -0
  3. package/dist/definers/functions/base-event.js +2 -0
  4. package/dist/definers/functions/base-event.js.map +1 -0
  5. package/dist/definers/functions/document.d.ts +26 -0
  6. package/dist/definers/functions/document.js +43 -0
  7. package/dist/definers/functions/document.js.map +1 -0
  8. package/dist/definers/functions/event.d.ts +20 -0
  9. package/dist/definers/functions/event.js +30 -0
  10. package/dist/definers/functions/event.js.map +1 -0
  11. package/dist/definers/functions/index.d.ts +15 -0
  12. package/dist/definers/functions/index.js +34 -0
  13. package/dist/definers/functions/index.js.map +1 -0
  14. package/dist/definers/functions/media-library.d.ts +23 -0
  15. package/dist/definers/functions/media-library.js +83 -0
  16. package/dist/definers/functions/media-library.js.map +1 -0
  17. package/dist/definers/functions/pipeline.d.ts +26 -0
  18. package/dist/definers/functions/pipeline.js +41 -0
  19. package/dist/definers/functions/pipeline.js.map +1 -0
  20. package/dist/definers/functions/queue.d.ts +30 -0
  21. package/dist/definers/functions/queue.js +47 -0
  22. package/dist/definers/functions/queue.js.map +1 -0
  23. package/dist/definers/functions/scheduled.d.ts +46 -0
  24. package/dist/definers/functions/scheduled.js +91 -0
  25. package/dist/definers/functions/scheduled.js.map +1 -0
  26. package/dist/definers/functions/sync-tag-invalidate.d.ts +31 -0
  27. package/dist/definers/functions/sync-tag-invalidate.js +43 -0
  28. package/dist/definers/functions/sync-tag-invalidate.js.map +1 -0
  29. package/dist/definers/studios.d.ts +22 -0
  30. package/dist/definers/studios.js +31 -0
  31. package/dist/definers/studios.js.map +1 -0
  32. package/dist/index.d.ts +11 -1
  33. package/dist/index.js +11 -1
  34. package/dist/index.js.map +1 -1
  35. package/dist/types/functions/event.d.ts +9 -26
  36. package/dist/types/functions/index.d.ts +64 -47
  37. package/dist/types/functions/index.js.map +1 -1
  38. package/dist/types/studios.d.ts +57 -0
  39. package/dist/types/studios.js +2 -0
  40. package/dist/types/studios.js.map +1 -0
  41. package/dist/validation/functions.d.ts +10 -1
  42. package/dist/validation/functions.js +91 -19
  43. package/dist/validation/functions.js.map +1 -1
  44. package/dist/validation/studios.d.ts +9 -0
  45. package/dist/validation/studios.js +70 -0
  46. package/dist/validation/studios.js.map +1 -0
  47. package/package.json +19 -9
  48. package/src/definers/functions/base-event.ts +4 -0
  49. package/src/definers/functions/document.ts +129 -0
  50. package/src/definers/functions/event.ts +32 -0
  51. package/src/definers/functions/index.ts +39 -0
  52. package/src/definers/functions/media-library.ts +98 -0
  53. package/src/definers/functions/pipeline.ts +42 -0
  54. package/src/definers/functions/queue.ts +49 -0
  55. package/src/definers/functions/scheduled.ts +112 -0
  56. package/src/definers/functions/sync-tag-invalidate.ts +51 -0
  57. package/src/definers/studios.ts +34 -0
  58. package/src/index.ts +11 -1
  59. package/src/types/functions/event.ts +4 -31
  60. package/src/types/functions/index.ts +75 -57
  61. package/src/types/studios.ts +68 -0
  62. package/src/validation/functions.ts +98 -21
  63. package/src/validation/studios.ts +76 -0
  64. package/dist/definers/functions.d.ts +0 -195
  65. package/dist/definers/functions.js +0 -368
  66. package/dist/definers/functions.js.map +0 -1
  67. package/src/definers/functions.ts +0 -526
@@ -181,7 +181,9 @@ function validateDocumentFunctionEvent(event: unknown): BlueprintError[] {
181
181
  function validateFunctionEventResourceDataset(event: unknown): BlueprintError[] {
182
182
  const errors: BlueprintError[] = []
183
183
  if (!event || typeof event !== 'object') return [{type: 'invalid_value', message: '`event` must be an object'}]
184
- if (!('resource' in event)) return [{type: 'invalid_value', message: '`event.resource` must exist'}]
184
+ // `resource` is optional for the event types that reach here (document and sync-tag-invalidate),
185
+ // so there is nothing to validate when it is absent.
186
+ if (!('resource' in event) || typeof event.resource === 'undefined') return []
185
187
  const resource = event.resource
186
188
  if (!resource || typeof resource !== 'object') return [{type: 'invalid_value', message: '`event.resource` must be an object'}]
187
189
  if (!('type' in resource) || !resource.type || resource.type !== 'dataset')
@@ -439,7 +441,6 @@ export function validateSyncTagInvalidateFunction(functionResource: unknown): Bl
439
441
  * @category Functions Types
440
442
  * @returns Array of validation errors, empty if valid
441
443
  */
442
-
443
444
  export function validateQueueFunction(functionResource: unknown): BlueprintError[] {
444
445
  if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
445
446
  if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
@@ -450,8 +451,24 @@ export function validateQueueFunction(functionResource: unknown): BlueprintError
450
451
  errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.queue`'})
451
452
  }
452
453
 
453
- if ('event' in functionResource) {
454
- errors.push(...validateQueueFunctionEvent(functionResource.event))
454
+ if ('concurrency' in functionResource) {
455
+ if (typeof functionResource.concurrency !== 'number') {
456
+ errors.push({type: 'invalid_type', message: '`concurrency` must be a number'})
457
+ } else if (functionResource.concurrency < 1) {
458
+ errors.push({type: 'invalid_type', message: '`concurrency` must be at least 1'})
459
+ } else if (functionResource.concurrency > 500) {
460
+ errors.push({type: 'invalid_value', message: '`concurrency` must be less than 500'})
461
+ }
462
+ }
463
+ if ('fifo' in functionResource && typeof functionResource.fifo !== 'boolean') {
464
+ errors.push({type: 'invalid_type', message: '`fifo` must be a boolean'})
465
+ }
466
+ if ('dlq' in functionResource && typeof functionResource.dlq !== 'boolean') {
467
+ errors.push({type: 'invalid_type', message: '`dlq` must be a boolean'})
468
+ }
469
+
470
+ if ('event' in functionResource && typeof functionResource.event !== 'undefined') {
471
+ errors.push(...validateFunctionEvent(functionResource.event))
455
472
  }
456
473
 
457
474
  errors.push(...validateFunction(functionResource))
@@ -459,44 +476,104 @@ export function validateQueueFunction(functionResource: unknown): BlueprintError
459
476
  return errors
460
477
  }
461
478
 
462
- function validateQueueFunctionEvent(event: unknown): BlueprintError[] {
479
+ /**
480
+ * Validates a function event configuration.
481
+ * The event is a discriminated union keyed by `type`; validation is delegated to the
482
+ * matching per-type event validator.
483
+ * @param event The event configuration to validate
484
+ * @returns Array of validation errors, empty if valid
485
+ */
486
+ function validateFunctionEvent(event: unknown): BlueprintError[] {
463
487
  if (!event || typeof event !== 'object') return [{type: 'invalid_type', message: '`event` must be an object'}]
488
+ if (!('type' in event)) return [{type: 'missing_parameter', message: '`event.type` is required'}]
489
+
490
+ switch (event.type) {
491
+ case 'document':
492
+ return validateDocumentFunctionEvent(event)
493
+ case 'media-library':
494
+ return validateMediaLibraryFunctionEvent(event)
495
+ case 'cron':
496
+ return validateScheduledFunctionEvent(event)
497
+ case 'sync-tag-invalidate':
498
+ return validateFunctionEventResourceDataset(event)
499
+ default:
500
+ return [
501
+ {
502
+ type: 'invalid_value',
503
+ message: '`event.type` must be either `cron`, `document`, `sync-tag-invalidate`, or `media-library`',
504
+ },
505
+ ]
506
+ }
507
+ }
508
+
509
+ /**
510
+ * Validates an event function resource configuration.
511
+ * @param functionResource The function resource to validate
512
+ * @alpha
513
+ * @hidden
514
+ * @category Functions Types
515
+ * @returns Array of validation errors, empty if valid
516
+ */
517
+
518
+ export function validateEventFunction(functionResource: unknown): BlueprintError[] {
519
+ if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
520
+ if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
464
521
 
465
522
  const errors: BlueprintError[] = []
466
523
 
467
- if (!('concurrency' in event) || typeof event.concurrency !== 'number') {
468
- errors.push({type: 'invalid_type', message: '`event.concurrency` must be a number'})
469
- }
470
- if ('concurrency' in event && typeof event.concurrency === 'number' && event.concurrency < 1) {
471
- errors.push({type: 'invalid_type', message: '`event.concurrency` must be at least 1'})
472
- }
473
- if (!('fifo' in event) || typeof event.fifo !== 'boolean') {
474
- errors.push({type: 'invalid_type', message: '`event.fifo` must be a boolean'})
475
- }
476
- if (!('dlq' in event) || typeof event.dlq !== 'boolean') {
477
- errors.push({type: 'invalid_type', message: '`event.dlq` must be a boolean'})
524
+ if ('type' in functionResource && functionResource.type !== 'sanity.function.event') {
525
+ errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.event`'})
478
526
  }
479
527
 
528
+ errors.push(...validateFunction(functionResource))
529
+
480
530
  return errors
481
531
  }
482
532
 
483
533
  /**
484
- * Validates a event function resource configuration.
534
+ * Validates a pipeline function resource configuration.
485
535
  * @param functionResource The function resource to validate
486
536
  * @alpha
487
537
  * @hidden
488
538
  * @category Functions Types
489
539
  * @returns Array of validation errors, empty if valid
490
540
  */
491
-
492
- export function validateEventFunction(functionResource: unknown): BlueprintError[] {
541
+ export function validatePipelineFunction(functionResource: unknown): BlueprintError[] {
493
542
  if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
494
543
  if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
495
544
 
496
545
  const errors: BlueprintError[] = []
497
546
 
498
- if ('type' in functionResource && functionResource.type !== 'sanity.function.event') {
499
- errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.event`'})
547
+ if ('type' in functionResource && functionResource.type !== 'sanity.function.pipeline') {
548
+ errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.pipeline`'})
549
+ }
550
+
551
+ if ('debounceKey' in functionResource && typeof functionResource.debounceKey !== 'string') {
552
+ errors.push({type: 'invalid_type', message: '`debounceKey` must be a string'})
553
+ }
554
+
555
+ if ('debounceKey' in functionResource && !('debounce' in functionResource)) {
556
+ errors.push({type: 'invalid_value', message: '`debounceKey` requires a `debounce` to be set'})
557
+ }
558
+
559
+ if ('concurrency' in functionResource && typeof functionResource.concurrency !== 'number') {
560
+ errors.push({type: 'invalid_type', message: '`concurrency` must be a number'})
561
+ }
562
+
563
+ if ('concurrency' in functionResource && typeof functionResource.concurrency === 'number' && functionResource.concurrency < 1) {
564
+ errors.push({type: 'invalid_value', message: '`concurrency` must be at least 1'})
565
+ }
566
+
567
+ if ('concurrency' in functionResource && typeof functionResource.concurrency === 'number' && functionResource.concurrency > 500) {
568
+ errors.push({type: 'invalid_value', message: '`concurrency` must be less than 500'})
569
+ }
570
+
571
+ if ('debounce' in functionResource && typeof functionResource.debounce !== 'number') {
572
+ errors.push({type: 'invalid_type', message: '`debounce` must be a number'})
573
+ }
574
+
575
+ if ('event' in functionResource) {
576
+ errors.push(...validateFunctionEvent(functionResource.event))
500
577
  }
501
578
 
502
579
  errors.push(...validateFunction(functionResource))
@@ -0,0 +1,76 @@
1
+ import type {BlueprintError} from '../types/errors.js'
2
+ import {validateResource} from './resources.js'
3
+
4
+ /**
5
+ * Validates that the given resource is a valid Studio.
6
+ * @param resource The Studio resource
7
+ * @hidden
8
+ * @category Validation
9
+ * @returns A list of validation errors
10
+ */
11
+ export function validateStudio(resource: unknown): BlueprintError[] {
12
+ if (!resource) return [{type: 'invalid_value', message: 'Studio config must be provided'}]
13
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Studio config must be an object'}]
14
+
15
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
16
+
17
+ if ('type' in resource && resource.type !== 'sanity.studio') {
18
+ errors.push({type: 'invalid_value', message: 'Studio type must be `sanity.studio`'})
19
+ }
20
+
21
+ if (!('src' in resource) || !resource.src) {
22
+ errors.push({type: 'missing_parameter', message: 'Studio src is required'})
23
+ } else if (typeof resource.src !== 'string') {
24
+ errors.push({type: 'invalid_type', message: 'Studio src must be a string'})
25
+ }
26
+
27
+ if (!('autoUpdates' in resource) || !resource.autoUpdates) {
28
+ errors.push({type: 'missing_parameter', message: 'Studio autoUpdates is required'})
29
+ } else if (typeof resource.autoUpdates !== 'object') {
30
+ errors.push({type: 'invalid_type', message: 'Studio autoUpdates must be an object'})
31
+ } else {
32
+ if (!('enabled' in resource.autoUpdates)) {
33
+ errors.push({type: 'missing_parameter', message: 'Studio autoUpdates.enabled is required'})
34
+ } else if (typeof resource.autoUpdates.enabled !== 'boolean') {
35
+ errors.push({type: 'invalid_type', message: 'Studio autoUpdates.enabled must be a boolean'})
36
+ }
37
+
38
+ if ('version' in resource.autoUpdates) {
39
+ if (typeof resource.autoUpdates.version !== 'string') {
40
+ errors.push({type: 'invalid_type', message: 'Studio autoUpdates.version must be a string'})
41
+ }
42
+ }
43
+ }
44
+
45
+ if ('basePath' in resource) {
46
+ if (typeof resource.basePath !== 'string') {
47
+ errors.push({type: 'invalid_type', message: 'Studio basePath must be a string'})
48
+ }
49
+ }
50
+
51
+ if ('minify' in resource) {
52
+ if (typeof resource.minify !== 'boolean') {
53
+ errors.push({type: 'invalid_type', message: 'Studio minify must be a boolean'})
54
+ }
55
+ }
56
+
57
+ if ('reactCompiler' in resource) {
58
+ if (typeof resource.reactCompiler !== 'boolean') {
59
+ errors.push({type: 'invalid_type', message: 'Studio reactCompiler must be a boolean'})
60
+ }
61
+ }
62
+
63
+ if ('sourceMap' in resource) {
64
+ if (typeof resource.sourceMap !== 'boolean') {
65
+ errors.push({type: 'invalid_type', message: 'Studio sourceMap must be a boolean'})
66
+ }
67
+ }
68
+
69
+ if ('project' in resource) {
70
+ if (typeof resource.project !== 'string') {
71
+ errors.push({type: 'invalid_type', message: 'Studio project must be a string'})
72
+ }
73
+ }
74
+
75
+ return errors
76
+ }
@@ -1,195 +0,0 @@
1
- import { type BlueprintBaseFunctionConfig, type BlueprintBaseFunctionResource, type BlueprintDocumentFunctionConfig, type BlueprintDocumentFunctionResource, type BlueprintDocumentFunctionResourceEvent, type BlueprintEventFunctionConfig, type BlueprintEventFunctionResource, type BlueprintMediaLibraryAssetFunctionConfig, type BlueprintMediaLibraryAssetFunctionResource, type BlueprintQueueFunctionConfig, type BlueprintQueueFunctionResource, type BlueprintScheduledFunctionConfig, type BlueprintScheduledFunctionResource, type BlueprintSyncTagInvalidateFunctionConfig, type BlueprintSyncTagInvalidateFunctionResource } from '../index.js';
2
- /**
3
- * Defines a function that is triggered by document events in Sanity datasets.
4
- *
5
- * ```ts
6
- * defineDocumentFunction({
7
- * name: 'my-document-function',
8
- * event: {
9
- * on: ['create', 'update'],
10
- * filter: "_type == 'post'",
11
- * projection: "{_id, title, slug}",
12
- * },
13
- * })
14
- * ```
15
- * @param functionConfig The configuration for the document function
16
- * @public
17
- * @category Definers
18
- * @expandType BlueprintDocumentFunctionConfig
19
- * @returns The validated document function resource
20
- */
21
- export declare function defineDocumentFunction(functionConfig: BlueprintDocumentFunctionConfig): BlueprintDocumentFunctionResource;
22
- /**
23
- * @deprecated Define event properties under the 'event' key instead of specifying them at the top level
24
- * @hidden
25
- */
26
- export declare function defineDocumentFunction(functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>): BlueprintDocumentFunctionResource;
27
- /**
28
- * Defines a function that is triggered by media library events.
29
- *
30
- * ```ts
31
- * defineMediaLibraryAssetFunction({
32
- * name: 'my-media-library-function',
33
- * event: {
34
- * on: ['create'],
35
- * resource: {
36
- * type: 'media-library',
37
- * id: 'my-media-library-id',
38
- * },
39
- * },
40
- * })
41
- * ```
42
- * @param functionConfig The configuration for the media library asset function
43
- * @public
44
- * @category Definers
45
- * @expandType BlueprintMediaLibraryAssetFunctionConfig
46
- * @returns The validated media library asset function resource
47
- */
48
- export declare function defineMediaLibraryAssetFunction(functionConfig: BlueprintMediaLibraryAssetFunctionConfig): BlueprintMediaLibraryAssetFunctionResource;
49
- /**
50
- * Defines a function that is triggered on a schedule.
51
- * Supports cron expressions or natural language schedules.
52
- *
53
- * @remarks
54
- * Using a cron expression:
55
- * ```ts
56
- * defineScheduledFunction({
57
- * name: 'daily-cleanup',
58
- * event: {expression: '0 9 * * *'},
59
- * })
60
- * ```
61
- *
62
- * Using explicit cron fields:
63
- * ```ts
64
- * defineScheduledFunction({
65
- * name: 'daily-cleanup',
66
- * event: {minute: '0', hour: '9', dayOfMonth: '*', month: '*', dayOfWeek: '*'},
67
- * })
68
- * ```
69
- *
70
- * The `event.expression` field accepts standard cron expressions or natural language:
71
- * `'every 15 minutes'`, `'weekdays at 8am'`, `'fridays in the evening'`,
72
- * `'mon, wed, fri at 9am'`, `'first of the month at noon'`
73
- *
74
- * ```ts
75
- * defineScheduledFunction({
76
- * name: 'daily-cleanup',
77
- * event: {expression: 'every day at 9am'},
78
- * })
79
- * ```
80
- * @public
81
- * @alpha Deploying Scheduled Functions via Blueprints is experimental. This feature is not available publicly yet.
82
- * @hidden
83
- * @category Definers
84
- * @expandType BlueprintScheduledFunctionConfig
85
- * @param functionConfig The configuration for the scheduled function
86
- * @returns The validated scheduled function resource
87
- */
88
- export declare function defineScheduledFunction(functionConfig: BlueprintScheduledFunctionConfig): BlueprintScheduledFunctionResource;
89
- /**
90
- * @deprecated Define scheduled functions using `defineScheduledFunction` instead
91
- * @hidden
92
- */
93
- export declare function defineScheduleFunction(functionConfig: BlueprintScheduledFunctionConfig): BlueprintScheduledFunctionResource;
94
- /**
95
- * Defines a function that is triggered on a sync tag invalidate event.
96
- *
97
- * @remarks
98
- * Using implicit event scoping to all datasets in the function project:
99
- * ```ts
100
- * defineSyncTagInvalidateFunction({
101
- * name: 'bustin-caches',
102
- * })
103
- * ```
104
- *
105
- * Using explicit event scoping to a particular dataset in the function project:
106
- * ```ts
107
- * defineSyncTagInvalidateFunction({
108
- * name: 'bustin-caches',
109
- * event: {
110
- * resource: {
111
- * type: 'dataset',
112
- * id: 'myProj.myDataset',
113
- * },
114
- * },
115
- * })
116
- * ```
117
- * @public
118
- * @category Definers
119
- * @expandType BlueprintSyncTagInvalidateFunctionConfig
120
- * @param functionConfig The configuration for the sync tag invalidate function
121
- * @returns The validated sync tag invalidate function resource
122
- */
123
- export declare function defineSyncTagInvalidateFunction(functionConfig: BlueprintSyncTagInvalidateFunctionConfig): BlueprintSyncTagInvalidateFunctionResource;
124
- /**
125
- * Defines a function that provide queueing behaviour.
126
- *
127
- * @remarks
128
- * Using the reasonable defaults of concurrency: 1, fifo: true, and dlq: true
129
- * ```ts
130
- * defineQueueFunction({
131
- * name: 'send-email',
132
- * })
133
- * ```
134
- *
135
- * Using the reasonable defaults of concurrency: 1, fifo: true, and dlq: true
136
- * ```ts
137
- * defineQueueFunction({
138
- * name: 'bustin-caches',
139
- * queue: true,
140
- * })
141
- * ```
142
- *
143
- * Specifying a concurrency of 5
144
- * ```ts
145
- * defineQueueFunction({
146
- * name: 'bustin-caches',
147
- * queue: {
148
- * concurrency: 5,
149
- * fifo: true,
150
- * dlq: true,
151
- * },
152
- * })
153
- * ```
154
- * @public
155
- * @alpha Deploying Queue Functions via Blueprints is experimental. This feature is not available publicly yet.
156
- * @hidden
157
- * @category Definers
158
- * @expandType BlueprintQueueFunctionConfig
159
- * @param functionConfig The configuration for the queue function
160
- * @returns The validated queue function resource
161
- */
162
- export declare function defineQueueFunction(functionConfig: BlueprintQueueFunctionConfig): BlueprintQueueFunctionResource;
163
- /**
164
- * Defines a function that provide event behaviour.
165
- *
166
- * @remarks
167
- * A barebones event function
168
- * ```ts
169
- * defineEventFunction({
170
- * name: 'send-email',
171
- * })
172
- * ```
173
- * @public
174
- * @alpha Deploying Event Functions via Blueprints is experimental. This feature is not available publicly yet.
175
- * @hidden
176
- * @category Definers
177
- * @expandType BlueprintEventFunctionConfig
178
- * @param functionConfig The configuration for the event function
179
- * @returns The validated event function resource
180
- */
181
- export declare function defineEventFunction(functionConfig: BlueprintEventFunctionConfig): BlueprintEventFunctionResource;
182
- /**
183
- * Defines a base function resource with common properties.
184
- *
185
- * @param functionConfig The configuration for the function
186
- * @param options Optional configuration including validation options
187
- * @category Definers
188
- * @internal
189
- * @expandType BlueprintBaseFunctionConfig
190
- * @returns The validated function resource
191
- */
192
- export declare function defineFunction(functionConfig: BlueprintBaseFunctionConfig, options?: {
193
- skipValidation?: boolean;
194
- scopeType?: 'organization';
195
- }): BlueprintBaseFunctionResource;