@wordpress/core-data 4.5.0 → 4.8.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 (58) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/README.md +175 -85
  3. package/build/entities.js +49 -46
  4. package/build/entities.js.map +1 -1
  5. package/build/entity-types/{navigation-area.js → entities.js} +1 -1
  6. package/build/entity-types/{navigation-area.js.map → entities.js.map} +0 -0
  7. package/build/hooks/constants.js +0 -2
  8. package/build/hooks/constants.js.map +1 -1
  9. package/build/hooks/index.js +38 -0
  10. package/build/hooks/index.js.map +1 -0
  11. package/build/hooks/use-entity-record.js +22 -8
  12. package/build/hooks/use-entity-record.js.map +1 -1
  13. package/build/hooks/use-entity-records.js +21 -8
  14. package/build/hooks/use-entity-records.js.map +1 -1
  15. package/build/index.js +17 -21
  16. package/build/index.js.map +1 -1
  17. package/build/queried-data/get-query-parts.js +4 -0
  18. package/build/queried-data/get-query-parts.js.map +1 -1
  19. package/build/selectors.js +163 -163
  20. package/build/selectors.js.map +1 -1
  21. package/build-module/entities.js +49 -46
  22. package/build-module/entities.js.map +1 -1
  23. package/build-module/entity-types/entities.js +2 -0
  24. package/build-module/entity-types/{navigation-area.js.map → entities.js.map} +0 -0
  25. package/build-module/hooks/constants.js +0 -1
  26. package/build-module/hooks/constants.js.map +1 -1
  27. package/build-module/hooks/index.js +3 -0
  28. package/build-module/hooks/index.js.map +1 -0
  29. package/build-module/hooks/use-entity-record.js +18 -7
  30. package/build-module/hooks/use-entity-record.js.map +1 -1
  31. package/build-module/hooks/use-entity-records.js +17 -7
  32. package/build-module/hooks/use-entity-records.js.map +1 -1
  33. package/build-module/index.js +2 -3
  34. package/build-module/index.js.map +1 -1
  35. package/build-module/queried-data/get-query-parts.js +4 -0
  36. package/build-module/queried-data/get-query-parts.js.map +1 -1
  37. package/build-module/selectors.js +164 -163
  38. package/build-module/selectors.js.map +1 -1
  39. package/package.json +12 -12
  40. package/src/entities.ts +342 -129
  41. package/src/entity-types/entities.ts +127 -0
  42. package/src/entity-types/index.ts +1 -3
  43. package/src/hooks/constants.ts +1 -2
  44. package/src/hooks/index.ts +8 -0
  45. package/src/hooks/test/use-entity-record.js +1 -0
  46. package/src/hooks/test/use-entity-records.js +1 -0
  47. package/src/hooks/test/use-query-select.js +1 -0
  48. package/src/hooks/use-entity-record.ts +32 -9
  49. package/src/hooks/use-entity-records.ts +28 -30
  50. package/src/index.js +2 -3
  51. package/src/queried-data/get-query-parts.js +3 -0
  52. package/src/queried-data/test/get-query-parts.js +4 -0
  53. package/src/selectors.ts +406 -216
  54. package/src/test/actions.js +30 -14
  55. package/src/test/entities.js +5 -0
  56. package/src/test/resolvers.js +11 -0
  57. package/build-module/entity-types/navigation-area.js +0 -2
  58. package/src/entity-types/navigation-area.ts +0 -29
@@ -19,6 +19,7 @@ import useEntityRecord from '../use-entity-record';
19
19
 
