@sanity/blueprints 0.14.0 → 0.15.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.
- package/dist/definers/functions.d.ts +34 -1
- package/dist/definers/functions.js +47 -5
- package/dist/definers/functions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/functions/event.d.ts +101 -0
- package/dist/types/functions/event.js +3 -0
- package/dist/types/functions/event.js.map +1 -0
- package/dist/types/{functions.d.ts → functions/index.d.ts} +45 -98
- package/dist/types/{functions.js → functions/index.js} +3 -2
- package/dist/types/functions/index.js.map +1 -0
- package/dist/validation/functions.d.ts +9 -0
- package/dist/validation/functions.js +80 -64
- package/dist/validation/functions.js.map +1 -1
- package/package.json +6 -6
- package/src/definers/functions.ts +59 -7
- package/src/index.ts +1 -1
- package/src/types/functions/event.ts +120 -0
- package/src/types/{functions.ts → functions/index.ts} +54 -112
- package/src/validation/functions.ts +79 -58
- package/dist/types/functions.js.map +0 -1
|
@@ -1,109 +1,16 @@
|
|
|
1
|
-
import type {BlueprintResource} from '
|
|
1
|
+
import type {BlueprintResource} from '../../index.js'
|
|
2
|
+
import type {
|
|
3
|
+
BlueprintDocumentFunctionResourceEvent,
|
|
4
|
+
BlueprintMediaLibraryFunctionResourceEvent,
|
|
5
|
+
BlueprintScheduledFunctionConfigEvent,
|
|
6
|
+
BlueprintScheduledFunctionResourceEvent,
|
|
7
|
+
BlueprintSyncTagInvalidateFunctionResourceEvent,
|
|
8
|
+
} from './event.js'
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
export * from './event.js'
|
|
4
11
|
|
|
5
12
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @category Functions Types
|
|
8
|
-
*/
|
|
9
|
-
export interface BlueprintFunctionBaseResourceEvent {
|
|
10
|
-
/**
|
|
11
|
-
* Event types that trigger the function
|
|
12
|
-
* @defaultValue ['publish']
|
|
13
|
-
*/
|
|
14
|
-
on?: [BlueprintFunctionResourceEventName, ...BlueprintFunctionResourceEventName[]]
|
|
15
|
-
/** GROQ filter expression to match specific documents (e.g., "_type == 'post'") */
|
|
16
|
-
filter?: string
|
|
17
|
-
/** GROQ projection to specify which fields to include (e.g., "{_id, title, slug}") */
|
|
18
|
-
projection?: `{${string}}`
|
|
19
|
-
/** Include draft documents in addition to published documents */
|
|
20
|
-
includeDrafts?: boolean
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Event configuration for document functions
|
|
25
|
-
* @category Functions Types
|
|
26
|
-
*/
|
|
27
|
-
export interface BlueprintDocumentFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
|
|
28
|
-
/** Include all versions of documents, not just the current version */
|
|
29
|
-
includeAllVersions?: boolean
|
|
30
|
-
/** Optional dataset resource scoping for the function */
|
|
31
|
-
resource?: BlueprintFunctionResourceEventResourceDataset
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Event configuration for media library asset functions
|
|
36
|
-
* @category Functions Types
|
|
37
|
-
*/
|
|
38
|
-
export interface BlueprintMediaLibraryFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
|
|
39
|
-
/** Media library resource scoping (required for media library functions) */
|
|
40
|
-
resource: BlueprintFunctionResourceEventResourceMediaLibrary
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Union type of all function resource event configurations
|
|
45
|
-
* @category Functions Types
|
|
46
|
-
*/
|
|
47
|
-
export type BlueprintFunctionResourceEvent = BlueprintDocumentFunctionResourceEvent | BlueprintMediaLibraryFunctionResourceEvent
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Explicit resource event for scheduled functions to specific minutes, hours, days of month, months, and days of week
|
|
51
|
-
* @example { minute: '0', hour: '9', dayOfMonth: '1', month: '1', dayOfWeek: '1' }
|
|
52
|
-
* @category Functions Types
|
|
53
|
-
*/
|
|
54
|
-
export interface BlueprintScheduledFunctionExplicitResourceEvent {
|
|
55
|
-
minute: string
|
|
56
|
-
hour: string
|
|
57
|
-
dayOfMonth: string
|
|
58
|
-
month: string
|
|
59
|
-
dayOfWeek: string
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Expression resource for scheduled functions to specific expressions
|
|
64
|
-
* @example { expression: '0 9 * * *' }
|
|
65
|
-
* @category Functions Types
|
|
66
|
-
*/
|
|
67
|
-
export interface BlueprintScheduledFunctionExpressionResourceEvent {
|
|
68
|
-
expression: string
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Union type of all scheduled function resource event configurations
|
|
73
|
-
* @category Functions Types
|
|
74
|
-
*/
|
|
75
|
-
export type BlueprintScheduledFunctionResourceEvent =
|
|
76
|
-
| BlueprintScheduledFunctionExplicitResourceEvent
|
|
77
|
-
| BlueprintScheduledFunctionExpressionResourceEvent
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Dataset resource for scoping document functions to specific datasets
|
|
81
|
-
* @example { type: 'dataset', id: 'my-project.production' }
|
|
82
|
-
* @category Functions Types
|
|
83
|
-
*/
|
|
84
|
-
export interface BlueprintFunctionResourceEventResourceDataset {
|
|
85
|
-
type: 'dataset'
|
|
86
|
-
/** A dataset ID in the format <projectId>.<datasetName>. <datasetName> can be `*` to signify "all datasets in project with ID <projectId>." */
|
|
87
|
-
id: string
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Media library resource for scoping media library functions
|
|
92
|
-
* @category Functions Types
|
|
93
|
-
*/
|
|
94
|
-
export interface BlueprintFunctionResourceEventResourceMediaLibrary {
|
|
95
|
-
type: 'media-library'
|
|
96
|
-
id: string
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Events that can trigger a function
|
|
101
|
-
* @category Functions Types
|
|
102
|
-
*/
|
|
103
|
-
export type BlueprintFunctionResourceEventName = 'publish' | 'create' | 'delete' | 'update'
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Current support Function runtimes
|
|
13
|
+
* Supported Function runtimes
|
|
107
14
|
* @category Functions Types
|
|
108
15
|
*/
|
|
109
16
|
export const VALID_RUNTIMES = ['node', 'nodejs22.x', 'nodejs24.x'] as const
|
|
@@ -119,7 +26,7 @@ export type FunctionRuntimes = (typeof VALID_RUNTIMES)[number]
|
|
|
119
26
|
* Base function resource with common properties for all function types
|
|
120
27
|
* @category Functions Types
|
|
121
28
|
*/
|
|
122
|
-
|
|
29
|
+
interface BlueprintCommonFunctionResource extends BlueprintResource {
|
|
123
30
|
/** Human-readable display name for the function */
|
|
124
31
|
displayName?: string
|
|
125
32
|
/** Path to the function source code */
|
|
@@ -133,12 +40,6 @@ export interface BlueprintBaseFunctionResource extends BlueprintResource {
|
|
|
133
40
|
/** Token provided during function invocation */
|
|
134
41
|
robotToken?: string
|
|
135
42
|
|
|
136
|
-
/**
|
|
137
|
-
* The project ID of the project that contains your function.
|
|
138
|
-
*
|
|
139
|
-
* The `project` attribute must be defined if your blueprint is scoped to an organization. */
|
|
140
|
-
project?: string
|
|
141
|
-
|
|
142
43
|
/**
|
|
143
44
|
* The runtime environment for the function (currently only Node.js is supported)
|
|
144
45
|
* @defaultValue 'nodejs24.x'
|
|
@@ -146,6 +47,18 @@ export interface BlueprintBaseFunctionResource extends BlueprintResource {
|
|
|
146
47
|
runtime?: FunctionRuntimes
|
|
147
48
|
}
|
|
148
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Base function resource with common properties for all function types that can belong to projects.
|
|
52
|
+
* @category Functions Types
|
|
53
|
+
*/
|
|
54
|
+
export interface BlueprintBaseFunctionResource extends BlueprintCommonFunctionResource {
|
|
55
|
+
/**
|
|
56
|
+
* The project ID of the project that contains your function.
|
|
57
|
+
*
|
|
58
|
+
* The `project` attribute must be defined if your blueprint is scoped to an organization. */
|
|
59
|
+
project?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
149
62
|
/**
|
|
150
63
|
* A function resource triggered by document events in Sanity datasets
|
|
151
64
|
* @category Functions Types
|
|
@@ -170,12 +83,21 @@ export interface BlueprintMediaLibraryAssetFunctionResource extends BlueprintBas
|
|
|
170
83
|
* A function resource triggered by scheduled events
|
|
171
84
|
* @category Functions Types
|
|
172
85
|
*/
|
|
173
|
-
export interface BlueprintScheduledFunctionResource extends
|
|
86
|
+
export interface BlueprintScheduledFunctionResource extends BlueprintCommonFunctionResource {
|
|
174
87
|
type: 'sanity.function.cron'
|
|
175
88
|
event: BlueprintScheduledFunctionResourceEvent
|
|
176
89
|
timezone?: string
|
|
177
90
|
}
|
|
178
91
|
|
|
92
|
+
/**
|
|
93
|
+
* A function resource triggered by sync tag invalidate events
|
|
94
|
+
* @category Functions Types
|
|
95
|
+
*/
|
|
96
|
+
export interface BlueprintSyncTagInvalidateFunctionResource extends BlueprintBaseFunctionResource {
|
|
97
|
+
type: 'sanity.function.sync-tag-invalidate'
|
|
98
|
+
event?: BlueprintSyncTagInvalidateFunctionResourceEvent
|
|
99
|
+
}
|
|
100
|
+
|
|
179
101
|
// --- Function Config Types ---
|
|
180
102
|
|
|
181
103
|
/**
|
|
@@ -233,7 +155,27 @@ export type BlueprintMediaLibraryAssetFunctionConfig = Omit<BlueprintMediaLibrar
|
|
|
233
155
|
* @category Functions Types
|
|
234
156
|
* @interface
|
|
235
157
|
*/
|
|
236
|
-
export type BlueprintScheduledFunctionConfig = Omit<BlueprintScheduledFunctionResource, 'type' | 'src'> & {
|
|
158
|
+
export type BlueprintScheduledFunctionConfig = Omit<BlueprintScheduledFunctionResource, 'type' | 'src' | 'event'> & {
|
|
159
|
+
/**
|
|
160
|
+
* Path to the function source code
|
|
161
|
+
* @defaultValue `functions/${name}`
|
|
162
|
+
*/
|
|
163
|
+
src?: string
|
|
164
|
+
/**
|
|
165
|
+
* Event configuration specifying when the function is triggered
|
|
166
|
+
*/
|
|
167
|
+
event: BlueprintScheduledFunctionConfigEvent
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Configuration for defining a sync tag invalidate function.
|
|
172
|
+
* @public
|
|
173
|
+
* @alpha Deploying Sync Tag Invalidate Functions via Blueprints is experimental. This feature is not available publicly yet.
|
|
174
|
+
* @hidden
|
|
175
|
+
* @category Functions Types
|
|
176
|
+
* @interface
|
|
177
|
+
*/
|
|
178
|
+
export type BlueprintSyncTagInvalidateFunctionConfig = Omit<BlueprintSyncTagInvalidateFunctionResource, 'type' | 'src'> & {
|
|
237
179
|
/**
|
|
238
180
|
* Path to the function source code
|
|
239
181
|
* @defaultValue `functions/${name}`
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
VALID_RUNTIMES,
|
|
8
8
|
validateResource,
|
|
9
9
|
} from '../index.js'
|
|
10
|
-
import {validateScheduledExpression} from '../utils/schedule-parser.js'
|
|
11
10
|
|
|
12
11
|
type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
|
|
13
12
|
const BASE_EVENT_KEYS = new Set<BaseFunctionEventKey>(['on', 'filter', 'projection', 'includeDrafts'])
|
|
@@ -151,14 +150,23 @@ function validateDocumentFunctionEvent(event: unknown): BlueprintError[] {
|
|
|
151
150
|
}
|
|
152
151
|
if (!Array.isArray(fullEvent.on)) errors.push({type: 'invalid_type', message: '`event.on` must be an array'})
|
|
153
152
|
if (fullEvent.resource) {
|
|
154
|
-
|
|
155
|
-
errors.push({type: 'invalid_value', message: '`event.resource.type` must be "dataset"'})
|
|
156
|
-
if (!fullEvent.resource.id || fullEvent.resource.id.split('.').length !== 2)
|
|
157
|
-
errors.push({type: 'invalid_format', message: '`event.resource.id` must be in the format <projectId>.<datasetName>'})
|
|
153
|
+
errors.push(...validateFunctionEventResourceDataset(fullEvent))
|
|
158
154
|
}
|
|
159
155
|
return errors
|
|
160
156
|
}
|
|
161
157
|
|
|
158
|
+
function validateFunctionEventResourceDataset(event: unknown): BlueprintError[] {
|
|
159
|
+
const errors: BlueprintError[] = []
|
|
160
|
+
if (!event || typeof event !== 'object') return [{type: 'invalid_value', message: '`event` must be an object'}]
|
|
161
|
+
if (!('resource' in event)) return [{type: 'invalid_value', message: '`event.resource` must exist'}]
|
|
162
|
+
const resource = event.resource
|
|
163
|
+
if (!resource || typeof resource !== 'object') return [{type: 'invalid_value', message: '`event.resource` must be an object'}]
|
|
164
|
+
if (!('type' in resource) || !resource.type || resource.type !== 'dataset')
|
|
165
|
+
errors.push({type: 'invalid_value', message: '`event.resource.type` must be "dataset"'})
|
|
166
|
+
if (!('id' in resource) || !resource.id || typeof resource.id !== 'string' || resource.id.split('.').length !== 2)
|
|
167
|
+
errors.push({type: 'invalid_format', message: '`event.resource.id` must be in the format <projectId>.<datasetName>'})
|
|
168
|
+
return errors
|
|
169
|
+
}
|
|
162
170
|
/**
|
|
163
171
|
* Validates a media library function event configuration.
|
|
164
172
|
* Checks event trigger types and ensures required media library resource is present.
|
|
@@ -233,66 +241,52 @@ function validateScheduledFunctionEvent(event: unknown): BlueprintError[] {
|
|
|
233
241
|
const errors: BlueprintError[] = []
|
|
234
242
|
|
|
235
243
|
const hasExpression = 'expression' in event
|
|
236
|
-
const hasExplicitFields = 'minute' in event || 'hour' in event || 'dayOfMonth' in event || 'month' in event || 'dayOfWeek' in event
|
|
237
244
|
|
|
238
|
-
if (hasExpression
|
|
245
|
+
if (hasExpression) {
|
|
239
246
|
errors.push({
|
|
240
247
|
type: 'invalid_property',
|
|
241
|
-
message: 'Cannot specify
|
|
248
|
+
message: 'Cannot specify `expression`. Use `defineScheduledFunction` to convert this to explicit fields',
|
|
242
249
|
})
|
|
243
|
-
}
|
|
250
|
+
}
|
|
251
|
+
if (!('minute' in event)) {
|
|
244
252
|
errors.push({
|
|
245
253
|
type: 'missing_parameter',
|
|
246
|
-
message: '
|
|
254
|
+
message: '`minute` must be provided',
|
|
247
255
|
})
|
|
248
|
-
} else if (
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
type: 'missing_parameter',
|
|
283
|
-
message: '`month` must be provided',
|
|
284
|
-
})
|
|
285
|
-
} else if (typeof event.month !== 'string') {
|
|
286
|
-
errors.push({type: 'invalid_type', message: '`month` must be a string'})
|
|
287
|
-
}
|
|
288
|
-
if (!('dayOfMonth' in event)) {
|
|
289
|
-
errors.push({
|
|
290
|
-
type: 'missing_parameter',
|
|
291
|
-
message: '`dayOfMonth` must be provided',
|
|
292
|
-
})
|
|
293
|
-
} else if (typeof event.dayOfMonth !== 'string') {
|
|
294
|
-
errors.push({type: 'invalid_type', message: '`dayOfMonth` must be a string'})
|
|
295
|
-
}
|
|
256
|
+
} else if (typeof event.minute !== 'string') {
|
|
257
|
+
errors.push({type: 'invalid_type', message: '`minute` must be a string'})
|
|
258
|
+
}
|
|
259
|
+
if (!('hour' in event)) {
|
|
260
|
+
errors.push({
|
|
261
|
+
type: 'missing_parameter',
|
|
262
|
+
message: '`hour` must be provided',
|
|
263
|
+
})
|
|
264
|
+
} else if (typeof event.hour !== 'string') {
|
|
265
|
+
errors.push({type: 'invalid_type', message: '`hour` must be a string'})
|
|
266
|
+
}
|
|
267
|
+
if (!('dayOfWeek' in event)) {
|
|
268
|
+
errors.push({
|
|
269
|
+
type: 'missing_parameter',
|
|
270
|
+
message: '`dayOfWeek` must be provided',
|
|
271
|
+
})
|
|
272
|
+
} else if (typeof event.dayOfWeek !== 'string') {
|
|
273
|
+
errors.push({type: 'invalid_type', message: '`dayOfWeek` must be a string'})
|
|
274
|
+
}
|
|
275
|
+
if (!('month' in event)) {
|
|
276
|
+
errors.push({
|
|
277
|
+
type: 'missing_parameter',
|
|
278
|
+
message: '`month` must be provided',
|
|
279
|
+
})
|
|
280
|
+
} else if (typeof event.month !== 'string') {
|
|
281
|
+
errors.push({type: 'invalid_type', message: '`month` must be a string'})
|
|
282
|
+
}
|
|
283
|
+
if (!('dayOfMonth' in event)) {
|
|
284
|
+
errors.push({
|
|
285
|
+
type: 'missing_parameter',
|
|
286
|
+
message: '`dayOfMonth` must be provided',
|
|
287
|
+
})
|
|
288
|
+
} else if (typeof event.dayOfMonth !== 'string') {
|
|
289
|
+
errors.push({type: 'invalid_type', message: '`dayOfMonth` must be a string'})
|
|
296
290
|
}
|
|
297
291
|
|
|
298
292
|
return errors
|
|
@@ -319,3 +313,30 @@ function validateScheduledFunctionTimezone(timezone: unknown): BlueprintError[]
|
|
|
319
313
|
|
|
320
314
|
return errors
|
|
321
315
|
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Validates a sync tag invalidate function resource configuration.
|
|
319
|
+
* @param functionResource The function resource to validate
|
|
320
|
+
* @alpha
|
|
321
|
+
* @hidden
|
|
322
|
+
* @category Functions Types
|
|
323
|
+
* @returns Array of validation errors, empty if valid
|
|
324
|
+
*/
|
|
325
|
+
export function validateSyncTagInvalidateFunction(functionResource: unknown): BlueprintError[] {
|
|
326
|
+
if (!functionResource) return [{type: 'invalid_value', message: 'Function config must be provided'}]
|
|
327
|
+
if (typeof functionResource !== 'object') return [{type: 'invalid_type', message: 'Function config must be an object'}]
|
|
328
|
+
|
|
329
|
+
const errors: BlueprintError[] = []
|
|
330
|
+
|
|
331
|
+
if ('type' in functionResource && functionResource.type !== 'sanity.function.sync-tag-invalidate') {
|
|
332
|
+
errors.push({type: 'invalid_value', message: '`type` must be `sanity.function.sync-tag-invalidate`'})
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if ('event' in functionResource) {
|
|
336
|
+
errors.push(...validateFunctionEventResourceDataset(functionResource.event))
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
errors.push(...validateFunction(functionResource))
|
|
340
|
+
|
|
341
|
+
return errors
|
|
342
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../../src/types/functions.ts"],"names":[],"mappings":"AAwGA;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,YAAY,EAAE,YAAY,CAAU,CAAA"}
|