diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index 23503ce411eda5..0ddd3858c97603 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -18,7 +18,7 @@ _Parameters_ - _state_ `State`: Data state. - _action_ `string`: Action to check. One of: 'create', 'read', 'update', 'delete'. -- _resource_ `string | Record< string, any >`: REST resource to check, e.g. 'media' or 'posts'. +- _resource_ `string | EntityResource`: Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }` or REST base as a string - `media`. - _id_ `EntityRecordKey`: Optional ID of the rest resource to check. _Returns_ diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 90f53a61a28331..8c262ebeee8d19 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -339,7 +339,7 @@ _Parameters_ - _state_ `State`: Data state. - _action_ `string`: Action to check. One of: 'create', 'read', 'update', 'delete'. -- _resource_ `string | Record< string, any >`: REST resource to check, e.g. 'media' or 'posts'. +- _resource_ `string | EntityResource`: Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }` or REST base as a string - `media`. - _id_ `EntityRecordKey`: Optional ID of the rest resource to check. _Returns_ diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 446ba8466161d0..d9b90e593620b7 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -352,10 +352,11 @@ export const getEmbedPreview = * Checks whether the current user can perform the given action on the given * REST resource. * - * @param {string} requestedAction Action to check. One of: 'create', 'read', 'update', - * 'delete'. - * @param {string} resource REST resource to check, e.g. 'media' or 'posts'. - * @param {?string} id ID of the rest resource to check. + * @param {string} requestedAction Action to check. One of: 'create', 'read', 'update', + * 'delete'. + * @param {string|Object} resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }` + * or REST base as a string - `media`. + * @param {?string} id ID of the rest resource to check. */ export const canUser = ( requestedAction, resource, id ) => diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 010cf347eee532..67c70d0310bcdf 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -120,6 +120,8 @@ type EntityRecordArgs = | [ string, string, EntityRecordKey ] | [ string, string, EntityRecordKey, GetRecordsHttpQuery ]; +type EntityResource = { kind: string; name: string; id?: EntityRecordKey }; + /** * Shared reference to an empty object for cases where it is important to avoid * returning a new object reference on every invocation, as in a connected or @@ -1136,7 +1138,8 @@ export function isPreviewEmbedFallback( state: State, url: string ): boolean { * * @param state Data state. * @param action Action to check. One of: 'create', 'read', 'update', 'delete'. - * @param resource REST resource to check, e.g. 'media' or 'posts'. + * @param resource Entity resource to check. Accepts entity object `{ kind: 'root', name: 'media', id: 1 }` + * or REST base as a string - `media`. * @param id Optional ID of the rest resource to check. * * @return Whether or not the user can perform the action, @@ -1145,7 +1148,7 @@ export function isPreviewEmbedFallback( state: State, url: string ): boolean { export function canUser( state: State, action: string, - resource: string | Record< string, any >, + resource: string | EntityResource, id?: EntityRecordKey ): boolean | undefined { const isEntity = typeof resource === 'object';