@sanity/blueprints 0.34.1 → 0.36.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 (164) hide show
  1. package/README.md +1 -2
  2. package/dist/definers/agents.d.ts +3 -1
  3. package/dist/definers/agents.js +4 -2
  4. package/dist/definers/agents.js.map +1 -1
  5. package/dist/definers/applications.d.ts +30 -18
  6. package/dist/definers/applications.js +36 -24
  7. package/dist/definers/applications.js.map +1 -1
  8. package/dist/definers/blueprints.d.ts +1 -0
  9. package/dist/definers/blueprints.js +5 -27
  10. package/dist/definers/blueprints.js.map +1 -1
  11. package/dist/definers/context.d.ts +6 -2
  12. package/dist/definers/context.js +8 -4
  13. package/dist/definers/context.js.map +1 -1
  14. package/dist/definers/cors.d.ts +11 -1
  15. package/dist/definers/cors.js +11 -13
  16. package/dist/definers/cors.js.map +1 -1
  17. package/dist/definers/datasets.d.ts +30 -3
  18. package/dist/definers/datasets.js +32 -16
  19. package/dist/definers/datasets.js.map +1 -1
  20. package/dist/definers/functions/document.d.ts +29 -0
  21. package/dist/definers/functions/document.js +1 -1
  22. package/dist/definers/functions/document.js.map +1 -1
  23. package/dist/definers/functions/durable.d.ts +4 -3
  24. package/dist/definers/functions/durable.js +5 -4
  25. package/dist/definers/functions/durable.js.map +1 -1
  26. package/dist/definers/functions/index.js +1 -1
  27. package/dist/definers/functions/index.js.map +1 -1
  28. package/dist/definers/functions/media-library.d.ts +30 -0
  29. package/dist/definers/functions/media-library.js +19 -21
  30. package/dist/definers/functions/media-library.js.map +1 -1
  31. package/dist/definers/functions/pubsub.d.ts +2 -3
  32. package/dist/definers/functions/pubsub.js +3 -4
  33. package/dist/definers/functions/pubsub.js.map +1 -1
  34. package/dist/definers/functions/queue.d.ts +26 -6
  35. package/dist/definers/functions/queue.js +31 -10
  36. package/dist/definers/functions/queue.js.map +1 -1
  37. package/dist/definers/functions/scheduled.d.ts +7 -6
  38. package/dist/definers/functions/scheduled.js +8 -7
  39. package/dist/definers/functions/scheduled.js.map +1 -1
  40. package/dist/definers/functions/sync-tag-invalidate.d.ts +5 -2
  41. package/dist/definers/functions/sync-tag-invalidate.js +6 -3
  42. package/dist/definers/functions/sync-tag-invalidate.js.map +1 -1
  43. package/dist/definers/installation-config.d.ts +4 -2
  44. package/dist/definers/installation-config.js +5 -3
  45. package/dist/definers/installation-config.js.map +1 -1
  46. package/dist/definers/permissions.d.ts +8 -2
  47. package/dist/definers/permissions.js +12 -5
  48. package/dist/definers/permissions.js.map +1 -1
  49. package/dist/definers/projects.d.ts +3 -4
  50. package/dist/definers/projects.js +4 -5
  51. package/dist/definers/projects.js.map +1 -1
  52. package/dist/definers/resources.d.ts +1 -0
  53. package/dist/definers/resources.js +1 -11
  54. package/dist/definers/resources.js.map +1 -1
  55. package/dist/definers/robotTokens.d.ts +22 -4
  56. package/dist/definers/robotTokens.js +53 -20
  57. package/dist/definers/robotTokens.js.map +1 -1
  58. package/dist/definers/roles.d.ts +49 -9
  59. package/dist/definers/roles.js +54 -55
  60. package/dist/definers/roles.js.map +1 -1
  61. package/dist/definers/studios.d.ts +5 -3
  62. package/dist/definers/studios.js +6 -4
  63. package/dist/definers/studios.js.map +1 -1
  64. package/dist/definers/webhooks.d.ts +19 -1
  65. package/dist/definers/webhooks.js +18 -22
  66. package/dist/definers/webhooks.js.map +1 -1
  67. package/dist/index.d.ts +18 -18
  68. package/dist/index.js +19 -20
  69. package/dist/index.js.map +1 -1
  70. package/dist/resolve/index.d.ts +2 -3
  71. package/dist/resolve/index.js +2 -3
  72. package/dist/resolve/index.js.map +1 -1
  73. package/dist/types/agents.d.ts +7 -3
  74. package/dist/types/applications.d.ts +68 -50
  75. package/dist/types/applications.js +9 -6
  76. package/dist/types/applications.js.map +1 -1
  77. package/dist/types/context.d.ts +17 -7
  78. package/dist/types/cors.d.ts +0 -2
  79. package/dist/types/datasets.d.ts +0 -6
  80. package/dist/types/functions/index.d.ts +96 -21
  81. package/dist/types/functions/index.js +2 -1
  82. package/dist/types/functions/index.js.map +1 -1
  83. package/dist/types/installation-config.d.ts +14 -11
  84. package/dist/types/permissions.d.ts +8 -4
  85. package/dist/types/projects.d.ts +5 -4
  86. package/dist/types/robotTokens.d.ts +14 -4
  87. package/dist/types/roles.d.ts +10 -9
  88. package/dist/types/studios.d.ts +29 -14
  89. package/dist/types/studios.js +3 -2
  90. package/dist/types/studios.js.map +1 -1
  91. package/dist/types/webhooks.d.ts +0 -3
  92. package/dist/utils/debounce.d.ts +0 -11
  93. package/dist/utils/debounce.js +0 -22
  94. package/dist/utils/debounce.js.map +1 -1
  95. package/dist/utils/parse-duration.d.ts +13 -0
  96. package/dist/utils/parse-duration.js +24 -0
  97. package/dist/utils/parse-duration.js.map +1 -1
  98. package/dist/utils/throttle.d.ts +9 -0
  99. package/dist/utils/throttle.js +22 -0
  100. package/dist/utils/throttle.js.map +1 -0
  101. package/dist/utils/validation.d.ts +14 -11
  102. package/dist/utils/validation.js +25 -17
  103. package/dist/utils/validation.js.map +1 -1
  104. package/dist/validation/applications.js +7 -2
  105. package/dist/validation/applications.js.map +1 -1
  106. package/dist/validation/blueprints.js +120 -4
  107. package/dist/validation/blueprints.js.map +1 -1
  108. package/dist/validation/functions.d.ts +19 -0
  109. package/dist/validation/functions.js +169 -36
  110. package/dist/validation/functions.js.map +1 -1
  111. package/dist/validation/projects.js +3 -0
  112. package/dist/validation/projects.js.map +1 -1
  113. package/dist/validation/resources.js +1 -1
  114. package/dist/validation/resources.js.map +1 -1
  115. package/dist/validation/studios.js +4 -0
  116. package/dist/validation/studios.js.map +1 -1
  117. package/package.json +1 -1
  118. package/src/definers/agents.ts +4 -2
  119. package/src/definers/applications.ts +36 -24
  120. package/src/definers/blueprints.ts +4 -27
  121. package/src/definers/context.ts +8 -4
  122. package/src/definers/cors.ts +11 -13
  123. package/src/definers/datasets.ts +40 -16
  124. package/src/definers/functions/document.ts +17 -25
  125. package/src/definers/functions/durable.ts +5 -4
  126. package/src/definers/functions/index.ts +1 -1
  127. package/src/definers/functions/media-library.ts +19 -21
  128. package/src/definers/functions/pubsub.ts +3 -4
  129. package/src/definers/functions/queue.ts +31 -10
  130. package/src/definers/functions/scheduled.ts +8 -7
  131. package/src/definers/functions/sync-tag-invalidate.ts +6 -3
  132. package/src/definers/installation-config.ts +5 -3
  133. package/src/definers/permissions.ts +14 -6
  134. package/src/definers/projects.ts +4 -5
  135. package/src/definers/resources.ts +1 -11
  136. package/src/definers/robotTokens.ts +55 -20
  137. package/src/definers/roles.ts +60 -58
  138. package/src/definers/studios.ts +6 -4
  139. package/src/definers/webhooks.ts +18 -23
  140. package/src/index.ts +20 -21
  141. package/src/resolve/index.ts +2 -4
  142. package/src/types/agents.ts +7 -3
  143. package/src/types/applications.ts +68 -50
  144. package/src/types/context.ts +17 -7
  145. package/src/types/cors.ts +0 -2
  146. package/src/types/datasets.ts +0 -6
  147. package/src/types/functions/index.ts +100 -21
  148. package/src/types/installation-config.ts +14 -11
  149. package/src/types/permissions.ts +8 -4
  150. package/src/types/projects.ts +6 -4
  151. package/src/types/robotTokens.ts +15 -4
  152. package/src/types/roles.ts +10 -9
  153. package/src/types/studios.ts +33 -14
  154. package/src/types/webhooks.ts +0 -3
  155. package/src/utils/debounce.ts +0 -25
  156. package/src/utils/parse-duration.ts +27 -0
  157. package/src/utils/throttle.ts +25 -0
  158. package/src/utils/validation.ts +24 -17
  159. package/src/validation/applications.ts +6 -2
  160. package/src/validation/blueprints.ts +132 -5
  161. package/src/validation/functions.ts +183 -37
  162. package/src/validation/projects.ts +4 -0
  163. package/src/validation/resources.ts +1 -1
  164. package/src/validation/studios.ts +3 -0
