@sanity/blueprints 0.6.0 → 0.7.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 (99) hide show
  1. package/dist/definers/blueprints.d.ts +1 -22
  2. package/dist/definers/blueprints.js +3 -2
  3. package/dist/definers/blueprints.js.map +1 -1
  4. package/dist/definers/cors.d.ts +1 -15
  5. package/dist/definers/cors.js +5 -12
  6. package/dist/definers/cors.js.map +1 -1
  7. package/dist/definers/datasets.d.ts +1 -15
  8. package/dist/definers/datasets.js +5 -19
  9. package/dist/definers/datasets.js.map +1 -1
  10. package/dist/definers/functions.d.ts +4 -41
  11. package/dist/definers/functions.js +25 -32
  12. package/dist/definers/functions.js.map +1 -1
  13. package/dist/definers/resources.d.ts +1 -1
  14. package/dist/definers/resources.js +4 -8
  15. package/dist/definers/resources.js.map +1 -1
  16. package/dist/definers/roles.d.ts +4 -22
  17. package/dist/definers/roles.js +11 -21
  18. package/dist/definers/roles.js.map +1 -1
  19. package/dist/definers/webhooks.d.ts +1 -21
  20. package/dist/definers/webhooks.js +8 -64
  21. package/dist/definers/webhooks.js.map +1 -1
  22. package/dist/index.d.ts +15 -1
  23. package/dist/index.js +18 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/types/blueprints.d.ts +22 -0
  26. package/dist/types/blueprints.js +2 -0
  27. package/dist/types/blueprints.js.map +1 -0
  28. package/dist/types/cors.d.ts +15 -0
  29. package/dist/types/cors.js +2 -0
  30. package/dist/types/cors.js.map +1 -0
  31. package/dist/types/datasets.d.ts +15 -0
  32. package/dist/types/datasets.js +2 -0
  33. package/dist/types/datasets.js.map +1 -0
  34. package/dist/types/errors.d.ts +5 -0
  35. package/dist/types/errors.js +2 -0
  36. package/dist/types/errors.js.map +1 -0
  37. package/dist/types/functions.d.ts +41 -0
  38. package/dist/types/functions.js +2 -0
  39. package/dist/types/functions.js.map +1 -0
  40. package/dist/types/resources.js +2 -0
  41. package/dist/types/resources.js.map +1 -0
  42. package/dist/types/roles.d.ts +21 -0
  43. package/dist/types/roles.js +2 -0
  44. package/dist/types/roles.js.map +1 -0
  45. package/dist/types/webhooks.d.ts +21 -0
  46. package/dist/types/webhooks.js +2 -0
  47. package/dist/types/webhooks.js.map +1 -0
  48. package/dist/utils/validation.d.ts +2 -0
  49. package/dist/utils/validation.js +8 -0
  50. package/dist/utils/validation.js.map +1 -0
  51. package/dist/validation/blueprints.d.ts +2 -0
  52. package/dist/validation/blueprints.js +14 -0
  53. package/dist/validation/blueprints.js.map +1 -0
  54. package/dist/validation/cors.d.ts +2 -0
  55. package/dist/validation/cors.js +32 -0
  56. package/dist/validation/cors.js.map +1 -0
  57. package/dist/validation/datasets.d.ts +2 -0
  58. package/dist/validation/datasets.js +39 -0
  59. package/dist/validation/datasets.js.map +1 -0
  60. package/dist/validation/functions.d.ts +4 -0
  61. package/dist/validation/functions.js +125 -0
  62. package/dist/validation/functions.js.map +1 -0
  63. package/dist/validation/resources.d.ts +3 -0
  64. package/dist/validation/resources.js +25 -0
  65. package/dist/validation/resources.js.map +1 -0
  66. package/dist/validation/roles.d.ts +8 -0
  67. package/dist/validation/roles.js +57 -0
  68. package/dist/validation/roles.js.map +1 -0
  69. package/dist/validation/webhooks.d.ts +2 -0
  70. package/dist/validation/webhooks.js +110 -0
  71. package/dist/validation/webhooks.js.map +1 -0
  72. package/package.json +1 -1
  73. package/src/definers/blueprints.ts +3 -27
  74. package/src/definers/cors.ts +7 -34
  75. package/src/definers/datasets.ts +7 -42
  76. package/src/definers/functions.ts +62 -88
  77. package/src/definers/resources.ts +6 -8
  78. package/src/definers/roles.ts +20 -50
  79. package/src/definers/webhooks.ts +10 -95
  80. package/src/index.ts +20 -1
  81. package/src/types/blueprints.ts +26 -0
  82. package/src/types/cors.ts +18 -0
  83. package/src/types/datasets.ts +19 -0
  84. package/src/types/errors.ts +5 -0
  85. package/src/types/functions.ts +48 -0
  86. package/src/types/roles.ts +25 -0
  87. package/src/types/webhooks.ts +22 -0
  88. package/src/utils/validation.ts +9 -0
  89. package/src/validation/blueprints.ts +16 -0
  90. package/src/validation/cors.ts +34 -0
  91. package/src/validation/datasets.ts +41 -0
  92. package/src/validation/functions.ts +144 -0
  93. package/src/validation/resources.ts +27 -0
  94. package/src/validation/roles.ts +60 -0
  95. package/src/validation/webhooks.ts +109 -0
  96. package/dist/types.js +0 -2
  97. package/dist/types.js.map +0 -1
  98. /package/dist/{types.d.ts → types/resources.d.ts} +0 -0
  99. /package/src/{types.ts → types/resources.ts} +0 -0
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Validates a role.
3
+ * @param parameters The configuration of the role
4
+ * @returns The errors
5
+ */
6
+ export function validateRole(parameters) {
7
+ if (!parameters)
8
+ return [{ type: 'invalid_value', message: 'Role config must be provided' }];
9
+ if (typeof parameters !== 'object')
10
+ return [{ type: 'invalid_type', message: 'Role config must be an object' }];
11
+ const errors = [];
12
+ if (!('name' in parameters)) {
13
+ errors.push({ type: 'missing_parameter', message: 'Role name is required' });
14
+ }
15
+ else if (typeof parameters.name !== 'string') {
16
+ errors.push({ type: 'invalid_type', message: 'Role name must be a string' });
17
+ }
18
+ if (!('type' in parameters)) {
19
+ errors.push({ type: 'missing_parameter', message: 'Role type is required' });
20
+ }
21
+ else if (parameters.type !== 'sanity.access.role') {
22
+ errors.push({ type: 'invalid_value', message: 'Role type must be `sanity.access.role`' });
23
+ }
24
+ if (!('title' in parameters)) {
25
+ errors.push({ type: 'missing_parameter', message: 'Role title is required' });
26
+ }
27
+ else if (typeof parameters.title !== 'string') {
28
+ errors.push({ type: 'invalid_type', message: 'Role title must be a string' });
29
+ }
30
+ else if (parameters.title.length > 100) {
31
+ errors.push({ type: 'invalid_value', message: 'Role title must be 100 characters or less' });
32
+ }
33
+ if (!('permissions' in parameters) || !Array.isArray(parameters.permissions) || parameters.permissions.length < 1) {
34
+ errors.push({ type: 'invalid_value', message: 'Role must have at least one permission' });
35
+ }
36
+ return errors;
37
+ }
38
+ export function validateProjectRole(parameters) {
39
+ const errors = validateRole(parameters);
40
+ // only do validation if parameters is provided, otherwise just return the errors
41
+ if (parameters && typeof parameters === 'object') {
42
+ if (!('resourceType' in parameters)) {
43
+ errors.push({ type: 'missing_parameter', message: 'Role resource type must be `project`' });
44
+ }
45
+ else if (parameters.resourceType !== 'project') {
46
+ errors.push({ type: 'invalid_value', message: 'Role resource type must be `project`' });
47
+ }
48
+ if (!('resourceId' in parameters)) {
49
+ errors.push({ type: 'missing_parameter', message: 'Role resource ID is required' });
50
+ }
51
+ else if (typeof parameters.resourceId !== 'string') {
52
+ errors.push({ type: 'invalid_type', message: 'Role resource ID must be a string' });
53
+ }
54
+ }
55
+ return errors;
56
+ }
57
+ //# sourceMappingURL=roles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.js","sourceRoot":"","sources":["../../src/validation/roles.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,UAAmB;IAC9C,IAAI,CAAC,UAAU;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,8BAA8B,EAAC,CAAC,CAAA;IAC1F,IAAI,OAAO,UAAU,KAAK,QAAQ;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,+BAA+B,EAAC,CAAC,CAAA;IAE7G,MAAM,MAAM,GAAqB,EAAE,CAAA;IAEnC,IAAI,CAAC,CAAC,MAAM,IAAI,UAAU,CAAC,EAAE,CAAC;QAC5B,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,uBAAuB,EAAC,CAAC,CAAA;IAC5E,CAAC;SAAM,IAAI,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC/C,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,4BAA4B,EAAC,CAAC,CAAA;IAC5E,CAAC;IAED,IAAI,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AAY,CAAC,UAAU,CAAC,CAAA;IAEvC,iFAAiF;IACjF,IAAI,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACjD,IAAI,CAAC,CAAC,cAAc,IAAI,UAAU,CAAC,EAAE,CAAC;YACpC,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,sCAAsC,EAAC,CAAC,CAAA;QAC3F,CAAC;aAAM,IAAI,UAAU,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;YACjD,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,sCAAsC,EAAC,CAAC,CAAA;QACvF,CAAC;QAED,IAAI,CAAC,CAAC,YAAY,IAAI,UAAU,CAAC,EAAE,CAAC;YAClC,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,8BAA8B,EAAC,CAAC,CAAA;QACnF,CAAC;aAAM,IAAI,OAAO,UAAU,CAAC,UAAU,KAAK,QAAQ,EAAE,CAAC;YACrD,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,mCAAmC,EAAC,CAAC,CAAA;QACnF,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { BlueprintError } from '../index.js';
2
+ export declare function validateDocumentWebhook(parameters: unknown): BlueprintError[];
@@ -0,0 +1,110 @@
1
+ export function validateDocumentWebhook(parameters) {
2
+ if (!parameters)
3
+ return [{ type: 'invalid_value', message: 'Webhook config must be provided' }];
4
+ if (typeof parameters !== 'object')
5
+ return [{ type: 'invalid_type', message: 'Webhook config must be an object' }];
6
+ const errors = [];
7
+ if (!('name' in parameters)) {
8
+ errors.push({ type: 'missing_parameter', message: 'Webhook name is required' });
9
+ }
10
+ else if (typeof parameters.name !== 'string') {
11
+ errors.push({ type: 'invalid_type', message: 'Webhook name must be a string' });
12
+ }
13
+ if (!('type' in parameters)) {
14
+ errors.push({ type: 'missing_parameter', message: 'Webhook type is required' });
15
+ }
16
+ else if (parameters.type !== 'sanity.project.webhook') {
17
+ errors.push({ type: 'invalid_value', message: 'Webhook type must be `sanity.project.webhook`' });
18
+ }
19
+ if ('displayName' in parameters) {
20
+ if (typeof parameters.displayName !== 'string') {
21
+ errors.push({ type: 'invalid_type', message: 'Display name must be a string' });
22
+ }
23
+ else if (parameters.displayName.length > 100) {
24
+ errors.push({ type: 'invalid_value', message: 'Display name must be 100 characters or less' });
25
+ }
26
+ }
27
+ if (!('url' in parameters)) {
28
+ errors.push({ type: 'missing_parameter', message: 'Webhook URL is required' });
29
+ }
30
+ else if (typeof parameters.url !== 'string') {
31
+ errors.push({ type: 'invalid_type', message: 'Webhook URL must be a string' });
32
+ }
33
+ else {
34
+ // Validate URL format
35
+ try {
36
+ new URL(parameters.url);
37
+ }
38
+ catch {
39
+ errors.push({ type: 'invalid_value', message: 'Webhook URL must be a valid URL' });
40
+ }
41
+ }
42
+ if (!('on' in parameters) || !Array.isArray(parameters.on) || parameters.on.length === 0) {
43
+ errors.push({ type: 'invalid_value', message: 'At least one event type must be specified in the "on" field' });
44
+ }
45
+ else {
46
+ // Validate event types
47
+ const validEvents = ['create', 'update', 'delete'];
48
+ if (parameters.on) {
49
+ const invalidEvents = parameters.on.filter((event) => !validEvents.includes(event));
50
+ if (invalidEvents.length > 0) {
51
+ errors.push({
52
+ type: 'invalid_value',
53
+ message: `Invalid event types: ${invalidEvents.join(', ')}. Valid events are: ${validEvents.join(', ')}`,
54
+ });
55
+ }
56
+ }
57
+ }
58
+ // Validate dataset pattern
59
+ if (!('dataset' in parameters)) {
60
+ errors.push({ type: 'missing_parameter', message: 'Webhook dataset is required' });
61
+ }
62
+ else if (typeof parameters.dataset !== 'string' || !/^[a-z0-9-_]+$/.test(parameters.dataset)) {
63
+ errors.push({ type: 'invalid_format', message: 'Dataset must match pattern: ^[a-z0-9-_]+$' });
64
+ }
65
+ // Validate HTTP method
66
+ if ('httpMethod' in parameters) {
67
+ const validMethods = ['POST', 'PUT', 'PATCH', 'DELETE', 'GET'];
68
+ const message = `Invalid HTTP method: ${parameters.httpMethod}. Valid methods are: ${validMethods.join(', ')}`;
69
+ if (typeof parameters.httpMethod !== 'string') {
70
+ errors.push({ type: 'invalid_type', message });
71
+ }
72
+ else {
73
+ if (!validMethods.includes(parameters.httpMethod)) {
74
+ errors.push({
75
+ type: 'invalid_value',
76
+ message,
77
+ });
78
+ }
79
+ }
80
+ }
81
+ // Validate status
82
+ if ('status' in parameters) {
83
+ const message = 'Status must be either "enabled" or "disabled"';
84
+ if (typeof parameters.status !== 'string') {
85
+ errors.push({ type: 'invalid_type', message });
86
+ }
87
+ else if (!['enabled', 'disabled'].includes(parameters.status)) {
88
+ errors.push({ type: 'invalid_value', message });
89
+ }
90
+ }
91
+ // Validate headers pattern
92
+ if ('headers' in parameters) {
93
+ if (typeof parameters.headers !== 'object' || !parameters.headers || Array.isArray(parameters.headers)) {
94
+ errors.push({ type: 'invalid_type', message: 'Webhook headers must be an object' });
95
+ }
96
+ else {
97
+ const headerNamePattern = /^[a-zA-Z][a-zA-Z0-9-_]*$/;
98
+ for (const [key, value] of Object.entries(parameters.headers)) {
99
+ if (!headerNamePattern.test(key)) {
100
+ errors.push({ type: 'invalid_format', message: `Header key "${key}" must match pattern: ${headerNamePattern.source}` });
101
+ }
102
+ if (typeof value !== 'string') {
103
+ errors.push({ type: 'invalid_type', message: `Header value for "${key}" must be a string` });
104
+ }
105
+ }
106
+ }
107
+ }
108
+ return errors;
109
+ }
110
+ //# sourceMappingURL=webhooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/validation/webhooks.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,uBAAuB,CAAC,UAAmB;IACzD,IAAI,CAAC,UAAU;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,eAA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iBAAiB,CAAC,MAAM,EAAE,EAAC,CAAC,CAAA;gBACvH,CAAC;gBACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;oBAC9B,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,qBAAqB,GAAG,oBAAoB,EAAC,CAAC,CAAA;gBAC5F,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/blueprints",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Helper methods and type definitions for Sanity Blueprints",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,34 +1,10 @@
1
- import type {BlueprintResource} from '../types.js'
2
-
3
- export interface BlueprintsApiConfig {
4
- organizationId: string
5
- projectId: string
6
- stackId: string
7
- }
8
-
9
- export interface BlueprintOutput {
10
- name: string
11
- value: string
12
- }
13
-
14
- export interface Blueprint {
15
- $schema: string
16
- blueprintVersion: string
17
- resources?: BlueprintResource[]
18
- values?: Record<string, unknown>
19
- outputs?: BlueprintOutput[]
20
- }
21
-
22
- export type BlueprintModule = ((args?: unknown) => Blueprint) & {
23
- organizationId?: string
24
- projectId?: string
25
- stackId?: string
26
- }
1
+ import {type Blueprint, type BlueprintModule, type BlueprintsApiConfig, validateBlueprint} from '../index.js'
2
+ import {runValidation} from '../utils/validation.js'
27
3
 
