@sanity/blueprints 0.13.0 → 0.14.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.
Files changed (45) hide show
  1. package/dist/definers/functions.d.ts +14 -9
  2. package/dist/definers/functions.js +27 -20
  3. package/dist/definers/functions.js.map +1 -1
  4. package/dist/definers/projects.d.ts +20 -2
  5. package/dist/definers/projects.js +22 -2
  6. package/dist/definers/projects.js.map +1 -1
  7. package/dist/definers/resources.d.ts +9 -1
  8. package/dist/definers/resources.js +22 -0
  9. package/dist/definers/resources.js.map +1 -1
  10. package/dist/definers/robotTokens.d.ts +19 -1
  11. package/dist/definers/robotTokens.js +21 -1
  12. package/dist/definers/robotTokens.js.map +1 -1
  13. package/dist/definers/roles.d.ts +19 -1
  14. package/dist/definers/roles.js +21 -1
  15. package/dist/definers/roles.js.map +1 -1
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.js +4 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/types/datasets.d.ts +2 -0
  20. package/dist/types/functions.d.ts +12 -12
  21. package/dist/types/resources.d.ts +35 -2
  22. package/dist/utils/schedule-parser.d.ts +7 -7
  23. package/dist/utils/schedule-parser.js +9 -9
  24. package/dist/utils/schedule-parser.js.map +1 -1
  25. package/dist/validation/datasets.js +6 -0
  26. package/dist/validation/datasets.js.map +1 -1
  27. package/dist/validation/functions.d.ts +2 -2
  28. package/dist/validation/functions.js +11 -11
  29. package/dist/validation/functions.js.map +1 -1
  30. package/dist/validation/resources.js +15 -1
  31. package/dist/validation/resources.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/definers/functions.ts +39 -31
  34. package/src/definers/projects.ts +30 -2
  35. package/src/definers/resources.ts +26 -1
  36. package/src/definers/robotTokens.ts +29 -1
  37. package/src/definers/roles.ts +24 -0
  38. package/src/index.ts +4 -0
  39. package/src/types/datasets.ts +2 -0
  40. package/src/types/functions.ts +14 -14
  41. package/src/types/resources.ts +40 -2
  42. package/src/utils/schedule-parser.ts +9 -9
  43. package/src/validation/datasets.ts +7 -0
  44. package/src/validation/functions.ts +11 -11
  45. package/src/validation/resources.ts +13 -1
@@ -1,4 +1,11 @@
1
- import {type BlueprintRobotTokenConfig, type BlueprintRobotTokenResource, validateRobotToken} from '../index.js'
1
+ import {
2
+ type BlueprintCrossStackReferenceConfig,
3
+ type BlueprintResource,
4
+ type BlueprintRobotTokenConfig,
5
+ type BlueprintRobotTokenResource,
6
+ referenceResource,
7
+ validateRobotToken,
8
+ } from '../index.js'
2
9
  import {runValidation} from '../utils/validation.js'
3
10
 
4
11
  /*
@@ -54,3 +61,24 @@ export function defineRobotToken(parameters: BlueprintRobotTokenConfig): Bluepri
54
61
 
55
62
  return robotResource
56
63
  }
64
+
65
+ /**
66
+ * Creates a reference to a robot token in another stack.
67
+ *
68
+ * ```ts
69
+ * referenceRobotToken({
70
+ * name: 'editor-token',
71
+ * stack: 'editorial',
72
+ * })
73
+ * ```
74
+ *
75
+ * @public
76
+ * @beta Referencing Robot Tokens via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
77
+ * @category Referencers
78
+ * @expandType BlueprintCrossStackReferenceConfig
79
+ * @param params The parameters for referencing the robot token
80
+ * @returns The robot token reference
81
+ */
82
+ export function referenceRobotToken({name, stack, localName}: BlueprintCrossStackReferenceConfig): BlueprintResource {
83
+ return referenceResource({name, stack, localName, type: 'sanity.access.robot'})
84
+ }
@@ -1,7 +1,10 @@
1
1
  import {
2
+ type BlueprintCrossStackReferenceConfig,
2
3
  type BlueprintProjectRoleResource,
4
+ type BlueprintResource,
3
5
  type BlueprintRoleConfig,
4
6
  type BlueprintRoleResource,
7
+ referenceResource,
5
8
  validateProjectRole,
6
9
  validateRole,
7
10
  } from '../index.js'
@@ -134,3 +137,24 @@ export function defineProjectRole(projectId: string, parameters: BlueprintRoleCo
134
137
 
135
138
  return projectRoleResource
136
139
  }
