@wordpress/core-data 4.6.0 → 4.9.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 (98) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/README.md +209 -119
  3. package/build/actions.js.map +1 -1
  4. package/build/batch/create-batch.js +1 -1
  5. package/build/batch/create-batch.js.map +1 -1
  6. package/build/entities.js +49 -35
  7. package/build/entities.js.map +1 -1
  8. package/build/entity-provider.js.map +1 -1
  9. package/build/entity-types/entities.js +6 -0
  10. package/build/entity-types/entities.js.map +1 -0
  11. package/build/hooks/constants.js +0 -2
  12. package/build/hooks/constants.js.map +1 -1
  13. package/build/hooks/index.js +38 -0
  14. package/build/hooks/index.js.map +1 -0
  15. package/build/hooks/use-entity-record.js +22 -8
  16. package/build/hooks/use-entity-record.js.map +1 -1
  17. package/build/hooks/use-entity-records.js +21 -8
  18. package/build/hooks/use-entity-records.js.map +1 -1
  19. package/build/hooks/use-query-select.js.map +1 -1
  20. package/build/index.js +17 -21
  21. package/build/index.js.map +1 -1
  22. package/build/queried-data/selectors.js.map +1 -1
  23. package/build/resolvers.js.map +1 -1
  24. package/build/selectors.js +206 -169
  25. package/build/selectors.js.map +1 -1
  26. package/build/utils/forward-resolver.js.map +1 -1
  27. package/build/utils/on-sub-key.js.map +1 -1
  28. package/build/utils/with-weak-map-cache.js +1 -7
  29. package/build/utils/with-weak-map-cache.js.map +1 -1
  30. package/build-module/actions.js.map +1 -1
  31. package/build-module/batch/create-batch.js +2 -2
  32. package/build-module/batch/create-batch.js.map +1 -1
  33. package/build-module/entities.js +49 -35
  34. package/build-module/entities.js.map +1 -1
  35. package/build-module/entity-provider.js.map +1 -1
  36. package/build-module/entity-types/entities.js +2 -0
  37. package/build-module/entity-types/entities.js.map +1 -0
  38. package/build-module/hooks/constants.js +0 -1
  39. package/build-module/hooks/constants.js.map +1 -1
  40. package/build-module/hooks/index.js +3 -0
  41. package/build-module/hooks/index.js.map +1 -0
  42. package/build-module/hooks/use-entity-record.js +18 -7
  43. package/build-module/hooks/use-entity-record.js.map +1 -1
  44. package/build-module/hooks/use-entity-records.js +17 -7
  45. package/build-module/hooks/use-entity-records.js.map +1 -1
  46. package/build-module/hooks/use-query-select.js.map +1 -1
  47. package/build-module/index.js +2 -3
  48. package/build-module/index.js.map +1 -1
  49. package/build-module/queried-data/selectors.js.map +1 -1
  50. package/build-module/resolvers.js.map +1 -1
  51. package/build-module/selectors.js +203 -166
  52. package/build-module/selectors.js.map +1 -1
  53. package/build-module/utils/forward-resolver.js.map +1 -1
  54. package/build-module/utils/on-sub-key.js.map +1 -1
  55. package/build-module/utils/with-weak-map-cache.js +1 -6
  56. package/build-module/utils/with-weak-map-cache.js.map +1 -1
  57. package/package.json +12 -12
  58. package/src/actions.js +389 -372
  59. package/src/batch/create-batch.js +2 -2
  60. package/src/entities.ts +357 -135
  61. package/src/entity-provider.js +4 -6
  62. package/src/entity-types/attachment.ts +4 -3
  63. package/src/entity-types/comment.ts +4 -3
  64. package/src/entity-types/entities.ts +130 -0
  65. package/src/entity-types/index.ts +115 -20
  66. package/src/entity-types/menu-location.ts +4 -3
  67. package/src/entity-types/nav-menu-item.ts +4 -3
  68. package/src/entity-types/nav-menu.ts +3 -3
  69. package/src/entity-types/page.ts +3 -3
  70. package/src/entity-types/plugin.ts +3 -3
  71. package/src/entity-types/post.ts +3 -3
  72. package/src/entity-types/settings.ts +3 -3
  73. package/src/entity-types/sidebar.ts +4 -3
  74. package/src/entity-types/taxonomy.ts +4 -3
  75. package/src/entity-types/theme.ts +3 -3
  76. package/src/entity-types/type.ts +3 -3
  77. package/src/entity-types/user.ts +3 -3
  78. package/src/entity-types/widget-type.ts +4 -3
  79. package/src/entity-types/widget.ts +3 -3
  80. package/src/entity-types/wp-template-part.ts +4 -3
  81. package/src/entity-types/wp-template.ts +4 -3
  82. package/src/fetch/test/__experimental-fetch-link-suggestions.js +2 -4
  83. package/src/hooks/constants.ts +1 -2
  84. package/src/hooks/index.ts +8 -0
  85. package/src/hooks/test/use-query-select.js +4 -2
  86. package/src/hooks/use-entity-record.ts +31 -9
  87. package/src/hooks/use-entity-records.ts +28 -30
  88. package/src/hooks/use-query-select.ts +26 -24
  89. package/src/index.js +2 -3
  90. package/src/locks/test/selectors.js +2 -1
  91. package/src/queried-data/selectors.js +2 -8
  92. package/src/resolvers.js +344 -325
  93. package/src/selectors.ts +639 -296
  94. package/src/test/resolvers.js +1 -3
  95. package/src/test/selectors.js +1 -2
  96. package/src/utils/forward-resolver.js +6 -5
  97. package/src/utils/on-sub-key.js +20 -20
  98. package/src/utils/with-weak-map-cache.js +1 -6