28
4
  export function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<BlueprintsApiConfig>): BlueprintModule {
29
5
  const {organizationId, projectId, stackId, blueprintVersion, resources, values, outputs} = blueprintConfig
30
6
 
31
- if (resources && !Array.isArray(resources)) throw new Error('`resources` must be an array')
7
+ runValidation(() => validateBlueprint(blueprintConfig))
32
8
 
33
9
  function blueprint(): Blueprint {
34
10
  return {
@@ -1,41 +1,14 @@
1
- import type {BlueprintResource} from '../types'
2
-
3
- /**
4
- * Represents a CORS Origin resource.
5
- */
6
- export interface BlueprintCorsOriginResource extends BlueprintResource {
7
- type: 'sanity.project.cors'
8
- origin: string
9
- allowCredentials: boolean
10
-
11
- /** The `project` attribute must be defined if your blueprint is scoped to an organization. */
12
- project?: string
13
- }
14
-
15
- /** Configuration for a CORS Origin resource. */
16
- export type BlueprintCorsOriginConfig = Omit<BlueprintCorsOriginResource, 'type' | 'allowCredentials'> & {
17
- allowCredentials?: boolean
18
- }
1
+ import {type BlueprintCorsOriginConfig, type BlueprintCorsOriginResource, validateCorsOrigin} from '../index.js'
2
+ import {runValidation} from '../utils/validation.js'
19
3
 
20
4
  export function defineCorsOrigin(parameters: BlueprintCorsOriginConfig): BlueprintCorsOriginResource {
21
- const errors: string[] = []
22
-
23
- if (!parameters.name || parameters.name.trim() === '') {
24
- errors.push('CORS Origin name is required')
25
- }
26
-
27
- if (!parameters.origin || parameters.origin.trim() === '') {
28
- errors.push('CORS Origin URL is required')
29
- }
30
-
31
- if (errors.length > 0) {
32
- const message = errors.join('\n')
33
- throw new Error(message)
34
- }
35
-
36
- return {
5
+ const corsResource: BlueprintCorsOriginResource = {
37
6
  ...parameters,
38
7
  allowCredentials: parameters.allowCredentials || false,
39
8
  type: 'sanity.project.cors',
40
9
  }
10
+
11
+ runValidation(() => validateCorsOrigin(corsResource))
12
+
13
+ return corsResource
41
14
  }
@@ -1,52 +1,17 @@
1
- import type {BlueprintResource} from '../types'
2
-
3
- export type AclMode = 'public' | 'private' | 'custom'
4
-
5
- /** Represents a Dataset resource. */
6
- export interface BlueprintDatasetResource extends BlueprintResource {
7
- type: 'sanity.project.dataset'
8
- datasetName: string
9
- aclMode?: AclMode
10
-
11
- /** The `project` attribute must be defined if your blueprint is scoped to an organization. */
12
- project?: string
13
- }
14
-
15
- /** Configuration for a Dataset resource. */
16
- export type BlueprintDatasetConfig = Omit<BlueprintDatasetResource, 'type' | 'datasetName'> & {
17
- /** The name of the dataset, defaults to the name of the resource. */
18
- datasetName?: string
19
- }
1
+ import {type BlueprintDatasetConfig, type BlueprintDatasetResource, validateDataset} from '../index.js'
2
+ import {runValidation} from '../utils/validation.js'
20
3
 
21
4
  export function defineDataset(parameters: BlueprintDatasetConfig): BlueprintDatasetResource {
22
- const errors: string[] = []
23
-
24
5
  // default dataset name
25
6
  const datasetName = parameters.datasetName || parameters.name
26
7
 
27
- // validate ACL mode if provided
28
- if (typeof parameters.aclMode !== 'undefined') {
29
- const aclMode: string = parameters.aclMode
30
- if (aclMode !== 'custom' && aclMode !== 'public' && aclMode !== 'private') {
31
- errors.push('aclMode must be one of `custom`, `public`, or `private`')
32
- }
33
- }
34
-
35
- // validate project if provided
36
- if (parameters.project) {
37
- if (typeof parameters.project !== 'string') {
38
- errors.push('project must be a string')
39
- }
40
- }
41
-
42
- if (errors.length > 0) {
43
- const message = errors.join('\n')
44
- throw new Error(message)
45
- }
46
-
47
- return {
8
+ const datasetResource: BlueprintDatasetResource = {
48
9
  ...parameters,
49
10
  datasetName,
50
11
  type: 'sanity.project.dataset',
51
12
  }
13
+
14
+ runValidation(() => validateDataset(datasetResource))
15
+
16
+ return datasetResource
52
17
  }
@@ -1,33 +1,15 @@
1
- import type {BlueprintResource} from '../types'
2
-
3
- // --- Function Event Types ---
4
-
5
- export interface BlueprintFunctionBaseResourceEvent {
6
- on?: [BlueprintFunctionResourceEventName, ...BlueprintFunctionResourceEventName[]]
7
- filter?: string
8
- projection?: `{${string}}`
9
- includeDrafts?: boolean
10
- }
11
- export interface BlueprintDocumentFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
12
- includeAllVersions?: boolean
13
- resource?: BlueprintFunctionResourceEventResourceDataset
14
- }
15
- export interface BlueprintMediaLibraryFunctionResourceEvent extends BlueprintFunctionBaseResourceEvent {
16
- resource: BlueprintFunctionResourceEventResourceMediaLibrary
17
- }
18
- export type BlueprintFunctionResourceEvent = BlueprintDocumentFunctionResourceEvent | BlueprintMediaLibraryFunctionResourceEvent
19
-
20
- interface BlueprintFunctionResourceEventResourceDataset {
21
- type: 'dataset'
22
- /** @description A dataset ID in the format <projectId>.<datasetName>. <datasetName> can be `*` to signify "all datasets in project with ID <projectId>." */
23
- id: string
24
- }
25
- interface BlueprintFunctionResourceEventResourceMediaLibrary {
26
- type: 'media-library'
27
- id: string
28
- }
29
-
30
- type BlueprintFunctionResourceEventName = 'publish' | 'create' | 'delete' | 'update'
1
+ import {
2
+ type BlueprintBaseFunctionResource,
3
+ type BlueprintDocumentFunctionResource,
4
+ type BlueprintDocumentFunctionResourceEvent,
5
+ type BlueprintFunctionBaseResourceEvent,
6
+ type BlueprintMediaLibraryAssetFunctionResource,
7
+ type BlueprintMediaLibraryFunctionResourceEvent,
8
+ validateDocumentFunction,
9
+ validateFunction,
10
+ validateMediaLibraryAssetFunction,
11
+ } from '../index.js'
12
+ import {runValidation} from '../utils/validation.js'
31
13
 
32
14
  type BaseFunctionEventKey = keyof BlueprintFunctionBaseResourceEvent
33
15
  const BASE_EVENT_KEYS = new Set<BaseFunctionEventKey>(['on', 'filter', 'projection', 'includeDrafts'])
@@ -36,26 +18,9 @@ const DOCUMENT_EVENT_KEYS = new Set<DocumentFunctionEventKey>(['includeAllVersio
36
18
  type MediaLibraryFunctionEventKey = keyof BlueprintMediaLibraryFunctionResourceEvent
37
19
  const MEDIA_LIBRARY_EVENT_KEYS = new Set<MediaLibraryFunctionEventKey>(['resource', ...BASE_EVENT_KEYS.values()])
38
20
 
39
- // --- Main Function Types ---
40
-
41
21
  interface RequiredFunctionProperties {
42
22
  name: string
43
23
  }
44
- export interface BlueprintBaseFunctionResource extends BlueprintResource {
45
- displayName?: string
46
- src: string
47
- timeout?: number
48
- memory?: number
49
- env?: Record<string, string>
50
- }
51
- export interface BlueprintDocumentFunctionResource extends BlueprintBaseFunctionResource {
52
- type: 'sanity.function.document'
53
- event: BlueprintDocumentFunctionResourceEvent
54
- }
55
- export interface BlueprintMediaLibraryAssetFunctionResource extends BlueprintBaseFunctionResource {
56
- type: 'sanity.function.media-library.asset'
57
- event: BlueprintMediaLibraryFunctionResourceEvent
58
- }
59
24
 
60
25
  export function defineDocumentFunction(
61
26
  functionConfig: Partial<BlueprintDocumentFunctionResource> & RequiredFunctionProperties,
@@ -72,35 +37,45 @@ export function defineDocumentFunction(
72
37
  let {name, src, event, timeout, memory, env, type, ...maybeEvent} = functionConfig
73
38
  if (!type) type = 'sanity.function.document'
74
39
 
75
- // event validation
40
+ // event validation and normalization
76
41
  if (event) {
77
42
  // `event` was specified, but event keys (aggregated in `maybeEvent`) were also specified at the top level. ambiguous and deprecated usage.
78
- const duplicateKeys = Object.keys(maybeEvent).filter((k) => DOCUMENT_EVENT_KEYS.has(k as DocumentFunctionEventKey))
43
+ const duplicateKeys = Array.from(DOCUMENT_EVENT_KEYS).filter((key) => key in maybeEvent)
79
44
  if (duplicateKeys.length > 0) {
80
45
  throw new Error(
81
46
  `\`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(', ')}`,
82
47
  )
83
48
  }
84
- event = validateDocumentFunctionEvent(event)
49
+
50
+ event = buildDocumentFunctionEvent(event)
85
51
  } else {
86
- event = validateDocumentFunctionEvent(maybeEvent)
52
+ event = buildDocumentFunctionEvent(maybeEvent)
87
53
  // deprecated usage of putting event properties at the top level, warn about this.
88
54
  console.warn(
89
55
  '⚠️ Deprecated usage of `defineDocumentFunction`: prefer to put `event` properties under the `event` key rather than at the top level.',
90
56
  )
91
57
  }
92
58
 
93
- return {
94
- ...defineFunction({
95
- name,
96
- src,
97
- timeout,
98
- memory,
99
- env,
100
- }),
59
+ const functionResource: BlueprintDocumentFunctionResource = {
60
+ ...defineFunction(
61
+ {
62
+ name,
63
+ src,
64
+ timeout,
65
+ memory,
66
+ env,
67
+ },
68
+ {
69
+ skipValidation: true, // already done below
70
+ },
71
+ ),
101
72
  type,
102
73
  event,
103
74
  }
75
+
76
+ runValidation(() => validateDocumentFunction(functionResource))
77
+
78
+ return functionResource
104
79
  }
105
80
 
106
81
  export function defineMediaLibraryAssetFunction(
@@ -112,34 +87,39 @@ export function defineMediaLibraryAssetFunction(
112
87
  let {name, src, event, timeout, memory, env, type} = functionConfig
113
88
  if (!type) type = 'sanity.function.media-library.asset'
114
89
 
115
- return {
116
- ...defineFunction({
117
- name,
118
- src,
119
- timeout,
120
- memory,
121
- env,
122
- }),
90
+ const functionResource: BlueprintMediaLibraryAssetFunctionResource = {
91
+ ...defineFunction(
92
+ {
93
+ name,
94
+ src,
95
+ timeout,
96
+ memory,
97
+ env,
98
+ },
99
+ {
100
+ skipValidation: true, // already done below
101
+ },
102
+ ),
123
103
  type,
124
- event: validateMediaLibraryFunctionEvent(event),
104
+ event: buildMediaLibraryFunctionEvent(event),
125
105
  }
106
+
107
+ runValidation(() => validateMediaLibraryAssetFunction(functionResource))
108
+
109
+ return functionResource
126
110
  }
111
+
127
112
  export function defineFunction(
128
113
  functionConfig: Partial<BlueprintBaseFunctionResource> & RequiredFunctionProperties,
114
+ options?: {skipValidation?: boolean},
129
115
  ): BlueprintBaseFunctionResource {
130
116
  let {name, src, timeout, memory, env, type} = functionConfig
131
117
 
132
- if (!name) throw new Error('`name` is required')
133
-
134
118
  // defaults
135
119
  if (!src) src = `functions/${name}`
136
120
  if (!type) type = 'sanity.function.document'
137
121
 
138
- // type validation
139
- if (memory && typeof memory !== 'number') throw new Error('`memory` must be a number')
140
- if (timeout && typeof timeout !== 'number') throw new Error('`timeout` must be a number')
141
-
142
- return {
122
+ const functionResource: BlueprintBaseFunctionResource = {
143
123
  type,
144
124
  name,
145
125
  src,
@@ -147,27 +127,24 @@ export function defineFunction(
147
127
  memory,
148
128
  env,
149
129
  }
130
+
131
+ if (options?.skipValidation !== true) runValidation(() => validateFunction(functionResource))
132
+
133
+ return functionResource
150
134
  }
151
135
 
152
- function validateDocumentFunctionEvent(event: Partial<BlueprintDocumentFunctionResourceEvent>): BlueprintDocumentFunctionResourceEvent {
136
+ function buildDocumentFunctionEvent(event: Partial<BlueprintDocumentFunctionResourceEvent>): BlueprintDocumentFunctionResourceEvent {
153
137
  const cleanEvent = Object.fromEntries(
154
138
  Object.entries(event).filter(([key]) => DOCUMENT_EVENT_KEYS.has(key as DocumentFunctionEventKey)),
155
139
  ) as Partial<BlueprintDocumentFunctionResourceEvent>
156
140
 
157
- const fullEvent = {
141
+ return {
158
142
  on: cleanEvent.on || ['publish'],
159
143
  ...cleanEvent,
160
144
  }
161
- if (!Array.isArray(fullEvent.on)) throw new Error('`event.on` must be an array')
162
- if (fullEvent.resource) {
163
- if (!fullEvent.resource.type || fullEvent.resource.type !== 'dataset') throw new Error('`event.resource.type` must be "dataset"')
164
- if (!fullEvent.resource.id || fullEvent.resource.id.split('.').length !== 2)
165
- throw new Error('`event.resource.id` must be in the format <projectId>.<datasetName>')
166
- }
167
- return fullEvent
168
145
  }
169
146
 
170
- function validateMediaLibraryFunctionEvent(event: BlueprintMediaLibraryFunctionResourceEvent): BlueprintMediaLibraryFunctionResourceEvent {
147
+ function buildMediaLibraryFunctionEvent(event: BlueprintMediaLibraryFunctionResourceEvent): BlueprintMediaLibraryFunctionResourceEvent {
171
148
  const cleanEvent = Object.fromEntries(
172
149
  Object.entries(event).filter(([key]) => MEDIA_LIBRARY_EVENT_KEYS.has(key as MediaLibraryFunctionEventKey)),
173
150
  ) as BlueprintMediaLibraryFunctionResourceEvent
@@ -176,8 +153,5 @@ function validateMediaLibraryFunctionEvent(event: BlueprintMediaLibraryFunctionR
176
153
  on: cleanEvent.on || ['publish'],
177
154
  ...cleanEvent,
178
155
  }
179
- if (!Array.isArray(fullEvent.on)) throw new Error('`event.on` must be an array')
180
- if (!fullEvent.resource.type || fullEvent.resource.type !== 'media-library')
181
- throw new Error('`event.resource.type` must be "media-library"')
182
156
  return fullEvent
183
157
  }
@@ -1,13 +1,11 @@
1
- import type {BlueprintResource} from '../types.js'
1
+ import {type BlueprintResource, assertResource} from '../index.js'
2
2
 
3
3
  export function defineResource(resourceConfig: Partial<BlueprintResource>): BlueprintResource {
4
- const {name, type} = resourceConfig
5
- if (!name) throw new Error('`name` is required')
6
- if (!type) throw new Error('`type` is required')
7
-
8
- return {
4
+ const resource = {
9
5
  ...resourceConfig,
10
- type,
11
- name,
12
6
  }
7
+
8
+ assertResource(resource)
9
+
10
+ return resource
13
11
  }