140
+
141
+ /**
142
+ * Creates a reference to a role in another stack.
143
+ *
144
+ * ```ts
145
+ * referenceRole({
146
+ * name: 'editor-role',
147
+ * stack: 'editorial',
148
+ * })
149
+ * ```
150
+ *
151
+ * @public
152
+ * @beta Referencing Roles via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
153
+ * @category Referencers
154
+ * @expandType BlueprintCrossStackReferenceConfig
155
+ * @param params The parameters for referencing the role
156
+ * @returns The role reference
157
+ */
158
+ export function referenceRole({name, stack, localName}: BlueprintCrossStackReferenceConfig): BlueprintResource {
159
+ return referenceResource({name, stack, localName, type: 'sanity.access.role'})
160
+ }
package/src/index.ts CHANGED
@@ -3,6 +3,10 @@
3
3
  * @categoryDescription Definers
4
4
  * These functions are used to define resources in a Blueprint.
5
5
  */
6
+ /**
7
+ * @categoryDescription Referencers
8
+ * These functions are used to reference resources in another Blueprint.
9
+ */
6
10
  export * from './definers/blueprints.js'
7
11
  export * from './definers/cors.js'
8
12
  export * from './definers/datasets.js'
@@ -19,6 +19,8 @@ export interface BlueprintDatasetResource extends BlueprintResource<BlueprintPro
19
19
  type: 'sanity.project.dataset'
20
20
  /** The name of the dataset. Must be unique within a project. */
21
21
  datasetName: string
22
+ /** The dataset description */
23
+ description?: string
22
24
  /** The ACL mode to set for the new dataset. Defaults to public. */
23
25
  aclMode?: AclMode
24
26
 
@@ -47,11 +47,11 @@ export interface BlueprintMediaLibraryFunctionResourceEvent extends BlueprintFun
47
47
  export type BlueprintFunctionResourceEvent = BlueprintDocumentFunctionResourceEvent | BlueprintMediaLibraryFunctionResourceEvent
48
48
 
49
49
  /**
50
- * Explicit resource event for schedule functions to specific minutes, hours, days of month, months, and days of week
50
+ * Explicit resource event for scheduled functions to specific minutes, hours, days of month, months, and days of week
51
51
  * @example { minute: '0', hour: '9', dayOfMonth: '1', month: '1', dayOfWeek: '1' }
52
52
  * @category Functions Types
53
53
  */
