@wordpress/core-data 4.2.0-next.e230fbab09.0 → 4.3.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 +7 -0
- package/README.md +41 -8
- package/build/actions.js +61 -41
- package/build/actions.js.map +1 -1
- package/build/entities.js +36 -23
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js +46 -44
- package/build/entity-provider.js.map +1 -1
- package/build/entity-types/{base-entity-types.js → base-entity-records.js} +9 -9
- package/build/entity-types/base-entity-records.js.map +1 -0
- package/build/fetch/__experimental-fetch-link-suggestions.js +1 -1
- package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build/fetch/__experimental-fetch-url-data.js +1 -1
- package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build/hooks/use-entity-record.js +15 -5
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +17 -5
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/index.js +9 -21
- package/build/index.js.map +1 -1
- package/build/queried-data/actions.js +4 -4
- package/build/queried-data/actions.js.map +1 -1
- package/build/queried-data/get-query-parts.js +7 -3
- package/build/queried-data/get-query-parts.js.map +1 -1
- package/build/queried-data/reducer.js +8 -6
- package/build/queried-data/reducer.js.map +1 -1
- package/build/reducer.js +16 -16
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +17 -23
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +92 -51
- package/build/selectors.js.map +1 -1
- package/build/utils/conservative-map-item.js +1 -1
- package/build/utils/conservative-map-item.js.map +1 -1
- package/build/utils/is-raw-attribute.js +1 -1
- package/build/utils/is-raw-attribute.js.map +1 -1
- package/build-module/actions.js +62 -42
- package/build-module/actions.js.map +1 -1
- package/build-module/entities.js +32 -19
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js +47 -45
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/entity-types/{base-entity-types.js → base-entity-records.js} +7 -7
- package/build-module/entity-types/base-entity-records.js.map +1 -0
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +1 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +15 -5
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +17 -5
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/index.js +10 -22
- package/build-module/index.js.map +1 -1
- package/build-module/queried-data/actions.js +4 -4
- package/build-module/queried-data/actions.js.map +1 -1
- package/build-module/queried-data/get-query-parts.js +7 -3
- package/build-module/queried-data/get-query-parts.js.map +1 -1
- package/build-module/queried-data/reducer.js +8 -6
- package/build-module/queried-data/reducer.js.map +1 -1
- package/build-module/reducer.js +17 -17
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +18 -24
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +88 -51
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/conservative-map-item.js +1 -1
- package/build-module/utils/conservative-map-item.js.map +1 -1
- package/build-module/utils/is-raw-attribute.js +1 -1
- package/build-module/utils/is-raw-attribute.js.map +1 -1
- package/package.json +11 -11
- package/src/actions.js +62 -42
- package/src/entities.js +43 -17
- package/src/entity-provider.js +50 -44
- package/src/entity-types/attachment.ts +5 -5
- package/src/entity-types/{base-entity-types.ts → base-entity-records.ts} +5 -5
- package/src/entity-types/comment.ts +5 -5
- package/src/entity-types/helpers.ts +1 -1
- package/src/entity-types/index.ts +4 -4
- package/src/entity-types/menu-location.ts +5 -5
- package/src/entity-types/nav-menu-item.ts +10 -5
- package/src/entity-types/nav-menu.ts +5 -5
- package/src/entity-types/navigation-area.ts +5 -5
- package/src/entity-types/page.ts +5 -5
- package/src/entity-types/plugin.ts +10 -5
- package/src/entity-types/post.ts +5 -5
- package/src/entity-types/settings.ts +10 -5
- package/src/entity-types/sidebar.ts +6 -7
- package/src/entity-types/taxonomy.ts +5 -5
- package/src/entity-types/theme.ts +5 -5
- package/src/entity-types/type.ts +5 -5
- package/src/entity-types/user.ts +10 -5
- package/src/entity-types/widget-type.ts +5 -5
- package/src/entity-types/widget.ts +5 -5
- package/src/entity-types/wp-template-part.ts +5 -5
- package/src/entity-types/wp-template.ts +5 -5
- package/src/fetch/__experimental-fetch-link-suggestions.js +1 -1
- package/src/fetch/__experimental-fetch-url-data.js +1 -0
- package/src/hooks/use-entity-record.ts +19 -8
- package/src/hooks/use-entity-records.ts +26 -9
- package/src/index.js +10 -15
- package/src/locks/test/selectors.js +4 -4
- package/src/queried-data/actions.js +4 -4
- package/src/queried-data/get-query-parts.js +5 -5
- package/src/queried-data/reducer.js +6 -6
- package/src/reducer.js +17 -17
- package/src/resolvers.js +25 -30
- package/src/selectors.js +92 -53
- package/src/test/actions.js +138 -33
- package/src/test/entities.js +11 -11
- package/src/test/reducer.js +4 -4
- package/src/test/resolvers.js +5 -5
- package/src/test/selectors.js +22 -22
- package/src/utils/conservative-map-item.js +1 -1
- package/src/utils/is-raw-attribute.js +1 -1
- package/src/utils/test/is-raw-attribute.js +4 -4
- package/build/entity-types/base-entity-types.js.map +0 -1
- package/build-module/entity-types/base-entity-types.js.map +0 -1
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import type {
|
|
5
|
+
CommentingStatus,
|
|
6
|
+
Context,
|
|
7
|
+
OmitNevers,
|
|
8
|
+
PingStatus,
|
|
9
|
+
} from './helpers';
|
|
5
10
|
|
|
6
|
-
import {
|
|
11
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
12
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
13
|
+
declare module './base-entity-records' {
|
|
14
|
+
export namespace BaseEntityRecords {
|
|
10
15
|
export interface Settings< C extends Context > {
|
|
11
16
|
/**
|
|
12
17
|
* What to show on the front page
|
|
@@ -89,5 +94,5 @@ declare module './base-entity-types' {
|
|
|
89
94
|
}
|
|
90
95
|
|
|
91
96
|
export type Settings< C extends Context > = OmitNevers<
|
|
92
|
-
|
|
97
|
+
_BaseEntityRecords.Settings< C >
|
|
93
98
|
>;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
import { Context, OmitNevers } from './helpers';
|
|
4
|
+
import type { Context, OmitNevers } from './helpers';
|
|
6
5
|
|
|
7
|
-
import {
|
|
6
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
8
7
|
|
|
9
|
-
declare module './base-entity-
|
|
10
|
-
export namespace
|
|
8
|
+
declare module './base-entity-records' {
|
|
9
|
+
export namespace BaseEntityRecords {
|
|
11
10
|
export interface Sidebar< C extends Context > {
|
|
12
11
|
/**
|
|
13
12
|
* ID of sidebar.
|
|
@@ -48,7 +47,7 @@ declare module './base-entity-types' {
|
|
|
48
47
|
/**
|
|
49
48
|
* Nested widgets.
|
|
50
49
|
*/
|
|
51
|
-
widgets: ( Widget< C > | string )[];
|
|
50
|
+
widgets: ( BaseEntityRecords.Widget< C > | string )[];
|
|
52
51
|
}
|
|
53
52
|
}
|
|
54
53
|
}
|
|
@@ -56,5 +55,5 @@ declare module './base-entity-types' {
|
|
|
56
55
|
type SidebarStatus = 'active' | 'inactive';
|
|
57
56
|
|
|
58
57
|
export type Sidebar< C extends Context > = OmitNevers<
|
|
59
|
-
|
|
58
|
+
_BaseEntityRecords.Sidebar< C >
|
|
60
59
|
>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { Context, ContextualField, OmitNevers } from './helpers';
|
|
4
|
+
import type { Context, ContextualField, OmitNevers } from './helpers';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
7
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
8
|
+
declare module './base-entity-records' {
|
|
9
|
+
export namespace BaseEntityRecords {
|
|
10
10
|
export interface Taxonomy< C extends Context > {
|
|
11
11
|
/**
|
|
12
12
|
* All capabilities used by the taxonomy.
|
|
@@ -88,5 +88,5 @@ declare module './base-entity-types' {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
export type Taxonomy< C extends Context > = OmitNevers<
|
|
91
|
-
|
|
91
|
+
_BaseEntityRecords.Taxonomy< C >
|
|
92
92
|
>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { Context, PostFormat, RenderedText, OmitNevers } from './helpers';
|
|
4
|
+
import type { Context, PostFormat, RenderedText, OmitNevers } from './helpers';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
7
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
8
|
+
declare module './base-entity-records' {
|
|
9
|
+
export namespace BaseEntityRecords {
|
|
10
10
|
export interface Theme< C extends Context > {
|
|
11
11
|
/**
|
|
12
12
|
* The theme's stylesheet. This uniquely identifies the theme.
|
|
@@ -218,5 +218,5 @@ declare module './base-entity-types' {
|
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
export type Theme< C extends Context > = OmitNevers<
|
|
221
|
-
|
|
221
|
+
_BaseEntityRecords.Theme< C >
|
|
222
222
|
>;
|
package/src/entity-types/type.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { Context, ContextualField, OmitNevers } from './helpers';
|
|
4
|
+
import type { Context, ContextualField, OmitNevers } from './helpers';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
7
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
8
|
+
declare module './base-entity-records' {
|
|
9
|
+
export namespace BaseEntityRecords {
|
|
10
10
|
export interface Type< C extends Context > {
|
|
11
11
|
/**
|
|
12
12
|
* All capabilities used by the post type.
|
|
@@ -76,5 +76,5 @@ declare module './base-entity-types' {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
export type Type< C extends Context > = OmitNevers<
|
|
79
|
-
|
|
79
|
+
_BaseEntityRecords.Type< C >
|
|
80
80
|
>;
|
package/src/entity-types/user.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import type {
|
|
5
|
+
AvatarUrls,
|
|
6
|
+
Context,
|
|
7
|
+
ContextualField,
|
|
8
|
+
OmitNevers,
|
|
9
|
+
} from './helpers';
|
|
5
10
|
|
|
6
|
-
import {
|
|
11
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
12
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
13
|
+
declare module './base-entity-records' {
|
|
14
|
+
export namespace BaseEntityRecords {
|
|
10
15
|
export interface User< C extends Context > {
|
|
11
16
|
/**
|
|
12
17
|
* Unique identifier for the user.
|
|
@@ -105,5 +110,5 @@ declare module './base-entity-types' {
|
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
export type User< C extends Context > = OmitNevers<
|
|
108
|
-
|
|
113
|
+
_BaseEntityRecords.User< C >
|
|
109
114
|
>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { Context, OmitNevers } from './helpers';
|
|
4
|
+
import type { Context, OmitNevers } from './helpers';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
7
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
8
|
+
declare module './base-entity-records' {
|
|
9
|
+
export namespace BaseEntityRecords {
|
|
10
10
|
export interface WidgetType< C extends Context > {
|
|
11
11
|
/**
|
|
12
12
|
* Unique slug identifying the widget type.
|
|
@@ -33,5 +33,5 @@ declare module './base-entity-types' {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export type WidgetType< C extends Context > = OmitNevers<
|
|
36
|
-
|
|
36
|
+
_BaseEntityRecords.WidgetType< C >
|
|
37
37
|
>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { Context, ContextualField, OmitNevers } from './helpers';
|
|
4
|
+
import type { Context, ContextualField, OmitNevers } from './helpers';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
7
7
|
|
|
8
|
-
declare module './base-entity-
|
|
9
|
-
export namespace
|
|
8
|
+
declare module './base-entity-records' {
|
|
9
|
+
export namespace BaseEntityRecords {
|
|
10
10
|
export interface Widget< C extends Context > {
|
|
11
11
|
/**
|
|
12
12
|
* Unique identifier for the widget.
|
|
@@ -60,5 +60,5 @@ declare module './base-entity-types' {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
export type Widget< C extends Context > = OmitNevers<
|
|
63
|
-
|
|
63
|
+
_BaseEntityRecords.Widget< C >
|
|
64
64
|
>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import type {
|
|
5
5
|
Context,
|
|
6
6
|
PostStatus,
|
|
7
7
|
RenderedText,
|
|
@@ -9,10 +9,10 @@ import {
|
|
|
9
9
|
ContextualField,
|
|
10
10
|
} from './helpers';
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
13
13
|
|
|
14
|
-
declare module './base-entity-
|
|
15
|
-
export namespace
|
|
14
|
+
declare module './base-entity-records' {
|
|
15
|
+
export namespace BaseEntityRecords {
|
|
16
16
|
export interface WpTemplatePart< C extends Context > {
|
|
17
17
|
/**
|
|
18
18
|
* ID of template.
|
|
@@ -90,5 +90,5 @@ declare module './base-entity-types' {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
export type WpTemplatePart< C extends Context > = OmitNevers<
|
|
93
|
-
|
|
93
|
+
_BaseEntityRecords.WpTemplatePart< C >
|
|
94
94
|
>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import type {
|
|
5
5
|
Context,
|
|
6
6
|
PostStatus,
|
|
7
7
|
RenderedText,
|
|
@@ -9,10 +9,10 @@ import {
|
|
|
9
9
|
ContextualField,
|
|
10
10
|
} from './helpers';
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
|
|
13
13
|
|
|
14
|
-
declare module './base-entity-
|
|
15
|
-
export namespace
|
|
14
|
+
declare module './base-entity-records' {
|
|
15
|
+
export namespace BaseEntityRecords {
|
|
16
16
|
export interface WpTemplate< C extends Context > {
|
|
17
17
|
/**
|
|
18
18
|
* ID of template.
|
|
@@ -90,5 +90,5 @@ declare module './base-entity-types' {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
export type WpTemplate< C extends Context > = OmitNevers<
|
|
93
|
-
|
|
93
|
+
_BaseEntityRecords.WpTemplate< C >
|
|
94
94
|
>;
|
|
@@ -170,7 +170,7 @@ const fetchLinkSuggestions = async (
|
|
|
170
170
|
.slice( 0, perPage )
|
|
171
171
|
.map(
|
|
172
172
|
/**
|
|
173
|
-
* @param {{ id: number, url:string, title?:string, subtype?: string, type?: string }} result
|
|
173
|
+
* @param {{ id: number, meta?: object, url:string, title?:string, subtype?: string, type?: string }} result
|
|
174
174
|
*/
|
|
175
175
|
( result ) => {
|
|
176
176
|
return {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import useQuerySelect from './use-query-select';
|
|
5
5
|
import { store as coreStore } from '../';
|
|
6
|
-
import { Status } from './constants';
|
|
6
|
+
import type { Status } from './constants';
|
|
7
7
|
|
|
8
8
|
interface EntityRecordResolution< RecordType > {
|
|
9
9
|
/** The requested entity record */
|
|
@@ -23,13 +23,18 @@ interface EntityRecordResolution< RecordType > {
|
|
|
23
23
|
status: Status;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
interface Options {
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
/**
|
|
27
31
|
* Resolves the specified entity record.
|
|
28
32
|
*
|
|
29
|
-
* @param kind
|
|
30
|
-
* @param name
|
|
31
|
-
* @param recordId
|
|
32
|
-
*
|
|
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.
|
|
33
38
|
* @example
|
|
34
39
|
* ```js
|
|
35
40
|
* import { useEntityRecord } from '@wordpress/core-data';
|
|
@@ -58,11 +63,17 @@ interface EntityRecordResolution< RecordType > {
|
|
|
58
63
|
export default function __experimentalUseEntityRecord< RecordType >(
|
|
59
64
|
kind: string,
|
|
60
65
|
name: string,
|
|
61
|
-
recordId: string | number
|
|
66
|
+
recordId: string | number,
|
|
67
|
+
options: Options = { enabled: true }
|
|
62
68
|
): EntityRecordResolution< RecordType > {
|
|
63
69
|
const { data: record, ...rest } = useQuerySelect(
|
|
64
|
-
( query ) =>
|
|
65
|
-
|
|
70
|
+
( query ) => {
|
|
71
|
+
if ( ! options.enabled ) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return query( coreStore ).getEntityRecord( kind, name, recordId );
|
|
75
|
+
},
|
|
76
|
+
[ kind, name, recordId, options.enabled ]
|
|
66
77
|
);
|
|
67
78
|
|
|
68
79
|
return {
|
|
@@ -8,7 +8,7 @@ import { addQueryArgs } from '@wordpress/url';
|
|
|
8
8
|
*/
|
|
9
9
|
import useQuerySelect from './use-query-select';
|
|
10
10
|
import { store as coreStore } from '../';
|
|
11
|
-
import { Status } from './constants';
|
|
11
|
+
import type { Status } from './constants';
|
|
12
12
|
|
|
13
13
|
interface EntityRecordsResolution< RecordType > {
|
|
14
14
|
/** The requested entity record */
|
|
@@ -28,12 +28,22 @@ interface EntityRecordsResolution< RecordType > {
|
|
|
28
28
|
status: Status;
|
|
29
29
|
}
|
|
30
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
|
+
}
|
|
39
|
+
|
|
31
40
|
/**
|
|
32
41
|
* Resolves the specified entity records.
|
|
33
42
|
*
|
|
34
|
-
* @param kind
|
|
35
|
-
* @param name
|
|
36
|
-
* @param queryArgs
|
|
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.
|
|
37
47
|
* @example
|
|
38
48
|
* ```js
|
|
39
49
|
* import { useEntityRecord } from '@wordpress/core-data';
|
|
@@ -62,13 +72,14 @@ interface EntityRecordsResolution< RecordType > {
|
|
|
62
72
|
* application, the list of records and the resolution details will be retrieved from
|
|
63
73
|
* the store state using `getEntityRecords()`, or resolved if missing.
|
|
64
74
|
*
|
|
65
|
-
* @return
|
|
75
|
+
* @return Entity records data.
|
|
66
76
|
* @template RecordType
|
|
67
77
|
*/
|
|
68
78
|
export default function __experimentalUseEntityRecords< RecordType >(
|
|
69
79
|
kind: string,
|
|
70
80
|
name: string,
|
|
71
|
-
queryArgs: unknown = {}
|
|
81
|
+
queryArgs: Record< string, unknown > = {},
|
|
82
|
+
options: Options = { enabled: true }
|
|
72
83
|
): EntityRecordsResolution< RecordType > {
|
|
73
84
|
// Serialize queryArgs to a string that can be safely used as a React dep.
|
|
74
85
|
// We can't just pass queryArgs as one of the deps, because if it is passed
|
|
@@ -77,9 +88,15 @@ export default function __experimentalUseEntityRecords< RecordType >(
|
|
|
77
88
|
const queryAsString = addQueryArgs( '', queryArgs );
|
|
78
89
|
|
|
79
90
|
const { data: records, ...rest } = useQuerySelect(
|
|
80
|
-
( query ) =>
|
|
81
|
-
|
|
82
|
-
|
|
91
|
+
( query ) => {
|
|
92
|
+
if ( ! options.enabled ) {
|
|
93
|
+
return {
|
|
94
|
+
data: [],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return query( coreStore ).getEntityRecords( kind, name, queryArgs );
|
|
98
|
+
},
|
|
99
|
+
[ kind, name, queryAsString, options.enabled ]
|
|
83
100
|
);
|
|
84
101
|
|
|
85
102
|
return {
|
package/src/index.js
CHANGED
|
@@ -11,41 +11,36 @@ import * as selectors from './selectors';
|
|
|
11
11
|
import * as actions from './actions';
|
|
12
12
|
import * as resolvers from './resolvers';
|
|
13
13
|
import createLocksActions from './locks/actions';
|
|
14
|
-
import {
|
|
14
|
+
import { rootEntitiesConfig, getMethodName } from './entities';
|
|
15
15
|
import { STORE_NAME } from './name';
|
|
16
16
|
|
|
17
17
|
// The entity selectors/resolvers and actions are shortcuts to their generic equivalents
|
|
18
|
-
// (getEntityRecord, getEntityRecords, updateEntityRecord,
|
|
19
|
-
// Instead of getEntityRecord, the consumer could use more user-
|
|
18
|
+
// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords)
|
|
19
|
+
// Instead of getEntityRecord, the consumer could use more user-friendly named selector: getPostType, getTaxonomy...
|
|
20
20
|
// The "kind" and the "name" of the entity are combined to generate these shortcuts.
|
|
21
21
|
|
|
22
|
-
const entitySelectors =
|
|
22
|
+
const entitySelectors = rootEntitiesConfig.reduce( ( result, entity ) => {
|
|
23
23
|
const { kind, name } = entity;
|
|
24
24
|
result[ getMethodName( kind, name ) ] = ( state, key, query ) =>
|
|
25
25
|
selectors.getEntityRecord( state, kind, name, key, query );
|
|
26
|
-
result[ getMethodName( kind, name, 'get', true ) ] = ( state,
|
|
27
|
-
selectors.getEntityRecords( state, kind, name,
|
|
26
|
+
result[ getMethodName( kind, name, 'get', true ) ] = ( state, query ) =>
|
|
27
|
+
selectors.getEntityRecords( state, kind, name, query );
|
|
28
28
|
return result;
|
|
29
29
|
}, {} );
|
|
30
30
|
|
|
31
|
-
const entityResolvers =
|
|
31
|
+
const entityResolvers = rootEntitiesConfig.reduce( ( result, entity ) => {
|
|
32
32
|
const { kind, name } = entity;
|
|
33
33
|
result[ getMethodName( kind, name ) ] = ( key, query ) =>
|
|
34
34
|
resolvers.getEntityRecord( kind, name, key, query );
|
|
35
35
|
const pluralMethodName = getMethodName( kind, name, 'get', true );
|
|
36
36
|
result[ pluralMethodName ] = ( ...args ) =>
|
|
37
37
|
resolvers.getEntityRecords( kind, name, ...args );
|
|
38
|
-
result[ pluralMethodName ].shouldInvalidate = ( action
|
|
39
|
-
resolvers.getEntityRecords.shouldInvalidate(
|
|
40
|
-
action,
|
|
41
|
-
kind,
|
|
42
|
-
name,
|
|
43
|
-
...args
|
|
44
|
-
);
|
|
38
|
+
result[ pluralMethodName ].shouldInvalidate = ( action ) =>
|
|
39
|
+
resolvers.getEntityRecords.shouldInvalidate( action, kind, name );
|
|
45
40
|
return result;
|
|
46
41
|
}, {} );
|
|
47
42
|
|
|
48
|
-
const entityActions =
|
|
43
|
+
const entityActions = rootEntitiesConfig.reduce( ( result, entity ) => {
|
|
49
44
|
const { kind, name } = entity;
|
|
50
45
|
result[ getMethodName( kind, name, 'save' ) ] = ( key ) =>
|
|
51
46
|
actions.saveEntityRecord( kind, name, key );
|
|
@@ -116,7 +116,7 @@ describe( 'isLockAvailable', () => {
|
|
|
116
116
|
store: 'core',
|
|
117
117
|
path: [
|
|
118
118
|
'entities',
|
|
119
|
-
'
|
|
119
|
+
'records',
|
|
120
120
|
'postType',
|
|
121
121
|
'post',
|
|
122
122
|
16,
|
|
@@ -163,7 +163,7 @@ describe( 'isLockAvailable', () => {
|
|
|
163
163
|
entities: {
|
|
164
164
|
locks: [],
|
|
165
165
|
children: {
|
|
166
|
-
|
|
166
|
+
records: {
|
|
167
167
|
locks: [],
|
|
168
168
|
children: {
|
|
169
169
|
postType: {
|
|
@@ -178,7 +178,7 @@ describe( 'isLockAvailable', () => {
|
|
|
178
178
|
path: [
|
|
179
179
|
'core',
|
|
180
180
|
'entities',
|
|
181
|
-
'
|
|
181
|
+
'records',
|
|
182
182
|
'postType',
|
|
183
183
|
'book',
|
|
184
184
|
67,
|
|
@@ -205,7 +205,7 @@ describe( 'isLockAvailable', () => {
|
|
|
205
205
|
isLockAvailable(
|
|
206
206
|
deepFreeze( subState ),
|
|
207
207
|
'core',
|
|
208
|
-
[ 'entities', '
|
|
208
|
+
[ 'entities', 'records', 'postType', 'book', 67 ],
|
|
209
209
|
{ exclusive: false }
|
|
210
210
|
)
|
|
211
211
|
).toBe( false );
|
|
@@ -23,10 +23,10 @@ export function receiveItems( items, edits ) {
|
|
|
23
23
|
* Returns an action object used in signalling that entity records have been
|
|
24
24
|
* deleted and they need to be removed from entities state.
|
|
25
25
|
*
|
|
26
|
-
* @param {string}
|
|
27
|
-
* @param {string}
|
|
28
|
-
* @param {Array|number} records Record IDs of the removed entities.
|
|
29
|
-
* @param {boolean}
|
|
26
|
+
* @param {string} kind Kind of the removed entities.
|
|
27
|
+
* @param {string} name Name of the removed entities.
|
|
28
|
+
* @param {Array|number|string} records Record IDs of the removed entities.
|
|
29
|
+
* @param {boolean} invalidateCache Controls whether we want to invalidate the cache.
|
|
30
30
|
* @return {Object} Action object.
|
|
31
31
|
*/
|
|
32
32
|
export function removeItems( kind, name, records, invalidateCache = false ) {
|
|
@@ -70,19 +70,19 @@ export function getQueryParts( query ) {
|
|
|
70
70
|
// While in theory, we could exclude "_fields" from the stableKey
|
|
71
71
|
// because two request with different fields have the same results
|
|
72
72
|
// We're not able to ensure that because the server can decide to omit
|
|
73
|
-
// fields from the response even if we
|
|
73
|
+
// fields from the response even if we explicitly asked for it.
|
|
74
74
|
// Example: Asking for titles in posts without title support.
|
|
75
75
|
if ( key === '_fields' ) {
|
|
76
|
-
parts.fields = getNormalizedCommaSeparable( value );
|
|
76
|
+
parts.fields = getNormalizedCommaSeparable( value ) ?? [];
|
|
77
77
|
// Make sure to normalize value for `stableKey`
|
|
78
78
|
value = parts.fields.join();
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// Two requests with different include values cannot have same results.
|
|
82
82
|
if ( key === 'include' ) {
|
|
83
|
-
parts.include =
|
|
84
|
-
|
|
85
|
-
);
|
|
83
|
+
parts.include = (
|
|
84
|
+
getNormalizedCommaSeparable( value ) ?? []
|
|
85
|
+
).map( Number );
|
|
86
86
|
// Normalize value for `stableKey`.
|
|
87
87
|
value = parts.include.join();
|
|
88
88
|
}
|
|
@@ -51,7 +51,7 @@ export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
|
|
|
51
51
|
// If later page has already been received, default to the larger known
|
|
52
52
|
// size of the existing array, else calculate as extending the existing.
|
|
53
53
|
const size = Math.max(
|
|
54
|
-
itemIds
|
|
54
|
+
itemIds?.length ?? 0,
|
|
55
55
|
nextItemIdsStartIndex + nextItemIds.length
|
|
56
56
|
);
|
|
57
57
|
|
|
@@ -66,7 +66,7 @@ export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
|
|
|
66
66
|
|
|
67
67
|
mergedItemIds[ i ] = isInNextItemsRange
|
|
68
68
|
? nextItemIds[ i - nextItemIdsStartIndex ]
|
|
69
|
-
: itemIds[ i ];
|
|
69
|
+
: itemIds?.[ i ];
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
return mergedItemIds;
|
|
@@ -116,10 +116,10 @@ export function items( state = {}, action ) {
|
|
|
116
116
|
* In such cases, completeness is used as an indication of whether it would be
|
|
117
117
|
* safe to use queried data for a non-`_fields`-limited request.
|
|
118
118
|
*
|
|
119
|
-
* @param {Object<string,boolean
|
|
120
|
-
* @param {Object}
|
|
119
|
+
* @param {Object<string,Object<string,boolean>>} state Current state.
|
|
120
|
+
* @param {Object} action Dispatched action.
|
|
121
121
|
*
|
|
122
|
-
* @return {Object<string,boolean
|
|
122
|
+
* @return {Object<string,Object<string,boolean>>} Next state.
|
|
123
123
|
*/
|
|
124
124
|
export function itemIsComplete( state = {}, action ) {
|
|
125
125
|
switch ( action.type ) {
|
|
@@ -130,7 +130,7 @@ export function itemIsComplete( state = {}, action ) {
|
|
|
130
130
|
// An item is considered complete if it is received without an associated
|
|
131
131
|
// fields query. Ideally, this would be implemented in such a way where the
|
|
132
132
|
// complete aggregate of all fields would satisfy completeness. Since the
|
|
133
|
-
// fields are not consistent across all
|
|
133
|
+
// fields are not consistent across all entities, this would require
|
|
134
134
|
// introspection on the REST schema for each entity to know which fields
|
|
135
135
|
// compose a complete item for that entity.
|
|
136
136
|
const queryParts = query ? getQueryParts( query ) : {};
|