20
20
  describe( 'useEntityRecord', () => {
21
21
  let registry;
22
+
22
23
  beforeEach( () => {
23
24
  jest.useFakeTimers();
24
25
 
@@ -19,6 +19,7 @@ import useEntityRecords from '../use-entity-records';
19
19
 
20
20
  describe( 'useEntityRecords', () => {
21
21
  let registry;
22
+
22
23
  beforeEach( () => {
23
24
  jest.useFakeTimers();
24
25
 
@@ -19,6 +19,7 @@ import useQuerySelect from '../use-query-select';
19
19
 
20
20
  describe( 'useQuerySelect', () => {
21
21
  let registry;
22
+
22
23
  beforeEach( () => {
23
24
  jest.useFakeTimers();
24
25
 
@@ -1,11 +1,17 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import deprecated from '@wordpress/deprecated';
5
+
1
6
  /**
2
7
  * Internal dependencies
3
8
  */
4
9
  import useQuerySelect from './use-query-select';
5
10
  import { store as coreStore } from '../';
6
11
  import type { Status } from './constants';
12
+ import useEntityRecords from './use-entity-records';
7
13
 
8
- interface EntityRecordResolution< RecordType > {
14
+ export interface EntityRecordResolution< RecordType > {
9
15
  /** The requested entity record */
10
16
  record: RecordType | null;
11
17
 
@@ -23,18 +29,22 @@ interface EntityRecordResolution< RecordType > {
23
29
  status: Status;
24
30
  }
25
31
 
26
- interface Options {
32
+ export interface Options {
33
+ /**
34
+ * Whether to run the query or short-circuit and return null.
35
+ *
36
+ * @default true
37
+ */
27
38
  enabled: boolean;
28
39
  }
29
40
 
30
41
  /**
31
42
  * Resolves the specified entity record.
32
43
  *
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.
44
+ * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
45
+ * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
46
+ * @param recordId ID of the requested entity record.
47
+ * @param options Optional hook options.
38
48
  * @example
39
49
  * ```js
40
50
  * import { useEntityRecord } from '@wordpress/core-data';
@@ -57,10 +67,10 @@ interface Options {
57
67
  * application, the page and the resolution details will be retrieved from
58
68
  * the store state using `getEntityRecord()`, or resolved if missing.
59
69
  *
60
- * @return {EntityRecordResolution<RecordType>} Entity record data.
70
+ * @return Entity record data.
61
71
  * @template RecordType
62
72
  */
63
- export default function __experimentalUseEntityRecord< RecordType >(
73
+ export default function useEntityRecord< RecordType >(
64
74
  kind: string,
65
75
  name: string,
66
76
  recordId: string | number,
@@ -81,3 +91,16 @@ export default function __experimentalUseEntityRecord< RecordType >(
81
91
  ...rest,
82
92
  };
83
93
  }
94
+
95
+ export function __experimentalUseEntityRecord(
96
+ kind: string,
97
+ name: string,
98
+ recordId: any,
99
+ options: any
100
+ ) {
101
+ deprecated( `wp.data.__experimentalUseEntityRecord`, {
102
+ alternative: 'wp.data.useEntityRecord',
103
+ since: '6.1',
104
+ } );
105
+ return useEntityRecord( kind, name, recordId, options );
106
+ }
@@ -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
+ }
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';
@@ -80,6 +80,9 @@ export function getQueryParts( query ) {
80
80
 
81
81
  // Two requests with different include values cannot have same results.
82
82
  if ( key === 'include' ) {
83
+ if ( typeof value === 'number' ) {
84
+ value = value.toString();
85
+ }
83
86
  parts.include = (
84
87
  getNormalizedCommaSeparable( value ) ?? []
85
88
  ).map( Number );
@@ -18,8 +18,12 @@ describe( 'getQueryParts', () => {
18
18
  } );
19
19
 
20
20
  it( 'parses out `include` ID filtering', () => {
21
+ const first = getQueryParts( { include: '1' } );
22
+ const second = getQueryParts( { include: 1 } );
21
23
  const parts = getQueryParts( { include: [ 1 ] } );
22
24
 
25
+ expect( first ).toEqual( second );
26
+ expect( second ).toEqual( parts );
23
27
  expect( parts ).toEqual( {
24
28
  context: 'default',
25
29
  page: 1,