@@ -1,29 +1,50 @@
1
1
  import {type BlueprintQueueFunctionConfig, type BlueprintQueueFunctionResource, validateQueueFunction} from '../../index.js'
2
2
  import {createDebounceObject} from '../../utils/debounce.js'
3
+ import {createThrottleObject} from '../../utils/throttle.js'
3
4
  import {runValidation} from '../../utils/validation.js'
4
5
  import {defineFunction} from './index.js'
5
6
  /**
6
7
  * Defines a function that provide queueing behaviour.
7
8
  *
9
+ * Deploying Queue Functions via Blueprints is experimental. This feature is not available publicly yet.
10
+ *
8
11
  * @remarks
9
- * Using the reasonable defaults of concurrency: 1, fifo: true, and dlq: true
12
+ * Fields left out use the functions service defaults: `concurrency: {limit: 2, fifo: false}` and `maxAttempts: 3`.
13
+ * Changing `concurrency.fifo` on a deployed function replaces its queue, and messages still waiting in the old queue are lost.
14
+ *
15
+ * @example
10
16
  * ```ts
11
17
  * defineQueueFunction({
12
18
  * name: 'send-email',
13
19
  * })
14
20
  * ```
15
21
  *
16
- * Specifying a concurrency of 5
22
+ * @example Processing messages one at a time, in order
23
+ * ```ts
24
+ * defineQueueFunction({
25
+ * name: 'sync-inventory',
26
+ * concurrency: {limit: 1, fifo: true},
27
+ * })
28
+ * ```
29
+ *
30
+ * @example Keeping each document's messages in order, with up to 5 documents at once
17
31
  * ```ts
18
32
  * defineQueueFunction({
19
33
  * name: 'bustin-caches',
20
- * concurrency: 5,
21
- * fifo: true,
22
- * dlq: true,
34
+ * concurrency: {limit: 5, fifo: true, key: 'event.data.documentId'},
35
+ * maxAttempts: 5,
36
+ * })
37
+ * ```
38
+ *
39
+ * @example Starting at most 100 messages per minute, paced per tenant, with bursts of up to 10
40
+ * ```ts
41
+ * defineQueueFunction({
42
+ * name: 'call-rate-limited-api',
43
+ * throttle: {limit: 100, period: '1 minute', burst: 10, key: 'event.data.tenantId'},
23
44
  * })
24
45
  * ```
25
46
  * @public
26
- * @alpha Deploying Queue Functions via Blueprints is experimental. This feature is not available publicly yet.
47
+ * @alpha
27
48
  * @hidden
28
49
  * @category Definers
29
50
  * @expandType BlueprintQueueFunctionConfig
@@ -31,19 +52,19 @@ import {defineFunction} from './index.js'
31
52
  * @returns The validated queue function resource
32
53
  */
