@sanity/blueprints 0.23.0 → 0.23.2
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/base-event.d.ts +2 -0
- package/dist/definers/functions/base-event.js +2 -0
- package/dist/definers/functions/base-event.js.map +1 -0
- package/dist/definers/functions/document.d.ts +26 -0
- package/dist/definers/functions/document.js +43 -0
- package/dist/definers/functions/document.js.map +1 -0
- package/dist/definers/functions/durable.d.ts +26 -0
- package/dist/definers/functions/durable.js +41 -0
- package/dist/definers/functions/durable.js.map +1 -0
- package/dist/definers/functions/index.d.ts +15 -0
- package/dist/definers/functions/index.js +34 -0
- package/dist/definers/functions/index.js.map +1 -0
- package/dist/definers/functions/media-library.d.ts +23 -0
- package/dist/definers/functions/media-library.js +83 -0
- package/dist/definers/functions/media-library.js.map +1 -0
- package/dist/definers/functions/pubsub.d.ts +20 -0
- package/dist/definers/functions/pubsub.js +30 -0
- package/dist/definers/functions/pubsub.js.map +1 -0
- package/dist/definers/functions/queue.d.ts +30 -0
- package/dist/definers/functions/queue.js +47 -0
- package/dist/definers/functions/queue.js.map +1 -0
- package/dist/definers/functions/scheduled.d.ts +46 -0
- package/dist/definers/functions/scheduled.js +91 -0
- package/dist/definers/functions/scheduled.js.map +1 -0
- package/dist/definers/functions/sync-tag-invalidate.d.ts +31 -0
- package/dist/definers/functions/sync-tag-invalidate.js +43 -0
- package/dist/definers/functions/sync-tag-invalidate.js.map +1 -0
- package/dist/definers/studios.js +5 -0
- package/dist/definers/studios.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/types/functions/event.d.ts +2 -4
- package/dist/types/functions/index.d.ts +61 -43
- package/dist/types/studios.d.ts +36 -7
- package/dist/types/studios.js +7 -1
- package/dist/types/studios.js.map +1 -1
- package/dist/validation/functions.d.ts +3 -3
- package/dist/validation/functions.js +11 -13
- package/dist/validation/functions.js.map +1 -1
- package/dist/validation/studios.js +29 -5
- package/dist/validation/studios.js.map +1 -1
- package/package.json +1 -1
- package/src/definers/functions/base-event.ts +4 -0
- package/src/definers/functions/document.ts +129 -0
- package/src/definers/functions/durable.ts +42 -0
- package/src/definers/functions/index.ts +39 -0
- package/src/definers/functions/media-library.ts +98 -0
- package/src/definers/functions/pubsub.ts +32 -0
- package/src/definers/functions/queue.ts +49 -0
- package/src/definers/functions/scheduled.ts +112 -0
- package/src/definers/functions/sync-tag-invalidate.ts +51 -0
- package/src/definers/studios.ts +5 -0
- package/src/index.ts +8 -1
- package/src/types/functions/event.ts +2 -3
- package/src/types/functions/index.ts +73 -54
- package/src/types/studios.ts +46 -8
- package/src/validation/functions.ts +11 -13
- package/src/validation/studios.ts +33 -6
- package/dist/definers/functions.d.ts +0 -210
- package/dist/definers/functions.js +0 -381
- package/dist/definers/functions.js.map +0 -1
- package/src/definers/functions.ts +0 -540
package/package.json
CHANGED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlueprintDocumentFunctionConfig,
|
|
3
|
+
type BlueprintDocumentFunctionResource,
|
|
4
|
+
type BlueprintDocumentFunctionResourceEvent,
|
|
5
|
+
validateDocumentFunction,
|
|
6
|
+
} from '../../index.js'
|
|
7
|
+
import {runValidation} from '../../utils/validation.js'
|
|
8
|
+
import {BASE_EVENT_KEYS} from './base-event.js'
|
|
9
|
+
import {defineFunction} from './index.js'
|
|
10
|
+
|
|
11
|
+
type DocumentFunctionEventKey = keyof BlueprintDocumentFunctionResourceEvent
|
|
12
|
+
const DOCUMENT_EVENT_KEYS = new Set<DocumentFunctionEventKey>(['includeAllVersions', 'resource', ...BASE_EVENT_KEYS.values()])
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* FUTURE example (move below @example when ready)
|
|
16
|
+
* @example With robot token reference
|
|
17
|
+
* ```ts
|
|
18
|
+
* defineRole({
|
|
19
|
+
* name: 'fn-role',
|
|
20
|
+
* title: 'Function Role',
|
|
21
|
+
* appliesToRobots: true,
|
|
22
|
+
* permissions: [{name: 'sanity-project-dataset', action: 'read'}],
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* defineRobotToken({
|
|
26
|
+
* name: 'fn-robot',
|
|
27
|
+
* memberships: [{
|
|
28
|
+
* roleNames: ['$.resources.fn-role'],
|
|
29
|
+
* }],
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* defineDocumentFunction({
|
|
33
|
+
* name: 'sync-to-external',
|
|
34
|
+
* src: 'functions/sync',
|
|
35
|
+
* memory: 3,
|
|
36
|
+
* timeout: 300,
|
|
37
|
+
* robotToken: '$.resources.fn-robot',
|
|
38
|
+
* event: {
|
|
39
|
+
* on: ['create', 'update'],
|
|
40
|
+
* filter: "_type == 'product'",
|
|
41
|
+
* projection: "{_id, title, slug}",
|
|
42
|
+
* includeDrafts: false,
|
|
43
|
+
* },
|
|
44
|
+
* env: {
|
|
45
|
+
* EXTERNAL_API_URL: 'https://api.example.com',
|
|
46
|
+
* SUPER_SECRET: process.env.SUPER_SECRET,
|
|
47
|
+
* },
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Defines a function that is triggered by document events in Sanity datasets.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* defineDocumentFunction({
|
|
56
|
+
* name: 'my-document-function',
|
|
57
|
+
* event: {
|
|
58
|
+
* on: ['create', 'update'],
|
|
59
|
+
* filter: "_type == 'post'",
|
|
60
|
+
* projection: "{_id, title, slug}",
|
|
61
|
+
* },
|
|
62
|
+
* })
|
|
63
|
+
* ```
|
|
64
|
+
* @param functionConfig The configuration for the document function
|
|
65
|
+
* @public
|
|
66
|
+
* @category Definers
|
|
67
|
+
* @expandType BlueprintDocumentFunctionConfig
|
|
68
|
+
* @returns The validated document function resource
|
|
69
|
+
*/
|
|
70
|
+
export function defineDocumentFunction(functionConfig: BlueprintDocumentFunctionConfig): BlueprintDocumentFunctionResource
|
|
71
|
+
/**
|
|
72
|
+
* @deprecated Define event properties under the 'event' key instead of specifying them at the top level
|
|
73
|
+
* @hidden
|
|
74
|
+
*/
|
|
75
|
+
export function defineDocumentFunction(
|
|
76
|
+
functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>,
|
|
77
|
+
): BlueprintDocumentFunctionResource
|
|
78
|
+
|
|
79
|
+
export function defineDocumentFunction(
|
|
80
|
+
functionConfig: BlueprintDocumentFunctionConfig & Partial<BlueprintDocumentFunctionResourceEvent>,
|
|
81
|
+
): BlueprintDocumentFunctionResource {
|
|
82
|
+
let {name, src, event, timeout, memory, env, robotToken, project, runtime, ...maybeEvent} = functionConfig
|
|
83
|
+
|
|
84
|
+
// event validation and normalization
|
|
85
|
+
if (event) {
|
|
86
|
+
// `event` was specified, but event keys (aggregated in `maybeEvent`) were also specified at the top level. ambiguous and deprecated usage.
|
|
87
|
+
const duplicateKeys = Array.from(DOCUMENT_EVENT_KEYS).filter((key) => key in maybeEvent)
|
|
88
|
+
if (duplicateKeys.length > 0) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`\`event\` properties should be specified under the \`event\` key - specifying them at the top level is deprecated. The following keys were specified at the top level: ${duplicateKeys.map((k) => `\`${k}\``).join(', ')}`,
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
event = buildDocumentFunctionEvent(event)
|
|
95
|
+
} else {
|
|
96
|
+
event = buildDocumentFunctionEvent(maybeEvent)
|
|
97
|
+
// deprecated usage of putting event properties at the top level, warn about this.
|
|
98
|
+
console.warn(
|
|
99
|
+
'⚠️ Deprecated usage of `defineDocumentFunction`: prefer to put `event` properties under the `event` key rather than at the top level.',
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const functionResource: BlueprintDocumentFunctionResource = {
|
|
104
|
+
...defineFunction(functionConfig, {skipValidation: true}),
|
|
105
|
+
type: 'sanity.function.document',
|
|
106
|
+
event,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
runValidation(() => validateDocumentFunction(functionResource))
|
|
110
|
+
|
|
111
|
+
return functionResource
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Builds a document function event configuration from partial event properties.
|
|
116
|
+
* Filters out non-event properties and applies defaults.
|
|
117
|
+
* @param event Partial document function event configuration
|
|
118
|
+
* @returns Complete document function event configuration
|
|
119
|
+
*/
|
|
120
|
+
function buildDocumentFunctionEvent(event: Partial<BlueprintDocumentFunctionResourceEvent>): BlueprintDocumentFunctionResourceEvent {
|
|
121
|
+
const cleanEvent = Object.fromEntries(
|
|
122
|
+
Object.entries(event).filter(([key]) => DOCUMENT_EVENT_KEYS.has(key as DocumentFunctionEventKey)),
|
|
123
|
+
) as Partial<BlueprintDocumentFunctionResourceEvent>
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
on: cleanEvent.on || ['publish'],
|
|
127
|
+
...cleanEvent,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {type BlueprintDurableConfig, type BlueprintDurableFunctionResource, validateDurableFunction} from '../../index.js'
|
|
2
|
+
import {runValidation} from '../../utils/validation.js'
|
|
3
|
+
import {defineFunction} from './index.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Defines a durable function resource.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* defineDurableFunction({
|
|
12
|
+
* name: 'daily-cleanup',
|
|
13
|
+
* event: {type: 'document', on: ['create'], filter: "_type == 'post'"},
|
|
14
|
+
* concurrency: 5,
|
|
15
|
+
* debounce: 10,
|
|
16
|
+
* debounceKey: 'document._id',
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
* @param functionConfig The configuration for the function
|
|
22
|
+
* @category Definers
|
|
23
|
+
* @alpha Deploying Durable Functions via Blueprints is experimental. This feature is not available publicly yet.
|
|
24
|
+
* @public
|
|
25
|
+
* @hidden
|
|
26
|
+
* @expandType BlueprintPipelineConfig
|
|
27
|
+
* @returns The validated durable function resource
|
|
28
|
+
*/
|
|
29
|
+
export function defineDurableFunction(functionConfig: BlueprintDurableConfig): BlueprintDurableFunctionResource {
|
|
30
|
+
const {name, event, concurrency, debounce, debounceKey, src} = functionConfig
|
|
31
|
+
const functionResource: BlueprintDurableFunctionResource = {
|
|
32
|
+
...defineFunction({...functionConfig, src: src ?? `functions/${name}`}, {skipValidation: true}),
|
|
33
|
+
type: 'sanity.function.durable',
|
|
34
|
+
...(event !== undefined && {event}),
|
|
35
|
+
...(concurrency !== undefined && {concurrency}),
|
|
36
|
+
...(debounce !== undefined && {debounce}),
|
|
37
|
+
...(debounceKey !== undefined && {debounceKey}),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
runValidation(() => validateDurableFunction(functionResource))
|
|
41
|
+
return functionResource
|
|
42
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {type BlueprintBaseFunctionConfig, type BlueprintBaseFunctionResource, validateFunction} from '../../index.js'
|
|
2
|
+
import {runValidation} from '../../utils/validation.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Defines a base function resource with common properties.
|
|
6
|
+
*
|
|
7
|
+
* @param functionConfig The configuration for the function
|
|
8
|
+
* @param options Optional configuration including validation options
|
|
9
|
+
* @category Definers
|
|
10
|
+
* @internal
|
|
11
|
+
* @expandType BlueprintBaseFunctionConfig
|
|
12
|
+
* @returns The validated function resource
|
|
13
|
+
*/
|
|
14
|
+
export function defineFunction(
|
|
15
|
+
functionConfig: BlueprintBaseFunctionConfig,
|
|
16
|
+
options?: {skipValidation?: boolean; scopeType?: 'organization'},
|
|
17
|
+
): BlueprintBaseFunctionResource {
|
|
18
|
+
const {name, displayName, src, timeout, memory, env, robotToken, project, runtime, lifecycle} = functionConfig
|
|
19
|
+
|
|
20
|
+
const functionResource: BlueprintBaseFunctionResource = {
|
|
21
|
+
type: 'sanity.function.document',
|
|
22
|
+
name,
|
|
23
|
+
src: src ?? `functions/${name}`,
|
|
24
|
+
displayName,
|
|
25
|
+
timeout,
|
|
26
|
+
memory,
|
|
27
|
+
env,
|
|
28
|
+
robotToken,
|
|
29
|
+
runtime,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options?.scopeType !== 'organization' && project) functionResource.project = project
|
|
33
|
+
|
|
34
|
+
if (lifecycle) functionResource.lifecycle = lifecycle
|
|
35
|
+
|
|
36
|
+
if (options?.skipValidation !== true) runValidation(() => validateFunction(functionResource))
|
|
37
|
+
|
|
38
|
+
return functionResource
|
|
39
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlueprintMediaLibraryAssetFunctionConfig,
|
|
3
|
+
type BlueprintMediaLibraryAssetFunctionResource,
|
|
4
|
+
type BlueprintMediaLibraryFunctionResourceEvent,
|
|
5
|
+
validateMediaLibraryAssetFunction,
|
|
6
|
+
} from '../../index.js'
|
|
7
|
+
import {runValidation} from '../../utils/validation.js'
|
|
8
|
+
import {BASE_EVENT_KEYS} from './base-event.js'
|
|
9
|
+
import {defineFunction} from './index.js'
|
|
10
|
+
|
|
11
|
+
type MediaLibraryFunctionEventKey = keyof BlueprintMediaLibraryFunctionResourceEvent
|
|
12
|
+
const MEDIA_LIBRARY_EVENT_KEYS = new Set<MediaLibraryFunctionEventKey>(['resource', ...BASE_EVENT_KEYS.values()])
|
|
13
|
+
/*
|
|
14
|
+
* FUTURE example (move below @example when ready)
|
|
15
|
+
* @example With robot token reference
|
|
16
|
+
* ```ts
|
|
17
|
+
* defineRobotToken({
|
|
18
|
+
* name: 'media-robot',
|
|
19
|
+
* memberships: [{
|
|
20
|
+
* resourceType: 'project',
|
|
21
|
+
* resourceId: projectId,
|
|
22
|
+
* roleNames: ['editor'],
|
|
23
|
+
* }],
|
|
24
|
+
* })
|
|
25
|
+
*
|
|
26
|
+
* defineMediaLibraryAssetFunction({
|
|
27
|
+
* name: 'process-uploads',
|
|
28
|
+
* src: 'functions/process-uploads-v2',
|
|
29
|
+
* robotToken: '$.resources.media-robot',
|
|
30
|
+
* event: {
|
|
31
|
+
* on: ['create', 'update'],
|
|
32
|
+
* resource: {
|
|
33
|
+
* type: 'media-library',
|
|
34
|
+
* id: 'my-media-library-id',
|
|
35
|
+
* },
|
|
36
|
+
* filter: "type == 'image'",
|
|
37
|
+
* projection: "{_id}",
|
|
38
|
+
* },
|
|
39
|
+
* env: {
|
|
40
|
+
* CDN_BUCKET: 'my-cdn-bucket',
|
|
41
|
+
* },
|
|
42
|
+
* })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* Defines a function that is triggered by media library events.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* defineMediaLibraryAssetFunction({
|
|
50
|
+
* name: 'my-media-library-function',
|
|
51
|
+
* event: {
|
|
52
|
+
* on: ['create'],
|
|
53
|
+
* resource: {
|
|
54
|
+
* type: 'media-library',
|
|
55
|
+
* id: 'my-media-library-id',
|
|
56
|
+
* },
|
|
57
|
+
* },
|
|
58
|
+
* })
|
|
59
|
+
* ```
|
|
60
|
+
* @param functionConfig The configuration for the media library asset function
|
|
61
|
+
* @public
|
|
62
|
+
* @category Definers
|
|
63
|
+
* @expandType BlueprintMediaLibraryAssetFunctionConfig
|
|
64
|
+
* @returns The validated media library asset function resource
|
|
65
|
+
*/
|
|
66
|
+
export function defineMediaLibraryAssetFunction(
|
|
67
|
+
functionConfig: BlueprintMediaLibraryAssetFunctionConfig,
|
|
68
|
+
): BlueprintMediaLibraryAssetFunctionResource {
|
|
69
|
+
const {event} = functionConfig
|
|
70
|
+
|
|
71
|
+
const functionResource: BlueprintMediaLibraryAssetFunctionResource = {
|
|
72
|
+
...defineFunction(functionConfig, {skipValidation: true}),
|
|
73
|
+
type: 'sanity.function.media-library.asset',
|
|
74
|
+
event: buildMediaLibraryFunctionEvent(event),
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
runValidation(() => validateMediaLibraryAssetFunction(functionResource))
|
|
78
|
+
|
|
79
|
+
return functionResource
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Builds a media library function event configuration.
|
|
84
|
+
* Filters out non-event properties and applies defaults.
|
|
85
|
+
* @param event Media library function event configuration
|
|
86
|
+
* @returns Complete media library function event configuration
|
|
87
|
+
*/
|
|
88
|
+
function buildMediaLibraryFunctionEvent(event: BlueprintMediaLibraryFunctionResourceEvent): BlueprintMediaLibraryFunctionResourceEvent {
|
|
89
|
+
const cleanEvent = Object.fromEntries(
|
|
90
|
+
Object.entries(event).filter(([key]) => MEDIA_LIBRARY_EVENT_KEYS.has(key as MediaLibraryFunctionEventKey)),
|
|
91
|
+
) as BlueprintMediaLibraryFunctionResourceEvent
|
|
92
|
+
|
|
93
|
+
const fullEvent = {
|
|
94
|
+
on: cleanEvent.on || ['publish'],
|
|
95
|
+
...cleanEvent,
|
|
96
|
+
}
|
|
97
|
+
return fullEvent
|
|
98
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {type BlueprintPubSubFunctionConfig, type BlueprintPubSubFunctionResource, validatePubSubFunction} from '../../index.js'
|
|
2
|
+
import {runValidation} from '../../utils/validation.js'
|
|
3
|
+
import {defineFunction} from './index.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Defines a function that provide pubsub behaviour.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* A barebones pubsub function
|
|
10
|
+
* ```ts
|
|
11
|
+
* definePubSubFunction({
|
|
12
|
+
* name: 'send-email',
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
* @public
|
|
16
|
+
* @alpha Deploying PubSub Functions via Blueprints is experimental. This feature is not available publicly yet.
|
|
17
|
+
* @hidden
|
|
18
|
+
* @category Definers
|
|
19
|
+
* @expandType BlueprintPubSubFunctionConfig
|
|
20
|
+
* @param functionConfig The configuration for the pubsub function
|
|
21
|
+
* @returns The validated pubsub function resource
|
|
22
|
+
*/
|
|
23
|
+
export function definePubSubFunction(functionConfig: BlueprintPubSubFunctionConfig): BlueprintPubSubFunctionResource {
|
|
24
|
+
const functionResource: BlueprintPubSubFunctionResource = {
|
|
25
|
+
...defineFunction(functionConfig, {skipValidation: true}),
|
|
26
|
+
type: 'sanity.function.pubsub',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
runValidation(() => validatePubSubFunction(functionResource))
|
|
30
|
+
|
|
31
|
+
return functionResource
|
|
32
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {type BlueprintQueueFunctionConfig, type BlueprintQueueFunctionResource, validateQueueFunction} from '../../index.js'
|
|
2
|
+
import {runValidation} from '../../utils/validation.js'
|
|
3
|
+
import {defineFunction} from './index.js'
|
|
4
|
+
/**
|
|
5
|
+
* Defines a function that provide queueing behaviour.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Using the reasonable defaults of concurrency: 1, fifo: true, and dlq: true
|
|
9
|
+
* ```ts
|
|
10
|
+
* defineQueueFunction({
|
|
11
|
+
* name: 'send-email',
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Specifying a concurrency of 5
|
|
16
|
+
* ```ts
|
|
17
|
+
* defineQueueFunction({
|
|
18
|
+
* name: 'bustin-caches',
|
|
19
|
+
* concurrency: 5,
|
|
20
|
+
* fifo: true,
|
|
21
|
+
* dlq: true,
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
* @public
|
|
25
|
+
* @alpha Deploying Queue Functions via Blueprints is experimental. This feature is not available publicly yet.
|
|
26
|
+
* @hidden
|
|
27
|
+
* @category Definers
|
|
28
|
+
* @expandType BlueprintQueueFunctionConfig
|
|
29
|
+
* @param functionConfig The configuration for the queue function
|
|
30
|
+
* @returns The validated queue function resource
|
|
31
|
+
*/
|
|
32
|
+
export function defineQueueFunction(functionConfig: BlueprintQueueFunctionConfig): BlueprintQueueFunctionResource {
|
|
33
|
+
const {concurrency = 1, fifo = true, dlq = true, debounce, debounceKey, event} = functionConfig
|
|
34
|
+
|
|
35
|
+
const functionResource: BlueprintQueueFunctionResource = {
|
|
36
|
+
...defineFunction(functionConfig, {skipValidation: true}),
|
|
37
|
+
type: 'sanity.function.queue',
|
|
38
|
+
...(concurrency !== undefined && {concurrency}),
|
|
39
|
+
...(debounce !== undefined && {debounce}),
|
|
40
|
+
...(debounceKey !== undefined && {debounceKey}),
|
|
41
|
+
...(dlq !== undefined && {dlq}),
|
|
42
|
+
...(event !== undefined && {event}),
|
|
43
|
+
...(fifo !== undefined && {fifo}),
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
runValidation(() => validateQueueFunction(functionResource))
|
|
47
|
+
|
|
48
|
+
return functionResource
|
|
49
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlueprintScheduledFunctionConfig,
|
|
3
|
+
type BlueprintScheduledFunctionConfigEvent,
|
|
4
|
+
type BlueprintScheduledFunctionExplicitResourceEvent,
|
|
5
|
+
type BlueprintScheduledFunctionExpressionResourceEvent,
|
|
6
|
+
type BlueprintScheduledFunctionResource,
|
|
7
|
+
type BlueprintScheduledFunctionResourceEvent,
|
|
8
|
+
validateScheduledFunction,
|
|
9
|
+
} from '../../index.js'
|
|
10
|
+
import {parseScheduledExpression} from '../../utils/schedule-parser.js'
|
|
11
|
+
import {runValidation} from '../../utils/validation.js'
|
|
12
|
+
import {defineFunction} from './index.js'
|
|
13
|
+
|
|
14
|
+
type ScheduledFunctionEventKey =
|
|
15
|
+
| keyof BlueprintScheduledFunctionExplicitResourceEvent
|
|
16
|
+
| keyof BlueprintScheduledFunctionExpressionResourceEvent
|
|
17
|
+
const SCHEDULED_EVENT_KEYS = new Set<ScheduledFunctionEventKey>(['minute', 'hour', 'dayOfWeek', 'month', 'dayOfMonth', 'expression'])
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Defines a function that is triggered on a schedule.
|
|
21
|
+
* Supports cron expressions or natural language schedules.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Using a cron expression:
|
|
25
|
+
* ```ts
|
|
26
|
+
* defineScheduledFunction({
|
|
27
|
+
* name: 'daily-cleanup',
|
|
28
|
+
* event: {expression: '0 9 * * *'},
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* Using explicit cron fields:
|
|
33
|
+
* ```ts
|
|
34
|
+
* defineScheduledFunction({
|
|
35
|
+
* name: 'daily-cleanup',
|
|
36
|
+
* event: {minute: '0', hour: '9', dayOfMonth: '*', month: '*', dayOfWeek: '*'},
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
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
|
+
* ```ts
|
|
45
|
+
* defineScheduledFunction({
|
|
46
|
+
* name: 'daily-cleanup',
|
|
47
|
+
* event: {expression: 'every day at 9am'},
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
* @public
|
|
51
|
+
* @alpha Deploying Scheduled Functions via Blueprints is experimental. This feature is not available publicly yet.
|
|
52
|
+
* @hidden
|
|
53
|
+
* @category Definers
|
|
54
|
+
* @expandType BlueprintScheduledFunctionConfig
|
|
55
|
+
* @param functionConfig The configuration for the scheduled function
|
|
56
|
+
* @returns The validated scheduled function resource
|
|
57
|
+
*/
|
|
58
|
+
export function defineScheduledFunction(functionConfig: BlueprintScheduledFunctionConfig): BlueprintScheduledFunctionResource {
|
|
59
|
+
const {event, timezone} = functionConfig
|
|
60
|
+
|
|
61
|
+
const functionResource: BlueprintScheduledFunctionResource = {
|
|
62
|
+
...defineFunction(functionConfig, {skipValidation: true, scopeType: 'organization'}),
|
|
63
|
+
type: 'sanity.function.cron',
|
|
64
|
+
event: buildScheduledFunctionEvent(event),
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (timezone) functionResource.timezone = timezone
|
|
68
|
+
|
|
69
|
+
// Always normalize to explicit fields (minute, hour, dayOfWeek, month, dayOfMonth)
|
|
70
|
+
if ('expression' in functionResource.event && typeof functionResource.event.expression === 'string') {
|
|
71
|
+
const cron = parseScheduledExpression(functionResource.event.expression)
|
|
72
|
+
functionResource.event = cronStringToExplicitEvent(cron)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
runValidation(() => validateScheduledFunction(functionResource))
|
|
76
|
+
|
|
77
|
+
return functionResource
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated Define scheduled functions using `defineScheduledFunction` instead
|
|
82
|
+
* @hidden
|
|
83
|
+
*/
|
|
84
|
+
export function defineScheduleFunction(functionConfig: BlueprintScheduledFunctionConfig): BlueprintScheduledFunctionResource {
|
|
85
|
+
return defineScheduledFunction(functionConfig)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Builds a scheduled function event configuration.
|
|
90
|
+
* Filters out non-event properties. Does not parse expressions.
|
|
91
|
+
* @param event Scheduled function event configuration
|
|
92
|
+
* @returns Cleaned scheduled function event configuration
|
|
93
|
+
*/
|
|
94
|
+
function buildScheduledFunctionEvent(event: BlueprintScheduledFunctionConfigEvent): BlueprintScheduledFunctionResourceEvent {
|
|
95
|
+
return Object.fromEntries(
|
|
96
|
+
Object.entries(event).filter(([key]) => SCHEDULED_EVENT_KEYS.has(key as ScheduledFunctionEventKey)),
|
|
97
|
+
) as BlueprintScheduledFunctionResourceEvent
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Converts a cron expression string (minute hour dayOfMonth month dayOfWeek) to explicit event fields.
|
|
102
|
+
* @param cron Cron string with five space-separated fields
|
|
103
|
+
* @returns Explicit scheduled event with minute, hour, dayOfMonth, month, dayOfWeek
|
|
104
|
+
*/
|
|
105
|
+
function cronStringToExplicitEvent(cron: string): BlueprintScheduledFunctionExplicitResourceEvent {
|
|
106
|
+
const parts = cron.trim().split(/\s+/)
|
|
107
|
+
if (parts.length !== 5) {
|
|
108
|
+
throw new Error(`Invalid cron string: expected 5 fields, got ${parts.length}`)
|
|
109
|
+
}
|
|
110
|
+
const [minute, hour, dayOfMonth, month, dayOfWeek] = parts
|
|
111
|
+
return {minute, hour, dayOfMonth, month, dayOfWeek}
|
|
112
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlueprintSyncTagInvalidateFunctionConfig,
|
|
3
|
+
type BlueprintSyncTagInvalidateFunctionResource,
|
|
4
|
+
validateSyncTagInvalidateFunction,
|
|
5
|
+
} from '../../index.js'
|
|
6
|
+
import {runValidation} from '../../utils/validation.js'
|
|
7
|
+
import {defineFunction} from './index.js'
|
|
8
|
+
/**
|
|
9
|
+
* Defines a function that is triggered on a sync tag invalidate event.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Using implicit event scoping to all datasets in the function project:
|
|
13
|
+
* ```ts
|
|
14
|
+
* defineSyncTagInvalidateFunction({
|
|
15
|
+
* name: 'bustin-caches',
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Using explicit event scoping to a particular dataset in the function project:
|
|
20
|
+
* ```ts
|
|
21
|
+
* defineSyncTagInvalidateFunction({
|
|
22
|
+
* name: 'bustin-caches',
|
|
23
|
+
* event: {
|
|
24
|
+
* resource: {
|
|
25
|
+
* type: 'dataset',
|
|
26
|
+
* id: 'myProj.myDataset',
|
|
27
|
+
* },
|
|
28
|
+
* },
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
* @public
|
|
32
|
+
* @category Definers
|
|
33
|
+
* @expandType BlueprintSyncTagInvalidateFunctionConfig
|
|
34
|
+
* @param functionConfig The configuration for the sync tag invalidate function
|
|
35
|
+
* @returns The validated sync tag invalidate function resource
|
|
36
|
+
*/
|
|
37
|
+
export function defineSyncTagInvalidateFunction(
|
|
38
|
+
functionConfig: BlueprintSyncTagInvalidateFunctionConfig,
|
|
39
|
+
): BlueprintSyncTagInvalidateFunctionResource {
|
|
40
|
+
const {event} = functionConfig
|
|
41
|
+
|
|
42
|
+
const functionResource: BlueprintSyncTagInvalidateFunctionResource = {
|
|
43
|
+
...defineFunction(functionConfig, {skipValidation: true}),
|
|
44
|
+
type: 'sanity.function.sync-tag-invalidate',
|
|
45
|
+
...(event?.resource ? {event} : {}),
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
runValidation(() => validateSyncTagInvalidateFunction(functionResource))
|
|
49
|
+
|
|
50
|
+
return functionResource
|
|
51
|
+
}
|
package/src/definers/studios.ts
CHANGED
|
@@ -24,6 +24,11 @@ import {runValidation} from '../utils/validation.js'
|
|
|
24
24
|
*/
|
|
25
25
|
export function defineStudio(config: BlueprintStudioConfig): BlueprintStudioResource {
|
|
26
26
|
const studioResource: BlueprintStudioResource = {
|
|
27
|
+
title: config.name,
|
|
28
|
+
autoUpdates: {
|
|
29
|
+
enabled: config.autoUpdates?.enabled ?? true,
|
|
30
|
+
version: config.autoUpdates?.version,
|
|
31
|
+
},
|
|
27
32
|
...config,
|
|
28
33
|
type: 'sanity.studio',
|
|
29
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,14 @@
|
|
|
10
10
|
export * from './definers/blueprints.js'
|
|
11
11
|
export * from './definers/cors.js'
|
|
12
12
|
export * from './definers/datasets.js'
|
|
13
|
-
export * from './definers/functions.js'
|
|
13
|
+
export * from './definers/functions/document.js'
|
|
14
|
+
export * from './definers/functions/durable.js'
|
|
15
|
+
export * from './definers/functions/index.js'
|
|
16
|
+
export * from './definers/functions/media-library.js'
|
|
17
|
+
export * from './definers/functions/pubsub.js'
|
|
18
|
+
export * from './definers/functions/queue.js'
|
|
19
|
+
export * from './definers/functions/scheduled.js'
|
|
20
|
+
export * from './definers/functions/sync-tag-invalidate.js'
|
|
14
21
|
export * from './definers/projects.js'
|
|
15
22
|
export * from './definers/resources.js'
|
|
16
23
|
export * from './definers/robots.js'
|
|
@@ -84,13 +84,12 @@ export type BlueprintScheduledFunctionConfigEvent =
|
|
|
84
84
|
| BlueprintScheduledFunctionExpressionResourceEvent
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
|
-
* Union type of all
|
|
87
|
+
* Union type of all Content Lake event source configurations
|
|
88
88
|
* @category Functions Types
|
|
89
89
|
*/
|
|
90
|
-
export type
|
|
90
|
+
export type BlueprintFunctionResourceContentLakeEvent =
|
|
91
91
|
| ({type: 'document'} & BlueprintDocumentFunctionResourceEvent)
|
|
92
92
|
| ({type: 'media-library'} & BlueprintMediaLibraryFunctionResourceEvent)
|
|
93
|
-
| ({type: 'cron'} & BlueprintScheduledFunctionResourceEvent)
|
|
94
93
|
| ({type: 'sync-tag-invalidate'} & BlueprintSyncTagInvalidateFunctionResourceEvent)
|
|
95
94
|
|
|
96
95
|
/**
|