@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/selectors.ts CHANGED
@@ -18,30 +18,19 @@ import { STORE_NAME } from './name';
18
18
  import { getQueriedItems } from './queried-data';
19
19
  import { DEFAULT_ENTITY_KEY } from './entities';
20
20
  import { getNormalizedCommaSeparable, isRawAttribute } from './utils';
21
- import type {
22
- Context,
23
- DefaultContextOf,
24
- EntityRecordOf,
25
- KeyOf,
26
- Kind,
27
- KindOf,
28
- Name,
29
- NameOf,
30
- User,
31
- WpTemplate,
32
- } from './entity-types';
21
+ import type * as ET from './entity-types';
33
22
 
34
23
  // This is an incomplete, high-level approximation of the State type.
35
24
  // It makes the selectors slightly more safe, but is intended to evolve
36
25
  // into a more detailed representation over time.
37
26
  // See https://github.com/WordPress/gutenberg/pull/40025#discussion_r865410589 for more context.
38
- interface State {
27
+ export interface State {
39
28
  autosaves: Record< string | number, Array< unknown > >;
40
29
  blockPatterns: Array< unknown >;
41
30
  blockPatternCategories: Array< unknown >;
42
31
  currentGlobalStylesId: string;
43
32
  currentTheme: string;
44
- currentUser: User< 'edit' >;
33
+ currentUser: ET.User< 'edit' >;
45
34
  embedPreviews: Record< string, { html: string } >;
46
35
  entities: EntitiesState;
47
36
  themeBaseGlobalStyles: Record< string, Object >;
@@ -50,19 +39,21 @@ interface State {
50
39
  users: UserState;
51
40
  }
52
41
 
42
+ type EntityRecordKey = string | number;
43
+
53
44
  interface EntitiesState {
54
45
  config: EntityConfig[];
55
- records: Record< Kind, Record< Name, EntityState< Kind, Name > > >;
46
+ records: Record< string, Record< string, EntityState< ET.EntityRecord > > >;
56
47
  }
57
48
 
58
- interface EntityState< K extends Kind, N extends Name > {
59
- edits: Record< KeyOf< K, N >, Partial< EntityRecordOf< K, N > > >;
60
- saving: Record< KeyOf< K, N >, { pending: boolean } >;
49
+ interface EntityState< EntityRecord extends ET.EntityRecord > {
50
+ edits: Record< string, Partial< EntityRecord > >;
51
+ saving: Record< string, { pending: boolean } >;
61
52
  }
62
53
 
63
54
  interface EntityConfig {
64
- name: Name;
65
- kind: Kind;
55
+ name: string;
56
+ kind: string;
66
57
  }
67
58
 
68
59
  interface UndoState extends Array< Object > {
@@ -71,31 +62,16 @@ interface UndoState extends Array< Object > {
71
62
  }
72
63
 
73
64
  interface UserState {
74
- queries: Record< string, GenericRecordKey[] >;
75
- byId: Record< GenericRecordKey, User< 'edit' > >;
65
+ queries: Record< string, EntityRecordKey[] >;
66
+ byId: Record< EntityRecordKey, ET.User< 'edit' > >;
76
67
  }
77
68
 
78
- type GenericRecordKey = number | string;
79
- type EntityRecord = any;
80
69
  type Optional< T > = T | undefined;
81
70
 
82
71
  /**
83
72
  * HTTP Query parameters sent with the API request to fetch the entity records.
84
73
  */
85
- export type EntityQuery<
86
- C extends Context,
87
- WithFields extends boolean = true
88
- > = Omit< Record< string, any >, '_fields' > & {
89
- context?: C;
90
- } & ( WithFields extends true
91
- ? {
92
- /**
93
- * The requested fields. If specified, the REST API will remove from the response
94
- * any fields not on that list.
95
- */
96
- _fields: string[];
97
- }
98
- : {} );
74
+ type GetRecordsHttpQuery = Record< string, any >;
99
75
 
100
76
  /**
101
77
  * Shared reference to an empty object for cases where it is important to avoid
@@ -116,7 +92,7 @@ const EMPTY_OBJECT = {};
116
92
  * @return Whether a request is in progress for an embed preview.
117
93
  */
118
94
  export const isRequestingEmbedPreview = createRegistrySelector(
119
- ( select ) =>
95
+ ( select: any ) =>
120
96
  ( state: State, url: string ): boolean => {
121
97
  return select( STORE_NAME ).isResolving( 'getEmbedPreview', [
122
98
  url,
@@ -136,8 +112,8 @@ export const isRequestingEmbedPreview = createRegistrySelector(
136
112
  */
137
113
  export function getAuthors(
138
114
  state: State,
139
- query?: EntityQuery< any >
140
- ): User< 'edit' >[] {
115
+ query?: GetRecordsHttpQuery
116
+ ): ET.User[] {
141
117
  deprecated( "select( 'core' ).getAuthors()", {
142
118
  since: '5.9',
143
119
  alternative: "select( 'core' ).getUsers({ who: 'authors' })",
@@ -157,7 +133,7 @@ export function getAuthors(
157
133
  *
158
134
  * @return Current user object.
159
135
  */
160
- export function getCurrentUser( state: State ): User< 'edit' > {
136
+ export function getCurrentUser( state: State ): ET.User< 'edit' > {
161
137
  return state.currentUser;
162
138
  }
163
139
 
@@ -170,7 +146,7 @@ export function getCurrentUser( state: State ): User< 'edit' > {
170
146
  * @return Users list.
171
147
  */
172
148
  export const getUserQueryResults = createSelector(
173
- ( state: State, queryID: string ): User< 'edit' >[] => {
149
+ ( state: State, queryID: string ): ET.User< 'edit' >[] => {
174
150
  const queryResults = state.users.queries[ queryID ];
175
151
 
176
152
  return map( queryResults, ( id ) => state.users.byId[ id ] );
@@ -190,7 +166,7 @@ export const getUserQueryResults = createSelector(
190
166
  *
191
167
  * @return Array of entities with config matching kind.
192
168
  */
193
- export function getEntitiesByKind( state: State, kind: Kind ): Array< any > {
169
+ export function getEntitiesByKind( state: State, kind: string ): Array< any > {
194
170
  deprecated( "wp.data.select( 'core' ).getEntitiesByKind()", {
195
171
  since: '6.0',
196
172
  alternative: "wp.data.select( 'core' ).getEntitiesConfig()",
@@ -206,7 +182,7 @@ export function getEntitiesByKind( state: State, kind: Kind ): Array< any > {
206
182
  *
207
183
  * @return Array of entities with config matching kind.
208
184
  */
209
- export function getEntitiesConfig( state: State, kind: Kind ): Array< any > {
185
+ export function getEntitiesConfig( state: State, kind: string ): Array< any > {
210
186
  return filter( state.entities.config, { kind } );
211
187
  }
212
188
 
@@ -220,7 +196,7 @@ export function getEntitiesConfig( state: State, kind: Kind ): Array< any > {
220
196
  *
221
197
  * @return Entity config
222
198
  */
223
- export function getEntity( state: State, kind: Kind, name: Name ): any {
199
+ export function getEntity( state: State, kind: string, name: string ): any {
224
200
  deprecated( "wp.data.select( 'core' ).getEntity()", {
225
201
  since: '6.0',
226
202
  alternative: "wp.data.select( 'core' ).getEntityConfig()",
@@ -237,56 +213,14 @@ export function getEntity( state: State, kind: Kind, name: Name ): any {
237
213
  *
238
214
  * @return Entity config
239
215
  */
240
- export function getEntityConfig( state: State, kind: Kind, name: Name ): any {
216
+ export function getEntityConfig(
217
+ state: State,
218
+ kind: string,
219
+ name: string
220
+ ): any {
241
221
  return find( state.entities.config, { kind, name } );
242
222
  }
243
223
 
244
- /**
245
- * GetEntityRecord is declared as an *interface*, but it actually describes
246
- * the specifies the getEntityRecord *function* signature. It may seem unusual,
247
- * but it's just how TypeScript implements function overloading.
248
- *
249
- * More accurately, GetEntityRecord distinguishes between two different signatures
250
- * the getEntityRecord selector has:
251
- *
252
- * 1. When query._fields is not given, the returned type is EntityRecordOf< K, N, C >
253
- * 2. When query._fields is given, the returned type is Partial<EntityRecordOf< K, N, C >>
254
- *
255
- * Unfortunately, due to a TypeScript limitation (https://github.com/microsoft/TypeScript/issues/23132)
256
- * we can't use a single function signature with a return type such as:
257
- *
258
- * Fields extends undefined
259
- * ? EntityRecordOf< K, N, C >
260
- * : Partial< EntityRecordOf< K, N, C > >
261
- */
262
- interface GetEntityRecord {
263
- <
264
- R extends EntityRecordOf< K, N >,
265
- C extends Context = DefaultContextOf< R >,
266
- K extends Kind = KindOf< R >,
267
- N extends Name = NameOf< R >
268
- >(
269
- state: State,
270
- kind: K,
271
- name: N,
272
- key: KeyOf< K, N >,
273
- query: EntityQuery< C, true >
274
- ): Partial< EntityRecordOf< K, N, C > > | null | undefined;
275
-
276
- <
277
- R extends EntityRecordOf< K, N >,
278
- C extends Context = DefaultContextOf< R >,
279
- K extends Kind = KindOf< R >,
280
- N extends Name = NameOf< R >
281
- >(
282
- state: State,
283
- kind: K,
284
- name: N,
285
- key: KeyOf< K, N >,
286
- query?: EntityQuery< C, false >
287
- ): EntityRecordOf< K, N, C > | null | undefined;
288
- }
289
-
290
224
  /**
291
225
  * Returns the Entity's record object by key. Returns `null` if the value is not
292
226
  * yet received, undefined if the value entity is known to not exist, or the
@@ -301,19 +235,18 @@ interface GetEntityRecord {
301
235
  *
302
236
  * @return Record.
303
237
  */
304
- export const getEntityRecord: GetEntityRecord = createSelector(
238
+ export const getEntityRecord = createSelector(
305
239
  <
306
- R extends EntityRecordOf< K, N >,
307
- C extends Context = DefaultContextOf< R >,
308
- K extends Kind = KindOf< R >,
309
- N extends Name = NameOf< R >
240
+ EntityRecord extends
241
+ | ET.EntityRecord< any >
242
+ | Partial< ET.EntityRecord< any > >
310
243
  >(
311
244
  state: State,
312
- kind: K,
313
- name: N,
314
- key: KeyOf< R >,
315
- query
316
- ) => {
245
+ kind: string,
246
+ name: string,
247
+ key: EntityRecordKey,
248
+ query?: GetRecordsHttpQuery
249
+ ): EntityRecord | undefined => {
317
250
  const queriedState = get( state.entities.records, [
318
251
  kind,
319
252
  name,
@@ -342,7 +275,7 @@ export const getEntityRecord: GetEntityRecord = createSelector(
342
275
  const value = get( item, field );
343
276
  set( filteredItem, field, value );
344
277
  }
345
- return filteredItem;
278
+ return filteredItem as EntityRecord;
346
279
  }
347
280
 
348
281
  return item;
@@ -381,10 +314,9 @@ export const getEntityRecord: GetEntityRecord = createSelector(
381
314
  * @return Record.
382
315
  */
383
316
  export function __experimentalGetEntityRecordNoResolver<
384
- K extends Kind,
385
- N extends Name
386
- >( state: State, kind: K, name: N, key: KeyOf< K, N > ) {
387
- return getEntityRecord( state, kind, name, key );
317
+ EntityRecord extends ET.EntityRecord< any >
318
+ >( state: State, kind: string, name: string, key: EntityRecordKey ) {
319
+ return getEntityRecord< EntityRecord >( state, kind, name, key );
388
320
  }
389
321
 
390
322
  /**
@@ -399,13 +331,18 @@ export function __experimentalGetEntityRecordNoResolver<
399
331
  * @return Object with the entity's raw attributes.
400
332
  */
401
333
  export const getRawEntityRecord = createSelector(
402
- < K extends Kind, N extends Name >(
334
+ < EntityRecord extends ET.EntityRecord< any > >(
403
335
  state: State,
404
- kind: K,
405
- name: N,
406
- key: KeyOf< K, N >
336
+ kind: string,
337
+ name: string,
338
+ key: EntityRecordKey
407
339
  ): EntityRecord | undefined => {
408
- const record = getEntityRecord( state, kind, name, key );
340
+ const record = getEntityRecord< EntityRecord >(
341
+ state,
342
+ kind,
343
+ name,
344
+ key
345
+ );
409
346
  return (
410
347
  record &&
411
348
  Object.keys( record ).reduce( ( accumulator, _key ) => {
@@ -424,15 +361,15 @@ export const getRawEntityRecord = createSelector(
424
361
  accumulator[ _key ] = record[ _key ];
425
362
  }
426
363
  return accumulator;
427
- }, {} )
364
+ }, {} as any )
428
365
  );
429
366
  },
430
367
  (
431
368
  state: State,
432
- kind: Kind,
433
- name: Name,
434
- recordId: GenericRecordKey,
435
- query?: EntityQuery< any >
369
+ kind: string,
370
+ name: string,
371
+ recordId: EntityRecordKey,
372
+ query?: GetRecordsHttpQuery
436
373
  ) => {
437
374
  const context = query?.context ?? 'default';
438
375
  return [
@@ -468,59 +405,15 @@ export const getRawEntityRecord = createSelector(
468
405
  *
469
406
  * @return Whether entity records have been received.
470
407
  */
471
- export function hasEntityRecords<
472
- R extends EntityRecordOf< K, N >,
473
- C extends Context = DefaultContextOf< R >,
474
- K extends Kind = KindOf< R >,
475
- N extends Name = NameOf< R >
476
- >( state: State, kind: K, name: N, query?: EntityQuery< C > ): boolean {
408
+ export function hasEntityRecords(
409
+ state: State,
410
+ kind: string,
411
+ name: string,
412
+ query?: GetRecordsHttpQuery
413
+ ): boolean {
477
414
  return Array.isArray( getEntityRecords( state, kind, name, query ) );
478
415
  }
479
416
 
480
- /**
481
- * GetEntityRecord is declared as an *interface*, but it actually describes
482
- * the specifies the getEntityRecord *function* signature. It may seem unusual,
483
- * but it's just how TypeScript implements function overloading.
484
- *
485
- * More accurately, GetEntityRecord distinguishes between two different signatures
486
- * the getEntityRecord selector has:
487
- *
488
- * 1. When query._fields is not given, the returned type is EntityRecordOf< K, N, C >[]
489
- * 2. When query._fields is given, the returned type is Partial<EntityRecordOf< K, N, C >>[]
490
- *
491
- * Unfortunately, due to a TypeScript limitation (https://github.com/microsoft/TypeScript/issues/23132)
492
- * we can't use a single function signature with a return type such as:
493
- *
494
- * Fields extends undefined
495
- * ? EntityRecordOf< K, N, C >[]
496
- * : Partial< EntityRecordOf< K, N, C > >[]
497
- */
498
- interface GetEntityRecords {
499
- <
500
- R extends EntityRecordOf< K, N >,
501
- C extends Context = DefaultContextOf< R >,
502
- K extends Kind = KindOf< R >,
503
- N extends Name = NameOf< R >
504
- >(
505
- state: State,
506
- kind: K,
507
- name: N,
508
- query: EntityQuery< C, true >
509
- ): Partial< EntityRecordOf< K, N, C > >[] | null | undefined;
510
-
511
- <
512
- R extends EntityRecordOf< K, N >,
513
- C extends Context = DefaultContextOf< R >,
514
- K extends Kind = KindOf< R >,
515
- N extends Name = NameOf< R >
516
- >(
517
- state: State,
518
- kind: K,
519
- name: N,
520
- query?: EntityQuery< C, false >
521
- ): EntityRecordOf< K, N, C >[] | null | undefined;
522
- }
523
-
524
417
  /**
525
418
  * Returns the Entity's records.
526
419
  *
@@ -532,17 +425,16 @@ interface GetEntityRecords {
532
425
  *
533
426
  * @return Records.
534
427
  */
535
- export const getEntityRecords: GetEntityRecords = <
536
- R extends EntityRecordOf< K, N >,
537
- C extends Context = DefaultContextOf< R >,
538
- K extends Kind = KindOf< R >,
539
- N extends Name = NameOf< R >
428
+ export const getEntityRecords = <
429
+ EntityRecord extends
430
+ | ET.EntityRecord< any >
431
+ | Partial< ET.EntityRecord< any > >
540
432
  >(
541
433
  state: State,
542
- kind: K,
543
- name: N,
544
- query
545
- ) => {
434
+ kind: string,
435
+ name: string,
436
+ query?: GetRecordsHttpQuery
437
+ ): EntityRecord[] | null => {
546
438
  // Queried data state is prepopulated for all known entities. If this is not
547
439
  // assigned for the given parameters, then it is known to not exist.
548
440
  const queriedState = get( state.entities.records, [
@@ -558,9 +450,9 @@ export const getEntityRecords: GetEntityRecords = <
558
450
 
559
451
  type DirtyEntityRecord = {
560
452
  title: string;
561
- key: GenericRecordKey;
562
- name: Name;
563
- kind: Kind;
453
+ key: EntityRecordKey;
454
+ name: string;
455
+ kind: string;
564
456
  };
565
457
  /**
566
458
  * Returns the list of dirty entity records.
@@ -575,64 +467,44 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
575
467
  entities: { records },
576
468
  } = state;
577
469
  const dirtyRecords: DirtyEntityRecord[] = [];
578
- ( Object.keys( records ) as Kind[] ).forEach(
579
- < K extends Kind >( kind: K ) => {
580
- ( Object.keys( records[ kind ] ) as Name[] ).forEach(
581
- < N extends Name >( name: N ) => {
582
- const primaryKeys = (
583
- Object.keys(
584
- records[ kind ][ name ].edits
585
- ) as KeyOf< K, N >[]
586
- ).filter(
587
- ( primaryKey ) =>
588
- // The entity record must exist (not be deleted),
589
- // and it must have edits.
590
- getEntityRecord(
591
- state,
592
- kind,
593
- name,
594
- primaryKey
595
- ) &&
596
- hasEditsForEntityRecord(
597
- state,
598
- kind,
599
- name,
600
- primaryKey
601
- )
602
- );
470
+ Object.keys( records ).forEach( ( kind ) => {
471
+ Object.keys( records[ kind ] ).forEach( ( name ) => {
472
+ const primaryKeys = (
473
+ Object.keys( records[ kind ][ name ].edits ) as string[]
474
+ ).filter(
475
+ ( primaryKey ) =>
476
+ // The entity record must exist (not be deleted),
477
+ // and it must have edits.
478
+ getEntityRecord( state, kind, name, primaryKey ) &&
479
+ hasEditsForEntityRecord( state, kind, name, primaryKey )
480
+ );
603
481
 
604
- if ( primaryKeys.length ) {
605
- const entityConfig = getEntityConfig(
606
- state,
607
- kind,
608
- name
609
- );
610
- primaryKeys.forEach( ( primaryKey ) => {
611
- const entityRecord = getEditedEntityRecord(
612
- state,
613
- kind,
614
- name,
615
- primaryKey
616
- );
617
- dirtyRecords.push( {
618
- // We avoid using primaryKey because it's transformed into a string
619
- // when it's used as an object key.
620
- key: entityRecord[
482
+ if ( primaryKeys.length ) {
483
+ const entityConfig = getEntityConfig( state, kind, name );
484
+ primaryKeys.forEach( ( primaryKey ) => {
485
+ const entityRecord = getEditedEntityRecord(
486
+ state,
487
+ kind,
488
+ name,
489
+ primaryKey
490
+ );
491
+ dirtyRecords.push( {
492
+ // We avoid using primaryKey because it's transformed into a string
493
+ // when it's used as an object key.
494
+ key: entityRecord
495
+ ? entityRecord[
621
496
  entityConfig.key || DEFAULT_ENTITY_KEY
622
- ],
623
- title:
624
- entityConfig?.getTitle?.(
625
- entityRecord
626
- ) || '',
627
- name,
628
- kind,
629
- } );
630
- } );
631
- }
632
- }
633
- );
634
- }
635
- );
497
+ ]
498
+ : undefined,
499
+ title:
500
+ entityConfig?.getTitle?.( entityRecord ) || '',
501
+ name,
502
+ kind,
503
+ } );
504
+ } );
505
+ }
506
+ } );
507
+ } );
636
508
 
637
509
  return dirtyRecords;
638
510
  },
@@ -652,55 +524,40 @@ export const __experimentalGetEntitiesBeingSaved = createSelector(
652
524
  entities: { records },
653
525
  } = state;
654
526
  const recordsBeingSaved: DirtyEntityRecord[] = [];
655
- ( Object.keys( records ) as Kind[] ).forEach(
656
- < K extends Kind >( kind: K ) => {
657
- ( Object.keys( records[ kind ] ) as Name[] ).forEach(
658
- < N extends Name >( name: N ) => {
659
- const primaryKeys = (
660
- Object.keys(
661
- records[ kind ][ name ].saving
662
- ) as KeyOf< K, N >[]
663
- ).filter( ( primaryKey ) =>
664
- isSavingEntityRecord(
665
- state,
666
- kind,
667
- name,
668
- primaryKey
669
- )
670
- );
527
+ Object.keys( records ).forEach( ( kind ) => {
528
+ Object.keys( records[ kind ] ).forEach( ( name ) => {
529
+ const primaryKeys = (
530
+ Object.keys( records[ kind ][ name ].saving ) as string[]
531
+ ).filter( ( primaryKey ) =>
532
+ isSavingEntityRecord( state, kind, name, primaryKey )
533
+ );
671
534
 
672
- if ( primaryKeys.length ) {
673
- const entityConfig = getEntityConfig(
674
- state,
675
- kind,
676
- name
677
- );
678
- primaryKeys.forEach( ( primaryKey ) => {
679
- const entityRecord = getEditedEntityRecord(
680
- state,
681
- kind,
682
- name,
683
- primaryKey
684
- );
685
- recordsBeingSaved.push( {
686
- // We avoid using primaryKey because it's transformed into a string
687
- // when it's used as an object key.
688
- key: entityRecord[
535
+ if ( primaryKeys.length ) {
536
+ const entityConfig = getEntityConfig( state, kind, name );
537
+ primaryKeys.forEach( ( primaryKey ) => {
538
+ const entityRecord = getEditedEntityRecord(
539
+ state,
540
+ kind,
541
+ name,
542
+ primaryKey
543
+ );
544
+ recordsBeingSaved.push( {
545
+ // We avoid using primaryKey because it's transformed into a string
546
+ // when it's used as an object key.
547
+ key: entityRecord
548
+ ? entityRecord[
689
549
  entityConfig.key || DEFAULT_ENTITY_KEY
690
- ],
691
- title:
692
- entityConfig?.getTitle?.(
693
- entityRecord
694
- ) || '',
695
- name,
696
- kind,
697
- } );
698
- } );
699
- }
700
- }
701
- );
702
- }
703
- );
550
+ ]
551
+ : undefined,
552
+ title:
553
+ entityConfig?.getTitle?.( entityRecord ) || '',
554
+ name,
555
+ kind,
556
+ } );
557
+ } );
558
+ }
559
+ } );
560
+ } );
704
561
  return recordsBeingSaved;
705
562
  },
706
563
  ( state ) => [ state.entities.records ]
@@ -716,11 +573,11 @@ export const __experimentalGetEntitiesBeingSaved = createSelector(
716
573
  *
717
574
  * @return The entity record's edits.
718
575
  */
719
- export function getEntityRecordEdits< K extends Kind, N extends Name >(
576
+ export function getEntityRecordEdits(
720
577
  state: State,
721
- kind: K,
722
- name: N,
723
- recordId: KeyOf< K, N >
578
+ kind: string,
579
+ name: string,
580
+ recordId: EntityRecordKey
724
581
  ): Optional< any > {
725
582
  return get( state.entities.records, [
726
583
  kind,
@@ -745,11 +602,11 @@ export function getEntityRecordEdits< K extends Kind, N extends Name >(
745
602
  * @return The entity record's non transient edits.
746
603
  */
747
604
  export const getEntityRecordNonTransientEdits = createSelector(
748
- < K extends Kind, N extends Name >(
605
+ (
749
606
  state: State,
750
- kind: K,
751
- name: N,
752
- recordId: KeyOf< K, N >
607
+ kind: string,
608
+ name: string,
609
+ recordId: EntityRecordKey
753
610
  ): Optional< any > => {
754
611
  const { transientEdits } = getEntityConfig( state, kind, name ) || {};
755
612
  const edits = getEntityRecordEdits( state, kind, name, recordId ) || {};
@@ -763,7 +620,7 @@ export const getEntityRecordNonTransientEdits = createSelector(
763
620
  return acc;
764
621
  }, {} );
765
622
  },
766
- ( state: State, kind: Kind, name: Name, recordId: GenericRecordKey ) => [
623
+ ( state: State, kind: string, name: string, recordId: EntityRecordKey ) => [
767
624
  state.entities.config,
768
625
  get( state.entities.records, [ kind, name, 'edits', recordId ] ),
769
626
  ]
@@ -780,11 +637,11 @@ export const getEntityRecordNonTransientEdits = createSelector(
780
637
  *
781
638
  * @return Whether the entity record has edits or not.
782
639
  */
783
- export function hasEditsForEntityRecord< K extends Kind, N extends Name >(
640
+ export function hasEditsForEntityRecord(
784
641
  state: State,
785
- kind: K,
786
- name: N,
787
- recordId: KeyOf< K, N >
642
+ kind: string,
643
+ name: string,
644
+ recordId: EntityRecordKey
788
645
  ): boolean {
789
646
  return (
790
647
  isSavingEntityRecord( state, kind, name, recordId ) ||
@@ -805,21 +662,21 @@ export function hasEditsForEntityRecord< K extends Kind, N extends Name >(
805
662
  * @return The entity record, merged with its edits.
806
663
  */
807
664
  export const getEditedEntityRecord = createSelector(
808
- < K extends Kind, N extends Name >(
665
+ < EntityRecord extends ET.EntityRecord< any > >(
809
666
  state: State,
810
- kind: K,
811
- name: N,
812
- recordId: KeyOf< K, N >
813
- ): EntityRecord | undefined => ( {
667
+ kind: string,
668
+ name: string,
669
+ recordId: EntityRecordKey
670
+ ): ET.Updatable< EntityRecord > | undefined => ( {
814
671
  ...getRawEntityRecord( state, kind, name, recordId ),
815
672
  ...getEntityRecordEdits( state, kind, name, recordId ),
816
673
  } ),
817
674
  (
818
675
  state: State,
819
- kind: Kind,
820
- name: Name,
821
- recordId: GenericRecordKey,
822
- query?: EntityQuery< any >
676
+ kind: string,
677
+ name: string,
678
+ recordId: EntityRecordKey,
679
+ query?: GetRecordsHttpQuery
823
680
  ) => {
824
681
  const context = query?.context ?? 'default';
825
682
  return [
@@ -857,9 +714,9 @@ export const getEditedEntityRecord = createSelector(
857
714
  */
858
715
  export function isAutosavingEntityRecord(
859
716
  state: State,
860
- kind: Kind,
861
- name: Name,
862
- recordId: GenericRecordKey
717
+ kind: string,
718
+ name: string,
719
+ recordId: EntityRecordKey
863
720
  ): boolean {
864
721
  const { pending, isAutosave } = get(
865
722
  state.entities.records,
@@ -879,15 +736,15 @@ export function isAutosavingEntityRecord(
879
736
  *
880
737
  * @return Whether the entity record is saving or not.
881
738
  */
882
- export function isSavingEntityRecord< K extends Kind, N extends Name >(
739
+ export function isSavingEntityRecord(
883
740
  state: State,
884
- kind: K,
885
- name: N,
886
- recordId: KeyOf< K, N >
741
+ kind: string,
742
+ name: string,
743
+ recordId: EntityRecordKey
887
744
  ): boolean {
888
745
  return get(
889
746
  state.entities.records,
890
- [ kind, name, 'saving', recordId as GenericRecordKey, 'pending' ],
747
+ [ kind, name, 'saving', recordId as EntityRecordKey, 'pending' ],
891
748
  false
892
749
  );
893
750
  }
@@ -904,9 +761,9 @@ export function isSavingEntityRecord< K extends Kind, N extends Name >(
904
761
  */
905
762
  export function isDeletingEntityRecord(
906
763
  state: State,
907
- kind: Kind,
908
- name: Name,
909
- recordId: GenericRecordKey
764
+ kind: string,
765
+ name: string,
766
+ recordId: EntityRecordKey
910
767
  ): boolean {
911
768
  return get(
912
769
  state.entities.records,
@@ -927,9 +784,9 @@ export function isDeletingEntityRecord(
927
784
  */
928
785
  export function getLastEntitySaveError(
929
786
  state: State,
930
- kind: Kind,
931
- name: Name,
932
- recordId: GenericRecordKey
787
+ kind: string,
788
+ name: string,
789
+ recordId: EntityRecordKey
933
790
  ): any {
934
791
  return get( state.entities.records, [
935
792
  kind,
@@ -952,9 +809,9 @@ export function getLastEntitySaveError(
952
809
  */
953
810
  export function getLastEntityDeleteError(
954
811
  state: State,
955
- kind: Kind,
956
- name: Name,
957
- recordId: GenericRecordKey
812
+ kind: string,
813
+ name: string,
814
+ recordId: EntityRecordKey
958
815
  ): any {
959
816
  return get( state.entities.records, [
960
817
  kind,
@@ -1115,7 +972,7 @@ export function canUser(
1115
972
  state: State,
1116
973
  action: string,
1117
974
  resource: string,
1118
- id?: GenericRecordKey
975
+ id?: EntityRecordKey
1119
976
  ): boolean | undefined {
1120
977
  const key = [ action, resource, id ].filter( Boolean ).join( '/' );
1121
978
  return get( state, [ 'userPermissions', key ] );
@@ -1138,9 +995,9 @@ export function canUser(
1138
995
  */
1139
996
  export function canUserEditEntityRecord(
1140
997
  state: State,
1141
- kind: Kind,
1142
- name: Name,
1143
- recordId: GenericRecordKey
998
+ kind: string,
999
+ name: string,
1000
+ recordId: EntityRecordKey
1144
1001
  ): boolean | undefined {
1145
1002
  const entityConfig = getEntityConfig( state, kind, name );
1146
1003
  if ( ! entityConfig ) {
@@ -1166,7 +1023,7 @@ export function canUserEditEntityRecord(
1166
1023
  export function getAutosaves(
1167
1024
  state: State,
1168
1025
  postType: string,
1169
- postId: GenericRecordKey
1026
+ postId: EntityRecordKey
1170
1027
  ): Array< any > | undefined {
1171
1028
  return state.autosaves[ postId ];
1172
1029
  }
@@ -1181,18 +1038,18 @@ export function getAutosaves(
1181
1038
  *
1182
1039
  * @return The autosave for the post and author.
1183
1040
  */
1184
- export function getAutosave(
1041
+ export function getAutosave< EntityRecord extends ET.EntityRecord< any > >(
1185
1042
  state: State,
1186
1043
  postType: string,
1187
- postId: GenericRecordKey,
1188
- authorId: GenericRecordKey
1044
+ postId: EntityRecordKey,
1045
+ authorId: EntityRecordKey
1189
1046
  ): EntityRecord | undefined {
1190
1047
  if ( authorId === undefined ) {
1191
1048
  return;
1192
1049
  }
1193
1050
 
1194
1051
  const autosaves = state.autosaves[ postId ];
1195
- return find( autosaves, { author: authorId } );
1052
+ return find( autosaves, { author: authorId } ) as EntityRecord | undefined;
1196
1053
  }
1197
1054
 
1198
1055
  /**
@@ -1209,7 +1066,7 @@ export const hasFetchedAutosaves = createRegistrySelector(
1209
1066
  (
1210
1067
  state: State,
1211
1068
  postType: string,
1212
- postId: GenericRecordKey
1069
+ postId: EntityRecordKey
1213
1070
  ): boolean => {
1214
1071
  return select( STORE_NAME ).hasFinishedResolution( 'getAutosaves', [
1215
1072
  postType,
@@ -1257,21 +1114,25 @@ export const getReferenceByDistinctEdits = createSelector(
1257
1114
  export function __experimentalGetTemplateForLink(
1258
1115
  state: State,
1259
1116
  link: string
1260
- ): WpTemplate< 'edit' > | null {
1261
- const records = getEntityRecords( state, 'postType', 'wp_template', {
1262
- 'find-template': link,
1263
- } );
1117
+ ): Optional< ET.Updatable< ET.WpTemplate > > | null {
1118
+ const records = getEntityRecords< ET.WpTemplate >(
1119
+ state,
1120
+ 'postType',
1121
+ 'wp_template',
1122
+ {
1123
+ 'find-template': link,
1124
+ }
1125
+ );
1264
1126
 
1265
- const template = records?.length ? records[ 0 ] : null;
1266
- if ( template ) {
1267
- return getEditedEntityRecord(
1127
+ if ( records?.length ) {
1128
+ return getEditedEntityRecord< ET.WpTemplate >(
1268
1129
  state,
1269
1130
  'postType',
1270
1131
  'wp_template',
1271
- template.id
1132
+ records[ 0 ].id
1272
1133
  );
1273
1134
  }
1274
- return template;
1135
+ return null;
1275
1136
  }
1276
1137
 
1277
1138
  /**