@wordpress/core-data 4.11.0 → 4.14.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +124 -81
  3. package/build/actions.js +27 -0
  4. package/build/actions.js.map +1 -1
  5. package/build/batch/create-batch.js +3 -15
  6. package/build/batch/create-batch.js.map +1 -1
  7. package/build/entities.js +7 -5
  8. package/build/entities.js.map +1 -1
  9. package/build/hooks/index.js +14 -0
  10. package/build/hooks/index.js.map +1 -1
  11. package/build/hooks/use-entity-record.js +84 -2
  12. package/build/hooks/use-entity-record.js.map +1 -1
  13. package/build/hooks/use-resource-permissions.js +72 -11
  14. package/build/hooks/use-resource-permissions.js.map +1 -1
  15. package/build/index.js +1 -28
  16. package/build/index.js.map +1 -1
  17. package/build/resolvers.js +22 -24
  18. package/build/resolvers.js.map +1 -1
  19. package/build/selectors.js +5 -3
  20. package/build/selectors.js.map +1 -1
  21. package/build-module/actions.js +27 -0
  22. package/build-module/actions.js.map +1 -1
  23. package/build-module/batch/create-batch.js +3 -14
  24. package/build-module/batch/create-batch.js.map +1 -1
  25. package/build-module/entities.js +7 -6
  26. package/build-module/entities.js.map +1 -1
  27. package/build-module/hooks/index.js +1 -0
  28. package/build-module/hooks/index.js.map +1 -1
  29. package/build-module/hooks/use-entity-record.js +82 -2
  30. package/build-module/hooks/use-entity-record.js.map +1 -1
  31. package/build-module/hooks/use-resource-permissions.js +68 -10
  32. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  33. package/build-module/index.js +0 -3
  34. package/build-module/index.js.map +1 -1
  35. package/build-module/resolvers.js +22 -24
  36. package/build-module/resolvers.js.map +1 -1
  37. package/build-module/selectors.js +6 -4
  38. package/build-module/selectors.js.map +1 -1
  39. package/package.json +12 -11
  40. package/src/actions.js +27 -0
  41. package/src/batch/create-batch.js +3 -12
  42. package/src/entities.ts +8 -6
  43. package/src/entity-types/theme.ts +4 -0
  44. package/src/hooks/index.ts +4 -0
  45. package/src/hooks/test/use-entity-record.js +49 -1
  46. package/src/hooks/test/use-resource-permissions.js +32 -36
  47. package/src/hooks/use-entity-record.ts +107 -2
  48. package/src/hooks/use-resource-permissions.ts +82 -20
  49. package/src/index.js +0 -3
  50. package/src/resolvers.js +41 -36
  51. package/src/selectors.ts +6 -4
package/src/resolvers.js CHANGED
@@ -1,16 +1,7 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- import {
5
- camelCase,
6
- compact,
7
- find,
8
- get,
9
- includes,
10
- map,
11
- mapKeys,
12
- uniq,
13
- } from 'lodash';
4
+ import { camelCase } from 'change-case';
14
5
 
15
6
  /**
16
7
  * WordPress dependencies
@@ -59,13 +50,16 @@ export const getCurrentUser =
59
50
  * @param {string} name Entity name.
60
51
  * @param {number|string} key Record's key
61
52
  * @param {Object|undefined} query Optional object of query parameters to
62
- * include with request.
53
+ * include with request. If requesting specific
54
+ * fields, fields must always include the ID.
63
55
  */
64
56
  export const getEntityRecord =
65
57
  ( kind, name, key = '', query ) =>
66
58
  async ( { select, dispatch } ) => {
67
59
  const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
68
- const entityConfig = find( configs, { kind, name } );
60
+ const entityConfig = configs.find(
61
+ ( config ) => config.name === name && config.kind === kind
62
+ );
69
63
  if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
70
64
  return;
71
65
  }
@@ -83,11 +77,13 @@ export const getEntityRecord =
83
77
  // the ID.
84
78
  query = {
85
79
  ...query,
86
- _fields: uniq( [
87
- ...( getNormalizedCommaSeparable( query._fields ) ||
88
- [] ),
89
- entityConfig.key || DEFAULT_ENTITY_KEY,
90
- ] ).join(),
80
+ _fields: [
81
+ ...new Set( [
82
+ ...( getNormalizedCommaSeparable( query._fields ) ||
83
+ [] ),
84
+ entityConfig.key || DEFAULT_ENTITY_KEY,
85
+ ] ),
86
+ ].join(),
91
87
  };
92
88
  }
93
89
 
@@ -140,13 +136,16 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
140
136
  *
141
137
  * @param {string} kind Entity kind.
142
138
  * @param {string} name Entity name.
143
- * @param {Object?} query Query Object.
139
+ * @param {Object?} query Query Object. If requesting specific fields, fields
140
+ * must always include the ID.
144
141
  */
145
142
  export const getEntityRecords =
146
143
  ( kind, name, query = {} ) =>
