@sanity/blueprints 0.10.0 → 0.12.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 (112) hide show
  1. package/README.md +55 -20
  2. package/dist/definers/blueprints.d.ts +17 -3
  3. package/dist/definers/blueprints.js +42 -3
  4. package/dist/definers/blueprints.js.map +1 -1
  5. package/dist/definers/cors.d.ts +9 -2
  6. package/dist/definers/cors.js +21 -2
  7. package/dist/definers/cors.js.map +1 -1
  8. package/dist/definers/datasets.d.ts +10 -2
  9. package/dist/definers/datasets.js +23 -2
  10. package/dist/definers/datasets.js.map +1 -1
  11. package/dist/definers/functions.d.ts +58 -53
  12. package/dist/definers/functions.js +101 -86
  13. package/dist/definers/functions.js.map +1 -1
  14. package/dist/definers/resources.d.ts +12 -0
  15. package/dist/definers/resources.js +23 -0
  16. package/dist/definers/resources.js.map +1 -1
  17. package/dist/definers/robotTokens.d.ts +22 -0
  18. package/dist/definers/robotTokens.js +53 -0
  19. package/dist/definers/robotTokens.js.map +1 -0
  20. package/dist/definers/robots.d.ts +2 -18
  21. package/dist/definers/robots.js +2 -29
  22. package/dist/definers/robots.js.map +1 -1
  23. package/dist/definers/roles.d.ts +22 -25
  24. package/dist/definers/roles.js +70 -23
  25. package/dist/definers/roles.js.map +1 -1
  26. package/dist/definers/webhooks.d.ts +9 -2
  27. package/dist/definers/webhooks.js +29 -2
  28. package/dist/definers/webhooks.js.map +1 -1
  29. package/dist/index.d.ts +23 -0
  30. package/dist/index.js +24 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/types/blueprints.d.ts +16 -4
  33. package/dist/types/cors.d.ts +6 -0
  34. package/dist/types/datasets.d.ts +19 -3
  35. package/dist/types/errors.d.ts +4 -1
  36. package/dist/types/functions.d.ts +139 -17
  37. package/dist/types/functions.js +5 -1
  38. package/dist/types/functions.js.map +1 -1
  39. package/dist/types/resources.d.ts +17 -0
  40. package/dist/types/robotTokens.d.ts +45 -0
  41. package/dist/types/robotTokens.js +2 -0
  42. package/dist/types/robotTokens.js.map +1 -0
  43. package/dist/types/robots.d.ts +8 -30
  44. package/dist/types/robots.js +1 -0
  45. package/dist/types/robots.js.map +1 -1
  46. package/dist/types/roles.d.ts +12 -3
  47. package/dist/types/webhooks.d.ts +23 -5
  48. package/dist/utils/schedule-parser.d.ts +42 -0
  49. package/dist/utils/schedule-parser.js +450 -0
  50. package/dist/utils/schedule-parser.js.map +1 -0
  51. package/dist/utils/validation.d.ts +1 -0
  52. package/dist/utils/validation.js +1 -0
  53. package/dist/utils/validation.js.map +1 -1
  54. package/dist/validation/blueprints.d.ts +1 -0
  55. package/dist/validation/blueprints.js +1 -0
  56. package/dist/validation/blueprints.js.map +1 -1
  57. package/dist/validation/cors.d.ts +1 -0
  58. package/dist/validation/cors.js +1 -0
  59. package/dist/validation/cors.js.map +1 -1
  60. package/dist/validation/datasets.d.ts +2 -0
  61. package/dist/validation/datasets.js +2 -0
  62. package/dist/validation/datasets.js.map +1 -1
  63. package/dist/validation/functions.d.ts +11 -0
  64. package/dist/validation/functions.js +37 -1
  65. package/dist/validation/functions.js.map +1 -1
  66. package/dist/validation/resources.d.ts +7 -1
  67. package/dist/validation/resources.js +20 -14
  68. package/dist/validation/resources.js.map +1 -1
  69. package/dist/validation/robotTokens.d.ts +15 -0
  70. package/dist/validation/robotTokens.js +104 -0
  71. package/dist/validation/robotTokens.js.map +1 -0
  72. package/dist/validation/robots.d.ts +4 -8
  73. package/dist/validation/robots.js +5 -96
  74. package/dist/validation/robots.js.map +1 -1
  75. package/dist/validation/roles.d.ts +3 -1
  76. package/dist/validation/roles.js +3 -1
  77. package/dist/validation/roles.js.map +1 -1
  78. package/dist/validation/webhooks.d.ts +1 -0
  79. package/dist/validation/webhooks.js +1 -0
  80. package/dist/validation/webhooks.js.map +1 -1
  81. package/package.json +7 -4
  82. package/src/definers/blueprints.ts +42 -3
  83. package/src/definers/cors.ts +21 -2
  84. package/src/definers/datasets.ts +23 -2
  85. package/src/definers/functions.ts +159 -123
  86. package/src/definers/resources.ts +23 -0
  87. package/src/definers/robotTokens.ts +56 -0
  88. package/src/definers/robots.ts +2 -33
  89. package/src/definers/roles.ts +70 -23
  90. package/src/definers/webhooks.ts +29 -2
  91. package/src/index.ts +25 -0
  92. package/src/types/blueprints.ts +16 -4
  93. package/src/types/cors.ts +6 -0
  94. package/src/types/datasets.ts +19 -3
  95. package/src/types/errors.ts +4 -1
  96. package/src/types/functions.ts +156 -16
  97. package/src/types/resources.ts +17 -1
  98. package/src/types/robotTokens.ts +50 -0
  99. package/src/types/robots.ts +10 -35
  100. package/src/types/roles.ts +12 -3
  101. package/src/types/webhooks.ts +23 -5
  102. package/src/utils/schedule-parser.ts +502 -0
  103. package/src/utils/validation.ts +1 -0
  104. package/src/validation/blueprints.ts +1 -0
  105. package/src/validation/cors.ts +1 -0
  106. package/src/validation/datasets.ts +2 -0
  107. package/src/validation/functions.ts +37 -0
  108. package/src/validation/resources.ts +20 -14
  109. package/src/validation/robotTokens.ts +103 -0
  110. package/src/validation/robots.ts +5 -95
  111. package/src/validation/roles.ts +3 -1
  112. package/src/validation/webhooks.ts +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/blueprints",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Helper methods and type definitions for Sanity Blueprints",
