@sanity/blueprints 0.0.1 → 0.2.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.
- package/README.md +0 -2
- package/dist/definers.d.ts +2 -0
- package/dist/definers.js +19 -13
- package/dist/definers.js.map +1 -1
- package/dist/types.d.ts +5 -1
- package/package.json +1 -1
- package/src/definers.ts +33 -11
- package/src/types.ts +5 -1
package/README.md
CHANGED
|
@@ -12,8 +12,6 @@ import {defineBlueprint, defineDocumentFunction, defineResource} from '@sanity/b
|
|
|
12
12
|
|
|
13
13
|
export default defineBlueprint({
|
|
14
14
|
resources: [
|
|
15
|
-
defineDocumentFunction({name: 'invalidate-cache', timeout: 60, projection: '_id'}),
|
|
16
|
-
defineDocumentFunction({name: 'send-email', filter: "_type == 'press-release'"}),
|
|
17
15
|
defineDocumentFunction({
|
|
18
16
|
name: 'Create Fancy Report',
|
|
19
17
|
src: 'functions/create-fancy-report',
|
package/dist/definers.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { Blueprint, BlueprintFunctionResource, BlueprintFunctionResourceEvent, BlueprintModule, BlueprintResource, BlueprintsApiConfig } from './types.js';
|
|
2
2
|
export declare function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<BlueprintsApiConfig>): BlueprintModule;
|
|
3
|
+
export declare function defineDocumentFunction(functionConfig: Partial<BlueprintFunctionResource>): BlueprintFunctionResource;
|
|
4
|
+
/** @deprecated Define event properties under the 'event' key instead of specifying them at the top level */
|
|
3
5
|
export declare function defineDocumentFunction(functionConfig: Partial<BlueprintFunctionResource> & Partial<BlueprintFunctionResourceEvent>): BlueprintFunctionResource;
|
|
4
6
|
export declare function defineResource(resourceConfig: Partial<BlueprintResource>): BlueprintResource;
|
package/dist/definers.js
CHANGED
|
@@ -16,7 +16,7 @@ export function defineBlueprint(blueprintConfig) {
|
|
|
16
16
|
blueprint.stackId = stackId;
|
|
17
17
|
return blueprint;
|
|
18
18
|
}
|
|
19
|
-
const EVENT_KEYS = new Set(['on', 'filter', 'projection']);
|
|
19
|
+
const EVENT_KEYS = new Set(['on', 'filter', 'includeDrafts', 'includeAllVersions', 'projection']);
|
|
20
20
|
export function defineDocumentFunction(functionConfig) {
|
|
21
21
|
let { name, src, event, timeout, memory, env, type, ...maybeEvent } = functionConfig;
|
|
22
22
|
if (!name)
|
|
@@ -33,21 +33,17 @@ export function defineDocumentFunction(functionConfig) {
|
|
|
33
33
|
throw new Error('`timeout` must be a number');
|
|
34
34
|
// event validation
|
|
35
35
|
if (event) {
|
|
36
|
-
//
|
|
37
|
-
const
|
|
38
|
-
if (
|
|
39
|
-
throw new Error(`\`event\`
|
|
36
|
+
// `event` was specified, but event keys (aggregated in `maybeEvent`) were also specified at the top level. ambiguous and deprecated usage.
|
|
37
|
+
const duplicateKeys = Object.keys(maybeEvent).filter((k) => EVENT_KEYS.has(k));
|
|
38
|
+
if (duplicateKeys.length > 0) {
|
|
39
|
+
throw new Error(`\`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(', ')}`);
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
throw new Error('`event.on` is required');
|
|
43
|
-
if (!Array.isArray(event.on))
|
|
44
|
-
throw new Error('`event.on` must be an array');
|
|
41
|
+
event = validateDocumentFunctionEvent(event);
|
|
45
42
|
}
|
|
46
43
|
else {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
event = { on: on ?? ['publish'], filter, projection };
|
|
44
|
+
event = validateDocumentFunctionEvent(maybeEvent);
|
|
45
|
+
// deprecated usage of putting event properties at the top level, warn about this.
|
|
46
|
+
console.warn('⚠️ Deprecated usage of `defineDocumentFunction`: prefer to put `event` properties under the `event` key rather than at the top level.');
|
|
51
47
|
}
|
|
52
48
|
return {
|
|
53
49
|
type,
|
|
@@ -71,4 +67,14 @@ export function defineResource(resourceConfig) {
|
|
|
71
67
|
name,
|
|
72
68
|
};
|
|
73
69
|
}
|
|
70
|
+
function validateDocumentFunctionEvent(event) {
|
|
71
|
+
const cleanEvent = Object.fromEntries(Object.entries(event).filter(([key]) => EVENT_KEYS.has(key)));
|
|
72
|
+
const fullEvent = {
|
|
73
|
+
on: cleanEvent.on || ['publish'],
|
|
74
|
+
...cleanEvent,
|
|
75
|
+
};
|
|
76
|
+
if (!Array.isArray(fullEvent.on))
|
|
77
|
+
throw new Error('`event.on` must be an array');
|
|
78
|
+
return fullEvent;
|
|
79
|
+
}
|
|
74
80
|
//# sourceMappingURL=definers.js.map
|
package/dist/definers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,eAAe,CAAC,eAAkE;IAChG,MAAM,EAAC,cAAc,EAAE,SAAS,EAAE,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAC,GAAG,eAAe,CAAA;IAE1G,IAAI,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;IAE3F,SAAS,SAAS;QAChB,OAAO;YACL,OAAO,EAAE,wEAAwE;YACjF,gBAAgB,EAAE,gBAAgB,IAAI,YAAY;YAClD,SAAS;YACT,MAAM;YACN,OAAO;SACR,CAAA;IACH,CAAC;IAED,SAAS,CAAC,cAAc,GAAG,cAAc,CAAA;IACzC,SAAS,CAAC,SAAS,GAAG,SAAS,CAAA;IAC/B,SAAS,CAAC,OAAO,GAAG,OAAO,CAAA;IAE3B,OAAO,SAAS,CAAA;AAClB,CAAC;AAGD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"definers.js","sourceRoot":"","sources":["../src/definers.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UAAU,GAAG,MAAM,CAAC,WAAW,CACnC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,GAAe,CAAC,CAAC,CAC9B,CAAA;IAE5C,MAAM,SAAS,GAAG;QAChB,EAAE,EAAE,UAAU,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC;QAChC,GAAG,UAAU;KACd,CAAA;IACD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;IAChF,OAAO,SAAS,CAAA;AAClB,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -3,10 +3,13 @@ export interface BlueprintResource {
|
|
|
3
3
|
name: string;
|
|
4
4
|
}
|
|
5
5
|
export interface BlueprintFunctionResourceEvent {
|
|
6
|
-
on
|
|
6
|
+
on?: [BlueprintFunctionResourceEventName, ...BlueprintFunctionResourceEventName[]];
|
|
7
7
|
filter?: string;
|
|
8
|
+
includeDrafts?: boolean;
|
|
9
|
+
includeAllVersions?: boolean;
|
|
8
10
|
projection?: string;
|
|
9
11
|
}
|
|
12
|
+
type BlueprintFunctionResourceEventName = 'publish' | 'create' | 'delete' | 'update';
|
|
10
13
|
export interface BlueprintFunctionResource extends BlueprintResource {
|
|
11
14
|
type: 'sanity.function.document';
|
|
12
15
|
src: string;
|
|
@@ -36,3 +39,4 @@ export type BlueprintModule = ((args?: unknown) => Blueprint) & {
|
|
|
36
39
|
projectId?: string;
|
|
37
40
|
stackId?: string;
|
|
38
41
|
};
|
|
42
|
+
export {};
|
package/package.json
CHANGED
package/src/definers.ts
CHANGED
|
@@ -30,7 +30,14 @@ export function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<Bl
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
type EventKey = keyof BlueprintFunctionResourceEvent
|
|
33
|
-
const EVENT_KEYS = new Set<EventKey>(['on', 'filter', 'projection'])
|
|
33
|
+
const EVENT_KEYS = new Set<EventKey>(['on', 'filter', 'includeDrafts', 'includeAllVersions', 'projection'])
|
|
34
|
+
|
|
35
|
+
export function defineDocumentFunction(functionConfig: Partial<BlueprintFunctionResource>): BlueprintFunctionResource
|
|
36
|
+
|
|
37
|
+
/** @deprecated Define event properties under the 'event' key instead of specifying them at the top level */
|
|
38
|
+
export function defineDocumentFunction(
|
|
39
|
+
functionConfig: Partial<BlueprintFunctionResource> & Partial<BlueprintFunctionResourceEvent>,
|
|
40
|
+
): BlueprintFunctionResource
|
|
34
41
|
|
|
35
42
|
export function defineDocumentFunction(
|
|
36
43
|
functionConfig: Partial<BlueprintFunctionResource> & Partial<BlueprintFunctionResourceEvent>,
|
|
@@ -49,18 +56,20 @@ export function defineDocumentFunction(
|
|
|
49
56
|
|
|
50
57
|
// event validation
|
|
51
58
|
if (event) {
|
|
52
|
-
//
|
|
53
|
-
const
|
|
54
|
-
if (
|
|
55
|
-
throw new Error(
|
|
59
|
+
// `event` was specified, but event keys (aggregated in `maybeEvent`) were also specified at the top level. ambiguous and deprecated usage.
|
|
60
|
+
const duplicateKeys = Object.keys(maybeEvent).filter((k) => EVENT_KEYS.has(k as EventKey))
|
|
61
|
+
if (duplicateKeys.length > 0) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`\`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(', ')}`,
|
|
64
|
+
)
|
|
56
65
|
}
|
|
57
|
-
|
|
58
|
-
if (!event.on) throw new Error('`event.on` is required')
|
|
59
|
-
if (!Array.isArray(event.on)) throw new Error('`event.on` must be an array')
|
|
66
|
+
event = validateDocumentFunctionEvent(event)
|
|
60
67
|
} else {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
event = validateDocumentFunctionEvent(maybeEvent)
|
|
69
|
+
// deprecated usage of putting event properties at the top level, warn about this.
|
|
70
|
+
console.warn(
|
|
71
|
+
'⚠️ Deprecated usage of `defineDocumentFunction`: prefer to put `event` properties under the `event` key rather than at the top level.',
|
|
72
|
+
)
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
return {
|
|
@@ -85,3 +94,16 @@ export function defineResource(resourceConfig: Partial<BlueprintResource>): Blue
|
|
|
85
94
|
name,
|
|
86
95
|
}
|
|
87
96
|
}
|
|
97
|
+
|
|
98
|
+
function validateDocumentFunctionEvent(event: Partial<BlueprintFunctionResourceEvent>): BlueprintFunctionResourceEvent {
|
|
99
|
+
const cleanEvent = Object.fromEntries(
|
|
100
|
+
Object.entries(event).filter(([key]) => EVENT_KEYS.has(key as EventKey)),
|
|
101
|
+
) as Partial<BlueprintFunctionResourceEvent>
|
|
102
|
+
|
|
103
|
+
const fullEvent = {
|
|
104
|
+
on: cleanEvent.on || ['publish'],
|
|
105
|
+
...cleanEvent,
|
|
106
|
+
}
|
|
107
|
+
if (!Array.isArray(fullEvent.on)) throw new Error('`event.on` must be an array')
|
|
108
|
+
return fullEvent
|
|
109
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -4,11 +4,15 @@ export interface BlueprintResource {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export interface BlueprintFunctionResourceEvent {
|
|
7
|
-
on
|
|
7
|
+
on?: [BlueprintFunctionResourceEventName, ...BlueprintFunctionResourceEventName[]]
|
|
8
8
|
filter?: string
|
|
9
|
+
includeDrafts?: boolean
|
|
10
|
+
includeAllVersions?: boolean
|
|
9
11
|
projection?: string
|
|
10
12
|
}
|
|
11
13
|
|
|
14
|
+
type BlueprintFunctionResourceEventName = 'publish' | 'create' | 'delete' | 'update'
|
|
15
|
+
|
|
12
16
|
export interface BlueprintFunctionResource extends BlueprintResource {
|
|
13
17
|
type: 'sanity.function.document'
|
|
14
18
|
src: string
|