@@ -1,3 +1,8 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import deprecated from '@wordpress/deprecated';
5
+
1
6
  /**
2
7
  * Internal dependencies
3
8
  */
@@ -5,7 +10,7 @@ import useQuerySelect from './use-query-select';
5
10
  import { store as coreStore } from '../';
6
11
  import type { Status } from './constants';
7
12
 
8
- interface EntityRecordResolution< RecordType > {
13
+ export interface EntityRecordResolution< RecordType > {
9
14
  /** The requested entity record */
10
15
  record: RecordType | null;
11
16
 
@@ -23,18 +28,22 @@ interface EntityRecordResolution< RecordType > {
23
28
  status: Status;
24
29
  }
25
30
 
26
- interface Options {
31
+ export interface Options {
32
+ /**
33
+ * Whether to run the query or short-circuit and return null.
34
+ *
35
+ * @default true
36
+ */
27
37
  enabled: boolean;
28
38
  }
29
39
 
30
40
  /**
31
41
  * Resolves the specified entity record.
32
42
  *
33
- * @param kind Kind of the requested entity.
34
- * @param name Name of the requested entity.
35
- * @param recordId Record ID of the requested entity.
36
- * @param options Hook options.
37
- * @param [options.enabled=true] Whether to run the query or short-circuit and return null. Defaults to true.
43
+ * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
44
+ * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
45
+ * @param recordId ID of the requested entity record.
46
+ * @param options Optional hook options.
38
47
  * @example
39
48
  * ```js
40
49
  * import { useEntityRecord } from '@wordpress/core-data';
@@ -57,10 +66,10 @@ interface Options {
57
66
  * application, the page and the resolution details will be retrieved from
58
67
  * the store state using `getEntityRecord()`, or resolved if missing.
59
68
  *
60
- * @return {EntityRecordResolution<RecordType>} Entity record data.
69
+ * @return Entity record data.
61
70
  * @template RecordType
62
71
  */
63
- export default function __experimentalUseEntityRecord< RecordType >(
72
+ export default function useEntityRecord< RecordType >(
64
73
  kind: string,
65
74
  name: string,
66
75
  recordId: string | number,
@@ -81,3 +90,16 @@ export default function __experimentalUseEntityRecord< RecordType >(
81
90
  ...rest,
82
91
  };
83
92
  }
93
+
94
+ export function __experimentalUseEntityRecord(
95
+ kind: string,
96
+ name: string,
97
+ recordId: any,
98
+ options: any
99
+ ) {
100
+ deprecated( `wp.data.__experimentalUseEntityRecord`, {
101
+ alternative: 'wp.data.useEntityRecord',
102
+ since: '6.1',
103
+ } );
104
+ return useEntityRecord( kind, name, recordId, options );
105
+ }
@@ -2,48 +2,32 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { addQueryArgs } from '@wordpress/url';
5
+ import deprecated from '@wordpress/deprecated';
5
6
 
6
7
  /**
7
8
  * Internal dependencies
8
9
  */
9
10
  import useQuerySelect from './use-query-select';
10
11
  import { store as coreStore } from '../';
11
- import type { Status } from './constants';
12
+ import type { Options, EntityRecordResolution } from './use-entity-record';
12
13
 
13
- interface EntityRecordsResolution< RecordType > {
14
+ type EntityRecordsResolution< RecordType > = Omit<
15
+ EntityRecordResolution< RecordType >,
16
+ 'record'
17
+ > & {
14
18
  /** The requested entity record */
15
19
  records: RecordType[] | null;
20
+ };
16
21
 
17
- /**
18
- * Is the record still being resolved?
19
- */
20
- isResolving: boolean;
21
-
22
- /**
23
- * Is the record resolved by now?
24
- */
25
- hasResolved: boolean;
26
-
27
- /** Resolution status */
28
- status: Status;
29
- }
30
-
31
- interface Options {
32
- /**
33
- * Whether to run the query or short-circuit and return null.
34
- *
35
- * @default true
36
- */
37
- enabled: boolean;
38
- }
22
+ const EMPTY_ARRAY = [];
39
23
 
40
24
  /**
41
25
  * Resolves the specified entity records.
42
26
  *
43
- * @param kind Kind of the requested entities.
44
- * @param name Name of the requested entities.
45
- * @param queryArgs HTTP query for the requested entities.
46
- * @param options Hook options.
27
+ * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
28
+ * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
29
+ * @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.
30
+ * @param options Optional hook options.
47
31
  * @example
48
32
  * ```js
49
33
  * import { useEntityRecord } from '@wordpress/core-data';
@@ -75,7 +59,7 @@ interface Options {
75
59
  * @return Entity records data.
76
60
  * @template RecordType
77
61
  */
78
- export default function __experimentalUseEntityRecords< RecordType >(
62
+ export default function useEntityRecords< RecordType >(
79
63
  kind: string,
80
64
  name: string,
81
65
  queryArgs: Record< string, unknown > = {},
@@ -91,7 +75,8 @@ export default function __experimentalUseEntityRecords< RecordType >(
91
75
  ( query ) => {
92
76
  if ( ! options.enabled ) {
93
77
  return {
94
- data: [],
78
+ // Avoiding returning a new reference on every execution.
79
+ data: EMPTY_ARRAY,
95
80
  };
96
81
  }
97
82
  return query( coreStore ).getEntityRecords( kind, name, queryArgs );
@@ -104,3 +89,16 @@ export default function __experimentalUseEntityRecords< RecordType >(
104
89
  ...rest,
105
90
  };
106
91
  }
92
+
93
+ export function __experimentalUseEntityRecords(
94
+ kind: string,
95
+ name: string,
96
+ queryArgs: any,
97
+ options: any
98
+ ) {
99
+ deprecated( `wp.data.__experimentalUseEntityRecords`, {
100
+ alternative: 'wp.data.useEntityRecords',
101
+ since: '6.1',
102
+ } );
103
+ return useEntityRecords( kind, name, queryArgs, options );
104
+ }
@@ -102,34 +102,36 @@ const enrichSelectors = memoize( ( ( selectors ) => {
102
102
  continue;
103
103
  }
104
104
  Object.defineProperty( resolvers, selectorName, {
105
- get: () => ( ...args: unknown[] ) => {
106
- const { getIsResolving, hasFinishedResolution } = selectors;
107
- const isResolving = !! getIsResolving( selectorName, args );
108
- const hasResolved =
109
- ! isResolving &&
110
- hasFinishedResolution( selectorName, args );
111
- const data = selectors[ selectorName ]( ...args );
105
+ get:
106
+ () =>
107
+ ( ...args: unknown[] ) => {
108
+ const { getIsResolving, hasFinishedResolution } = selectors;
109
+ const isResolving = !! getIsResolving( selectorName, args );
110
+ const hasResolved =
111
+ ! isResolving &&
112
+ hasFinishedResolution( selectorName, args );
113
+ const data = selectors[ selectorName ]( ...args );
112
114
 
113
- let status;
114
- if ( isResolving ) {
115
- status = Status.Resolving;
116
- } else if ( hasResolved ) {
117
- if ( data ) {
118
- status = Status.Success;
115
+ let status;
116
+ if ( isResolving ) {
117
+ status = Status.Resolving;
118
+ } else if ( hasResolved ) {
119
+ if ( data ) {
120
+ status = Status.Success;
121
+ } else {
122
+ status = Status.Error;
123
+ }
119
124
  } else {
120
- status = Status.Error;
125
+ status = Status.Idle;
121
126
  }
122
- } else {
123
- status = Status.Idle;
124
- }
125
127
 
126
- return {
127
- data,
128
- status,
129
- isResolving,
130
- hasResolved,
131
- };
132
- },
128
+ return {
129
+ data,
130
+ status,
131
+ isResolving,
132
+ hasResolved,
133
+ };
134
+ },
133
135
  } );
134
136
  }
135
137
  return resolvers;
package/src/index.js CHANGED
@@ -68,8 +68,7 @@ export const store = createReduxStore( STORE_NAME, storeConfig() );
68
68
  register( store );
69
69
 
70
70
  export { default as EntityProvider } from './entity-provider';
71
- export { default as __experimentalUseEntityRecord } from './hooks/use-entity-record';
72
- export { default as __experimentalUseEntityRecords } from './hooks/use-entity-records';
73
71
  export * from './entity-provider';
74
- export * from './fetch';
75
72
  export * from './entity-types';
73
+ export * from './fetch';
74
+ export * from './hooks';
@@ -186,7 +186,8 @@ describe( 'isLockAvailable', () => {
186
186
  exclusive: true,
187
187
  },
188
188
  ],
189
- children: {},
189
+ children:
190
+ {},
190
191
  },
191
192
  },
192
193
  },
@@ -28,14 +28,8 @@ const queriedItemsCacheByState = new WeakMap();
28
28
  * @return {?Array} Query items.
29
29
  */
30
30
  function getQueriedItemsUncached( state, query ) {
31
- const {
32
- stableKey,
33
- page,
34
- perPage,
35
- include,
36
- fields,
37
- context,
38
- } = getQueryParts( query );
31
+ const { stableKey, page, perPage, include, fields, context } =
32
+ getQueryParts( query );
39
33
  let itemIds;
40
34
 
41
35
  if ( state.queries?.[ context ]?.[ stableKey ] ) {