@sanity/blueprints 0.6.0 → 0.7.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.
Files changed (100) hide show
  1. package/README.md +1 -0
  2. package/dist/definers/blueprints.d.ts +1 -22
  3. package/dist/definers/blueprints.js +3 -2
  4. package/dist/definers/blueprints.js.map +1 -1
  5. package/dist/definers/cors.d.ts +1 -15
  6. package/dist/definers/cors.js +5 -12
  7. package/dist/definers/cors.js.map +1 -1
  8. package/dist/definers/datasets.d.ts +1 -15
  9. package/dist/definers/datasets.js +5 -19
  10. package/dist/definers/datasets.js.map +1 -1
  11. package/dist/definers/functions.d.ts +4 -41
  12. package/dist/definers/functions.js +25 -32
  13. package/dist/definers/functions.js.map +1 -1
  14. package/dist/definers/resources.d.ts +1 -1
  15. package/dist/definers/resources.js +4 -8
  16. package/dist/definers/resources.js.map +1 -1
  17. package/dist/definers/roles.d.ts +4 -22
  18. package/dist/definers/roles.js +11 -21
  19. package/dist/definers/roles.js.map +1 -1
  20. package/dist/definers/webhooks.d.ts +1 -21
  21. package/dist/definers/webhooks.js +8 -64
  22. package/dist/definers/webhooks.js.map +1 -1
  23. package/dist/index.d.ts +15 -1
  24. package/dist/index.js +18 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/types/blueprints.d.ts +22 -0
  27. package/dist/types/blueprints.js +2 -0
  28. package/dist/types/blueprints.js.map +1 -0
  29. package/dist/types/cors.d.ts +15 -0
  30. package/dist/types/cors.js +2 -0
  31. package/dist/types/cors.js.map +1 -0
  32. package/dist/types/datasets.d.ts +15 -0
  33. package/dist/types/datasets.js +2 -0
  34. package/dist/types/datasets.js.map +1 -0
  35. package/dist/types/errors.d.ts +5 -0
  36. package/dist/types/errors.js +2 -0
  37. package/dist/types/errors.js.map +1 -0
  38. package/dist/types/functions.d.ts +41 -0
  39. package/dist/types/functions.js +2 -0
  40. package/dist/types/functions.js.map +1 -0
  41. package/dist/types/resources.js +2 -0
  42. package/dist/types/resources.js.map +1 -0
  43. package/dist/types/roles.d.ts +21 -0
  44. package/dist/types/roles.js +2 -0
  45. package/dist/types/roles.js.map +1 -0
  46. package/dist/types/webhooks.d.ts +21 -0
  47. package/dist/types/webhooks.js +2 -0
  48. package/dist/types/webhooks.js.map +1 -0
  49. package/dist/utils/validation.d.ts +2 -0
  50. package/dist/utils/validation.js +8 -0
  51. package/dist/utils/validation.js.map +1 -0
  52. package/dist/validation/blueprints.d.ts +2 -0
  53. package/dist/validation/blueprints.js +14 -0
  54. package/dist/validation/blueprints.js.map +1 -0
  55. package/dist/validation/cors.d.ts +2 -0
  56. package/dist/validation/cors.js +32 -0
  57. package/dist/validation/cors.js.map +1 -0
  58. package/dist/validation/datasets.d.ts +2 -0
  59. package/dist/validation/datasets.js +39 -0
  60. package/dist/validation/datasets.js.map +1 -0
  61. package/dist/validation/functions.d.ts +4 -0
  62. package/dist/validation/functions.js +125 -0
  63. package/dist/validation/functions.js.map +1 -0
  64. package/dist/validation/resources.d.ts +3 -0
  65. package/dist/validation/resources.js +25 -0
  66. package/dist/validation/resources.js.map +1 -0
  67. package/dist/validation/roles.d.ts +8 -0
  68. package/dist/validation/roles.js +57 -0
  69. package/dist/validation/roles.js.map +1 -0
  70. package/dist/validation/webhooks.d.ts +2 -0
  71. package/dist/validation/webhooks.js +110 -0
  72. package/dist/validation/webhooks.js.map +1 -0
  73. package/package.json +1 -1
  74. package/src/definers/blueprints.ts +3 -27
  75. package/src/definers/cors.ts +7 -34
  76. package/src/definers/datasets.ts +7 -42
  77. package/src/definers/functions.ts +62 -88
  78. package/src/definers/resources.ts +6 -8
  79. package/src/definers/roles.ts +20 -50
  80. package/src/definers/webhooks.ts +10 -95
  81. package/src/index.ts +20 -1
  82. package/src/types/blueprints.ts +26 -0
  83. package/src/types/cors.ts +18 -0
  84. package/src/types/datasets.ts +19 -0
  85. package/src/types/errors.ts +5 -0
  86. package/src/types/functions.ts +48 -0
  87. package/src/types/roles.ts +25 -0
  88. package/src/types/webhooks.ts +22 -0
  89. package/src/utils/validation.ts +9 -0
  90. package/src/validation/blueprints.ts +16 -0
  91. package/src/validation/cors.ts +34 -0
  92. package/src/validation/datasets.ts +41 -0
  93. package/src/validation/functions.ts +144 -0
  94. package/src/validation/resources.ts +27 -0
  95. package/src/validation/roles.ts +60 -0
  96. package/src/validation/webhooks.ts +109 -0
  97. package/dist/types.js +0 -2
  98. package/dist/types.js.map +0 -1
  99. /package/dist/{types.d.ts → types/resources.d.ts} +0 -0
  100. /package/src/{types.ts → types/resources.ts} +0 -0