5
5
  "keywords": [
6
6
  "sanity",
@@ -36,6 +36,8 @@
36
36
  "scripts": {
37
37
  "build": "rimraf dist && tsc",
38
38
  "coverage": "vitest --coverage",
39
+ "docs:typedoc": "typedoc --options typedoc.json",
40
+ "docs:watch": "npm run docs:typedoc && open ./docs/html/index.html && npm run docs:typedoc -- --watch",
39
41
  "lint": "biome check .",
40
42
  "lint:write": "biome check --write .",
41
43
  "posttest": "npm run lint",
@@ -47,12 +49,13 @@
47
49
  "typecheck": "tsc --noEmit --project tsconfig.test.json"
48
50
  },
49
51
  "devDependencies": {
50
- "@biomejs/biome": "2.3.11",
52
+ "@biomejs/biome": "2.3.14",
51
53
  "@types/node": "^20",
52
- "@vitest/coverage-v8": "^4.0.17",
54
+ "@vitest/coverage-v8": "^4.0.18",
53
55
  "rimraf": "^6.1.2",
56
+ "typedoc": "^0.28.16",
54
57
  "typescript": "^5.9.3",
55
- "vitest": "4.0.17"
58
+ "vitest": "4.0.18"
56
59
  },
57
60
  "engines": {
58
61
  "node": ">=20"
@@ -1,16 +1,55 @@
1
1
  import {type Blueprint, type BlueprintModule, type BlueprintsApiConfig, validateBlueprint} from '../index.js'
2
2
  import {runValidation} from '../utils/validation.js'
3
3
 
4
+ /*
5
+ * FUTURE example (move below @example when ready)
6
+ * @example With deployment API config
7
+ * ```ts
8
+ * defineBlueprint({
9
+ * resources: [
10
+ * defineCorsOrigin({
11
+ * name: 'cors-localhost',
12
+ * origin: 'http://localhost:3333'
13
+ * }),
14
+ * defineRole({
15
+ * name: 'role-ci',
16
+ * title: 'CI Role',
17
+ * appliesToRobots: true,
18
+ * permissions: []
19
+ * }),
20
+ * defineRobotToken({
21
+ * name: 'ci-robot',
22
+ * memberships: [{
23
+ * roleNames: ['$.resources.ci-role']}],
24
+ * }),
25
+ * ],
26
+ * })
27
+ * ```
28
+ */
4
29
  /**
5
30
  * Define a Blueprint to manage Sanity resources
6
- * ```
31
+ *
32
+ * ```ts
7
33
  * defineBlueprint({
8
34
  * resources: [
9
- * // list of managed resources
10
- * ]
35
+ * defineCorsOrigin({
36
+ * name: 'localhost-origin',
37
+ * origin: 'http://localhost:3333',
38
+ * }),
39
+ * defineDocumentFunction({
40
+ * name: 'update-search-index',
41
+ * event: {
42
+ * on: ['create', 'update'],
43
+ * filter: "_type == 'post'",
44
+ * projection: "{_id, title, slug}",
45
+ * },
46
+ * }),
47
+ * ],
11
48
  * })
12
49
  * ```
13
50
  * @param blueprintConfig The blueprint configuration
51
+ * @public
52
+ * @category Definers
14
53
  * @returns A blueprint module
15
54
  */
16
55
  export function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<BlueprintsApiConfig>): BlueprintModule {
@@ -1,15 +1,34 @@
1
1
  import {type BlueprintCorsOriginConfig, type BlueprintCorsOriginResource, validateCorsOrigin} from '../index.js'
2
2
  import {runValidation} from '../utils/validation.js'
3
3
 
4
+ /*
5
+ * FUTURE example (move below @example when ready)
6
+ * @example All options
7
+ * ```ts
8
+ * defineCorsOrigin({
9
+ * name: 'studio-cors',
10
+ * origin: 'https://my-studio.sanity.studio',
11
+ * allowCredentials: true,
12
+ * project: 'my-project-id',
13
+ * })
14
+ * ```
15
+ */
4
16
  /**
5
17
  * Defines a CORS Origin to be managed in a Blueprint.
6
- * ```
18
+ *
19
+ * ```ts
7
20
  * defineCorsOrigin({
21
+ * name: 'my-cors-origin',
8
22
  * origin: 'https://mydomain.com',
9
- * allowCredentials: true
23
+ * allowCredentials: true,
10
24
  * })
11
25
  * ```
12
26
  * @param parameters The CORS Origin configuration
27
+ * @public
28
+ * @beta Deploying CORS Origins via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
29
+ * @category Definers
30
+ * @expandType BlueprintCorsOriginConfig
31
+ * @returns The CORS Origin resource
13
32
  */
14
33
  export function defineCorsOrigin(parameters: BlueprintCorsOriginConfig): BlueprintCorsOriginResource {
15
34
  const corsResource: BlueprintCorsOriginResource = {
@@ -1,14 +1,35 @@
1
1
  import {type BlueprintDatasetConfig, type BlueprintDatasetResource, validateDataset} from '../index.js'
2
2
  import {runValidation} from '../utils/validation.js'
3
3
 
4
+ /*
5
+ * FUTURE example (move below @example when ready)
6
+ * @example All options
7
+ * ```ts
8
+ * defineDataset({
9
+ * name: 'staging',
10
+ * datasetName: 'staging-v2',
11
+ * aclMode: 'private',
12
+ * project: 'my-project-id',
13
+ * lifecycle: {deletionPolicy: 'protect'},
14
+ * })
15
+ * ```
16
+ */
4
17
  /**
5
18
  * Defines a Dataset to be managed in a Blueprint.
6
- * ```
19
+ *
20
+ * ```ts
7
21
  * defineDataset({
8
- * name: 'production'
22
+ * name: 'staging-dataset',
23
+ * datasetName: 'staging',
9
24
  * })
10
25
  * ```
11
26
  * @param parameters The dataset configuration
27
+ * @public
28
+ * @alpha Deploying Datasets via Blueprints is experimental. This feature is subject to breaking changes.
29
+ * @hidden
30
+ * @category Definers
31
+ * @expandType BlueprintDatasetConfig
32
+ * @returns The dataset resource
12
33
  */
13
34
  export function defineDataset(parameters: BlueprintDatasetConfig): BlueprintDatasetResource {
14
35
  // default dataset name
@@ -1,10 +1,14 @@
1
1
  import {
2
+ type BlueprintBaseFunctionConfig,
2
3
  type BlueprintBaseFunctionResource,
4
+ type BlueprintDocumentFunctionConfig,
3
5
  type BlueprintDocumentFunctionResource,
4
6
  type BlueprintDocumentFunctionResourceEvent,
5
7
  type BlueprintFunctionBaseResourceEvent,
8
+ type BlueprintMediaLibraryAssetFunctionConfig,
6
9
  type BlueprintMediaLibraryAssetFunctionResource,
7
10
  type BlueprintMediaLibraryFunctionResourceEvent,
11
+ type BlueprintScheduleFunctionConfig,
8
12
  type BlueprintScheduleFunctionExplicitResourceEvent,
9
13
  type BlueprintScheduleFunctionExpressionResourceEvent,
10
14
  type BlueprintScheduleFunctionResource,
@@ -14,6 +18,7 @@ import {
14
18
  validateMediaLibraryAssetFunction,
15
19
  validateScheduleFunction,
16
20
  } from '../index.js'
21
+ import {parseScheduleExpression} from '../utils/schedule-parser.js'
17
22
  import {runValidation} from '../utils/validation.js'
18
23
 
19
24
  type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
@@ -27,46 +32,75 @@ type ScheduleFunctionEventKey =
27
32
  | keyof BlueprintScheduleFunctionExpressionResourceEvent
28
33
  const SCHEDULE_EVENT_KEYS = new Set<ScheduleFunctionEventKey>(['minute', 'hour', 'dayOfWeek', 'month', 'dayOfMonth', 'expression'])
29
34
 
30
- interface RequiredFunctionProperties {
31
- name: string
32
- }
33
-
34
- /**
35
- * Defines a function that is triggered by document events in Sanity datasets.
36
- * ```
35
+ /*
36
+ * FUTURE example (move below @example when ready)
37
+ * @example With robot token reference
38
+ * ```ts
39
+ * defineRole({
40
+ * name: 'fn-role',
41
+ * title: 'Function Role',
42
+ * appliesToRobots: true,
43
+ * permissions: [{name: 'sanity-project-dataset', action: 'read'}],
44
+ * })
45
+ *
46
+ * defineRobotToken({
47
+ * name: 'fn-robot',
48
+ * memberships: [{
49
+ * roleNames: ['$.resources.fn-role'],
50
+ * }],
51
+ * })
52
+ *
37
53
  * defineDocumentFunction({
38
- * name: 'my-document-function',
39
- * src: 'functions/my-function',
54
+ * name: 'sync-to-external',
55
+ * src: 'functions/sync',
40
56
  * memory: 3,
41
57
  * timeout: 300,
58
+ * robotToken: '$.resources.fn-robot',
42
59
  * event: {
43
60
  * on: ['create', 'update'],
44
- * filter: "_type == 'some-type'",
45
- * projection: "{title, _id, _type}",
61
+ * filter: "_type == 'product'",
62
+ * projection: "{_id, title, slug}",
46
63
  * includeDrafts: false,
47
64
  * },
48
65
  * env: {
49
- * MY_ENV_VAR: 'custom-value',
66
+ * EXTERNAL_API_URL: 'https://api.example.com',
67
+ * SUPER_SECRET: process.env.SUPER_SECRET,
68
+ * },
69
+ * })
70
+ * ```
71
+ */
72
+ /**
73
+ * Defines a function that is triggered by document events in Sanity datasets.
74
+ *
75
+ * ```ts
76
+ * defineDocumentFunction({
77
+ * name: 'my-document-function',
78
+ * event: {
79
+ * on: ['create', 'update'],
80
+ * filter: "_type == 'post'",
81
+ * projection: "{_id, title, slug}",
50
82
  * },
51
83
  * })
52
84
  * ```
53
85
  * @param functionConfig The configuration for the document function
86
+ * @public
87
+ * @category Definers
88
+ * @expandType BlueprintDocumentFunctionConfig
54
89
  * @returns The validated document function resource
55
90
  */
91
+ export function defineDocumentFunction(functionConfig: BlueprintDocumentFunctionConfig): BlueprintDocumentFunctionResource
92
+ /**
93
+ * @deprecated Define event properties under the 'event' key instead of specifying them at the top level
94
+ * @hidden
95
+ */
56
96
  export function defineDocumentFunction(
57
- functionConfig: Partial<BlueprintDocumentFunctionResource> & RequiredFunctionProperties,
58
- ): BlueprintDocumentFunctionResource
59
-
60
- /** @deprecated Define event properties under the 'event' key instead of specifying them at the top level */
61
- export function defineDocumentFunction(
62
- functionConfig: Partial<BlueprintDocumentFunctionResource> & RequiredFunctionProperties & Partial<BlueprintDocumentFunctionResourceEvent>,
97
+ functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>,
63
98
  ): BlueprintDocumentFunctionResource
64
99
 
65
100
  export function defineDocumentFunction(
66
- functionConfig: Partial<BlueprintDocumentFunctionResource> & RequiredFunctionProperties & Partial<BlueprintDocumentFunctionResourceEvent>,
101
+ functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>,
67
102
  ): BlueprintDocumentFunctionResource {
68
- let {name, src, event, timeout, memory, env, type, robotToken, project, ...maybeEvent} = functionConfig
69
- if (!type) type = 'sanity.function.document'
103
+ let {name, src, event, timeout, memory, env, robotToken, project, runtime, ...maybeEvent} = functionConfig
70
104
 
71
105
  // event validation and normalization
72
106
  if (event) {
@@ -88,21 +122,8 @@ export function defineDocumentFunction(
88
122
  }
89
123
 
90
124
  const functionResource: BlueprintDocumentFunctionResource = {
91
- ...defineFunction(
92
- {
93
- name,
94
- src,
95
- timeout,
96
- memory,
97
- env,
98
- robotToken,
99
- project,
100
- },
101
- {
102
- skipValidation: true, // already done below
103
- },
104
- ),
105
- type,
125
+ ...defineFunction(functionConfig, {skipValidation: true}),
126
+ type: 'sanity.function.document',
106
127
  event,
107
128
  }
108
129
 
@@ -111,54 +132,67 @@ export function defineDocumentFunction(
111
132
  return functionResource
112
133
  }
113
134
 
114
- /**
115
- * Defines a function that is triggered by media library events.
116
- * ```
135
+ /*
136
+ * FUTURE example (move below @example when ready)
137
+ * @example With robot token reference
138
+ * ```ts
139
+ * defineRobotToken({
140
+ * name: 'media-robot',
141
+ * memberships: [{
142
+ * resourceType: 'project',
143
+ * resourceId: projectId,
144
+ * roleNames: ['editor'],
145
+ * }],
146
+ * })
147
+ *
117
148
  * defineMediaLibraryAssetFunction({
118
- * name: 'my-media-library-function',
119
- * src: 'functions/media-library-function',
149
+ * name: 'process-uploads',
150
+ * src: 'functions/process-uploads-v2',
151
+ * robotToken: '$.resources.media-robot',
120
152
  * event: {
121
153
  * on: ['create', 'update'],
122
154
  * resource: {
123
155
  * type: 'media-library',
124
156
  * id: 'my-media-library-id',
125
157
  * },
126
- * filter: "type == 'my-type'",
158
+ * filter: "type == 'image'",
127
159
  * projection: "{_id}",
128
160
  * },
129
161
  * env: {
130
- * MY_ENV_VAR: 'custom-value',
162
+ * CDN_BUCKET: 'my-cdn-bucket',
163
+ * },
164
+ * })
165
+ * ```
166
+ */
167
+ /**
168
+ * Defines a function that is triggered by media library events.
169
+ *
170
+ * ```ts
171
+ * defineMediaLibraryAssetFunction({
172
+ * name: 'my-media-library-function',
173
+ * event: {
174
+ * on: ['create'],
175
+ * resource: {
176
+ * type: 'media-library',
177
+ * id: 'my-media-library-id',
178
+ * },
131
179
  * },
132
180
  * })
133
181
  * ```
134
182
  * @param functionConfig The configuration for the media library asset function
183
+ * @public
184
+ * @category Definers
185
+ * @expandType BlueprintMediaLibraryAssetFunctionConfig
135
186
  * @returns The validated media library asset function resource
136
187
  */
137
188
  export function defineMediaLibraryAssetFunction(
138
- functionConfig: Partial<BlueprintMediaLibraryAssetFunctionResource> &
139
- RequiredFunctionProperties &
140
- Pick<BlueprintMediaLibraryAssetFunctionResource, 'event'> &
141
- Partial<BlueprintMediaLibraryFunctionResourceEvent>,
189
+ functionConfig: BlueprintMediaLibraryAssetFunctionConfig,
142
190
  ): BlueprintMediaLibraryAssetFunctionResource {
143
- let {name, src, event, timeout, memory, env, type, robotToken, project} = functionConfig
144
- if (!type) type = 'sanity.function.media-library.asset'
191
+ const {event} = functionConfig
145
192
 
146
193
  const functionResource: BlueprintMediaLibraryAssetFunctionResource = {
147
- ...defineFunction(
148
- {
149
- name,
150
- src,
151
- timeout,
152
- memory,
153
- env,
154
- robotToken,
155
- project,
156
- },
157
- {
158
- skipValidation: true, // already done below
159
- },
160
- ),
161
- type,
194
+ ...defineFunction(functionConfig, {skipValidation: true}),
195
+ type: 'sanity.function.media-library.asset',
162
196
  event: buildMediaLibraryFunctionEvent(event),
163
197
  }
164
198
 
@@ -169,97 +203,95 @@ export function defineMediaLibraryAssetFunction(
169
203
 
170
204
  /**
171
205
  * Defines a function that is triggered on a schedule.
206
+ * Supports cron expressions or natural language schedules.
207
+ *
208
+ * @remarks
209
+ * Using a cron expression:
210
+ * ```ts
211
+ * defineScheduleFunction({
212
+ * name: 'daily-cleanup',
213
+ * event: {expression: '0 9 * * *'},
214
+ * })
172
215
  * ```
216
+ *
217
+ * Using explicit cron fields:
218
+ * ```ts
173
219
  * defineScheduleFunction({
174
- * name: 'my-schedule-function',
175
- * src: 'functions/schedule-function',
176
- * memory: 3,
177
- * timeout: 300,
178
- * event: {
179
- * minute: "0",
180
- * hour: "0",
181
- * dayOfMonth: "*",
182
- * month: "*",
183
- * dayOfWeek: "*",
184
- * },
220
+ * name: 'daily-cleanup',
221
+ * event: {minute: '0', hour: '9', dayOfMonth: '*', month: '*', dayOfWeek: '*'},
185
222
  * })
186
223
  * ```
187
- * @beta
188
- * @param functionConfig The configuration for the document function
224
+ *
225
+ * The `event.expression` field accepts standard cron expressions or natural language:
226
+ * `'every 15 minutes'`, `'weekdays at 8am'`, `'fridays in the evening'`,
227
+ * `'mon, wed, fri at 9am'`, `'first of the month at noon'`
228
+ *
229
+ * ```ts
230
+ * defineScheduleFunction({
231
+ * name: 'daily-cleanup',
232
+ * event: {expression: 'every day at 9am'},
233
+ * })
234
+ * ```
235
+ * @public
236
+ * @alpha Deploying Schedule Functions via Blueprints is experimental. This feature is not available publicly yet.
237
+ * @hidden
238
+ * @category Definers
239
+ * @expandType BlueprintScheduleFunctionConfig
240
+ * @param functionConfig The configuration for the schedule function
189
241
  * @returns The validated schedule function resource
190
242
  */
191
- export function defineScheduleFunction(
192
- functionConfig: Partial<BlueprintScheduleFunctionResource> &
193
- RequiredFunctionProperties &
194
- Pick<BlueprintScheduleFunctionResource, 'event'> &
195
- Partial<BlueprintScheduleFunctionResourceEvent>,
196
- ): BlueprintScheduleFunctionResource {
197
- let {name, src, event, timeout, memory, env, type, timezone} = functionConfig
198
- if (!type) type = 'sanity.function.cron'
243
+ export function defineScheduleFunction(functionConfig: BlueprintScheduleFunctionConfig): BlueprintScheduleFunctionResource {
244
+ const {event, timezone} = functionConfig
199
245
 
200
246
  const functionResource: BlueprintScheduleFunctionResource = {
201
- ...defineFunction(
202
- {
203
- name,
204
- src,
205
- timeout,
206
- memory,
207
- env,
208
- },
209
- {
210
- skipValidation: true, // already done below
211
- },
212
- ),
213
- type,
247
+ ...defineFunction(functionConfig, {skipValidation: true}),
248
+ type: 'sanity.function.cron',
214
249
  event: buildScheduleFunctionEvent(event),
215
250
  }
216
- if (timezone) {
217
- functionResource.timezone = timezone
218
- }
251
+
252
+ if (timezone) functionResource.timezone = timezone
219
253
 
220
254
  runValidation(() => validateScheduleFunction(functionResource))
221
255
 
256
+ // Parse expression after validation (validation ensures it's valid)
257
+ if ('expression' in functionResource.event && functionResource.event.expression) {
258
+ functionResource.event.expression = parseScheduleExpression(functionResource.event.expression)
259
+ }
260
+
222
261
  return functionResource
223
262
  }
224
263
 
225
264
  /**
226
265
  * Defines a base function resource with common properties.
227
- * ```
228
- * defineFunction({
229
- * name: 'my-function',
230
- * src: 'functions/my-function',
231
- * timeout: 300,
232
- * memory: 1,
233
- * env: {
234
- * MY_ENV_VAR: 'my-custom-value'
235
- * },
236
- * })
237
- * ```
266
+ *
238
267
  * @param functionConfig The configuration for the function
239
268
  * @param options Optional configuration including validation options
269
+ * @category Definers
270
+ * @internal
271
+ * @expandType BlueprintBaseFunctionConfig
240
272
  * @returns The validated function resource
241
273
  */
242
274
  export function defineFunction(
243
- functionConfig: Partial<BlueprintBaseFunctionResource> & RequiredFunctionProperties,
275
+ functionConfig: BlueprintBaseFunctionConfig,
244
276
  options?: {skipValidation?: boolean},
245
277
  ): BlueprintBaseFunctionResource {
246
- let {name, src, timeout, memory, env, type, robotToken, project} = functionConfig
247
-
248
- // defaults
249
- if (!src) src = `functions/${name}`
250
- if (!type) type = 'sanity.function.document'
278
+ const {name, displayName, src, timeout, memory, env, robotToken, project, runtime, lifecycle} = functionConfig
251
279
 
252
280
  const functionResource: BlueprintBaseFunctionResource = {
253
- type,
281
+ type: 'sanity.function.document',
254
282
  name,
255
- src,
283
+ src: src ?? `functions/${name}`,
284
+ displayName,
256
285
  timeout,
257
286
  memory,
258
287
  env,
259
288
  robotToken,
260
289
  project,
290
+ runtime,
261
291
  }
262
292
 
293
+ if (lifecycle) functionResource.lifecycle = lifecycle
294
+
263
295
  if (options?.skipValidation !== true) runValidation(() => validateFunction(functionResource))
264
296
 
265
297
  return functionResource
@@ -300,10 +332,14 @@ function buildMediaLibraryFunctionEvent(event: BlueprintMediaLibraryFunctionReso
300
332
  return fullEvent
301
333
  }
302
334
 
335
+ /**
336
+ * Builds a schedule function event configuration.
337
+ * Filters out non-event properties. Does not parse expressions.
338
+ * @param event Schedule function event configuration
339
+ * @returns Cleaned schedule function event configuration
340
+ */
303
341
  function buildScheduleFunctionEvent(event: BlueprintScheduleFunctionResourceEvent): BlueprintScheduleFunctionResourceEvent {
304
- const cleanEvent = Object.fromEntries(
342
+ return Object.fromEntries(
305
343
  Object.entries(event).filter(([key]) => SCHEDULE_EVENT_KEYS.has(key as ScheduleFunctionEventKey)),
306
344
  ) as BlueprintScheduleFunctionResourceEvent
307
-
308
- return cleanEvent
309
345
  }
@@ -1,9 +1,32 @@
1
1
  import {assertResource, 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
+ */
3
14
  /**
4
15
  * Defines a generic resource to be managed in Blueprints.
16
+ *
17
+ * @remarks
5
18
  * This is useful if the resource type does not yet have a `define*` function.
19
+ *
20
+ * ```ts
21
+ * defineResource({
22
+ * name: 'my-resource',
23
+ * type: 'sanity.new.resource',
24
+ * })
25
+ * ```
6
26
  * @param resourceConfig The resource configuration
27
+ * @category Definers
28
+ * @expandType BlueprintResource
29
+ * @internal
7
30
  */
8
31
  export function defineResource(resourceConfig: Partial<BlueprintResource>): BlueprintResource {
9
32
  const resource = {
@@ -0,0 +1,56 @@
1
+ import {type BlueprintRobotTokenConfig, type BlueprintRobotTokenResource, validateRobotToken} from '../index.js'
2
+ import {runValidation} from '../utils/validation.js'
3
+
4
+ /*
5
+ * FUTURE example (move below @example when ready)
6
+ * @example Cross-resource references
7
+ * ```ts
8
+ * defineRole({
9
+ * name: 'ci-deploy-role',
10
+ * title: 'CI Deploy Role',
11
+ * appliesToRobots: true,
12
+ * permissions: [{name: 'sanity-project-cors', action: 'create'}],
13
+ * })
14
+ *
15
+ * defineRobotToken({
16
+ * name: 'ci-robot',
17
+ * label: 'CI Deploy Robot',
18
+ * memberships: [{
19
+ * roleNames: ['$.resources.ci-deploy-role'],
20
+ * }],
21
+ * })
22
+ * ```
23
+ */
24
+ /**
25
+ * Defines a Robot Token for automated access. Has a token property provided during deployment that can be referenced by other resources.
26
+ *
27
+ * ```ts
28
+ * defineRobotToken({
29
+ * name: 'my-robot',
30
+ * memberships: [{
31
+ * resourceType: 'project',
32
+ * resourceId: projectId,
33
+ * roleNames: ['editor'],
34
+ * }],
35
+ * })
36
+ * ```
37
+ * @param parameters The robot token configuration
38
+ * @public
39
+ * @beta Deploying Robot Tokens via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
40
+ * @category Definers
41
+ * @expandType BlueprintRobotTokenConfig
42
+ * @returns The robot token resource
43
+ */
44
+ export function defineRobotToken(parameters: BlueprintRobotTokenConfig): BlueprintRobotTokenResource {
45
+ const label = parameters.label || parameters.name
46
+
47
+ const robotResource: BlueprintRobotTokenResource = {
48
+ ...parameters,
49
+ label,
50
+ type: 'sanity.access.robot',
51
+ }
52
+
53
+ runValidation(() => validateRobotToken(robotResource))
54
+
55
+ return robotResource
56
+ }