@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.
Files changed (202) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +148 -65
  3. package/build/entities.js +38 -51
  4. package/build/entities.js.map +1 -1
  5. package/build/hooks/index.js +14 -0
  6. package/build/hooks/index.js.map +1 -1
  7. package/build/hooks/use-entity-record.js +10 -6
  8. package/build/hooks/use-entity-record.js.map +1 -1
  9. package/build/hooks/use-entity-records.js +2 -0
  10. package/build/hooks/use-entity-records.js.map +1 -1
  11. package/build/hooks/use-query-select.js +5 -2
  12. package/build/hooks/use-query-select.js.map +1 -1
  13. package/build/hooks/use-resource-permissions.js +74 -11
  14. package/build/hooks/use-resource-permissions.js.map +1 -1
  15. package/build/index.js +1 -30
  16. package/build/index.js.map +1 -1
  17. package/build/resolvers.js +59 -39
  18. package/build/resolvers.js.map +1 -1
  19. package/build/selectors.js +9 -46
  20. package/build/selectors.js.map +1 -1
  21. package/build-module/entities.js +38 -52
  22. package/build-module/entities.js.map +1 -1
  23. package/build-module/hooks/index.js +1 -0
  24. package/build-module/hooks/index.js.map +1 -1
  25. package/build-module/hooks/use-entity-record.js +10 -6
  26. package/build-module/hooks/use-entity-record.js.map +1 -1
  27. package/build-module/hooks/use-entity-records.js +2 -0
  28. package/build-module/hooks/use-entity-records.js.map +1 -1
  29. package/build-module/hooks/use-query-select.js +4 -1
  30. package/build-module/hooks/use-query-select.js.map +1 -1
  31. package/build-module/hooks/use-resource-permissions.js +70 -10
  32. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  33. package/build-module/index.js +0 -5
  34. package/build-module/index.js.map +1 -1
  35. package/build-module/resolvers.js +59 -39
  36. package/build-module/resolvers.js.map +1 -1
  37. package/build-module/selectors.js +7 -44
  38. package/build-module/selectors.js.map +1 -1
  39. package/build-types/actions.d.ts +188 -0
  40. package/build-types/actions.d.ts.map +1 -0
  41. package/build-types/batch/create-batch.d.ts +71 -0
  42. package/build-types/batch/create-batch.d.ts.map +1 -0
  43. package/build-types/batch/default-processor.d.ts +11 -0
  44. package/build-types/batch/default-processor.d.ts.map +1 -0
  45. package/build-types/batch/index.d.ts +3 -0
  46. package/build-types/batch/index.d.ts.map +1 -0
  47. package/build-types/entities.d.ts +128 -0
  48. package/build-types/entities.d.ts.map +1 -0
  49. package/build-types/entity-provider.d.ts +68 -0
  50. package/build-types/entity-provider.d.ts.map +1 -0
  51. package/build-types/entity-types/attachment.d.ts +121 -0
  52. package/build-types/entity-types/attachment.d.ts.map +1 -0
  53. package/build-types/entity-types/base-entity-records.d.ts +37 -0
  54. package/build-types/entity-types/base-entity-records.d.ts.map +1 -0
  55. package/build-types/entity-types/comment.d.ts +82 -0
  56. package/build-types/entity-types/comment.d.ts.map +1 -0
  57. package/build-types/entity-types/helpers.d.ts +123 -0
  58. package/build-types/entity-types/helpers.d.ts.map +1 -0
  59. package/build-types/entity-types/index.d.ts +64 -0
  60. package/build-types/entity-types/index.d.ts.map +1 -0
  61. package/build-types/entity-types/menu-location.d.ts +25 -0
  62. package/build-types/entity-types/menu-location.d.ts.map +1 -0
  63. package/build-types/entity-types/nav-menu-item.d.ts +88 -0
  64. package/build-types/entity-types/nav-menu-item.d.ts.map +1 -0
  65. package/build-types/entity-types/nav-menu.d.ts +45 -0
  66. package/build-types/entity-types/nav-menu.d.ts.map +1 -0
  67. package/build-types/entity-types/page.d.ts +120 -0
  68. package/build-types/entity-types/page.d.ts.map +1 -0
  69. package/build-types/entity-types/plugin.d.ts +62 -0
  70. package/build-types/entity-types/plugin.d.ts.map +1 -0
  71. package/build-types/entity-types/post.d.ts +128 -0
  72. package/build-types/entity-types/post.d.ts.map +1 -0
  73. package/build-types/entity-types/settings.d.ts +89 -0
  74. package/build-types/entity-types/settings.d.ts.map +1 -0
  75. package/build-types/entity-types/sidebar.d.ts +55 -0
  76. package/build-types/entity-types/sidebar.d.ts.map +1 -0
  77. package/build-types/entity-types/taxonomy.d.ts +83 -0
  78. package/build-types/entity-types/taxonomy.d.ts.map +1 -0
  79. package/build-types/entity-types/theme.d.ts +206 -0
  80. package/build-types/entity-types/theme.d.ts.map +1 -0
  81. package/build-types/entity-types/type.d.ts +71 -0
  82. package/build-types/entity-types/type.d.ts.map +1 -0
  83. package/build-types/entity-types/user.d.ts +93 -0
  84. package/build-types/entity-types/user.d.ts.map +1 -0
  85. package/build-types/entity-types/widget-type.d.ts +33 -0
  86. package/build-types/entity-types/widget-type.d.ts.map +1 -0
  87. package/build-types/entity-types/widget.d.ts +59 -0
  88. package/build-types/entity-types/widget.d.ts.map +1 -0
  89. package/build-types/entity-types/wp-template-part.d.ts +80 -0
  90. package/build-types/entity-types/wp-template-part.d.ts.map +1 -0
  91. package/build-types/entity-types/wp-template.d.ts +80 -0
  92. package/build-types/entity-types/wp-template.d.ts.map +1 -0
  93. package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts +139 -0
  94. package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts.map +1 -0
  95. package/build-types/fetch/__experimental-fetch-url-data.d.ts +35 -0
  96. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -0
  97. package/build-types/fetch/index.d.ts +3 -0
  98. package/build-types/fetch/index.d.ts.map +1 -0
  99. package/build-types/hooks/constants.d.ts +7 -0
  100. package/build-types/hooks/constants.d.ts.map +1 -0
  101. package/build-types/hooks/index.d.ts +4 -0
  102. package/build-types/hooks/index.d.ts.map +1 -0
  103. package/build-types/hooks/memoize.d.ts +3 -0
  104. package/build-types/hooks/memoize.d.ts.map +1 -0
  105. package/build-types/hooks/use-entity-record.d.ts +124 -0
  106. package/build-types/hooks/use-entity-record.d.ts.map +1 -0
  107. package/build-types/hooks/use-entity-records.d.ts +49 -0
  108. package/build-types/hooks/use-entity-records.d.ts.map +1 -0
  109. package/build-types/hooks/use-query-select.d.ts +46 -0
  110. package/build-types/hooks/use-query-select.d.ts.map +1 -0
  111. package/build-types/hooks/use-resource-permissions.d.ts +99 -0
  112. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -0
  113. package/build-types/index.d.ts +131 -0
  114. package/build-types/index.d.ts.map +1 -0
  115. package/build-types/locks/actions.d.ts +7 -0
  116. package/build-types/locks/actions.d.ts.map +1 -0
  117. package/build-types/locks/engine.d.ts +5 -0
  118. package/build-types/locks/engine.d.ts.map +1 -0
  119. package/build-types/locks/reducer.d.ts +10 -0
  120. package/build-types/locks/reducer.d.ts.map +1 -0
  121. package/build-types/locks/selectors.d.ts +5 -0
  122. package/build-types/locks/selectors.d.ts.map +1 -0
  123. package/build-types/locks/utils.d.ts +8 -0
  124. package/build-types/locks/utils.d.ts.map +1 -0
  125. package/build-types/name.d.ts +8 -0
  126. package/build-types/name.d.ts.map +1 -0
  127. package/build-types/queried-data/actions.d.ts +32 -0
  128. package/build-types/queried-data/actions.d.ts.map +1 -0
  129. package/build-types/queried-data/get-query-parts.d.ts +60 -0
  130. package/build-types/queried-data/get-query-parts.d.ts.map +1 -0
  131. package/build-types/queried-data/index.d.ts +4 -0
  132. package/build-types/queried-data/index.d.ts.map +1 -0
  133. package/build-types/queried-data/reducer.d.ts +54 -0
  134. package/build-types/queried-data/reducer.d.ts.map +1 -0
  135. package/build-types/queried-data/selectors.d.ts +16 -0
  136. package/build-types/queried-data/selectors.d.ts.map +1 -0
  137. package/build-types/reducer.d.ts +156 -0
  138. package/build-types/reducer.d.ts.map +1 -0
  139. package/build-types/resolvers.d.ts +75 -0
  140. package/build-types/resolvers.d.ts.map +1 -0
  141. package/build-types/selectors.d.ts +524 -0
  142. package/build-types/selectors.d.ts.map +1 -0
  143. package/build-types/types.d.ts +4 -0
  144. package/build-types/types.d.ts.map +1 -0
  145. package/build-types/utils/conservative-map-item.d.ts +12 -0
  146. package/build-types/utils/conservative-map-item.d.ts.map +1 -0
  147. package/build-types/utils/forward-resolver.d.ts +10 -0
  148. package/build-types/utils/forward-resolver.d.ts.map +1 -0
  149. package/build-types/utils/get-normalized-comma-separable.d.ts +12 -0
  150. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -0
  151. package/build-types/utils/if-matching-action.d.ts +14 -0
  152. package/build-types/utils/if-matching-action.d.ts.map +1 -0
  153. package/build-types/utils/index.d.ts +9 -0
  154. package/build-types/utils/index.d.ts.map +1 -0
  155. package/build-types/utils/is-raw-attribute.d.ts +10 -0
  156. package/build-types/utils/is-raw-attribute.d.ts.map +1 -0
  157. package/build-types/utils/on-sub-key.d.ts +4 -0
  158. package/build-types/utils/on-sub-key.d.ts.map +1 -0
  159. package/build-types/utils/replace-action.d.ts +13 -0
  160. package/build-types/utils/replace-action.d.ts.map +1 -0
  161. package/build-types/utils/with-weak-map-cache.d.ts +12 -0
  162. package/build-types/utils/with-weak-map-cache.d.ts.map +1 -0
  163. package/package.json +13 -11
  164. package/src/entities.js +325 -0
  165. package/src/entity-types/attachment.ts +3 -4
  166. package/src/entity-types/comment.ts +3 -4
  167. package/src/entity-types/index.ts +31 -88
  168. package/src/entity-types/menu-location.ts +3 -4
  169. package/src/entity-types/nav-menu-item.ts +3 -4
  170. package/src/entity-types/nav-menu.ts +3 -3
  171. package/src/entity-types/page.ts +3 -3
  172. package/src/entity-types/plugin.ts +3 -3
  173. package/src/entity-types/post.ts +3 -3
  174. package/src/entity-types/settings.ts +3 -3
  175. package/src/entity-types/sidebar.ts +3 -4
  176. package/src/entity-types/taxonomy.ts +3 -4
  177. package/src/entity-types/theme.ts +7 -3
  178. package/src/entity-types/type.ts +3 -3
  179. package/src/entity-types/user.ts +3 -3
  180. package/src/entity-types/widget-type.ts +3 -4
  181. package/src/entity-types/widget.ts +3 -3
  182. package/src/entity-types/wp-template-part.ts +3 -4
  183. package/src/entity-types/wp-template.ts +3 -4
  184. package/src/hooks/index.ts +4 -0
  185. package/src/hooks/test/use-entity-record.js +41 -1
  186. package/src/hooks/test/use-resource-permissions.js +32 -36
  187. package/src/hooks/use-entity-record.ts +18 -6
  188. package/src/hooks/use-entity-records.ts +2 -0
  189. package/src/hooks/use-query-select.ts +4 -1
  190. package/src/hooks/use-resource-permissions.ts +84 -20
  191. package/src/index.js +0 -5
  192. package/src/resolvers.js +80 -45
  193. package/src/selectors.ts +202 -341
  194. package/src/test/resolvers.js +118 -4
  195. package/tsconfig.json +21 -0
  196. package/tsconfig.tsbuildinfo +1 -0
  197. package/build/entity-types/entities.js +0 -6
  198. package/build/entity-types/entities.js.map +0 -1
  199. package/build-module/entity-types/entities.js +0 -2
  200. package/build-module/entity-types/entities.js.map +0 -1
  201. package/src/entities.ts +0 -548
  202. 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, find, get, includes, map, mapKeys, uniq } from 'lodash';
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( configs, { kind, name } );
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: uniq( [
79
- ...( getNormalizedCommaSeparable( query._fields ) ||
80
- [] ),
81
- entityConfig.key || DEFAULT_ENTITY_KEY,
82
- ] ).join(),
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( configs, { kind, name } );
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: uniq( [
161
- ...( getNormalizedCommaSeparable( query._fields ) ||
162
- [] ),
163
- entityConfig.key || DEFAULT_ENTITY_KEY,
164
- ] ).join(),
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} action Action to check. One of: 'create', 'read', 'update',
269
- * 'delete'.
270
- * @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
271
- * @param {?string} id ID of the rest resource to check.
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
- ( action, resource, id ) =>
275
- async ( { dispatch } ) => {
276
- const methods = {
277
- create: 'POST',
278
- read: 'GET',
279
- update: 'PUT',
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
- const method = methods[ action ];
284
- if ( ! method ) {
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
- const path = id
289
- ? `/wp/v2/${ resource }/${ id }`
290
- : `/wp/v2/${ resource }`;
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 key = [ action, resource, id ].filter( Boolean ).join( '/' );
310
- const isAllowed = includes( allowHeader, method );
311
- dispatch.receiveUserPermission( key, isAllowed );
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( configs, { kind, name } );
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 = get( activeThemes, [
431
- 0,
432
- '_links',
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( restPatterns, ( pattern ) =>
480
- mapKeys( pattern, ( value, key ) => camelCase( key ) )
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
  };