@@ -0,0 +1,27 @@
1
+ import type {BlueprintError, BlueprintResource} from '../index.js'
2
+ import {runValidation} from '../utils/validation.js'
3
+
4
+ export function validateResource(resourceConfig: unknown): BlueprintError[] {
5
+ if (!resourceConfig) return [{type: 'invalid_value', message: 'Resource config must be provided'}]
6
+ if (typeof resourceConfig !== 'object') return [{type: 'invalid_type', message: 'Resource config must be an object'}]
7
+
8
+ const errors: BlueprintError[] = []
9
+
10
+ if (!('name' in resourceConfig)) {
11
+ errors.push({type: 'missing_parameter', message: '`name` is required'})
12
+ } else if (typeof resourceConfig.name !== 'string') {
13
+ errors.push({type: 'invalid_type', message: '`name` must be a string'})
14
+ }
15
+
16
+ if (!('type' in resourceConfig)) {
17
+ errors.push({type: 'missing_parameter', message: '`type` is required'})
18
+ } else if (typeof resourceConfig.type !== 'string') {
19
+ errors.push({type: 'invalid_type', message: '`type` must be a string'})
20
+ }
21
+
22
+ return errors
23
+ }
24
+
25
+ export function assertResource(resource: unknown): asserts resource is BlueprintResource {
26
+ runValidation(() => validateResource(resource))
27
+ }
@@ -0,0 +1,60 @@
1
+ import type {BlueprintError} from '../index.js'
2
+
3
+ /**
4
+ * Validates a role.
5
+ * @param parameters The configuration of the role
6
+ * @returns The errors
7
+ */
8
+ export function validateRole(parameters: unknown): BlueprintError[] {
9
+ if (!parameters) return [{type: 'invalid_value', message: 'Role config must be provided'}]
10
+ if (typeof parameters !== 'object') return [{type: 'invalid_type', message: 'Role config must be an object'}]
11
+
12
+ const errors: BlueprintError[] = []
13
+
14
+ if (!('name' in parameters)) {
15
+ errors.push({type: 'missing_parameter', message: 'Role name is required'})
16
+ } else if (typeof parameters.name !== 'string') {
17
+ errors.push({type: 'invalid_type', message: 'Role name must be a string'})
18
+ }
19
+
20
+ if (!('type' in parameters)) {
21
+ errors.push({type: 'missing_parameter', message: 'Role type is required'})
22
+ } else if (parameters.type !== 'sanity.access.role') {
23
+ errors.push({type: 'invalid_value', message: 'Role type must be `sanity.access.role`'})
24
+ }
25
+
26
+ if (!('title' in parameters)) {
27
+ errors.push({type: 'missing_parameter', message: 'Role title is required'})
28
+ } else if (typeof parameters.title !== 'string') {
29
+ errors.push({type: 'invalid_type', message: 'Role title must be a string'})
30
+ } else if (parameters.title.length > 100) {
31
+ errors.push({type: 'invalid_value', message: 'Role title must be 100 characters or less'})
32
+ }
33
+
34
+ if (!('permissions' in parameters) || !Array.isArray(parameters.permissions) || parameters.permissions.length < 1) {
35
+ errors.push({type: 'invalid_value', message: 'Role must have at least one permission'})
36
+ }
37
+
38
+ return errors
39
+ }
40
+
41
+ export function validateProjectRole(parameters: unknown): BlueprintError[] {
42
+ const errors = validateRole(parameters)
43
+
44
+ // only do validation if parameters is provided, otherwise just return the errors
45
+ if (parameters && typeof parameters === 'object') {
46
+ if (!('resourceType' in parameters)) {
47
+ errors.push({type: 'missing_parameter', message: 'Role resource type must be `project`'})
48
+ } else if (parameters.resourceType !== 'project') {
49
+ errors.push({type: 'invalid_value', message: 'Role resource type must be `project`'})
50
+ }
51
+
52
+ if (!('resourceId' in parameters)) {
53
+ errors.push({type: 'missing_parameter', message: 'Role resource ID is required'})
54
+ } else if (typeof parameters.resourceId !== 'string') {
55
+ errors.push({type: 'invalid_type', message: 'Role resource ID must be a string'})
56
+ }
57
+ }
58
+
59
+ return errors
60
+ }
@@ -0,0 +1,109 @@
1
+ import type {BlueprintError} from '../index.js'
2
+
3
+ export function validateDocumentWebhook(parameters: unknown): BlueprintError[] {
4
+ if (!parameters) return [{type: 'invalid_value', message: 'Webhook config must be provided'}]
5
+ if (typeof parameters !== 'object') return [{type: 'invalid_type', message: 'Webhook config must be an object'}]
6
+
7
+ const errors: BlueprintError[] = []
8
+
9
+ if (!('name' in parameters)) {
10
+ errors.push({type: 'missing_parameter', message: 'Webhook name is required'})
11
+ } else if (typeof parameters.name !== 'string') {
12
+ errors.push({type: 'invalid_type', message: 'Webhook name must be a string'})
13
+ }
14
+
15
+ if (!('type' in parameters)) {
16
+ errors.push({type: 'missing_parameter', message: 'Webhook type is required'})
17
+ } else if (parameters.type !== 'sanity.project.webhook') {
18
+ errors.push({type: 'invalid_value', message: 'Webhook type must be `sanity.project.webhook`'})
19
+ }
20
+
21
+ if ('displayName' in parameters) {
22
+ if (typeof parameters.displayName !== 'string') {
23
+ errors.push({type: 'invalid_type', message: 'Display name must be a string'})
24
+ } else if (parameters.displayName.length > 100) {
25
+ errors.push({type: 'invalid_value', message: 'Display name must be 100 characters or less'})
26
+ }
27
+ }
28
+
29
+ if (!('url' in parameters)) {
30
+ errors.push({type: 'missing_parameter', message: 'Webhook URL is required'})
31
+ } else if (typeof parameters.url !== 'string') {
32
+ errors.push({type: 'invalid_type', message: 'Webhook URL must be a string'})
33
+ } else {
34
+ // Validate URL format
35
+ try {
36
+ new URL(parameters.url)
37
+ } catch {
38
+ errors.push({type: 'invalid_value', message: 'Webhook URL must be a valid URL'})
39
+ }
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
+ } else {
45
+ // Validate event types
46
+ const validEvents = ['create', 'update', 'delete']
47
+ if (parameters.on) {
48
+ const invalidEvents = parameters.on.filter((event: string) => !validEvents.includes(event))
49
+ if (invalidEvents.length > 0) {
50
+ errors.push({
51
+ type: 'invalid_value',
52
+ message: `Invalid event types: ${invalidEvents.join(', ')}. Valid events are: ${validEvents.join(', ')}`,
53
+ })
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
+ } else if (typeof parameters.dataset !== 'string' || !/^[a-z0-9-_]+$/.test(parameters.dataset)) {
62
+ errors.push({type: 'invalid_format', message: 'Dataset must match pattern: ^[a-z0-9-_]+$'})
63
+ }
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
+ } else {
72
+ if (!validMethods.includes(parameters.httpMethod)) {
73
+ errors.push({
74
+ type: 'invalid_value',
75
+ message,
76
+ })
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
+ } else if (!['enabled', 'disabled'].includes(parameters.status)) {
87
+ errors.push({type: 'invalid_value', message})
88
+ }
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
+ } else {
96
+ const headerNamePattern = /^[a-zA-Z][a-zA-Z0-9-_]*$/
97
+ for (const [key, value] of Object.entries(parameters.headers)) {
98
+ if (!headerNamePattern.test(key)) {
99
+ errors.push({type: 'invalid_format', message: `Header key "${key}" must match pattern: ${headerNamePattern.source}`})
100
+ }
101
+ if (typeof value !== 'string') {
102
+ errors.push({type: 'invalid_type', message: `Header value for "${key}" must be a string`})
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ return errors
109
+ }
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
File without changes
File without changes