diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 148988cf723199..ee1789856c3d3c 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -19,7 +19,7 @@ import { forwardResolver, getNormalizedCommaSeparable, getUserPermissionCacheKey, - getUserPermissionsFromResponse, + getUserPermissionsFromAllowHeader, ALLOWED_RESOURCE_ACTIONS, } from './utils'; import { getSyncProvider } from './sync'; @@ -173,7 +173,9 @@ export const getEntityRecord = const response = await apiFetch( { path, parse: false } ); const record = await response.json(); - const permissions = getUserPermissionsFromResponse( response ); + const permissions = getUserPermissionsFromAllowHeader( + response.headers?.get( 'allow' ) + ); registry.batch( () => { dispatch.receiveEntityRecords( kind, name, record, query ); @@ -440,7 +442,12 @@ export const canUser = return; } - const permissions = getUserPermissionsFromResponse( response ); + // Optional chaining operator is used here because the API requests don't + // return the expected result in the React native version. Instead, API requests + // only return the result, without including response properties like the headers. + const permissions = getUserPermissionsFromAllowHeader( + response.headers?.get( 'allow' ) + ); registry.batch( () => { for ( const action of ALLOWED_RESOURCE_ACTIONS ) { const key = getUserPermissionCacheKey( action, resource, id ); diff --git a/packages/core-data/src/utils/index.js b/packages/core-data/src/utils/index.js index bb4b5544435021..189635647779e5 100644 --- a/packages/core-data/src/utils/index.js +++ b/packages/core-data/src/utils/index.js @@ -11,6 +11,6 @@ export { default as getNestedValue } from './get-nested-value'; export { default as isNumericID } from './is-numeric-id'; export { getUserPermissionCacheKey, - getUserPermissionsFromResponse, + getUserPermissionsFromAllowHeader, ALLOWED_RESOURCE_ACTIONS, } from './user-permissions'; diff --git a/packages/core-data/src/utils/user-permissions.js b/packages/core-data/src/utils/user-permissions.js index a81c83f9e5af50..f1f02b5f5ba702 100644 --- a/packages/core-data/src/utils/user-permissions.js +++ b/packages/core-data/src/utils/user-permissions.js @@ -5,13 +5,11 @@ export const ALLOWED_RESOURCE_ACTIONS = [ 'delete', ]; -export function getUserPermissionsFromResponse( response ) { +export function getUserPermissionsFromAllowHeader( allowedMethods ) { const permissions = {}; - - // Optional chaining operator is used here because the API requests don't - // return the expected result in the React native version. Instead, API requests - // only return the result, without including response properties like the headers. - const allowedMethods = response.headers?.get( 'allow' ) || ''; + if ( ! allowedMethods ) { + return permissions; + } const methods = { create: 'POST',