147
144
  async ( { dispatch } ) => {
148
145
  const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
149
- const entityConfig = find( configs, { kind, name } );
146
+ const entityConfig = configs.find(
147
+ ( config ) => config.name === name && config.kind === kind
148
+ );
150
149
  if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
151
150
  return;
152
151
  }
@@ -164,11 +163,13 @@ export const getEntityRecords =
164
163
  // the ID.
165
164
  query = {
166
165
  ...query,
167
- _fields: uniq( [
168
- ...( getNormalizedCommaSeparable( query._fields ) ||
169
- [] ),
170
- entityConfig.key || DEFAULT_ENTITY_KEY,
171
- ] ).join(),
166
+ _fields: [
167
+ ...new Set( [
168
+ ...( getNormalizedCommaSeparable( query._fields ) ||
169
+ [] ),
170
+ entityConfig.key || DEFAULT_ENTITY_KEY,
171
+ ] ),
172
+ ].join(),
172
173
  };
173
174
  }
174
175
 
@@ -313,8 +314,9 @@ export const canUser =
313
314
  // return the expected result in the native version. Instead, API requests
314
315
  // only return the result, without including response properties like the headers.
315
316
  const allowHeader = response.headers?.get( 'allow' );
316
- const key = compact( [ action, resource, id ] ).join( '/' );
317
- const isAllowed = includes( allowHeader, method );
317
+ const key = [ action, resource, id ].filter( Boolean ).join( '/' );
318
+ const isAllowed =
319
+ allowHeader?.includes?.( method ) || allowHeader?.allow === method;
318
320
  dispatch.receiveUserPermission( key, isAllowed );
319
321
  };
320
322
 
@@ -330,7 +332,9 @@ export const canUserEditEntityRecord =
330
332
  ( kind, name, recordId ) =>
331
333
  async ( { dispatch } ) => {
332
334
  const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
333
- const entityConfig = find( configs, { kind, name } );
335
+ const entityConfig = configs.find(
336
+ ( config ) => config.name === name && config.kind === kind
337
+ );
334
338
  if ( ! entityConfig ) {
335
339
  return;
336
340
  }
@@ -434,13 +438,9 @@ export const __experimentalGetCurrentGlobalStylesId =
434
438
  'theme',
435
439
  { status: 'active' }
436
440
  );
437
- const globalStylesURL = get( activeThemes, [
438
- 0,
439
- '_links',
440
- 'wp:user-global-styles',
441
- 0,
442
- 'href',
443
- ] );
441
+ const globalStylesURL =
442
+ activeThemes?.[ 0 ]?._links?.[ 'wp:user-global-styles' ]?.[ 0 ]
443
+ ?.href;
444
444
  if ( globalStylesURL ) {
445
445
  const globalStylesObject = await apiFetch( {
446
446
  url: globalStylesURL,
@@ -483,8 +483,13 @@ export const getBlockPatterns =
483
483
  const restPatterns = await apiFetch( {
484
484
  path: '/wp/v2/block-patterns/patterns',
485
485
  } );
486
- const patterns = map( restPatterns, ( pattern ) =>
487
- mapKeys( pattern, ( value, key ) => camelCase( key ) )
486
+ const patterns = restPatterns?.map( ( pattern ) =>
487
+ Object.fromEntries(
488
+ Object.entries( pattern ).map( ( [ key, value ] ) => [
489
+ camelCase( key ),
490
+ value,
491
+ ] )
492
+ )
488
493
  );
489
494
  dispatch( { type: 'RECEIVE_BLOCK_PATTERNS', patterns } );
490
495
  };
package/src/selectors.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * External dependencies
3
3
  */
4
4
  import createSelector from 'rememo';
5
- import { set, map, find, get, filter, compact } from 'lodash';
5
+ import { set, map, find, get, filter } from 'lodash';
6
6
 
7
7
  /**
8
8
  * WordPress dependencies
@@ -296,7 +296,8 @@ interface GetEntityRecord {
296
296
  * @param kind Entity kind.
297
297
  * @param name Entity name.
298
298
  * @param key Record's key
299
- * @param query Optional query.
299
+ * @param query Optional query. If requesting specific
300
+ * fields, fields must always include the ID.
300
301
  *
301
302
  * @return Record.
302
303
  */
@@ -526,7 +527,8 @@ interface GetEntityRecords {
526
527
  * @param state State tree
527
528
  * @param kind Entity kind.
528
529
  * @param name Entity name.
529
- * @param query Optional terms query.
530
+ * @param query Optional terms query. If requesting specific
531
+ * fields, fields must always include the ID.
530
532
  *
531
533
  * @return Records.
532
534
  */
@@ -1115,7 +1117,7 @@ export function canUser(
1115
1117
  resource: string,
1116
1118
  id?: GenericRecordKey
1117
1119
  ): boolean | undefined {
1118
- const key = compact( [ action, resource, id ] ).join( '/' );
1120
+ const key = [ action, resource, id ].filter( Boolean ).join( '/' );
1119
1121
  return get( state, [ 'userPermissions', key ] );
1120
1122
  }
1121
1123