33
54
  export function defineQueueFunction(functionConfig: BlueprintQueueFunctionConfig): BlueprintQueueFunctionResource {
34
- const {concurrency = 1, fifo = true, dlq = true, debounce, event} = functionConfig
55
+ const {concurrency, debounce, event, maxAttempts, throttle} = functionConfig
35
56
 
36
57
  const functionResource: BlueprintQueueFunctionResource = {
37
58
  ...defineFunction(functionConfig, {skipValidation: true}),
38
59
  type: 'sanity.function.queue',
39
60
  ...(concurrency !== undefined && {concurrency}),
40
61
  ...(debounce !== undefined && {debounce: createDebounceObject(debounce)}),
41
- ...(dlq !== undefined && {dlq}),
42
62
  ...(event !== undefined && {event}),
43
- ...(fifo !== undefined && {fifo}),
63
+ ...(maxAttempts !== undefined && {maxAttempts}),
64
+ ...(throttle !== undefined && {throttle: createThrottleObject(throttle)}),
44
65
  }
45
66
 
46
- runValidation(() => validateQueueFunction(functionResource))
67
+ runValidation(functionResource, () => validateQueueFunction(functionResource))
47
68
 
48
69
  return functionResource
49
70
  }
@@ -21,7 +21,11 @@ const SCHEDULED_EVENT_KEYS = new Set<ScheduledFunctionEventKey>(['minute', 'hour
21
21
  * Supports cron expressions or natural language schedules.
22
22
  *
23
23
  * @remarks
24
- * Using a cron expression:
24
+ * The `event.expression` field accepts standard cron expressions or natural language:
25
+ * `'every 15 minutes'`, `'weekdays at 8am'`, `'fridays in the evening'`,
26
+ * `'mon, wed, fri at 9am'`, `'first of the month at noon'`
27
+ *
28
+ * @example
25
29
  * ```ts
26
30
  * defineScheduledFunction({
27
31
  * name: 'daily-cleanup',
@@ -29,7 +33,7 @@ const SCHEDULED_EVENT_KEYS = new Set<ScheduledFunctionEventKey>(['minute', 'hour
29
33
  * })
30
34
  * ```
31
35
  *
32
- * Using explicit cron fields:
36
+ * @example Explicit cron fields
33
37
  * ```ts
34
38
  * defineScheduledFunction({
35
39
  * name: 'daily-cleanup',
@@ -37,10 +41,7 @@ const SCHEDULED_EVENT_KEYS = new Set<ScheduledFunctionEventKey>(['minute', 'hour
37
41
  * })
38
42
  * ```
39
43
  *
40
- * The `event.expression` field accepts standard cron expressions or natural language:
41
- * `'every 15 minutes'`, `'weekdays at 8am'`, `'fridays in the evening'`,
42
- * `'mon, wed, fri at 9am'`, `'first of the month at noon'`
43
- *
44
+ * @example Natural language schedule
44
45
  * ```ts
45
46
  * defineScheduledFunction({
46
47
  * name: 'daily-cleanup',
@@ -70,7 +71,7 @@ export function defineScheduledFunction(functionConfig: BlueprintScheduledFuncti
70
71
  functionResource.event = cronStringToExplicitEvent(cron)
71
72
  }
72
73
 
73
- runValidation(() => validateScheduledFunction(functionResource))
74
+ runValidation(functionResource, () => validateScheduledFunction(functionResource))
74
75
 
75
76
  return functionResource
76
77
  }
@@ -9,14 +9,17 @@ import {defineFunction} from './index.js'
9
9
  * Defines a function that is triggered on a sync tag invalidate event.
10
10
  *
11
11
  * @remarks
12
- * Using implicit event scoping to all datasets in the function project:
12
+ * By default, the event covers all datasets in the function project.
13
+ * Set `event.resource` to scope the event to one dataset.
14
+ *
15
+ * @example
13
16
  * ```ts
14
17
  * defineSyncTagInvalidateFunction({
15
18
  * name: 'bustin-caches',
16
19
  * })
17
20
  * ```
18
21
  *
19
- * Using explicit event scoping to a particular dataset in the function project:
22
+ * @example Scoped to one dataset
20
23
  * ```ts
21
24
  * defineSyncTagInvalidateFunction({
22
25
  * name: 'bustin-caches',
@@ -45,7 +48,7 @@ export function defineSyncTagInvalidateFunction(
45
48
  ...(event?.resource ? {event} : {}),
46
49
  }
47
50
 
48
- runValidation(() => validateSyncTagInvalidateFunction(functionResource))
51
+ runValidation(functionResource, () => validateSyncTagInvalidateFunction(functionResource))
49
52
 
50
53
  return functionResource
51
54
  }
@@ -4,12 +4,15 @@ import {runValidation} from '../utils/validation.js'
4
4
  /**
5
5
  * Defines a singleton Media Library configuration.
6
6
  *
7
+ * Configuring the Media Library via Blueprints is experimental. This may be subject to breaking changes.
8
+ *
7
9
  * @remarks
8
10
  * The Media Library is a Sanity-owned singleton, so this resource carries no
9
11
  * application identity of its own — only the config `root` and its `fields`.
10
12
  * The provider resolves the organization's media library installation id at
11
13
  * deploy time.
12
14
  *
15
+ * @example
13
16
  * ```ts
14
17
  * defineMediaLibraryConfig({
15
18
  * name: 'media-library',
@@ -19,11 +22,10 @@ import {runValidation} from '../utils/validation.js'
19
22
  * ```
20
23
  * @param config The Media Library configuration
21
24
  * @public
22
- * @beta Configuring the Media Library via Blueprints is experimental. This may be subject to breaking changes.
25
+ * @beta
23
26
  * @category Definers
24
27
  * @expandType BlueprintMediaLibraryConfigConfig
25
28
  * @returns The Media Library configuration resource
26
- * @hidden
27
29
  */
28
30
  export function defineMediaLibraryConfig(config: BlueprintMediaLibraryConfigConfig): BlueprintMediaLibraryConfigResource {
29
31
  const resource = {
@@ -32,7 +34,7 @@ export function defineMediaLibraryConfig(config: BlueprintMediaLibraryConfigConf
32
34
  appType: 'media-library',
33
35
  } satisfies BlueprintMediaLibraryConfigResource
34
36
 
35
- runValidation(() => validateMediaLibraryConfig(resource))
37
+ runValidation(resource, () => validateMediaLibraryConfig(resource))
36
38
 
37
39
  return resource
38
40
  }
@@ -10,6 +10,9 @@ import {runValidation} from '../utils/validation.js'
10
10
  /**
11
11
  * Defines a custom permission that roles can grant, in the shape of the Access API.
12
12
  *
13
+ * Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
14
+ *
15
+ * @example
13
16
  * ```ts
14
17
  * definePermission({
15
18
  * name: 'articles-only',
@@ -42,7 +45,7 @@ import {runValidation} from '../utils/validation.js'
42
45
  * ```
43
46
  * @param parameters The configuration of the permission
44
47
  * @public
45
- * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
48
+ * @beta
46
49
  * @category Definers
47
50
  * @expandType BlueprintPermissionConfig
48
51
  * @returns The permission resource
@@ -53,7 +56,7 @@ export function definePermission(parameters: BlueprintPermissionConfig): Bluepri
53
56
  type: 'sanity.access.permission',
54
57
  }
55
58
 
56
- runValidation(() => validatePermission(permissionResource))
59
+ runValidation(permissionResource, () => validatePermission(permissionResource))
57
60
 
58
61
  return permissionResource
59
62
  }
@@ -61,6 +64,9 @@ export function definePermission(parameters: BlueprintPermissionConfig): Bluepri
61
64
  /**
62
65
  * Defines a content resource as the Manage UI makes it: a custom permission of type `sanity.document.filter.mode`.
63
66
  *
67
+ * Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
68
+ *
69
+ * @example
64
70
  * ```ts
65
71
  * defineContentResource({
66
72
  * name: 'movies',
@@ -81,7 +87,7 @@ export function definePermission(parameters: BlueprintPermissionConfig): Bluepri
81
87
  * ```
82
88
  * @param parameters The configuration of the content resource
83
89
  * @public
84
- * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
90
+ * @beta
85
91
  * @category Definers
86
92
  * @expandType BlueprintContentResourceConfig
87
93
  * @returns The permission resource
@@ -89,13 +95,15 @@ export function definePermission(parameters: BlueprintPermissionConfig): Bluepri
89
95
  export function defineContentResource(parameters: BlueprintContentResourceConfig): BlueprintPermissionResource {
90
96
  const {documentTypes, filter, ...permission} = parameters
91
97
 
92
- runValidation(() => validateContentResource(parameters))
93
-
94
- return definePermission({
98
+ const permissionResource = definePermission({
95
99
  ...permission,
96
100
  permissionType: 'sanity.document.filter.mode',
97
101
  config: {filter: contentResourceFilter(documentTypes, filter)},
98
102
  })
103
+
104
+ runValidation(permissionResource, () => validateContentResource(parameters))
105
+
106
+ return permissionResource
99
107
  }
100
108
 
101
109
  function contentResourceFilter(documentTypes: unknown, filter?: string): string {
@@ -11,19 +11,19 @@ import {runValidation} from '../utils/validation.js'
11
11
  /**
12
12
  * Defines a project.
13
13
  *
14
+ * @example
14
15
  * ```ts
15
16
  * defineProject({
16
17
  * name: 'my-project',
17
18
  * displayName: 'My Project',
19
+ * logo: './logo.png',
18
20
  * })
19
21
  * ```
20
22
  * @param parameters The project configuration
21
23
  * @public
22
- * @beta Deploying Projects via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
23
24
  * @category Definers
24
25
  * @expandType BlueprintProjectConfig
25
26
  * @returns The project resource
26
- * @hidden
27
27
  */
28
28
  export function defineProject(parameters: BlueprintProjectConfig): BlueprintProjectResource {
29
29
  // default project name
@@ -35,7 +35,7 @@ export function defineProject(parameters: BlueprintProjectConfig): BlueprintProj
35
35
  type: 'sanity.project',
36
36
  }
37
37
 
38
- runValidation(() => validateProject(projectResource))
38
+ runValidation(projectResource, () => validateProject(projectResource))
39
39
 
40
40
  return projectResource
41
41
  }
@@ -43,6 +43,7 @@ export function defineProject(parameters: BlueprintProjectConfig): BlueprintProj
43
43
  /**
44
44
  * Creates a reference to a project in another stack.
45
45
  *
46
+ * @example
46
47
  * ```ts
47
48
  * referenceProject({
48
49
  * name: 'editorial-project',
@@ -52,10 +53,8 @@ export function defineProject(parameters: BlueprintProjectConfig): BlueprintProj
52
53
  *
53
54
  * @param params The parameters for referencing the project
54
55
  * @public
55
- * @beta Referencing Projects via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
56
56
  * @category Referencers
57
57
  * @returns The project reference
58
- * @hidden
59
58
  */
60
59
  export function referenceProject({name, stack, localName}: BlueprintCrossStackReferenceConfig): BlueprintResource {
61
60
  return referenceResource({name, stack, localName, type: 'sanity.project'})
@@ -1,22 +1,12 @@
1
1
  import {assertResource, type BlueprintCrossStackReferenceResourceConfig, type BlueprintResource} from '../index.js'
2
2
 
3
- /*
4
- * FUTURE example (move below @example when ready)
5
- * @example With lifecycle
6
- * ```ts
7
- * defineResource({
8
- * name: 'my-resource',
9
- * type: 'sanity.new.resource',
10
- * lifecycle: {deletionPolicy: 'retain'},
11
- * })
12
- * ```
13
- */
14
3
  /**
15
4
  * Defines a generic resource to be managed in Blueprints.
16
5
  *
17
6
  * @remarks
18
7
  * This is useful if the resource type does not yet have a `define*` function.
19
8
  *
9
+ * @example
20
10
  * ```ts
21
11
  * defineResource({
22
12
  * name: 'my-resource',
@@ -1,21 +1,37 @@
1
1
  import {
2
2
  type BlueprintCrossStackReferenceConfig,
3
+ type BlueprintError,
3
4
  type BlueprintResource,
4
5
  type BlueprintRobotTokenConfig,
5
6
  type BlueprintRobotTokenResource,
7
+ type RobotTokenMembership,
8
+ type RobotTokenMembershipConfig,
6
9
  referenceResource,
7
10
  validateRobotToken,
8
11
  } from '../index.js'
9
12
  import {runValidation} from '../utils/validation.js'
10
13
 
11
- /*
12
- * FUTURE example (move below @example when ready)
14
+ /**
15
+ * Defines a Robot Token for automated access. Has a token property provided during deployment that can be referenced by other resources.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * defineRobotToken({
20
+ * name: 'my-robot',
21
+ * memberships: [{
22
+ * project: 'my-project-id',
23
+ * roleNames: ['editor'],
24
+ * }],
25
+ * })
26
+ * ```
27
+ *
13
28
  * @example Cross-resource references
14
29
  * ```ts
15
30
  * defineRole({
16
31
  * name: 'ci-deploy-role',
17
32
  * title: 'CI Deploy Role',
18
33
  * appliesToRobots: true,
34
+ * appliesToUsers: false,
19
35
  * permissions: [{name: 'sanity-project-cors', action: 'create'}],
20
36
  * })
21
37
  *
@@ -27,44 +43,64 @@ import {runValidation} from '../utils/validation.js'
27
43
  * }],
28
44
  * })
29
45
  * ```
30
- */
31
- /**
32
- * Defines a Robot Token for automated access. Has a token property provided during deployment that can be referenced by other resources.
33
- *
34
- * ```ts
35
- * defineRobotToken({
36
- * name: 'my-robot',
37
- * memberships: [{
38
- * resourceType: 'project',
39
- * resourceId: projectId,
40
- * roleNames: ['editor'],
41
- * }],
42
- * })
43
- * ```
44
46
  * @param parameters The robot token configuration
45
47
  * @public
46
- * @beta Deploying Robot Tokens via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
47
48
  * @category Definers
48
49
  * @expandType BlueprintRobotTokenConfig
49
50
  * @returns The robot token resource
50
51
  */
51
52
  export function defineRobotToken(parameters: BlueprintRobotTokenConfig): BlueprintRobotTokenResource {
53
+ const {project, memberships, ...rest} = parameters
52
54
  const label = parameters.label || parameters.name
53
55
 
54
56
  const robotResource: BlueprintRobotTokenResource = {
55
- ...parameters,
57
+ ...rest,
56
58
  label,
57
59
  type: 'sanity.access.robot',
60
+ // malformed memberships pass through untouched, so validation reports them instead of throwing here
61
+ memberships: Array.isArray(memberships) ? memberships.map(toMembership) : memberships,
62
+ }
63
+ if (project !== undefined) {
64
+ robotResource.resourceType = 'project'
65
+ robotResource.resourceId = project
58
66
  }
59
67
 
60
- runValidation(() => validateRobotToken(robotResource))
68
+ runValidation(robotResource, () => [
69
+ ...validateProjectScope(parameters, 'Robot token'),
70
+ ...(Array.isArray(memberships) ? memberships.flatMap((membership) => validateProjectScope(membership, 'Membership')) : []),
71
+ ...validateRobotToken(robotResource),
72
+ ])
61
73
 
62
74
  return robotResource
63
75
  }
64
76
 
77
+ function toMembership(membership: RobotTokenMembershipConfig): RobotTokenMembership {
78
+ if (typeof membership !== 'object' || membership === null) return membership
79
+ const {project, ...rest} = membership
80
+ const result: RobotTokenMembership = rest
81
+ if (project !== undefined) {
82
+ result.resourceType = 'project'
83
+ result.resourceId = project
84
+ }
85
+ return result
86
+ }
87
+
88
+ /**
89
+ * `project` sets `resourceType` and `resourceId`, so it can't be combined with them.
90
+ * @param scope The robot token or membership options that were passed to the definer
91
+ * @param label The label used as the prefix of error messages
92
+ * @returns A list of validation errors
93
+ */
94
+ function validateProjectScope(scope: unknown, label: string): BlueprintError[] {
95
+ if (typeof scope !== 'object' || scope === null || !('project' in scope) || scope.project === undefined) return []
96
+ if (!('resourceType' in scope) && !('resourceId' in scope)) return []
97
+ return [{type: 'invalid_value', message: `${label} \`project\` can't be combined with \`resourceType\` or \`resourceId\``}]
98
+ }
99
+
65
100
  /**
66
101
  * Creates a reference to a robot token in another stack.
67
102
  *
103
+ * @example
68
104
  * ```ts
69
105
  * referenceRobotToken({
70
106
  * name: 'editor-token',
@@ -73,7 +109,6 @@ export function defineRobotToken(parameters: BlueprintRobotTokenConfig): Bluepri
73
109
  * ```
74
110
  *
75
111
  * @public
76
- * @beta Referencing Robot Tokens via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
77
112
  * @category Referencers
78
113
  * @expandType BlueprintCrossStackReferenceConfig
79
114
  * @param params The parameters for referencing the robot token
@@ -10,14 +10,30 @@ import {
10
10
  } from '../index.js'
11
11
  import {runValidation} from '../utils/validation.js'
12
12
 
13
- /*
14
- * FUTURE example (move below @example when ready)
13
+ /**
14
+ * Defines a custom role. Set `project` to scope the role to a project. Without it, the role is scoped to the same resource
15
+ * as the blueprint.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * defineRole({
20
+ * name: 'custom-robot-role',
21
+ * title: 'Custom Robot Role',
22
+ * appliesToRobots: true,
23
+ * appliesToUsers: false,
24
+ * permissions: [{
25
+ * name: 'sanity-project-cors',
26
+ * action: 'create',
27
+ * }],
28
+ * })
29
+ * ```
30
+ *
15
31
  * @example All options
16
32
  * ```ts
17
33
  * defineRole({
18
34
  * name: 'ci-deploy-role',
19
35
  * title: 'CI Deploy Role',
20
- * description: 'Permissions for CI/CD pipelines to manage CORS and datasets',
36
+ * description: 'Permissions for CI/CD pipelines to manage CORS origins',
21
37
  * appliesToRobots: true,
22
38
  * appliesToUsers: false,
23
39
  * permissions: [{
@@ -32,29 +48,40 @@ import {runValidation} from '../utils/validation.js'
32
48
  * }],
33
49
  * })
34
50
  * ```
35
- */
36
- /**
37
- * Defines a role that is scoped to the same resource as the blueprint.
38
51
  *
52
+ * @example In a project, for a robot token
39
53
  * ```ts
40
54
  * defineRole({
41
- * name: 'custom-robot-role',
42
- * title: 'Custom Robot Role',
55
+ * name: 'ci-deploy-role',
56
+ * title: 'CI Deploy Role',
57
+ * project: 'my-project-id',
43
58
  * appliesToRobots: true,
44
- * permissions: [{
45
- * name: 'sanity-project-cors',
46
- * action: 'create',
47
- * }],
59
+ * appliesToUsers: false,
60
+ * permissions: [{name: 'sanity-project-cors', action: 'create'}],
61
+ * })
62
+ *
63
+ * defineRobotToken({
64
+ * name: 'ci-robot',
65
+ * memberships: [{project: 'my-project-id', roleNames: ['$.resources.ci-deploy-role']}],
48
66
  * })
49
67
  * ```
50
68
  * @param parameters The configuration of the role
51
69
  * @public
52
- * @beta Deploying Roles via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
53
70
  * @category Definers
54
71
  * @expandType BlueprintRoleConfig
55
72
  * @returns The role resource
56
73
  */
57
- export function defineRole(parameters: BlueprintRoleConfig, options?: {skipValidation?: boolean}): BlueprintRoleResource {
74
+ export function defineRole(parameters: BlueprintRoleConfig): BlueprintRoleResource {
75
+ if (parameters.project !== undefined) return defineRoleInProject(parameters.project, parameters)
76
+
77
+ const roleResource = buildRoleResource(parameters)
78
+
79
+ runValidation(roleResource, () => validateRole(roleResource))
80
+
81
+ return roleResource
82
+ }
83
+
84
+ function buildRoleResource(parameters: BlueprintRoleConfig): BlueprintRoleResource {
58
85
  const roleResource: BlueprintRoleResource = {
59
86
  name: parameters.name,
60
87
  type: 'sanity.access.role',
@@ -69,46 +96,33 @@ export function defineRole(parameters: BlueprintRoleConfig, options?: {skipValid
69
96
  roleResource.lifecycle = parameters.lifecycle
70
97
  }
71
98
 
72
- if (options?.skipValidation !== true) runValidation(() => validateRole(roleResource))
73
-
74
99
  return roleResource
75
100
  }
76
101
 
77
- /*
78
- * FUTURE example (move below @example when ready)
79
- * @example Cross-resource references
80
- * ```ts
81
- * const role = defineProjectRole(projectId, {
82
- * name: 'ci-deploy-role',
83
- * title: 'CI Deploy Role',
84
- * appliesToRobots: true,
85
- * permissions: [{
86
- * name: 'sanity-project-cors',
87
- * action: 'create',
88
- * }, {
89
- * name: 'sanity-project-cors',
90
- * action: 'delete',
91
- * }],
92
- * })
93
- *
94
- * defineRobotToken({
95
- * name: 'ci-robot',
96
- * memberships: [{
97
- * resourceType: 'project',
98
- * resourceId: projectId,
99
- * roleNames: ['$.resources.ci-deploy-role'],
100
- * }],
101
- * })
102
- * ```
103
- */
102
+ function defineRoleInProject(project: string, parameters: BlueprintRoleConfig): BlueprintProjectRoleResource {
103
+ const projectRoleResource: BlueprintProjectRoleResource = {
104
+ ...buildRoleResource(parameters),
105
+ resourceType: 'project',
106
+ resourceId: project,
107
+ }
108
+
109
+ runValidation(projectRoleResource, () => validateProjectRole(projectRoleResource))
110
+
111
+ return projectRoleResource
112
+ }
113
+
104
114
  /**
105
115
  * Defines a role that is scoped to the specified project.
106
116
  *
117
+ * @deprecated Use {@link defineRole} with `project` instead.
118
+ *
119
+ * @example
107
120
  * ```ts
108
- * defineProjectRole(projectId, {
121
+ * defineProjectRole('my-project-id', {
109
122
  * name: 'viewer-role',
110
123
  * title: 'Viewer',
111
124
  * appliesToUsers: true,
125
+ * appliesToRobots: false,
112
126
  * permissions: [{
113
127
  * name: 'sanity-project-cors',
114
128
  * action: 'read',
@@ -116,8 +130,6 @@ export function defineRole(parameters: BlueprintRoleConfig, options?: {skipValid
116
130
  * })
117
131
  * ```
118
132
  * @public
119
- * @beta Deploying Roles via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
120
- * @hidden
121
133
  * @category Definers
122
134
  * @expandType BlueprintRoleConfig
123
135
  * @param projectId The ID of the project to which the role will be scoped
@@ -125,22 +137,13 @@ export function defineRole(parameters: BlueprintRoleConfig, options?: {skipValid
125
137
  * @returns The role resource
126
138
  */
127
139
  export function defineProjectRole(projectId: string, parameters: BlueprintRoleConfig): BlueprintProjectRoleResource {
128
- const roleResource = defineRole(parameters, {skipValidation: true})
129
-
130
- const projectRoleResource: BlueprintProjectRoleResource = {
131
- ...roleResource,
132
- resourceType: 'project',
133
- resourceId: projectId,
134
- }
135
-
136
- runValidation(() => validateProjectRole(projectRoleResource))
137
-
138
- return projectRoleResource
140
+ return defineRoleInProject(projectId, parameters)
139
141
  }
140
142
 
141
143
  /**
142
144
  * Creates a reference to a role in another stack.
143
145
  *
146
+ * @example
144
147
  * ```ts
145
148
  * referenceRole({
146
149
  * name: 'editor-role',
@@ -149,7 +152,6 @@ export function defineProjectRole(projectId: string, parameters: BlueprintRoleCo
149
152
  * ```
150
153
  *
151
154
  * @public
152
- * @beta Referencing Roles via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
153
155
  * @category Referencers
154
156
  * @expandType BlueprintCrossStackReferenceConfig
155
157
  * @param params The parameters for referencing the role
@@ -5,6 +5,9 @@ import {runValidation} from '../utils/validation.js'
5
5
  /**
6
6
  * Defines a studio.
7
7
  *
8
+ * Deploying Studios via Blueprints is experimental. This may be subject to breaking changes.
9
+ *
10
+ * @example
8
11
  * ```ts
9
12
  * defineStudio({
10
13
  * name: 'my-studio',
@@ -15,13 +18,12 @@ import {runValidation} from '../utils/validation.js'
15
18
  * }
16
19
  * })
17
20
  * ```
18
- * @param parameters The studio configuration
21
+ * @param config The studio configuration
19
22
  * @public
20
- * @beta Deploying Studios via Blueprints is experimental. This may be subject to breaking changes.
23
+ * @beta
21
24
  * @category Definers
22
25
  * @expandType BlueprintStudioConfig
23
26
  * @returns The studio resource
24
- * @hidden
25
27
  */
26
28
  export function defineStudio(config: BlueprintStudioConfig): BlueprintStudioResource {
27
29
  const autoUpdates: BlueprintStudioResource['autoUpdates'] = {enabled: config.autoUpdates?.enabled ?? true}
@@ -37,7 +39,7 @@ export function defineStudio(config: BlueprintStudioConfig): BlueprintStudioReso
37
39
  type: 'sanity.studio',
38
40
  }
39
41
 
40
- runValidation(() => validateStudio(studioResource))
42
+ runValidation(studioResource, () => validateStudio(studioResource))
41
43
 
42
44
  return studioResource
43
45
  }