@sanity/blueprints 0.0.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/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # @sanity/blueprints
2
+
3
+ > [!IMPORTANT]
4
+ > This package is currently in beta and may change. Refer to the [CHANGELOG](./CHANGELOG.md) for details.
5
+
6
+ Helper methods for building valid Sanity Blueprints.
7
+
8
+ ## Usage
9
+
10
+ ```ts
11
+ import {defineBlueprint, defineDocumentFunction, defineResource} from '@sanity/blueprints'
12
+
13
+ export default defineBlueprint({
14
+ resources: [
15
+ defineDocumentFunction({name: 'invalidate-cache', timeout: 60, projection: '_id'}),
16
+ defineDocumentFunction({name: 'send-email', filter: "_type == 'press-release'"}),
17
+ defineDocumentFunction({
18
+ name: 'Create Fancy Report',
19
+ src: 'functions/create-fancy-report',
20
+ memory: 2,
21
+ timeout: 360,
22
+ event: {
23
+ on: ['publish'],
24
+ filter: "_type == 'customer'",
25
+ projection: "totalSpend, lastOrderDate",
26
+ },
27
+ env: {
28
+ currency: 'USD',
29
+ },
30
+ }),
31
+
32
+ defineResource({name: 'test-resource', type: 'test'}),
33
+ ],
34
+ })
35
+ ```
@@ -0,0 +1,4 @@
1
+ import type { Blueprint, BlueprintFunctionResource, BlueprintFunctionResourceEvent, BlueprintModule, BlueprintResource, BlueprintsApiConfig } from './types.js';
2
+ export declare function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<BlueprintsApiConfig>): BlueprintModule;
3
+ export declare function defineDocumentFunction(functionConfig: Partial<BlueprintFunctionResource> & Partial<BlueprintFunctionResourceEvent>): BlueprintFunctionResource;
4
+ export declare function defineResource(resourceConfig: Partial<BlueprintResource>): BlueprintResource;
@@ -0,0 +1,74 @@
1
+ export function defineBlueprint(blueprintConfig) {
2
+ const { organizationId, projectId, stackId, blueprintVersion, resources, values, outputs } = blueprintConfig;
3
+ if (resources && !Array.isArray(resources))
4
+ throw new Error('`resources` must be an array');
5
+ function blueprint() {
6
+ return {
7
+ $schema: 'https://schemas.sanity.io/blueprints/v2024-10-01/blueprint.schema.json',
8
+ blueprintVersion: blueprintVersion ?? '2024-10-01',
9
+ resources,
10
+ values,
11
+ outputs,
12
+ };
13
+ }
14
+ blueprint.organizationId = organizationId;
15
+ blueprint.projectId = projectId;
16
+ blueprint.stackId = stackId;
17
+ return blueprint;
18
+ }
19
+ const EVENT_KEYS = new Set(['on', 'filter', 'projection']);
20
+ export function defineDocumentFunction(functionConfig) {
21
+ let { name, src, event, timeout, memory, env, type, ...maybeEvent } = functionConfig;
22
+ if (!name)
23
+ throw new Error('`name` is required');
24
+ // defaults
25
+ if (!src)
26
+ src = `functions/${name}`;
27
+ if (!type)
28
+ type = 'sanity.function.document';
29
+ // type validation
30
+ if (memory && typeof memory !== 'number')
31
+ throw new Error('`memory` must be a number');
32
+ if (timeout && typeof timeout !== 'number')
33
+ throw new Error('`timeout` must be a number');
34
+ // event validation
35
+ if (event) {
36
+ // check for `event` and `maybeEvent` collision
37
+ const invalidKeys = Object.keys(maybeEvent).filter((k) => EVENT_KEYS.has(k));
38
+ if (invalidKeys.length > 0) {
39
+ throw new Error(`\`event\` cannot be specified with ${invalidKeys.map((k) => `\`${k}\``).join(', ')}`);
40
+ }
41
+ if (!event.on)
42
+ throw new Error('`event.on` is required');
43
+ if (!Array.isArray(event.on))
44
+ throw new Error('`event.on` must be an array');
45
+ }
46
+ else {
47
+ const { on, filter, projection } = maybeEvent;
48
+ if (on && !Array.isArray(on))
49
+ throw new Error('`on` must be an array');
50
+ event = { on: on ?? ['publish'], filter, projection };
51
+ }
52
+ return {
53
+ type,
54
+ name,
55
+ src,
56
+ event,
57
+ timeout,
58
+ memory,
59
+ env,
60
+ };
61
+ }
62
+ export function defineResource(resourceConfig) {
63
+ const { name, type } = resourceConfig;
64
+ if (!name)
65
+ throw new Error('`name` is required');
66
+ if (!type)
67
+ throw new Error('`type` is required');
68
+ return {
69
+ ...resourceConfig,
70
+ type,
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L,OAAO;QACP,MAAM;QACN,GAAG;KACJ,CAAA;AACH,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,cAA0C;IACvE,MAAM,EAAC,IAAI,EAAE,IAAI,EAAC,GAAG,cAAc,CAAA;IACnC,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;IAChD,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;IAEhD,OAAO;QACL,GAAG,cAAc;QACjB,IAAI;QACJ,IAAI;KACL,CAAA;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export * from './definers.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './definers.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA"}
@@ -0,0 +1,38 @@
1
+ export interface BlueprintResource {
2
+ type: string;
3
+ name: string;
4
+ }
5
+ export interface BlueprintFunctionResourceEvent {
6
+ on: string[];
7
+ filter?: string;
8
+ projection?: string;
9
+ }
10
+ export interface BlueprintFunctionResource extends BlueprintResource {
11
+ type: 'sanity.function.document';
12
+ src: string;
13
+ event: BlueprintFunctionResourceEvent;
14
+ timeout?: number;
15
+ memory?: number;
16
+ env?: Record<string, string>;
17
+ }
18
+ export interface BlueprintOutput {
19
+ name: string;
20
+ value: string;
21
+ }
22
+ export interface Blueprint {
23
+ $schema: string;
24
+ blueprintVersion: string;
25
+ resources?: BlueprintResource[];
26
+ values?: Record<string, unknown>;
27
+ outputs?: BlueprintOutput[];
28
+ }
29
+ export interface BlueprintsApiConfig {
30
+ organizationId: string;
31
+ projectId: string;
32
+ stackId: string;
33
+ }
34
+ export type BlueprintModule = ((args?: unknown) => Blueprint) & {
35
+ organizationId?: string;
36
+ projectId?: string;
37
+ stackId?: string;
38
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@sanity/blueprints",
3
+ "version": "0.0.1",
4
+ "description": "Helper methods and type definitions for Sanity Blueprints",
5
+ "keywords": [
6
+ "sanity",
7
+ "blueprints",
8
+ "types"
9
+ ],
10
+ "homepage": "https://github.com/sanity-io/blueprints-node#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/sanity-io/blueprints-node/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+ssh://git@github.com/sanity-io/blueprints-node.git"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Sanity.io <hello@sanity.io>",
20
+ "sideEffects": false,
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "source": "./src/index.ts",
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "main": "dist/index.js",
32
+ "files": [
33
+ "dist",
34
+ "src"
35
+ ],
36
+ "scripts": {
37
+ "build": "rimraf dist && tsc",
38
+ "coverage": "vitest --coverage",
39
+ "lint": "biome check .",
40
+ "lint:write": "biome check --write .",
41
+ "prepublishOnly": "npm run build",
42
+ "pretest": "npm run typecheck",
43
+ "test": "vitest run --typecheck --reporter=verbose",
44
+ "posttest": "npm run lint",
45
+ "test:watch": "vitest --typecheck --reporter=verbose",
46
+ "typecheck": "tsc --noEmit --project tsconfig.test.json"
47
+ },
48
+ "devDependencies": {
49
+ "@biomejs/biome": "1.9.4",
50
+ "@types/node": "^20",
51
+ "@vitest/coverage-v8": "^3.2.1",
52
+ "rimraf": "^6.0.1",
53
+ "typescript": "^5.8.3",
54
+ "vitest": "3.2.1"
55
+ },
56
+ "engines": {
57
+ "node": ">=20"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public"
61
+ }
62
+ }
@@ -0,0 +1,87 @@
1
+ import type {
2
+ Blueprint,
3
+ BlueprintFunctionResource,
4
+ BlueprintFunctionResourceEvent,
5
+ BlueprintModule,
6
+ BlueprintResource,
7
+ BlueprintsApiConfig,
8
+ } from './types.js'
9
+
10
+ export function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<BlueprintsApiConfig>): BlueprintModule {
11
+ const {organizationId, projectId, stackId, blueprintVersion, resources, values, outputs} = blueprintConfig
12
+
13
+ if (resources && !Array.isArray(resources)) throw new Error('`resources` must be an array')
14
+
15
+ function blueprint(): Blueprint {
16
+ return {
17
+ $schema: 'https://schemas.sanity.io/blueprints/v2024-10-01/blueprint.schema.json',
18
+ blueprintVersion: blueprintVersion ?? '2024-10-01',
19
+ resources,
20
+ values,
21
+ outputs,
22
+ }
23
+ }
24
+
25
+ blueprint.organizationId = organizationId
26
+ blueprint.projectId = projectId
27
+ blueprint.stackId = stackId
28
+
29
+ return blueprint
30
+ }
31
+
32
+ type EventKey = keyof BlueprintFunctionResourceEvent
33
+ const EVENT_KEYS = new Set<EventKey>(['on', 'filter', 'projection'])
34
+
35
+ export function defineDocumentFunction(
36
+ functionConfig: Partial<BlueprintFunctionResource> & Partial<BlueprintFunctionResourceEvent>,
37
+ ): BlueprintFunctionResource {
38
+ let {name, src, event, timeout, memory, env, type, ...maybeEvent} = functionConfig
39
+
40
+ if (!name) throw new Error('`name` is required')
41
+
42
+ // defaults
43
+ if (!src) src = `functions/${name}`
44
+ if (!type) type = 'sanity.function.document'
45
+
46
+ // type validation
47
+ if (memory && typeof memory !== 'number') throw new Error('`memory` must be a number')
48
+ if (timeout && typeof timeout !== 'number') throw new Error('`timeout` must be a number')
49
+
50
+ // event validation
51
+ if (event) {
52
+ // check for `event` and `maybeEvent` collision
53
+ const invalidKeys = Object.keys(maybeEvent).filter((k) => EVENT_KEYS.has(k as EventKey))
54
+ if (invalidKeys.length > 0) {
55
+ throw new Error(`\`event\` cannot be specified with ${invalidKeys.map((k) => `\`${k}\``).join(', ')}`)
56
+ }
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')
60
+ } else {
61
+ const {on, filter, projection} = maybeEvent
62
+ if (on && !Array.isArray(on)) throw new Error('`on` must be an array')
63
+ event = {on: on ?? ['publish'], filter, projection}
64
+ }
65
+
66
+ return {
67
+ type,
68
+ name,
69
+ src,
70
+ event,
71
+ timeout,
72
+ memory,
73
+ env,
74
+ }
75
+ }
76
+
77
+ export function defineResource(resourceConfig: Partial<BlueprintResource>): BlueprintResource {
78
+ const {name, type} = resourceConfig
79
+ if (!name) throw new Error('`name` is required')
80
+ if (!type) throw new Error('`type` is required')
81
+
82
+ return {
83
+ ...resourceConfig,
84
+ type,
85
+ name,
86
+ }
87
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './types.js'
2
+ export * from './definers.js'
package/src/types.ts ADDED
@@ -0,0 +1,44 @@
1
+ export interface BlueprintResource {
2
+ type: string
3
+ name: string
4
+ }
5
+
6
+ export interface BlueprintFunctionResourceEvent {
7
+ on: string[]
8
+ filter?: string
9
+ projection?: string
10
+ }
11
+
12
+ export interface BlueprintFunctionResource extends BlueprintResource {
13
+ type: 'sanity.function.document'
14
+ src: string
15
+ event: BlueprintFunctionResourceEvent
16
+ timeout?: number
17
+ memory?: number
18
+ env?: Record<string, string>
19
+ }
20
+
21
+ export interface BlueprintOutput {
22
+ name: string
23
+ value: string
24
+ }
25
+
26
+ export interface Blueprint {
27
+ $schema: string
28
+ blueprintVersion: string
29
+ resources?: BlueprintResource[]
30
+ values?: Record<string, unknown>
31
+ outputs?: BlueprintOutput[]
32
+ }
33
+
34
+ export interface BlueprintsApiConfig {
35
+ organizationId: string
36
+ projectId: string
37
+ stackId: string
38
+ }
39
+
40
+ export type BlueprintModule = ((args?: unknown) => Blueprint) & {
41
+ organizationId?: string
42
+ projectId?: string
43
+ stackId?: string
44
+ }