@wordpress/core-data 4.13.0 → 5.0.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/CHANGELOG.md +12 -0
- package/README.md +148 -65
- package/build/entities.js +38 -51
- package/build/entities.js.map +1 -1
- package/build/hooks/index.js +14 -0
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/use-entity-record.js +10 -6
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +2 -0
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js +5 -2
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/hooks/use-resource-permissions.js +74 -11
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/index.js +1 -30
- package/build/index.js.map +1 -1
- package/build/resolvers.js +59 -39
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +9 -46
- package/build/selectors.js.map +1 -1
- package/build-module/entities.js +38 -52
- package/build-module/entities.js.map +1 -1
- package/build-module/hooks/index.js +1 -0
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +10 -6
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +2 -0
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js +4 -1
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/hooks/use-resource-permissions.js +70 -10
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/index.js +0 -5
- package/build-module/index.js.map +1 -1
- package/build-module/resolvers.js +59 -39
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +7 -44
- package/build-module/selectors.js.map +1 -1
- package/build-types/actions.d.ts +188 -0
- package/build-types/actions.d.ts.map +1 -0
- package/build-types/batch/create-batch.d.ts +71 -0
- package/build-types/batch/create-batch.d.ts.map +1 -0
- package/build-types/batch/default-processor.d.ts +11 -0
- package/build-types/batch/default-processor.d.ts.map +1 -0
- package/build-types/batch/index.d.ts +3 -0
- package/build-types/batch/index.d.ts.map +1 -0
- package/build-types/entities.d.ts +128 -0
- package/build-types/entities.d.ts.map +1 -0
- package/build-types/entity-provider.d.ts +68 -0
- package/build-types/entity-provider.d.ts.map +1 -0
- package/build-types/entity-types/attachment.d.ts +121 -0
- package/build-types/entity-types/attachment.d.ts.map +1 -0
- package/build-types/entity-types/base-entity-records.d.ts +37 -0
- package/build-types/entity-types/base-entity-records.d.ts.map +1 -0
- package/build-types/entity-types/comment.d.ts +82 -0
- package/build-types/entity-types/comment.d.ts.map +1 -0
- package/build-types/entity-types/helpers.d.ts +123 -0
- package/build-types/entity-types/helpers.d.ts.map +1 -0
- package/build-types/entity-types/index.d.ts +64 -0
- package/build-types/entity-types/index.d.ts.map +1 -0
- package/build-types/entity-types/menu-location.d.ts +25 -0
- package/build-types/entity-types/menu-location.d.ts.map +1 -0
- package/build-types/entity-types/nav-menu-item.d.ts +88 -0
- package/build-types/entity-types/nav-menu-item.d.ts.map +1 -0
- package/build-types/entity-types/nav-menu.d.ts +45 -0
- package/build-types/entity-types/nav-menu.d.ts.map +1 -0
- package/build-types/entity-types/page.d.ts +120 -0
- package/build-types/entity-types/page.d.ts.map +1 -0
- package/build-types/entity-types/plugin.d.ts +62 -0
- package/build-types/entity-types/plugin.d.ts.map +1 -0
- package/build-types/entity-types/post.d.ts +128 -0
- package/build-types/entity-types/post.d.ts.map +1 -0
- package/build-types/entity-types/settings.d.ts +89 -0
- package/build-types/entity-types/settings.d.ts.map +1 -0
- package/build-types/entity-types/sidebar.d.ts +55 -0
- package/build-types/entity-types/sidebar.d.ts.map +1 -0
- package/build-types/entity-types/taxonomy.d.ts +83 -0
- package/build-types/entity-types/taxonomy.d.ts.map +1 -0
- package/build-types/entity-types/theme.d.ts +206 -0
- package/build-types/entity-types/theme.d.ts.map +1 -0
- package/build-types/entity-types/type.d.ts +71 -0
- package/build-types/entity-types/type.d.ts.map +1 -0
- package/build-types/entity-types/user.d.ts +93 -0
- package/build-types/entity-types/user.d.ts.map +1 -0
- package/build-types/entity-types/widget-type.d.ts +33 -0
- package/build-types/entity-types/widget-type.d.ts.map +1 -0
- package/build-types/entity-types/widget.d.ts +59 -0
- package/build-types/entity-types/widget.d.ts.map +1 -0
- package/build-types/entity-types/wp-template-part.d.ts +80 -0
- package/build-types/entity-types/wp-template-part.d.ts.map +1 -0
- package/build-types/entity-types/wp-template.d.ts +80 -0
- package/build-types/entity-types/wp-template.d.ts.map +1 -0
- package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts +139 -0
- package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts.map +1 -0
- package/build-types/fetch/__experimental-fetch-url-data.d.ts +35 -0
- package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -0
- package/build-types/fetch/index.d.ts +3 -0
- package/build-types/fetch/index.d.ts.map +1 -0
- package/build-types/hooks/constants.d.ts +7 -0
- package/build-types/hooks/constants.d.ts.map +1 -0
- package/build-types/hooks/index.d.ts +4 -0
- package/build-types/hooks/index.d.ts.map +1 -0
- package/build-types/hooks/memoize.d.ts +3 -0
- package/build-types/hooks/memoize.d.ts.map +1 -0
- package/build-types/hooks/use-entity-record.d.ts +124 -0
- package/build-types/hooks/use-entity-record.d.ts.map +1 -0
- package/build-types/hooks/use-entity-records.d.ts +49 -0
- package/build-types/hooks/use-entity-records.d.ts.map +1 -0
- package/build-types/hooks/use-query-select.d.ts +46 -0
- package/build-types/hooks/use-query-select.d.ts.map +1 -0
- package/build-types/hooks/use-resource-permissions.d.ts +99 -0
- package/build-types/hooks/use-resource-permissions.d.ts.map +1 -0
- package/build-types/index.d.ts +131 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/locks/actions.d.ts +7 -0
- package/build-types/locks/actions.d.ts.map +1 -0
- package/build-types/locks/engine.d.ts +5 -0
- package/build-types/locks/engine.d.ts.map +1 -0
- package/build-types/locks/reducer.d.ts +10 -0
- package/build-types/locks/reducer.d.ts.map +1 -0
- package/build-types/locks/selectors.d.ts +5 -0
- package/build-types/locks/selectors.d.ts.map +1 -0
- package/build-types/locks/utils.d.ts +8 -0
- package/build-types/locks/utils.d.ts.map +1 -0
- package/build-types/name.d.ts +8 -0
- package/build-types/name.d.ts.map +1 -0
- package/build-types/queried-data/actions.d.ts +32 -0
- package/build-types/queried-data/actions.d.ts.map +1 -0
- package/build-types/queried-data/get-query-parts.d.ts +60 -0
- package/build-types/queried-data/get-query-parts.d.ts.map +1 -0
- package/build-types/queried-data/index.d.ts +4 -0
- package/build-types/queried-data/index.d.ts.map +1 -0
- package/build-types/queried-data/reducer.d.ts +54 -0
- package/build-types/queried-data/reducer.d.ts.map +1 -0
- package/build-types/queried-data/selectors.d.ts +16 -0
- package/build-types/queried-data/selectors.d.ts.map +1 -0
- package/build-types/reducer.d.ts +156 -0
- package/build-types/reducer.d.ts.map +1 -0
- package/build-types/resolvers.d.ts +75 -0
- package/build-types/resolvers.d.ts.map +1 -0
- package/build-types/selectors.d.ts +524 -0
- package/build-types/selectors.d.ts.map +1 -0
- package/build-types/types.d.ts +4 -0
- package/build-types/types.d.ts.map +1 -0
- package/build-types/utils/conservative-map-item.d.ts +12 -0
- package/build-types/utils/conservative-map-item.d.ts.map +1 -0
- package/build-types/utils/forward-resolver.d.ts +10 -0
- package/build-types/utils/forward-resolver.d.ts.map +1 -0
- package/build-types/utils/get-normalized-comma-separable.d.ts +12 -0
- package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -0
- package/build-types/utils/if-matching-action.d.ts +14 -0
- package/build-types/utils/if-matching-action.d.ts.map +1 -0
- package/build-types/utils/index.d.ts +9 -0
- package/build-types/utils/index.d.ts.map +1 -0
- package/build-types/utils/is-raw-attribute.d.ts +10 -0
- package/build-types/utils/is-raw-attribute.d.ts.map +1 -0
- package/build-types/utils/on-sub-key.d.ts +4 -0
- package/build-types/utils/on-sub-key.d.ts.map +1 -0
- package/build-types/utils/replace-action.d.ts +13 -0
- package/build-types/utils/replace-action.d.ts.map +1 -0
- package/build-types/utils/with-weak-map-cache.d.ts +12 -0
- package/build-types/utils/with-weak-map-cache.d.ts.map +1 -0
- package/package.json +13 -11
- package/src/entities.js +325 -0
- package/src/entity-types/attachment.ts +3 -4
- package/src/entity-types/comment.ts +3 -4
- package/src/entity-types/index.ts +31 -88
- package/src/entity-types/menu-location.ts +3 -4
- package/src/entity-types/nav-menu-item.ts +3 -4
- package/src/entity-types/nav-menu.ts +3 -3
- package/src/entity-types/page.ts +3 -3
- package/src/entity-types/plugin.ts +3 -3
- package/src/entity-types/post.ts +3 -3
- package/src/entity-types/settings.ts +3 -3
- package/src/entity-types/sidebar.ts +3 -4
- package/src/entity-types/taxonomy.ts +3 -4
- package/src/entity-types/theme.ts +7 -3
- package/src/entity-types/type.ts +3 -3
- package/src/entity-types/user.ts +3 -3
- package/src/entity-types/widget-type.ts +3 -4
- package/src/entity-types/widget.ts +3 -3
- package/src/entity-types/wp-template-part.ts +3 -4
- package/src/entity-types/wp-template.ts +3 -4
- package/src/hooks/index.ts +4 -0
- package/src/hooks/test/use-entity-record.js +41 -1
- package/src/hooks/test/use-resource-permissions.js +32 -36
- package/src/hooks/use-entity-record.ts +18 -6
- package/src/hooks/use-entity-records.ts +2 -0
- package/src/hooks/use-query-select.ts +4 -1
- package/src/hooks/use-resource-permissions.ts +84 -20
- package/src/index.js +0 -5
- package/src/resolvers.js +80 -45
- package/src/selectors.ts +202 -341
- package/src/test/resolvers.js +118 -4
- package/tsconfig.json +21 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/build/entity-types/entities.js +0 -6
- package/build/entity-types/entities.js.map +0 -1
- package/build-module/entity-types/entities.js +0 -2
- package/build-module/entity-types/entities.js.map +0 -1
- package/src/entities.ts +0 -548
- package/src/entity-types/entities.ts +0 -130
package/src/resolvers.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { camelCase
|
|
4
|
+
import { camelCase } from 'change-case';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* WordPress dependencies
|
|
@@ -57,7 +57,9 @@ export const getEntityRecord =
|
|
|
57
57
|
( kind, name, key = '', query ) =>
|
|
58
58
|
async ( { select, dispatch } ) => {
|
|
59
59
|
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
60
|
-
const entityConfig = find(
|
|
60
|
+
const entityConfig = configs.find(
|
|
61
|
+
( config ) => config.name === name && config.kind === kind
|
|
62
|
+
);
|
|
61
63
|
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
|
|
62
64
|
return;
|
|
63
65
|
}
|
|
@@ -75,11 +77,13 @@ export const getEntityRecord =
|
|
|
75
77
|
// the ID.
|
|
76
78
|
query = {
|
|
77
79
|
...query,
|
|
78
|
-
_fields:
|
|
79
|
-
...
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
_fields: [
|
|
81
|
+
...new Set( [
|
|
82
|
+
...( getNormalizedCommaSeparable( query._fields ) ||
|
|
83
|
+
[] ),
|
|
84
|
+
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
85
|
+
] ),
|
|
86
|
+
].join(),
|
|
83
87
|
};
|
|
84
88
|
}
|
|
85
89
|
|
|
@@ -139,7 +143,9 @@ export const getEntityRecords =
|
|
|
139
143
|
( kind, name, query = {} ) =>
|
|
140
144
|
async ( { dispatch } ) => {
|
|
141
145
|
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
142
|
-
const entityConfig = find(
|
|
146
|
+
const entityConfig = configs.find(
|
|
147
|
+
( config ) => config.name === name && config.kind === kind
|
|
148
|
+
);
|
|
143
149
|
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
|
|
144
150
|
return;
|
|
145
151
|
}
|
|
@@ -157,11 +163,13 @@ export const getEntityRecords =
|
|
|
157
163
|
// the ID.
|
|
158
164
|
query = {
|
|
159
165
|
...query,
|
|
160
|
-
_fields:
|
|
161
|
-
...
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
166
|
+
_fields: [
|
|
167
|
+
...new Set( [
|
|
168
|
+
...( getNormalizedCommaSeparable( query._fields ) ||
|
|
169
|
+
[] ),
|
|
170
|
+
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
171
|
+
] ),
|
|
172
|
+
].join(),
|
|
165
173
|
};
|
|
166
174
|
}
|
|
167
175
|
|
|
@@ -265,34 +273,42 @@ export const getEmbedPreview =
|
|
|
265
273
|
* Checks whether the current user can perform the given action on the given
|
|
266
274
|
* REST resource.
|
|
267
275
|
*
|
|
268
|
-
* @param {string}
|
|
269
|
-
*
|
|
270
|
-
* @param {string} resource
|
|
271
|
-
* @param {?string} id
|
|
276
|
+
* @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
|
|
277
|
+
* 'delete'.
|
|
278
|
+
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
|
|
279
|
+
* @param {?string} id ID of the rest resource to check.
|
|
272
280
|
*/
|
|
273
281
|
export const canUser =
|
|
274
|
-
(
|
|
275
|
-
async ( { dispatch } ) => {
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
delete: 'DELETE',
|
|
281
|
-
};
|
|
282
|
+
( requestedAction, resource, id ) =>
|
|
283
|
+
async ( { dispatch, registry } ) => {
|
|
284
|
+
const { hasStartedResolution } = registry.select( STORE_NAME );
|
|
285
|
+
|
|
286
|
+
const resourcePath = id ? `${ resource }/${ id }` : resource;
|
|
287
|
+
const retrievedActions = [ 'create', 'read', 'update', 'delete' ];
|
|
282
288
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
throw new Error( `'${ action }' is not a valid action.` );
|
|
289
|
+
if ( ! retrievedActions.includes( requestedAction ) ) {
|
|
290
|
+
throw new Error( `'${ requestedAction }' is not a valid action.` );
|
|
286
291
|
}
|
|
287
292
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
293
|
+
// Prevent resolving the same resource twice.
|
|
294
|
+
for ( const relatedAction of retrievedActions ) {
|
|
295
|
+
if ( relatedAction === requestedAction ) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const isAlreadyResolving = hasStartedResolution( 'canUser', [
|
|
299
|
+
relatedAction,
|
|
300
|
+
resource,
|
|
301
|
+
id,
|
|
302
|
+
] );
|
|
303
|
+
if ( isAlreadyResolving ) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
291
307
|
|
|
292
308
|
let response;
|
|
293
309
|
try {
|
|
294
310
|
response = await apiFetch( {
|
|
295
|
-
path
|
|
311
|
+
path: `/wp/v2/${ resourcePath }`,
|
|
296
312
|
method: 'OPTIONS',
|
|
297
313
|
parse: false,
|
|
298
314
|
} );
|
|
@@ -306,9 +322,25 @@ export const canUser =
|
|
|
306
322
|
// return the expected result in the native version. Instead, API requests
|
|
307
323
|
// only return the result, without including response properties like the headers.
|
|
308
324
|
const allowHeader = response.headers?.get( 'allow' );
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
325
|
+
const allowedMethods = allowHeader?.allow || allowHeader || '';
|
|
326
|
+
|
|
327
|
+
const permissions = {};
|
|
328
|
+
const methods = {
|
|
329
|
+
create: 'POST',
|
|
330
|
+
read: 'GET',
|
|
331
|
+
update: 'PUT',
|
|
332
|
+
delete: 'DELETE',
|
|
333
|
+
};
|
|
334
|
+
for ( const [ actionName, methodName ] of Object.entries( methods ) ) {
|
|
335
|
+
permissions[ actionName ] = allowedMethods.includes( methodName );
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
for ( const action of retrievedActions ) {
|
|
339
|
+
dispatch.receiveUserPermission(
|
|
340
|
+
`${ action }/${ resourcePath }`,
|
|
341
|
+
permissions[ action ]
|
|
342
|
+
);
|
|
343
|
+
}
|
|
312
344
|
};
|
|
313
345
|
|
|
314
346
|
/**
|
|
@@ -323,7 +355,9 @@ export const canUserEditEntityRecord =
|
|
|
323
355
|
( kind, name, recordId ) =>
|
|
324
356
|
async ( { dispatch } ) => {
|
|
325
357
|
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
326
|
-
const entityConfig = find(
|
|
358
|
+
const entityConfig = configs.find(
|
|
359
|
+
( config ) => config.name === name && config.kind === kind
|
|
360
|
+
);
|
|
327
361
|
if ( ! entityConfig ) {
|
|
328
362
|
return;
|
|
329
363
|
}
|
|
@@ -427,13 +461,9 @@ export const __experimentalGetCurrentGlobalStylesId =
|
|
|
427
461
|
'theme',
|
|
428
462
|
{ status: 'active' }
|
|
429
463
|
);
|
|
430
|
-
const globalStylesURL =
|
|
431
|
-
0
|
|
432
|
-
|
|
433
|
-
'wp:user-global-styles',
|
|
434
|
-
0,
|
|
435
|
-
'href',
|
|
436
|
-
] );
|
|
464
|
+
const globalStylesURL =
|
|
465
|
+
activeThemes?.[ 0 ]?._links?.[ 'wp:user-global-styles' ]?.[ 0 ]
|
|
466
|
+
?.href;
|
|
437
467
|
if ( globalStylesURL ) {
|
|
438
468
|
const globalStylesObject = await apiFetch( {
|
|
439
469
|
url: globalStylesURL,
|
|
@@ -476,8 +506,13 @@ export const getBlockPatterns =
|
|
|
476
506
|
const restPatterns = await apiFetch( {
|
|
477
507
|
path: '/wp/v2/block-patterns/patterns',
|
|
478
508
|
} );
|
|
479
|
-
const patterns = map(
|
|
480
|
-
|
|
509
|
+
const patterns = restPatterns?.map( ( pattern ) =>
|
|
510
|
+
Object.fromEntries(
|
|
511
|
+
Object.entries( pattern ).map( ( [ key, value ] ) => [
|
|
512
|
+
camelCase( key ),
|
|
513
|
+
value,
|
|
514
|
+
] )
|
|
515
|
+
)
|
|
481
516
|
);
|
|
482
517
|
dispatch( { type: 'RECEIVE_BLOCK_PATTERNS', patterns } );
|
|
483
518
|
};
|