54
- export interface BlueprintScheduleFunctionExplicitResourceEvent {
54
+ export interface BlueprintScheduledFunctionExplicitResourceEvent {
55
55
  minute: string
56
56
  hour: string
57
57
  dayOfMonth: string
@@ -60,21 +60,21 @@ export interface BlueprintScheduleFunctionExplicitResourceEvent {
60
60
  }
61
61
 
62
62
  /**
63
- * Expression resource for schedule functions to specific expressions
63
+ * Expression resource for scheduled functions to specific expressions
64
64
  * @example { expression: '0 9 * * *' }
65
65
  * @category Functions Types
66
66
  */
67
- export interface BlueprintScheduleFunctionExpressionResourceEvent {
67
+ export interface BlueprintScheduledFunctionExpressionResourceEvent {
68
68
  expression: string
69
69
  }
70
70
 
71
71
  /**
72
- * Union type of all schedule function resource event configurations
72
+ * Union type of all scheduled function resource event configurations
73
73
  * @category Functions Types
74
74
  */
75
- export type BlueprintScheduleFunctionResourceEvent =
76
- | BlueprintScheduleFunctionExplicitResourceEvent
77
- | BlueprintScheduleFunctionExpressionResourceEvent
75
+ export type BlueprintScheduledFunctionResourceEvent =
76
+ | BlueprintScheduledFunctionExplicitResourceEvent
77
+ | BlueprintScheduledFunctionExpressionResourceEvent
78
78
 
79
79
  /**
80
80
  * Dataset resource for scoping document functions to specific datasets
@@ -167,12 +167,12 @@ export interface BlueprintMediaLibraryAssetFunctionResource extends BlueprintBas
167
167
  }
168
168
 
169
169
  /**
170
- * A function resource triggered by schedule events
170
+ * A function resource triggered by scheduled events
171
171
  * @category Functions Types
172
172
  */
173
- export interface BlueprintScheduleFunctionResource extends BlueprintBaseFunctionResource {
173
+ export interface BlueprintScheduledFunctionResource extends BlueprintBaseFunctionResource {
174
174
  type: 'sanity.function.cron'
175
- event: BlueprintScheduleFunctionResourceEvent
175
+ event: BlueprintScheduledFunctionResourceEvent
176
176
  timezone?: string
177
177
  }
178
178
 
@@ -226,14 +226,14 @@ export type BlueprintMediaLibraryAssetFunctionConfig = Omit<BlueprintMediaLibrar
226
226
  }
227
227
 
228
228
  /**
229
- * Configuration for defining a schedule function.
229
+ * Configuration for defining a scheduled function.
230
230
  * @public
231
- * @alpha Deploying Schedule Functions via Blueprints is experimental. This feature is not available publicly yet.
231
+ * @alpha Deploying Scheduled Functions via Blueprints is experimental. This feature is not available publicly yet.
232
232
  * @hidden
233
233
  * @category Functions Types
234
234
  * @interface
235
235
  */
236
- export type BlueprintScheduleFunctionConfig = Omit<BlueprintScheduleFunctionResource, 'type' | 'src'> & {
236
+ export type BlueprintScheduledFunctionConfig = Omit<BlueprintScheduledFunctionResource, 'type' | 'src'> & {
237
237
  /**
238
238
  * Path to the function source code
239
239
  * @defaultValue `functions/${name}`
@@ -25,12 +25,25 @@ export interface BlueprintOwnershipAttachAction {
25
25
  export interface BlueprintOwnershipDetachAction {
26
26
  type: 'detach'
27
27
  }
28
+ /**
29
+ * An ownership action that will cause the resource to be read during deployment so it can be referenced
30
+ * by other resources. Note that referenced resources must be in the same project or organization as the
31
+ * current stack.
32
+ * @category Blueprint Internals
33
+ */
34
+ export interface BlueprintOwnershipReferenceAction {
35
+ type: 'reference'
36
+ /** The name or id of the stack where the resource is attached */
37
+ stack: string
38
+ /** The name of the resource to be referenced */
39
+ name: string
40
+ }
28
41
  /**
29
42
  * A union of all possible ownership actions.
30
43
  * @category Blueprint Internals
31
44
  * @expand
32
45
  */
33
- export type BlueprintOwnershipAction = BlueprintOwnershipAttachAction | BlueprintOwnershipDetachAction
46
+ export type BlueprintOwnershipAction = BlueprintOwnershipAttachAction | BlueprintOwnershipDetachAction | BlueprintOwnershipReferenceAction
34
47
 
35
48
  /**
36
49
  * An ownership action that will cause the referenced project-contained resource to be attached to the current stack.
@@ -45,7 +58,10 @@ export interface BlueprintProjectOwnershipAttachAction extends BlueprintOwnershi
45
58
  * @category Blueprint Internals
46
59
  * @expand
47
60
  */
48
- export type BlueprintProjectOwnershipAction = BlueprintProjectOwnershipAttachAction | BlueprintOwnershipDetachAction
61
+ export type BlueprintProjectOwnershipAction =
62
+ | BlueprintProjectOwnershipAttachAction
63
+ | BlueprintOwnershipDetachAction
64
+ | BlueprintOwnershipReferenceAction
49
65
 
50
66
  /**
51
67
  * Defines the lifcycle policy for this resource.
@@ -98,3 +114,25 @@ export interface BlueprintResource<Lifecycle extends BlueprintResourceLifecycle
98
114
  */
99
115
  lifecycle?: Lifecycle
100
116
  }
117
+
118
+ /**
119
+ * The configuration for referencing a resource in another stack.
120
+ * @category Blueprint Internals
121
+ */
122
+ export interface BlueprintCrossStackReferenceConfig {
123
+ /** The name of the resource in the other stack */
124
+ name: string
125
+ /** The name or id of the stack being referenced */
126
+ stack: string
127
+ /** The local name of the resource, defaults to the name of the other resource */
128
+ localName?: string
129
+ }
130
+
131
+ /**
132
+ * The configuration for referencing a resource in another stack including the type of resource.
133
+ * @category Blueprint Internals
134
+ */
135
+ export interface BlueprintCrossStackReferenceResourceConfig extends BlueprintCrossStackReferenceConfig {
136
+ /** The type of resource being referenced (e.g. `sanity.project`) */
137
+ type: string
138
+ }
@@ -428,7 +428,7 @@ function parseExpressionInternal(expression: string): {
428
428
  if (errors.length === 0) {
429
429
  errors.push({
430
430
  type: 'invalid_value',
431
- message: `Could not parse schedule expression \`${trimmed}\``,
431
+ message: `Could not parse scheduled expression \`${trimmed}\``,
432
432
  })
433
433
  }
434
434
  return {errors, cron: null}
@@ -438,7 +438,7 @@ function parseExpressionInternal(expression: string): {
438
438
  }
439
439
 
440
440
  /**
441
- * Validates a schedule expression and returns any errors found.
441
+ * Validates a scheduled expression and returns any errors found.
442
442
  *
443
443
  * Checks for:
444
444
  * - Empty expressions
@@ -447,11 +447,11 @@ function parseExpressionInternal(expression: string): {
447
447
  * - Invalid day of month values
448
448
  * - Unrecognizable expressions
449
449
  *
450
- * @param expression Natural language schedule or cron expression
450
+ * @param expression Natural language scheduled or cron expression
451
451
  * @returns Array of validation errors, empty if valid
452
452
  * @internal
453
453
  */
454
- export function validateScheduleExpression(expression: string): BlueprintError[] {
454
+ export function validateScheduledExpression(expression: string): BlueprintError[] {
455
455
  if (!expression || expression.trim() === '') {
456
456
  return [{type: 'invalid_value', message: '`expression` cannot be empty'}]
457
457
  }
@@ -461,7 +461,7 @@ export function validateScheduleExpression(expression: string): BlueprintError[]
461
461
  }
462
462
 
463
463
  /**
464
- * Parse a natural language schedule expression into a cron expression.
464
+ * Parse a natural language scheduled expression into a cron expression.
465
465
  *
466
466
  * The parser is **forgiving** and **freeform** - it extracts recognized parts
467
467
  * (days, times, intervals) and ignores unrecognized tokens like "at", "on", "the".
@@ -479,13 +479,13 @@ export function validateScheduleExpression(expression: string): BlueprintError[]
479
479
  * If the input is already a valid cron expression, it is returned unchanged.
480
480
  *
481
481
  * This function assumes the expression has already been validated via
482
- * `validateScheduleExpression`. For invalid input, behavior is undefined.
482
+ * `validateScheduledExpression`. For invalid input, behavior is undefined.
483
483
  *
484
- * @param expression Natural language schedule or cron expression
484
+ * @param expression Natural language scheduled or cron expression
485
485
  * @returns Cron expression string
486
486
  * @internal
487
487
  */
488
- export function parseScheduleExpression(expression: string): string {
488
+ export function parseScheduledExpression(expression: string): string {
489
489
  const trimmed = expression.trim()
490
490
 
491
491
  // Fast path for cron expressions
@@ -495,7 +495,7 @@ export function parseScheduleExpression(expression: string): string {
495
495
 
496
496
  const result = parseExpressionInternal(expression)
497
497
  if (!result.cron) {
498
- // Should never happen if validateScheduleExpression was called first
498
+ // Should never happen if validateScheduledExpression was called first
499
499
  throw new Error(`Failed to parse schedule expression: ${expression}`)
500
500
  }
501
501
  return result.cron
@@ -17,6 +17,13 @@ export function validateDataset(resource: unknown): BlueprintError[] {
17
17
  errors.push({type: 'invalid_value', message: 'Dataset type must be `sanity.project.dataset`'})
18
18
  }
19
19
 
20
+ // validate description if provided
21
+ if ('description' in resource) {
22
+ if (typeof resource.description !== 'string') {
23
+ errors.push({type: 'invalid_type', message: 'Dataset description must be a string'})
24
+ }
25
+ }
26
+
20
27
  // validate ACL mode if provided
21
28
  if ('aclMode' in resource) {
22
29
  if (typeof resource.aclMode !== 'string') {
@@ -7,7 +7,7 @@ import {
7
7
  VALID_RUNTIMES,
8
8
  validateResource,
9
9
  } from '../index.js'
10
- import {validateScheduleExpression} from '../utils/schedule-parser.js'
10
+ import {validateScheduledExpression} from '../utils/schedule-parser.js'
11
11
 
12
12
  type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
13
13
  const BASE_EVENT_KEYS = new Set<BaseFunctionEventKey>(['on', 'filter', 'projection', 'includeDrafts'])
@@ -189,23 +189,23 @@ function validateMediaLibraryFunctionEvent(event: unknown): BlueprintError[] {
189
189
  }
190
190
 
191
191
  /**
192
- * Validates a schedule function resource configuration.
192
+ * Validates a scheduled function resource configuration.
193
193
  * @param functionResource The function resource to validate
194
194
  * @alpha
195
195
  * @hidden
196
196
  * @category Functions Types
197
197
  * @returns Array of validation errors, empty if valid
198
198
  */
199
- export function validateScheduleFunction(functionResource: unknown): BlueprintError[] {
199
+ export function validateScheduledFunction(functionResource: unknown): BlueprintError[] {
200
200
  if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
201
201
  if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
202
202
 
203
203
  const errors: BlueprintError[] = []
204
204
 
205
205
  if ('event' in functionResource) {
206
- errors.push(...validateScheduleFunctionEvent(functionResource.event))
206
+ errors.push(...validateScheduledFunctionEvent(functionResource.event))
207
207
  } else {
208
- errors.push({type: 'missing_parameter', message: '`event` is required for a schedule function'})
208
+ errors.push({type: 'missing_parameter', message: '`event` is required for a scheduled function'})
209
209
  }
210
210
 
211
211
  if ('type' in functionResource && functionResource.type !== 'sanity.function.cron') {
@@ -213,7 +213,7 @@ export function validateScheduleFunction(functionResource: unknown): BlueprintEr
213
213
  }
214
214
 
215
215
  if ('timezone' in functionResource) {
216
- errors.push(...validateScheduleFunctionTimezone(functionResource.timezone))
216
+ errors.push(...validateScheduledFunctionTimezone(functionResource.timezone))
217
217
  }
218
218
 
219
219
  errors.push(...validateFunction(functionResource))
@@ -222,11 +222,11 @@ export function validateScheduleFunction(functionResource: unknown): BlueprintEr
222
222
  }
223
223
 
224
224
  /**
225
- * Validates a schedule function event configuration.
225
+ * Validates a scheduled function event configuration.
226
226
  * @param event The event configuration to validate
227
227
  * @returns Array of validation errors, empty if valid
228
228
  */
229
- function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
229
+ function validateScheduledFunctionEvent(event: unknown): BlueprintError[] {
230
230
  if (!event) return [{type: 'invalid_value', message: 'Function event must be provided'}]
231
231
  if (typeof event !== 'object') return [{type: 'invalid_type', message: 'Function event must be an object'}]
232
232
 
@@ -250,7 +250,7 @@ function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
250
250
  if (typeof expr !== 'string') {
251
251
  errors.push({type: 'invalid_type', message: '`expression` must be a string'})
252
252
  } else {
253
- errors.push(...validateScheduleExpression(expr))
253
+ errors.push(...validateScheduledExpression(expr))
254
254
  }
255
255
  } else if (hasExplicitFields) {
256
256
  if (!('minute' in event)) {
@@ -299,11 +299,11 @@ function validateScheduleFunctionEvent(event: unknown): BlueprintError[] {
299
299
  }
300
300
 
301
301
  /**
302
- * Validates a schedule function timezone configuration.
302
+ * Validates a scheduled function timezone configuration.
303
303
  * @param timezone The timezone to validate
304
304
  * @returns Array of validation errors, empty if valid
305
305
  */
306
- function validateScheduleFunctionTimezone(timezone: unknown): BlueprintError[] {
306
+ function validateScheduledFunctionTimezone(timezone: unknown): BlueprintError[] {
307
307
  if (typeof timezone !== 'string') return [{type: 'invalid_type', message: 'Function timezone must be a string'}]
308
308
 
309
309
  const errors: BlueprintError[] = []
@@ -39,7 +39,7 @@ export function validateResource(resource: unknown, options?: {projectContained?
39
39
  }
40
40
 
41
41
  if ('ownershipAction' in resource.lifecycle) {
42
- const ownershipActionTypes = ['attach', 'detach']
42
+ const ownershipActionTypes = ['attach', 'detach', 'reference']
43
43
  const ownershipAction = resource.lifecycle.ownershipAction
44
44
 
45
45
  if (typeof ownershipAction !== 'object' || ownershipAction === null) {
@@ -65,6 +65,18 @@ export function validateResource(resource: unknown, options?: {projectContained?
65
65
  errors.push({type: 'invalid_type', message: '`ownershipAction.projectId` must be a string'})
66
66
  }
67
67
  }
68
+ } else if (ownershipAction.type === 'reference') {
69
+ if (!('name' in ownershipAction)) {
70
+ errors.push({type: 'missing_parameter', message: '`ownershipAction.name` is required for reference'})
71
+ } else if (typeof ownershipAction.name !== 'string') {
72
+ errors.push({type: 'invalid_type', message: '`ownershipAction.name` must be a string'})
73
+ }
74
+
75
+ if (!('stack' in ownershipAction)) {
76
+ errors.push({type: 'missing_parameter', message: '`ownershipAction.stack` is required for reference'})
77
+ } else if (typeof ownershipAction.stack !== 'string') {
78
+ errors.push({type: 'invalid_type', message: '`ownershipAction.stack` must be a string'})
79
+ }
68
80
  }
69
81
  }
70
82
  }