@sanity/blueprints 0.29.0 → 0.30.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 CHANGED
@@ -67,15 +67,17 @@ export default defineBlueprint({
67
67
 
68
68
  ## Available resources
69
69
 
70
- | Definer | Description |
71
- | --------------------------------- | ----------------------------------------------------- |
72
- | `defineBlueprint` | Top-level blueprint module containing resources |
73
- | `defineDocumentFunction` | Function triggered by document events |
74
- | `defineMediaLibraryAssetFunction` | Function triggered by media library events |
75
- | `defineCorsOrigin` β | CORS origin allowing browser requests to your project |
76
- | `defineDocumentWebhook` β | Webhook triggered by document changes |
77
- | `defineRole` β | Custom role with permissions |
78
- | `defineRobotToken` β | Robot token for automated access |
70
+ | Definer | Description |
71
+ | --------------------------------- | --------------------------------------------------------------------------- |
72
+ | `defineBlueprint` | Top-level blueprint module containing resources |
73
+ | `defineDocumentFunction` | Function triggered by document events |
74
+ | `defineMediaLibraryAssetFunction` | Function triggered by media library events |
75
+ | `defineContentResource` β | Content resource as in the Manage UI, made from document types and a filter |
76
+ | `defineCorsOrigin` β | CORS origin allowing browser requests to your project |
77
+ | `defineDocumentWebhook` β | Webhook triggered by document changes |
78
+ | `definePermission` β | Custom permission that roles can grant, in the Access API shape |
79
+ | `defineRole` β | Custom role with permissions |
80
+ | `defineRobotToken` β | Robot token for automated access |
79
81
 
80
82
  Each definer validates its input at call time and returns a typed resource object. See the [reference docs](https://reference.sanity.io/_sanity/blueprints) for full configuration details and additional resource types.
81
83
 
@@ -0,0 +1,71 @@
1
+ import { type BlueprintContentResourceConfig, type BlueprintPermissionConfig, type BlueprintPermissionResource } from '../index.js';
2
+ /**
3
+ * Defines a custom permission that roles can grant, in the shape of the Access API.
4
+ *
5
+ * ```ts
6
+ * definePermission({
7
+ * name: 'articles-only',
8
+ * permissionType: 'sanity.document.filter.mode',
9
+ * config: {filter: '_type == "article"'},
10
+ * })
11
+ * ```
12
+ *
13
+ * @example Grant the permission with a role
14
+ * ```ts
15
+ * definePermission({
16
+ * name: 'articles-only',
17
+ * title: 'Articles only',
18
+ * description: 'Access to article documents',
19
+ * permissionType: 'sanity.document.filter',
20
+ * config: {filter: '_type == "article"'},
21
+ * })
22
+ *
23
+ * defineRole({
24
+ * name: 'article-editor',
25
+ * title: 'Article editor',
26
+ * appliesToUsers: true,
27
+ * appliesToRobots: false,
28
+ * permissions: [
29
+ * {name: 'sanity-project', action: 'read'},
30
+ * {name: '$.resources.articles-only', action: 'read'},
31
+ * {name: '$.resources.articles-only', action: 'update'},
32
+ * ],
33
+ * })
34
+ * ```
35
+ * @param parameters The configuration of the permission
36
+ * @public
37
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
38
+ * @category Definers
39
+ * @expandType BlueprintPermissionConfig
40
+ * @returns The permission resource
41
+ */
42
+ export declare function definePermission(parameters: BlueprintPermissionConfig): BlueprintPermissionResource;
43
+ /**
44
+ * Defines a content resource as the Manage UI makes it: a custom permission of type `sanity.document.filter.mode`.
45
+ *
46
+ * ```ts
47
+ * defineContentResource({
48
+ * name: 'movies',
49
+ * documentTypes: ['movie'],
50
+ * })
51
+ * // config.filter: '_type == "movie"'
52
+ * ```
53
+ *
54
+ * @example Document types and a filter
55
+ * ```ts
56
+ * defineContentResource({
57
+ * name: 'london-posts',
58
+ * title: 'London posts',
59
+ * documentTypes: ['post', 'event'],
60
+ * filter: 'branch == "london"',
61
+ * })
62
+ * // config.filter: '_type in ["post", "event"] && (branch == "london")'
63
+ * ```
64
+ * @param parameters The configuration of the content resource
65
+ * @public
66
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
67
+ * @category Definers
68
+ * @expandType BlueprintContentResourceConfig
69
+ * @returns The permission resource
70
+ */
71
+ export declare function defineContentResource(parameters: BlueprintContentResourceConfig): BlueprintPermissionResource;
@@ -0,0 +1,95 @@
1
+ import { validateContentResource, validatePermission, } from '../index.js';
2
+ import { runValidation } from '../utils/validation.js';
3
+ /**
4
+ * Defines a custom permission that roles can grant, in the shape of the Access API.
5
+ *
6
+ * ```ts
7
+ * definePermission({
8
+ * name: 'articles-only',
9
+ * permissionType: 'sanity.document.filter.mode',
10
+ * config: {filter: '_type == "article"'},
11
+ * })
12
+ * ```
13
+ *
14
+ * @example Grant the permission with a role
15
+ * ```ts
16
+ * definePermission({
17
+ * name: 'articles-only',
18
+ * title: 'Articles only',
19
+ * description: 'Access to article documents',
20
+ * permissionType: 'sanity.document.filter',
21
+ * config: {filter: '_type == "article"'},
22
+ * })
23
+ *
24
+ * defineRole({
25
+ * name: 'article-editor',
26
+ * title: 'Article editor',
27
+ * appliesToUsers: true,
28
+ * appliesToRobots: false,
29
+ * permissions: [
30
+ * {name: 'sanity-project', action: 'read'},
31
+ * {name: '$.resources.articles-only', action: 'read'},
32
+ * {name: '$.resources.articles-only', action: 'update'},
33
+ * ],
34
+ * })
35
+ * ```
36
+ * @param parameters The configuration of the permission
37
+ * @public
38
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
39
+ * @category Definers
40
+ * @expandType BlueprintPermissionConfig
41
+ * @returns The permission resource
42
+ */
43
+ export function definePermission(parameters) {
44
+ const permissionResource = {
45
+ ...parameters,
46
+ type: 'sanity.access.permission',
47
+ };
48
+ runValidation(() => validatePermission(permissionResource));
49
+ return permissionResource;
50
+ }
51
+ /**
52
+ * Defines a content resource as the Manage UI makes it: a custom permission of type `sanity.document.filter.mode`.
53
+ *
54
+ * ```ts
55
+ * defineContentResource({
56
+ * name: 'movies',
57
+ * documentTypes: ['movie'],
58
+ * })
59
+ * // config.filter: '_type == "movie"'
60
+ * ```
61
+ *
62
+ * @example Document types and a filter
63
+ * ```ts
64
+ * defineContentResource({
65
+ * name: 'london-posts',
66
+ * title: 'London posts',
67
+ * documentTypes: ['post', 'event'],
68
+ * filter: 'branch == "london"',
69
+ * })
70
+ * // config.filter: '_type in ["post", "event"] && (branch == "london")'
71
+ * ```
72
+ * @param parameters The configuration of the content resource
73
+ * @public
74
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
75
+ * @category Definers
76
+ * @expandType BlueprintContentResourceConfig
77
+ * @returns The permission resource
78
+ */
79
+ export function defineContentResource(parameters) {
80
+ const { documentTypes, filter, ...permission } = parameters;
81
+ runValidation(() => validateContentResource(parameters));
82
+ return definePermission({
83
+ ...permission,
84
+ permissionType: 'sanity.document.filter.mode',
85
+ config: { filter: contentResourceFilter(documentTypes, filter) },
86
+ });
87
+ }
88
+ function contentResourceFilter(documentTypes, filter) {
89
+ const types = Array.isArray(documentTypes) ? documentTypes.map((type) => JSON.stringify(type)) : [];
90
+ const typeClause = types.length > 1 ? `_type in [${types.join(', ')}]` : types.length === 1 ? `_type == ${types[0]}` : '';
91
+ if (!typeClause)
92
+ return filter ?? '';
93
+ return filter ? `${typeClause} && (${filter})` : typeClause;
94
+ }
95
+ //# sourceMappingURL=permissions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permissions.js","sourceRoot":"","sources":["../../src/definers/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAC,aAAa,EAAC,MAAM,wBAAwB,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAAqC;IACpE,MAAM,kBAAkB,GAAgC;QACtD,GAAG,UAAU;QACb,IAAI,EAAE,0BAA0B;KACjC,CAAA;IAED,aAAa,CAAC,GAAG,EAAE,CAAC,kBAAkB,CAAC,kBAAkB,CAAC,CAAC,CAAA;IAE3D,OAAO,kBAAkB,CAAA;AAC3B,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAA0C;IAC9E,MAAM,EAAC,aAAa,EAAE,MAAM,EAAE,GAAG,UAAU,EAAC,GAAG,UAAU,CAAA;IAEzD,aAAa,CAAC,GAAG,EAAE,CAAC,uBAAuB,CAAC,UAAU,CAAC,CAAC,CAAA;IAExD,OAAO,gBAAgB,CAAC;QACtB,GAAG,UAAU;QACb,cAAc,EAAE,6BAA6B;QAC7C,MAAM,EAAE,EAAC,MAAM,EAAE,qBAAqB,CAAC,aAAa,EAAE,MAAM,CAAC,EAAC;KAC/D,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,qBAAqB,CAAC,aAAsB,EAAE,MAAe;IACpE,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IACnG,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,aAAa,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;IAEzH,IAAI,CAAC,UAAU;QAAE,OAAO,MAAM,IAAI,EAAE,CAAA;IACpC,OAAO,MAAM,CAAC,CAAC,CAAC,GAAG,UAAU,QAAQ,MAAM,GAAG,CAAC,CAAC,CAAC,UAAU,CAAA;AAC7D,CAAC"}
package/dist/index.d.ts CHANGED
@@ -19,6 +19,7 @@ export * from './definers/functions/queue.js';
19
19
  export * from './definers/functions/scheduled.js';
20
20
  export * from './definers/functions/sync-tag-invalidate.js';
21
21
  export * from './definers/installation-config.js';
22
+ export * from './definers/permissions.js';
22
23
  export * from './definers/projects.js';
23
24
  export * from './definers/resources.js';
24
25
  export * from './definers/robots.js';
@@ -41,6 +42,7 @@ export * from './types/datasets.js';
41
42
  export * from './types/errors.js';
42
43
  export * from './types/functions/index.js';
43
44
  export * from './types/installation-config.js';
45
+ export * from './types/permissions.js';
44
46
  export * from './types/projects.js';
45
47
  export * from './types/resources.js';
46
48
  export * from './types/robots.js';
@@ -58,6 +60,7 @@ export * from './validation/cors.js';
58
60
  export * from './validation/datasets.js';
59
61
  export * from './validation/functions.js';
60
62
  export * from './validation/installation-config.js';
63
+ export * from './validation/permissions.js';
61
64
  export * from './validation/projects.js';
62
65
  export * from './validation/resources.js';
63
66
  export * from './validation/robots.js';
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ export * from './definers/functions/queue.js';
20
20
  export * from './definers/functions/scheduled.js';
21
21
  export * from './definers/functions/sync-tag-invalidate.js';
22
22
  export * from './definers/installation-config.js';
23
+ export * from './definers/permissions.js';
23
24
  export * from './definers/projects.js';
24
25
  export * from './definers/resources.js';
25
26
  export * from './definers/robots.js';
@@ -43,6 +44,7 @@ export * from './types/datasets.js';
43
44
  export * from './types/errors.js';
44
45
  export * from './types/functions/index.js';
45
46
  export * from './types/installation-config.js';
47
+ export * from './types/permissions.js';
46
48
  export * from './types/projects.js';
47
49
  export * from './types/resources.js';
48
50
  export * from './types/robots.js';
@@ -61,6 +63,7 @@ export * from './validation/cors.js';
61
63
  export * from './validation/datasets.js';
62
64
  export * from './validation/functions.js';
63
65
  export * from './validation/installation-config.js';
66
+ export * from './validation/permissions.js';
64
67
  export * from './validation/projects.js';
65
68
  export * from './validation/resources.js';
66
69
  export * from './validation/robots.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD;;;GAGG;AACH;;;GAGG;AACH,cAAc,4BAA4B,CAAA;AAC1C,cAAc,0BAA0B,CAAA;AACxC,cAAc,oBAAoB,CAAA;AAClC,cAAc,wBAAwB,CAAA;AACtC,cAAc,kCAAkC,CAAA;A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0B,CAAA;AACxC,cAAc,2BAA2B,CAAA;AACzC,cAAc,qCAAqC,CAAA;AACnD,cAAc,6BAA6B,CAAA;AAC3C,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AACtC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,uBAAuB,CAAA;AACrC,cAAc,yBAAyB,CAAA;AACvC,cAAc,0BAA0B,CAAA;AAExC,yEAAyE;AACzE;;;GAGG"}
@@ -0,0 +1,63 @@
1
+ import type { BlueprintProjectResourceLifecycle, BlueprintResource } from '../index.js';
2
+ /**
3
+ * The type of a custom permission.
4
+ *
5
+ * - **sanity.document.filter** A role grants document actions, such as `read` and `update`, on the documents that match the filter.
6
+ * - **sanity.document.filter.mode** A role grants the `mode` action, with a `mode` param such as `read` or `publish`, on the documents that match the filter.
7
+ * @beta This feature is subject to breaking changes.
8
+ * @category Resource Types
9
+ */
10
+ export type BlueprintPermissionType = 'sanity.document.filter' | 'sanity.document.filter.mode';
11
+ /**
12
+ * A custom permission resource that roles can grant.
13
+ * @beta This feature is subject to breaking changes.
14
+ * @category Resource Types
15
+ */
16
+ export interface BlueprintPermissionResource extends BlueprintResource<BlueprintProjectResourceLifecycle> {
17
+ type: 'sanity.access.permission';
18
+ /**
19
+ * The name of the permission.
20
+ *
21
+ * Lowercase letters, numbers, hyphens, and underscores only. Must not start with `sanity` or `res-`.
22
+ */
23
+ name: string;
24
+ /** A human-readable title. Defaults to `name`. */
25
+ title?: string;
26
+ /** A description of the permission. Defaults to an empty string. */
27
+ description?: string;
28
+ /**
29
+ * The type of the permission. It cannot change after the permission is created.
30
+ *
31
+ * Manage creates content resources as `sanity.document.filter.mode`, and lists only custom permissions of that type.
32
+ */
33
+ permissionType: BlueprintPermissionType;
34
+ /** The configuration of the permission. */
35
+ config: {
36
+ /** A GROQ filter that selects the documents that the permission applies to, e.g. `_type == "article"`. */
37
+ filter: string;
38
+ };
39
+ /**
40
+ * The project ID of the project that contains the permission.
41
+ *
42
+ * The `project` attribute must be defined if your blueprint is scoped to an organization and the stack has no default project.
43
+ */
44
+ project?: string;
45
+ }
46
+ /**
47
+ * Configuration for a custom permission resource.
48
+ * @beta This feature is subject to breaking changes.
49
+ * @category Resource Types
50
+ * @interface
51
+ */
52
+ export type BlueprintPermissionConfig = Omit<BlueprintPermissionResource, 'type'>;
53
+ /**
54
+ * Configuration for a content resource: a custom permission of type `sanity.document.filter.mode`, as the Manage UI makes it.
55
+ * @beta This feature is subject to breaking changes.
56
+ * @category Resource Types
57
+ */
58
+ export interface BlueprintContentResourceConfig extends Omit<BlueprintPermissionConfig, 'permissionType' | 'config'> {
59
+ /** The document types that the content resource applies to. Omit to apply to documents of any type. */
60
+ documentTypes?: string[];
61
+ /** A GROQ filter that selects documents, e.g. `branch == "london"`. It is joined to `documentTypes` with `&&`. */
62
+ filter?: string;
63
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=permissions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permissions.js","sourceRoot":"","sources":["../../src/types/permissions.ts"],"names":[],"mappings":""}
@@ -6,7 +6,7 @@ import type { BlueprintProjectResourceLifecycle, BlueprintResource } from '../in
6
6
  * @expand
7
7
  */
8
8
  export interface RolePermission {
9
- /** Predefined permission name (e.g., 'sanity-all-documents') */
9
+ /** Predefined permission name (e.g., 'sanity-all-documents'), or a reference to a permission in the blueprint (e.g., '$.resources.articles-only') */
10
10
  name: string;
11
11
  /** Permission action (e.g., 'read', 'mode') */
12
12
  action: string;
@@ -0,0 +1,15 @@
1
+ import { type BlueprintError } from '../index.js';
2
+ /**
3
+ * Validates that the given resource is a valid Permission.
4
+ * @param resource The Permission resource
5
+ * @category Validation
6
+ * @returns A list of validation errors
7
+ */
8
+ export declare function validatePermission(resource: unknown): BlueprintError[];
9
+ /**
10
+ * Validates the fields of a content resource that make its filter.
11
+ * @param resource The content resource configuration
12
+ * @category Validation
13
+ * @returns A list of validation errors
14
+ */
15
+ export declare function validateContentResource(resource: unknown): BlueprintError[];
@@ -0,0 +1,95 @@
1
+ import { validateResource } from '../index.js';
2
+ const PERMISSION_TYPES = ['sanity.document.filter', 'sanity.document.filter.mode'];
3
+ const PERMISSION_NAME = /^(?!res-)(?!sanity)[a-z0-9-_]+$/;
4
+ /**
5
+ * Validates that the given resource is a valid Permission.
6
+ * @param resource The Permission resource
7
+ * @category Validation
8
+ * @returns A list of validation errors
9
+ */
10
+ export function validatePermission(resource) {
11
+ if (!resource)
12
+ return [{ type: 'invalid_value', message: 'Permission config must be provided' }];
13
+ if (typeof resource !== 'object')
14
+ return [{ type: 'invalid_type', message: 'Permission config must be an object' }];
15
+ const errors = validateResource(resource, { projectContained: true });
16
+ if (!('name' in resource) || !resource.name) {
17
+ errors.push({ type: 'missing_parameter', message: 'Permission name is required' });
18
+ }
19
+ else if (typeof resource.name !== 'string') {
20
+ errors.push({ type: 'invalid_type', message: 'Permission name must be a string' });
21
+ }
22
+ else if (!PERMISSION_NAME.test(resource.name)) {
23
+ errors.push({
24
+ type: 'invalid_format',
25
+ message: 'Permission name must only contain lowercase letters, numbers, hyphens, and underscores, and must not start with "sanity" or "res-"',
26
+ });
27
+ }
28
+ if (!('type' in resource)) {
29
+ errors.push({ type: 'missing_parameter', message: 'Permission type is required' });
30
+ }
31
+ else if (resource.type !== 'sanity.access.permission') {
32
+ errors.push({ type: 'invalid_value', message: 'Permission type must be `sanity.access.permission`' });
33
+ }
34
+ if ('title' in resource && resource.title !== undefined) {
35
+ if (typeof resource.title !== 'string') {
36
+ errors.push({ type: 'invalid_type', message: 'Permission title must be a string' });
37
+ }
38
+ else if (resource.title.length === 0) {
39
+ errors.push({ type: 'invalid_value', message: 'Permission title must not be empty' });
40
+ }
41
+ }
42
+ if ('description' in resource && resource.description !== undefined && typeof resource.description !== 'string') {
43
+ errors.push({ type: 'invalid_type', message: 'Permission description must be a string' });
44
+ }
45
+ if (!('permissionType' in resource) || !resource.permissionType) {
46
+ errors.push({ type: 'missing_parameter', message: 'Permission permissionType is required' });
47
+ }
48
+ else if (typeof resource.permissionType !== 'string' || !PERMISSION_TYPES.includes(resource.permissionType)) {
49
+ errors.push({ type: 'invalid_value', message: `Permission permissionType must be one of ${PERMISSION_TYPES.join(', ')}` });
50
+ }
51
+ if (!('config' in resource) || !resource.config) {
52
+ errors.push({ type: 'missing_parameter', message: 'Permission `config` is required' });
53
+ }
54
+ else if (typeof resource.config !== 'object') {
55
+ errors.push({ type: 'invalid_type', message: 'Permission `config` must be an object' });
56
+ }
57
+ else if (!('filter' in resource.config) || !resource.config.filter) {
58
+ errors.push({ type: 'missing_parameter', message: 'Permission filter is required' });
59
+ }
60
+ else if (typeof resource.config.filter !== 'string') {
61
+ errors.push({ type: 'invalid_type', message: 'Permission filter must be a string' });
62
+ }
63
+ if ('project' in resource && resource.project !== undefined) {
64
+ if (typeof resource.project !== 'string') {
65
+ errors.push({ type: 'invalid_type', message: 'Permission project must be a string' });
66
+ }
67
+ else if (resource.project.length === 0) {
68
+ errors.push({ type: 'invalid_value', message: 'Permission project must not be empty' });
69
+ }
70
+ }
71
+ return errors;
72
+ }
73
+ /**
74
+ * Validates the fields of a content resource that make its filter.
75
+ * @param resource The content resource configuration
76
+ * @category Validation
77
+ * @returns A list of validation errors
78
+ */
79
+ export function validateContentResource(resource) {
80
+ if (!resource)
81
+ return [{ type: 'invalid_value', message: 'Content resource config must be provided' }];
82
+ if (typeof resource !== 'object')
83
+ return [{ type: 'invalid_type', message: 'Content resource config must be an object' }];
84
+ const errors = [];
85
+ if ('documentTypes' in resource &&
86
+ resource.documentTypes !== undefined &&
87
+ (!Array.isArray(resource.documentTypes) || !resource.documentTypes.every((type) => typeof type === 'string' && type.length > 0))) {
88
+ errors.push({ type: 'invalid_type', message: 'Content resource documentTypes must be an array of non-empty strings' });
89
+ }
90
+ if ('filter' in resource && resource.filter !== undefined && typeof resource.filter !== 'string') {
91
+ errors.push({ type: 'invalid_type', message: 'Content resource filter must be a string' });
92
+ }
93
+ return errors;
94
+ }
95
+ //# sourceMappingURL=permissions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permissions.js","sourceRoot":"","sources":["../../src/validation/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,gBAAgB,EAAC,MAAM,aAAa,CAAA;AAEjE,MAAM,gBAAgB,GAAG,CAAC,wBAAwB,EAAE,6BAA6B,CAAC,CAAA;AAClF,MAAM,eAAe,GAAG,iCAAiC,CAAA;AAEzD;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,QAAiB;IAClD,IAAI,CAAC,QAAQ;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,oCAAoC,EAAC,CAAC,CAAA;IAC9F,IAAI,OAAO,QAAQ,KAAK,QAAQ;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,qCAAqC,EAAC,CAAC,CAAA;IAEjH,MAAM,MAAM,GAAqB,gBAAgB,CAAC,QAAQ,EAAE,EAAC,gBAAgB,EAAE,IAAI,EAAC,CAAC,CAAA;IAErF,IAAI,CAAC,CAAC,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC5C,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,6BAA6B,EAAC,CAAC,CAAA;IAClF,CAAC;SAAM,IAAI,OAAO,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7C,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,kCAAkC,EAAC,CAAC,CAAA;IAClF,CAAC;SAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAChD,MAAM,CAAC,IAAI,CAAC;YACV,IAAI,EAAE,gBAAgB;YACtB,OAAO,EACL,oIAA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,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,0CAA0C,EAAC,CAAC,CAAA;IAC1F,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.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "Helper methods and type definitions for Sanity Blueprints",
5
5
  "keywords": [
6
6
  "sanity",
@@ -0,0 +1,107 @@
1
+ import {
2
+ type BlueprintContentResourceConfig,
3
+ type BlueprintPermissionConfig,
4
+ type BlueprintPermissionResource,
5
+ validateContentResource,
6
+ validatePermission,
7
+ } from '../index.js'
8
+ import {runValidation} from '../utils/validation.js'
9
+
10
+ /**
11
+ * Defines a custom permission that roles can grant, in the shape of the Access API.
12
+ *
13
+ * ```ts
14
+ * definePermission({
15
+ * name: 'articles-only',
16
+ * permissionType: 'sanity.document.filter.mode',
17
+ * config: {filter: '_type == "article"'},
18
+ * })
19
+ * ```
20
+ *
21
+ * @example Grant the permission with a role
22
+ * ```ts
23
+ * definePermission({
24
+ * name: 'articles-only',
25
+ * title: 'Articles only',
26
+ * description: 'Access to article documents',
27
+ * permissionType: 'sanity.document.filter',
28
+ * config: {filter: '_type == "article"'},
29
+ * })
30
+ *
31
+ * defineRole({
32
+ * name: 'article-editor',
33
+ * title: 'Article editor',
34
+ * appliesToUsers: true,
35
+ * appliesToRobots: false,
36
+ * permissions: [
37
+ * {name: 'sanity-project', action: 'read'},
38
+ * {name: '$.resources.articles-only', action: 'read'},
39
+ * {name: '$.resources.articles-only', action: 'update'},
40
+ * ],
41
+ * })
42
+ * ```
43
+ * @param parameters The configuration of the permission
44
+ * @public
45
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
46
+ * @category Definers
47
+ * @expandType BlueprintPermissionConfig
48
+ * @returns The permission resource
49
+ */
50
+ export function definePermission(parameters: BlueprintPermissionConfig): BlueprintPermissionResource {
51
+ const permissionResource: BlueprintPermissionResource = {
52
+ ...parameters,
53
+ type: 'sanity.access.permission',
54
+ }
55
+
56
+ runValidation(() => validatePermission(permissionResource))
57
+
58
+ return permissionResource
59
+ }
60
+
61
+ /**
62
+ * Defines a content resource as the Manage UI makes it: a custom permission of type `sanity.document.filter.mode`.
63
+ *
64
+ * ```ts
65
+ * defineContentResource({
66
+ * name: 'movies',
67
+ * documentTypes: ['movie'],
68
+ * })
69
+ * // config.filter: '_type == "movie"'
70
+ * ```
71
+ *
72
+ * @example Document types and a filter
73
+ * ```ts
74
+ * defineContentResource({
75
+ * name: 'london-posts',
76
+ * title: 'London posts',
77
+ * documentTypes: ['post', 'event'],
78
+ * filter: 'branch == "london"',
79
+ * })
80
+ * // config.filter: '_type in ["post", "event"] && (branch == "london")'
81
+ * ```
82
+ * @param parameters The configuration of the content resource
83
+ * @public
84
+ * @beta Deploying Permissions via Blueprints is experimental. This feature is stabilizing but may still be subject to breaking changes.
85
+ * @category Definers
86
+ * @expandType BlueprintContentResourceConfig
87
+ * @returns The permission resource
88
+ */
89
+ export function defineContentResource(parameters: BlueprintContentResourceConfig): BlueprintPermissionResource {
90
+ const {documentTypes, filter, ...permission} = parameters
91
+
92
+ runValidation(() => validateContentResource(parameters))
93
+
94
+ return definePermission({
95
+ ...permission,
96
+ permissionType: 'sanity.document.filter.mode',
97
+ config: {filter: contentResourceFilter(documentTypes, filter)},
98
+ })
99
+ }
100
+
101
+ function contentResourceFilter(documentTypes: unknown, filter?: string): string {
102
+ const types = Array.isArray(documentTypes) ? documentTypes.map((type) => JSON.stringify(type)) : []
103
+ const typeClause = types.length > 1 ? `_type in [${types.join(', ')}]` : types.length === 1 ? `_type == ${types[0]}` : ''
104
+
105
+ if (!typeClause) return filter ?? ''
106
+ return filter ? `${typeClause} && (${filter})` : typeClause
107
+ }
package/src/index.ts CHANGED
@@ -20,6 +20,7 @@ export * from './definers/functions/queue.js'
20
20
  export * from './definers/functions/scheduled.js'
21
21
  export * from './definers/functions/sync-tag-invalidate.js'
22
22
  export * from './definers/installation-config.js'
23
+ export * from './definers/permissions.js'
23
24
  export * from './definers/projects.js'
24
25
  export * from './definers/resources.js'
25
26
  export * from './definers/robots.js'
@@ -44,6 +45,7 @@ export * from './types/datasets.js'
44
45
  export * from './types/errors.js'
45
46
  export * from './types/functions/index.js'
46
47
  export * from './types/installation-config.js'
48
+ export * from './types/permissions.js'
47
49
  export * from './types/projects.js'
48
50
  export * from './types/resources.js'
49
51
  export * from './types/robots.js'
@@ -63,6 +65,7 @@ export * from './validation/cors.js'
63
65
  export * from './validation/datasets.js'
64
66
  export * from './validation/functions.js'
65
67
  export * from './validation/installation-config.js'
68
+ export * from './validation/permissions.js'
66
69
  export * from './validation/projects.js'
67
70
  export * from './validation/resources.js'
68
71
  export * from './validation/robots.js'
@@ -0,0 +1,67 @@
1
+ import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../index.js'
2
+
3
+ /**
4
+ * The type of a custom permission.
5
+ *
6
+ * - **sanity.document.filter** A role grants document actions, such as `read` and `update`, on the documents that match the filter.
7
+ * - **sanity.document.filter.mode** A role grants the `mode` action, with a `mode` param such as `read` or `publish`, on the documents that match the filter.
8
+ * @beta This feature is subject to breaking changes.
9
+ * @category Resource Types
10
+ */
11
+ export type BlueprintPermissionType = 'sanity.document.filter' | 'sanity.document.filter.mode'
12
+
13
+ /**
14
+ * A custom permission resource that roles can grant.
15
+ * @beta This feature is subject to breaking changes.
16
+ * @category Resource Types
17
+ */
18
+ export interface BlueprintPermissionResource extends BlueprintResource<BlueprintProjectResourceLifecycle> {
19
+ type: 'sanity.access.permission'
20
+ /**
21
+ * The name of the permission.
22
+ *
23
+ * Lowercase letters, numbers, hyphens, and underscores only. Must not start with `sanity` or `res-`.
24
+ */
25
+ name: string
26
+ /** A human-readable title. Defaults to `name`. */
27
+ title?: string
28
+ /** A description of the permission. Defaults to an empty string. */
29
+ description?: string
30
+ /**
31
+ * The type of the permission. It cannot change after the permission is created.
32
+ *
33
+ * Manage creates content resources as `sanity.document.filter.mode`, and lists only custom permissions of that type.
34
+ */
35
+ permissionType: BlueprintPermissionType
36
+ /** The configuration of the permission. */
37
+ config: {
38
+ /** A GROQ filter that selects the documents that the permission applies to, e.g. `_type == "article"`. */
39
+ filter: string
40
+ }
41
+ /**
42
+ * The project ID of the project that contains the permission.
43
+ *
44
+ * The `project` attribute must be defined if your blueprint is scoped to an organization and the stack has no default project.
45
+ */
46
+ project?: string
47
+ }
48
+
49
+ /**
50
+ * Configuration for a custom permission resource.
51
+ * @beta This feature is subject to breaking changes.
52
+ * @category Resource Types
53
+ * @interface
54
+ */
55
+ export type BlueprintPermissionConfig = Omit<BlueprintPermissionResource, 'type'>
56
+
57
+ /**
58
+ * Configuration for a content resource: a custom permission of type `sanity.document.filter.mode`, as the Manage UI makes it.
59
+ * @beta This feature is subject to breaking changes.
60
+ * @category Resource Types
61
+ */
62
+ export interface BlueprintContentResourceConfig extends Omit<BlueprintPermissionConfig, 'permissionType' | 'config'> {
63
+ /** The document types that the content resource applies to. Omit to apply to documents of any type. */
64
+ documentTypes?: string[]
65
+ /** A GROQ filter that selects documents, e.g. `branch == "london"`. It is joined to `documentTypes` with `&&`. */
66
+ filter?: string
67
+ }
@@ -7,7 +7,7 @@ import type {BlueprintProjectResourceLifecycle, BlueprintResource} from '../inde
7
7
  * @expand
8
8
  */
9
9
  export interface RolePermission {
10
- /** Predefined permission name (e.g., 'sanity-all-documents') */
10
+ /** Predefined permission name (e.g., 'sanity-all-documents'), or a reference to a permission in the blueprint (e.g., '$.resources.articles-only') */
11
11
  name: string
12
12
  /** Permission action (e.g., 'read', 'mode') */
13
13
  action: string
@@ -0,0 +1,100 @@
1
+ import {type BlueprintError, validateResource} from '../index.js'
2
+
3
+ const PERMISSION_TYPES = ['sanity.document.filter', 'sanity.document.filter.mode']
4
+ const PERMISSION_NAME = /^(?!res-)(?!sanity)[a-z0-9-_]+$/
5
+
6
+ /**
7
+ * Validates that the given resource is a valid Permission.
8
+ * @param resource The Permission resource
9
+ * @category Validation
10
+ * @returns A list of validation errors
11
+ */
12
+ export function validatePermission(resource: unknown): BlueprintError[] {
13
+ if (!resource) return [{type: 'invalid_value', message: 'Permission config must be provided'}]
14
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Permission config must be an object'}]
15
+
16
+ const errors: BlueprintError[] = validateResource(resource, {projectContained: true})
17
+
18
+ if (!('name' in resource) || !resource.name) {
19
+ errors.push({type: 'missing_parameter', message: 'Permission name is required'})
20
+ } else if (typeof resource.name !== 'string') {
21
+ errors.push({type: 'invalid_type', message: 'Permission name must be a string'})
22
+ } else if (!PERMISSION_NAME.test(resource.name)) {
23
+ errors.push({
24
+ type: 'invalid_format',
25
+ message:
26
+ 'Permission name must only contain lowercase letters, numbers, hyphens, and underscores, and must not start with "sanity" or "res-"',
27
+ })
28
+ }
29
+
30
+ if (!('type' in resource)) {
31
+ errors.push({type: 'missing_parameter', message: 'Permission type is required'})
32
+ } else if (resource.type !== 'sanity.access.permission') {
33
+ errors.push({type: 'invalid_value', message: 'Permission type must be `sanity.access.permission`'})
34
+ }
35
+
36
+ if ('title' in resource && resource.title !== undefined) {
37
+ if (typeof resource.title !== 'string') {
38
+ errors.push({type: 'invalid_type', message: 'Permission title must be a string'})
39
+ } else if (resource.title.length === 0) {
40
+ errors.push({type: 'invalid_value', message: 'Permission title must not be empty'})
41
+ }
42
+ }
43
+
44
+ if ('description' in resource && resource.description !== undefined && typeof resource.description !== 'string') {
45
+ errors.push({type: 'invalid_type', message: 'Permission description must be a string'})
46
+ }
47
+
48
+ if (!('permissionType' in resource) || !resource.permissionType) {
49
+ errors.push({type: 'missing_parameter', message: 'Permission permissionType is required'})
50
+ } else if (typeof resource.permissionType !== 'string' || !PERMISSION_TYPES.includes(resource.permissionType)) {
51
+ errors.push({type: 'invalid_value', message: `Permission permissionType must be one of ${PERMISSION_TYPES.join(', ')}`})
52
+ }
53
+
54
+ if (!('config' in resource) || !resource.config) {
55
+ errors.push({type: 'missing_parameter', message: 'Permission `config` is required'})
56
+ } else if (typeof resource.config !== 'object') {
57
+ errors.push({type: 'invalid_type', message: 'Permission `config` must be an object'})
58
+ } else if (!('filter' in resource.config) || !resource.config.filter) {
59
+ errors.push({type: 'missing_parameter', message: 'Permission filter is required'})
60
+ } else if (typeof resource.config.filter !== 'string') {
61
+ errors.push({type: 'invalid_type', message: 'Permission filter must be a string'})
62
+ }
63
+
64
+ if ('project' in resource && resource.project !== undefined) {
65
+ if (typeof resource.project !== 'string') {
66
+ errors.push({type: 'invalid_type', message: 'Permission project must be a string'})
67
+ } else if (resource.project.length === 0) {
68
+ errors.push({type: 'invalid_value', message: 'Permission project must not be empty'})
69
+ }
70
+ }
71
+
72
+ return errors
73
+ }
74
+
75
+ /**
76
+ * Validates the fields of a content resource that make its filter.
77
+ * @param resource The content resource configuration
78
+ * @category Validation
79
+ * @returns A list of validation errors
80
+ */
81
+ export function validateContentResource(resource: unknown): BlueprintError[] {
82
+ if (!resource) return [{type: 'invalid_value', message: 'Content resource config must be provided'}]
83
+ if (typeof resource !== 'object') return [{type: 'invalid_type', message: 'Content resource config must be an object'}]
84
+
85
+ const errors: BlueprintError[] = []
86
+
87
+ if (
88
+ 'documentTypes' in resource &&
89
+ resource.documentTypes !== undefined &&
90
+ (!Array.isArray(resource.documentTypes) || !resource.documentTypes.every((type) => typeof type === 'string' && type.length > 0))
91
+ ) {
92
+ errors.push({type: 'invalid_type', message: 'Content resource documentTypes must be an array of non-empty strings'})
93
+ }
94
+
95
+ if ('filter' in resource && resource.filter !== undefined && typeof resource.filter !== 'string') {
96
+ errors.push({type: 'invalid_type', message: 'Content resource filter must be a string'})
97
+ }
98
+
99
+ return errors
100
+ }