@wordpress/core-data 4.14.1-next.d6164808d3.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 (197) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +73 -61
  3. package/build/entities.js +31 -46
  4. package/build/entities.js.map +1 -1
  5. package/build/hooks/use-entity-record.js +4 -2
  6. package/build/hooks/use-entity-record.js.map +1 -1
  7. package/build/hooks/use-entity-records.js +2 -0
  8. package/build/hooks/use-entity-records.js.map +1 -1
  9. package/build/hooks/use-query-select.js +5 -2
  10. package/build/hooks/use-query-select.js.map +1 -1
  11. package/build/hooks/use-resource-permissions.js +22 -6
  12. package/build/hooks/use-resource-permissions.js.map +1 -1
  13. package/build/index.js +0 -2
  14. package/build/index.js.map +1 -1
  15. package/build/resolvers.js +45 -21
  16. package/build/resolvers.js.map +1 -1
  17. package/build/selectors.js +9 -46
  18. package/build/selectors.js.map +1 -1
  19. package/build-module/entities.js +31 -46
  20. package/build-module/entities.js.map +1 -1
  21. package/build-module/hooks/use-entity-record.js +4 -2
  22. package/build-module/hooks/use-entity-record.js.map +1 -1
  23. package/build-module/hooks/use-entity-records.js +2 -0
  24. package/build-module/hooks/use-entity-records.js.map +1 -1
  25. package/build-module/hooks/use-query-select.js +4 -1
  26. package/build-module/hooks/use-query-select.js.map +1 -1
  27. package/build-module/hooks/use-resource-permissions.js +22 -6
  28. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  29. package/build-module/index.js +0 -2
  30. package/build-module/index.js.map +1 -1
  31. package/build-module/resolvers.js +45 -21
  32. package/build-module/resolvers.js.map +1 -1
  33. package/build-module/selectors.js +7 -44
  34. package/build-module/selectors.js.map +1 -1
  35. package/build-types/actions.d.ts +188 -0
  36. package/build-types/actions.d.ts.map +1 -0
  37. package/build-types/batch/create-batch.d.ts +71 -0
  38. package/build-types/batch/create-batch.d.ts.map +1 -0
  39. package/build-types/batch/default-processor.d.ts +11 -0
  40. package/build-types/batch/default-processor.d.ts.map +1 -0
  41. package/build-types/batch/index.d.ts +3 -0
  42. package/build-types/batch/index.d.ts.map +1 -0
  43. package/build-types/entities.d.ts +128 -0
  44. package/build-types/entities.d.ts.map +1 -0
  45. package/build-types/entity-provider.d.ts +68 -0
  46. package/build-types/entity-provider.d.ts.map +1 -0
  47. package/build-types/entity-types/attachment.d.ts +121 -0
  48. package/build-types/entity-types/attachment.d.ts.map +1 -0
  49. package/build-types/entity-types/base-entity-records.d.ts +37 -0
  50. package/build-types/entity-types/base-entity-records.d.ts.map +1 -0
  51. package/build-types/entity-types/comment.d.ts +82 -0
  52. package/build-types/entity-types/comment.d.ts.map +1 -0
  53. package/build-types/entity-types/helpers.d.ts +123 -0
  54. package/build-types/entity-types/helpers.d.ts.map +1 -0
  55. package/build-types/entity-types/index.d.ts +64 -0
  56. package/build-types/entity-types/index.d.ts.map +1 -0
  57. package/build-types/entity-types/menu-location.d.ts +25 -0
  58. package/build-types/entity-types/menu-location.d.ts.map +1 -0
  59. package/build-types/entity-types/nav-menu-item.d.ts +88 -0
  60. package/build-types/entity-types/nav-menu-item.d.ts.map +1 -0
  61. package/build-types/entity-types/nav-menu.d.ts +45 -0
  62. package/build-types/entity-types/nav-menu.d.ts.map +1 -0
  63. package/build-types/entity-types/page.d.ts +120 -0
  64. package/build-types/entity-types/page.d.ts.map +1 -0
  65. package/build-types/entity-types/plugin.d.ts +62 -0
  66. package/build-types/entity-types/plugin.d.ts.map +1 -0
  67. package/build-types/entity-types/post.d.ts +128 -0
  68. package/build-types/entity-types/post.d.ts.map +1 -0
  69. package/build-types/entity-types/settings.d.ts +89 -0
  70. package/build-types/entity-types/settings.d.ts.map +1 -0
  71. package/build-types/entity-types/sidebar.d.ts +55 -0
  72. package/build-types/entity-types/sidebar.d.ts.map +1 -0
  73. package/build-types/entity-types/taxonomy.d.ts +83 -0
  74. package/build-types/entity-types/taxonomy.d.ts.map +1 -0
  75. package/build-types/entity-types/theme.d.ts +206 -0
  76. package/build-types/entity-types/theme.d.ts.map +1 -0
  77. package/build-types/entity-types/type.d.ts +71 -0
  78. package/build-types/entity-types/type.d.ts.map +1 -0
  79. package/build-types/entity-types/user.d.ts +93 -0
  80. package/build-types/entity-types/user.d.ts.map +1 -0
  81. package/build-types/entity-types/widget-type.d.ts +33 -0
  82. package/build-types/entity-types/widget-type.d.ts.map +1 -0
  83. package/build-types/entity-types/widget.d.ts +59 -0
  84. package/build-types/entity-types/widget.d.ts.map +1 -0
  85. package/build-types/entity-types/wp-template-part.d.ts +80 -0
  86. package/build-types/entity-types/wp-template-part.d.ts.map +1 -0
  87. package/build-types/entity-types/wp-template.d.ts +80 -0
  88. package/build-types/entity-types/wp-template.d.ts.map +1 -0
  89. package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts +139 -0
  90. package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts.map +1 -0
  91. package/build-types/fetch/__experimental-fetch-url-data.d.ts +35 -0
  92. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -0
  93. package/build-types/fetch/index.d.ts +3 -0
  94. package/build-types/fetch/index.d.ts.map +1 -0
  95. package/build-types/hooks/constants.d.ts +7 -0
  96. package/build-types/hooks/constants.d.ts.map +1 -0
  97. package/build-types/hooks/index.d.ts +4 -0
  98. package/build-types/hooks/index.d.ts.map +1 -0
  99. package/build-types/hooks/memoize.d.ts +3 -0
  100. package/build-types/hooks/memoize.d.ts.map +1 -0
  101. package/build-types/hooks/use-entity-record.d.ts +124 -0
  102. package/build-types/hooks/use-entity-record.d.ts.map +1 -0
  103. package/build-types/hooks/use-entity-records.d.ts +49 -0
  104. package/build-types/hooks/use-entity-records.d.ts.map +1 -0
  105. package/build-types/hooks/use-query-select.d.ts +46 -0
  106. package/build-types/hooks/use-query-select.d.ts.map +1 -0
  107. package/build-types/hooks/use-resource-permissions.d.ts +99 -0
  108. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -0
  109. package/build-types/index.d.ts +131 -0
  110. package/build-types/index.d.ts.map +1 -0
  111. package/build-types/locks/actions.d.ts +7 -0
  112. package/build-types/locks/actions.d.ts.map +1 -0
  113. package/build-types/locks/engine.d.ts +5 -0
  114. package/build-types/locks/engine.d.ts.map +1 -0
  115. package/build-types/locks/reducer.d.ts +10 -0
  116. package/build-types/locks/reducer.d.ts.map +1 -0
  117. package/build-types/locks/selectors.d.ts +5 -0
  118. package/build-types/locks/selectors.d.ts.map +1 -0
  119. package/build-types/locks/utils.d.ts +8 -0
  120. package/build-types/locks/utils.d.ts.map +1 -0
  121. package/build-types/name.d.ts +8 -0
  122. package/build-types/name.d.ts.map +1 -0
  123. package/build-types/queried-data/actions.d.ts +32 -0
  124. package/build-types/queried-data/actions.d.ts.map +1 -0
  125. package/build-types/queried-data/get-query-parts.d.ts +60 -0
  126. package/build-types/queried-data/get-query-parts.d.ts.map +1 -0
  127. package/build-types/queried-data/index.d.ts +4 -0
  128. package/build-types/queried-data/index.d.ts.map +1 -0
  129. package/build-types/queried-data/reducer.d.ts +54 -0
  130. package/build-types/queried-data/reducer.d.ts.map +1 -0
  131. package/build-types/queried-data/selectors.d.ts +16 -0
  132. package/build-types/queried-data/selectors.d.ts.map +1 -0
  133. package/build-types/reducer.d.ts +156 -0
  134. package/build-types/reducer.d.ts.map +1 -0
  135. package/build-types/resolvers.d.ts +75 -0
  136. package/build-types/resolvers.d.ts.map +1 -0
  137. package/build-types/selectors.d.ts +524 -0
  138. package/build-types/selectors.d.ts.map +1 -0
  139. package/build-types/types.d.ts +4 -0
  140. package/build-types/types.d.ts.map +1 -0
  141. package/build-types/utils/conservative-map-item.d.ts +12 -0
  142. package/build-types/utils/conservative-map-item.d.ts.map +1 -0
  143. package/build-types/utils/forward-resolver.d.ts +10 -0
  144. package/build-types/utils/forward-resolver.d.ts.map +1 -0
  145. package/build-types/utils/get-normalized-comma-separable.d.ts +12 -0
  146. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -0
  147. package/build-types/utils/if-matching-action.d.ts +14 -0
  148. package/build-types/utils/if-matching-action.d.ts.map +1 -0
  149. package/build-types/utils/index.d.ts +9 -0
  150. package/build-types/utils/index.d.ts.map +1 -0
  151. package/build-types/utils/is-raw-attribute.d.ts +10 -0
  152. package/build-types/utils/is-raw-attribute.d.ts.map +1 -0
  153. package/build-types/utils/on-sub-key.d.ts +4 -0
  154. package/build-types/utils/on-sub-key.d.ts.map +1 -0
  155. package/build-types/utils/replace-action.d.ts +13 -0
  156. package/build-types/utils/replace-action.d.ts.map +1 -0
  157. package/build-types/utils/with-weak-map-cache.d.ts +12 -0
  158. package/build-types/utils/with-weak-map-cache.d.ts.map +1 -0
  159. package/package.json +12 -11
  160. package/src/entities.js +325 -0
  161. package/src/entity-types/attachment.ts +3 -4
  162. package/src/entity-types/comment.ts +3 -4
  163. package/src/entity-types/index.ts +31 -88
  164. package/src/entity-types/menu-location.ts +3 -4
  165. package/src/entity-types/nav-menu-item.ts +3 -4
  166. package/src/entity-types/nav-menu.ts +3 -3
  167. package/src/entity-types/page.ts +3 -3
  168. package/src/entity-types/plugin.ts +3 -3
  169. package/src/entity-types/post.ts +3 -3
  170. package/src/entity-types/settings.ts +3 -3
  171. package/src/entity-types/sidebar.ts +3 -4
  172. package/src/entity-types/taxonomy.ts +3 -4
  173. package/src/entity-types/theme.ts +3 -3
  174. package/src/entity-types/type.ts +3 -3
  175. package/src/entity-types/user.ts +3 -3
  176. package/src/entity-types/widget-type.ts +3 -4
  177. package/src/entity-types/widget.ts +3 -3
  178. package/src/entity-types/wp-template-part.ts +3 -4
  179. package/src/entity-types/wp-template.ts +3 -4
  180. package/src/hooks/test/use-entity-record.js +41 -1
  181. package/src/hooks/test/use-resource-permissions.js +4 -0
  182. package/src/hooks/use-entity-record.ts +12 -2
  183. package/src/hooks/use-entity-records.ts +2 -0
  184. package/src/hooks/use-query-select.ts +4 -1
  185. package/src/hooks/use-resource-permissions.ts +28 -6
  186. package/src/index.js +0 -2
  187. package/src/resolvers.js +46 -23
  188. package/src/selectors.ts +202 -341
  189. package/src/test/resolvers.js +118 -4
  190. package/tsconfig.json +21 -0
  191. package/tsconfig.tsbuildinfo +1 -0
  192. package/build/entity-types/entities.js +0 -6
  193. package/build/entity-types/entities.js.map +0 -1
  194. package/build-module/entity-types/entities.js +0 -2
  195. package/build-module/entity-types/entities.js.map +0 -1
  196. package/src/entities.ts +0 -550
  197. package/src/entity-types/entities.ts +0 -130
@@ -0,0 +1,12 @@
1
+ export default withWeakMapCache;
2
+ /**
3
+ * Given a function, returns an enhanced function which caches the result and
4
+ * tracks in WeakMap. The result is only cached if the original function is
5
+ * passed a valid object-like argument (requirement for WeakMap key).
6
+ *
7
+ * @param {Function} fn Original function.
8
+ *
9
+ * @return {Function} Enhanced caching function.
10
+ */
11
+ declare function withWeakMapCache(fn: Function): Function;
12
+ //# sourceMappingURL=with-weak-map-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-weak-map-cache.d.ts","sourceRoot":"","sources":["../../src/utils/with-weak-map-cache.js"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AACH,0DAoBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/core-data",
3
- "version": "4.14.1-next.d6164808d3.0",
3
+ "version": "5.0.0",
4
4
  "description": "Access to and manipulation of core WordPress entities.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -25,20 +25,21 @@
25
25
  "main": "build/index.js",
26
26
  "module": "build-module/index.js",
27
27
  "react-native": "src/index",
28
+ "types": "build-types",
28
29
  "sideEffects": [
29
30
  "{src,build,build-module}/index.js"
30
31
  ],
31
32
  "dependencies": {
32
33
  "@babel/runtime": "^7.16.0",
33
- "@wordpress/api-fetch": "^6.12.1-next.d6164808d3.0",
34
- "@wordpress/blocks": "^11.14.1-next.d6164808d3.0",
35
- "@wordpress/data": "^7.0.1-next.d6164808d3.0",
36
- "@wordpress/deprecated": "^3.15.1-next.d6164808d3.0",
37
- "@wordpress/element": "^4.13.1-next.d6164808d3.0",
38
- "@wordpress/html-entities": "^3.15.1-next.d6164808d3.0",
39
- "@wordpress/i18n": "^4.15.1-next.d6164808d3.0",
40
- "@wordpress/is-shallow-equal": "^4.15.1-next.d6164808d3.0",
41
- "@wordpress/url": "^3.16.1-next.d6164808d3.0",
34
+ "@wordpress/api-fetch": "^6.14.0",
35
+ "@wordpress/blocks": "^11.16.0",
36
+ "@wordpress/data": "^7.1.0",
37
+ "@wordpress/deprecated": "^3.17.0",
38
+ "@wordpress/element": "^4.15.0",
39
+ "@wordpress/html-entities": "^3.17.0",
40
+ "@wordpress/i18n": "^4.17.0",
41
+ "@wordpress/is-shallow-equal": "^4.17.0",
42
+ "@wordpress/url": "^3.18.0",
42
43
  "change-case": "^4.1.2",
43
44
  "equivalent-key-map": "^0.2.2",
44
45
  "lodash": "^4.17.21",
@@ -52,5 +53,5 @@
52
53
  "publishConfig": {
53
54
  "access": "public"
54
55
  },
55
- "gitHead": "ba8a396d2f418e53a6c4c50575582f3f3eb11ff7"
56
+ "gitHead": "0d732d4b184adcb28cc83087603e81b764390d4b"
56
57
  }
@@ -0,0 +1,325 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { capitalCase, pascalCase } from 'change-case';
5
+ import { map, find, get } from 'lodash';
6
+
7
+ /**
8
+ * WordPress dependencies
9
+ */
10
+ import apiFetch from '@wordpress/api-fetch';
11
+ import { __ } from '@wordpress/i18n';
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+ import { addEntities } from './actions';
17
+
18
+ export const DEFAULT_ENTITY_KEY = 'id';
19
+
20
+ const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];
21
+
22
+ export const rootEntitiesConfig = [
23
+ {
24
+ label: __( 'Base' ),
25
+ kind: 'root',
26
+ name: '__unstableBase',
27
+ baseURL: '/',
28
+ baseURLParams: {
29
+ _fields: [
30
+ 'description',
31
+ 'gmt_offset',
32
+ 'home',
33
+ 'name',
34
+ 'site_icon',
35
+ 'site_icon_url',
36
+ 'site_logo',
37
+ 'timezone_string',
38
+ 'url',
39
+ ].join( ',' ),
40
+ },
41
+ },
42
+ {
43
+ label: __( 'Site' ),
44
+ name: 'site',
45
+ kind: 'root',
46
+ baseURL: '/wp/v2/settings',
47
+ getTitle: ( record ) => {
48
+ return get( record, [ 'title' ], __( 'Site Title' ) );
49
+ },
50
+ },
51
+ {
52
+ label: __( 'Post Type' ),
53
+ name: 'postType',
54
+ kind: 'root',
55
+ key: 'slug',
56
+ baseURL: '/wp/v2/types',
57
+ baseURLParams: { context: 'edit' },
58
+ },
59
+ {
60
+ name: 'media',
61
+ kind: 'root',
62
+ baseURL: '/wp/v2/media',
63
+ baseURLParams: { context: 'edit' },
64
+ plural: 'mediaItems',
65
+ label: __( 'Media' ),
66
+ rawAttributes: [ 'caption', 'title', 'description' ],
67
+ },
68
+ {
69
+ name: 'taxonomy',
70
+ kind: 'root',
71
+ key: 'slug',
72
+ baseURL: '/wp/v2/taxonomies',
73
+ baseURLParams: { context: 'edit' },
74
+ plural: 'taxonomies',
75
+ label: __( 'Taxonomy' ),
76
+ },
77
+ {
78
+ name: 'sidebar',
79
+ kind: 'root',
80
+ baseURL: '/wp/v2/sidebars',
81
+ baseURLParams: { context: 'edit' },
82
+ plural: 'sidebars',
83
+ transientEdits: { blocks: true },
84
+ label: __( 'Widget areas' ),
85
+ },
86
+ {
87
+ name: 'widget',
88
+ kind: 'root',
89
+ baseURL: '/wp/v2/widgets',
90
+ baseURLParams: { context: 'edit' },
91
+ plural: 'widgets',
92
+ transientEdits: { blocks: true },
93
+ label: __( 'Widgets' ),
94
+ },
95
+ {
96
+ name: 'widgetType',
97
+ kind: 'root',
98
+ baseURL: '/wp/v2/widget-types',
99
+ baseURLParams: { context: 'edit' },
100
+ plural: 'widgetTypes',
101
+ label: __( 'Widget types' ),
102
+ },
103
+ {
104
+ label: __( 'User' ),
105
+ name: 'user',
106
+ kind: 'root',
107
+ baseURL: '/wp/v2/users',
108
+ baseURLParams: { context: 'edit' },
109
+ plural: 'users',
110
+ },
111
+ {
112
+ name: 'comment',
113
+ kind: 'root',
114
+ baseURL: '/wp/v2/comments',
115
+ baseURLParams: { context: 'edit' },
116
+ plural: 'comments',
117
+ label: __( 'Comment' ),
118
+ },
119
+ {
120
+ name: 'menu',
121
+ kind: 'root',
122
+ baseURL: '/wp/v2/menus',
123
+ baseURLParams: { context: 'edit' },
124
+ plural: 'menus',
125
+ label: __( 'Menu' ),
126
+ },
127
+ {
128
+ name: 'menuItem',
129
+ kind: 'root',
130
+ baseURL: '/wp/v2/menu-items',
131
+ baseURLParams: { context: 'edit' },
132
+ plural: 'menuItems',
133
+ label: __( 'Menu Item' ),
134
+ rawAttributes: [ 'title' ],
135
+ },
136
+ {
137
+ name: 'menuLocation',
138
+ kind: 'root',
139
+ baseURL: '/wp/v2/menu-locations',
140
+ baseURLParams: { context: 'edit' },
141
+ plural: 'menuLocations',
142
+ label: __( 'Menu Location' ),
143
+ key: 'name',
144
+ },
145
+ {
146
+ label: __( 'Global Styles' ),
147
+ name: 'globalStyles',
148
+ kind: 'root',
149
+ baseURL: '/wp/v2/global-styles',
150
+ baseURLParams: { context: 'edit' },
151
+ plural: 'globalStylesVariations', // Should be different than name.
152
+ getTitle: ( record ) => record?.title?.rendered || record?.title,
153
+ },
154
+ {
155
+ label: __( 'Themes' ),
156
+ name: 'theme',
157
+ kind: 'root',
158
+ baseURL: '/wp/v2/themes',
159
+ baseURLParams: { context: 'edit' },
160
+ key: 'stylesheet',
161
+ },
162
+ {
163
+ label: __( 'Plugins' ),
164
+ name: 'plugin',
165
+ kind: 'root',
166
+ baseURL: '/wp/v2/plugins',
167
+ baseURLParams: { context: 'edit' },
168
+ key: 'plugin',
169
+ },
170
+ ];
171
+
172
+ export const additionalEntityConfigLoaders = [
173
+ { kind: 'postType', loadEntities: loadPostTypeEntities },
174
+ { kind: 'taxonomy', loadEntities: loadTaxonomyEntities },
175
+ ];
176
+
177
+ /**
178
+ * Returns a function to be used to retrieve extra edits to apply before persisting a post type.
179
+ *
180
+ * @param {Object} persistedRecord Already persisted Post
181
+ * @param {Object} edits Edits.
182
+ * @return {Object} Updated edits.
183
+ */
184
+ export const prePersistPostType = ( persistedRecord, edits ) => {
185
+ const newEdits = {};
186
+
187
+ if ( persistedRecord?.status === 'auto-draft' ) {
188
+ // Saving an auto-draft should create a draft by default.
189
+ if ( ! edits.status && ! newEdits.status ) {
190
+ newEdits.status = 'draft';
191
+ }
192
+
193
+ // Fix the auto-draft default title.
194
+ if (
195
+ ( ! edits.title || edits.title === 'Auto Draft' ) &&
196
+ ! newEdits.title &&
197
+ ( ! persistedRecord?.title ||
198
+ persistedRecord?.title === 'Auto Draft' )
199
+ ) {
200
+ newEdits.title = '';
201
+ }
202
+ }
203
+
204
+ return newEdits;
205
+ };
206
+
207
+ /**
208
+ * Returns the list of post type entities.
209
+ *
210
+ * @return {Promise} Entities promise
211
+ */
212
+ async function loadPostTypeEntities() {
213
+ const postTypes = await apiFetch( {
214
+ path: '/wp/v2/types?context=view',
215
+ } );
216
+ return map( postTypes, ( postType, name ) => {
217
+ const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
218
+ name
219
+ );
220
+ const namespace = postType?.rest_namespace ?? 'wp/v2';
221
+ return {
222
+ kind: 'postType',
223
+ baseURL: `/${ namespace }/${ postType.rest_base }`,
224
+ baseURLParams: { context: 'edit' },
225
+ name,
226
+ label: postType.name,
227
+ transientEdits: {
228
+ blocks: true,
229
+ selection: true,
230
+ },
231
+ mergedEdits: { meta: true },
232
+ rawAttributes: POST_RAW_ATTRIBUTES,
233
+ getTitle: ( record ) =>
234
+ record?.title?.rendered ||
235
+ record?.title ||
236
+ ( isTemplate
237
+ ? capitalCase( record.slug ?? '' )
238
+ : String( record.id ) ),
239
+ __unstablePrePersist: isTemplate ? undefined : prePersistPostType,
240
+ __unstable_rest_base: postType.rest_base,
241
+ };
242
+ } );
243
+ }
244
+
245
+ /**
246
+ * Returns the list of the taxonomies entities.
247
+ *
248
+ * @return {Promise} Entities promise
249
+ */
250
+ async function loadTaxonomyEntities() {
251
+ const taxonomies = await apiFetch( {
252
+ path: '/wp/v2/taxonomies?context=view',
253
+ } );
254
+ return map( taxonomies, ( taxonomy, name ) => {
255
+ const namespace = taxonomy?.rest_namespace ?? 'wp/v2';
256
+ return {
257
+ kind: 'taxonomy',
258
+ baseURL: `/${ namespace }/${ taxonomy.rest_base }`,
259
+ baseURLParams: { context: 'edit' },
260
+ name,
261
+ label: taxonomy.name,
262
+ };
263
+ } );
264
+ }
265
+
266
+ /**
267
+ * Returns the entity's getter method name given its kind and name.
268
+ *
269
+ * @example
270
+ * ```js
271
+ * const nameSingular = getMethodName( 'root', 'theme', 'get' );
272
+ * // nameSingular is getRootTheme
273
+ *
274
+ * const namePlural = getMethodName( 'root', 'theme', 'set' );
275
+ * // namePlural is setRootThemes
276
+ * ```
277
+ *
278
+ * @param {string} kind Entity kind.
279
+ * @param {string} name Entity name.
280
+ * @param {string} prefix Function prefix.
281
+ * @param {boolean} usePlural Whether to use the plural form or not.
282
+ *
283
+ * @return {string} Method name
284
+ */
285
+ export const getMethodName = (
286
+ kind,
287
+ name,
288
+ prefix = 'get',
289
+ usePlural = false
290
+ ) => {
291
+ const entityConfig = find( rootEntitiesConfig, { kind, name } );
292
+ const kindPrefix = kind === 'root' ? '' : pascalCase( kind );
293
+ const nameSuffix = pascalCase( name ) + ( usePlural ? 's' : '' );
294
+ const suffix =
295
+ usePlural && 'plural' in entityConfig && entityConfig?.plural
296
+ ? pascalCase( entityConfig.plural )
297
+ : nameSuffix;
298
+ return `${ prefix }${ kindPrefix }${ suffix }`;
299
+ };
300
+
301
+ /**
302
+ * Loads the kind entities into the store.
303
+ *
304
+ * @param {string} kind Kind
305
+ *
306
+ * @return {(thunkArgs: object) => Promise<Array>} Entities
307
+ */
308
+ export const getOrLoadEntitiesConfig =
309
+ ( kind ) =>
310
+ async ( { select, dispatch } ) => {
311
+ let configs = select.getEntitiesConfig( kind );
312
+ if ( configs && configs.length !== 0 ) {
313
+ return configs;
314
+ }
315
+
316
+ const loader = find( additionalEntityConfigLoaders, { kind } );
317
+ if ( ! loader ) {
318
+ return [];
319
+ }
320
+
321
+ configs = await loader.loadEntities();
322
+ dispatch( addEntities( configs ) );
323
+
324
+ return configs;
325
+ };
@@ -13,7 +13,6 @@ import type {
13
13
  } from './helpers';
14
14
 
15
15
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
16
- import type { DefaultContextOf } from './index';
17
16
 
18
17
  declare module './base-entity-records' {
19
18
  export namespace BaseEntityRecords {
@@ -142,6 +141,6 @@ declare module './base-entity-records' {
142
141
  }
143
142
  }
144
143
 
145
- export type Attachment<
146
- C extends Context = DefaultContextOf< 'root', 'media' >
147
- > = OmitNevers< _BaseEntityRecords.Attachment< C > >;
144
+ export type Attachment< C extends Context = 'edit' > = OmitNevers<
145
+ _BaseEntityRecords.Attachment< C >
146
+ >;
@@ -9,7 +9,6 @@ import type {
9
9
  RenderedText,
10
10
  } from './helpers';
11
11
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
12
- import type { DefaultContextOf } from './index';
13
12
 
14
13
  export type CommentStatus = 'hold' | 'approve' | 'spam' | 'trash' | '1' | '0';
15
14
 
@@ -92,6 +91,6 @@ declare module './base-entity-records' {
92
91
  }
93
92
  }
94
93
 
95
- export type Comment<
96
- C extends Context = DefaultContextOf< 'root', 'comment' >
97
- > = OmitNevers< _BaseEntityRecords.Comment< C > >;
94
+ export type Comment< C extends Context = 'edit' > = OmitNevers<
95
+ _BaseEntityRecords.Comment< C >
96
+ >;
@@ -20,9 +20,7 @@ import type { Widget } from './widget';
20
20
  import type { WidgetType } from './widget-type';
21
21
  import type { WpTemplate } from './wp-template';
22
22
  import type { WpTemplatePart } from './wp-template-part';
23
- import type { CoreEntities } from '../entities';
24
23
 
25
- export type { EntityType } from './entities';
26
24
  export type { BaseEntityRecords } from './base-entity-records';
27
25
 
28
26
  export type {
@@ -48,8 +46,6 @@ export type {
48
46
  WpTemplatePart,
49
47
  };
50
48
 
51
- export type UpdatableEntityRecord = Updatable< EntityRecord< 'edit' > >;
52
-
53
49
  /**
54
50
  * An interface that may be extended to add types for new entities. Each entry
55
51
  * must be a union of entity definitions adhering to the EntityInterface type.
@@ -60,105 +56,52 @@ export type UpdatableEntityRecord = Updatable< EntityRecord< 'edit' > >;
60
56
  * import type { Context } from '@wordpress/core-data';
61
57
  * // ...
62
58
  *
63
- * interface Order {
59
+ * interface Client {
64
60
  * id: number;
65
- * clientId: number;
61
+ * name: string;
66
62
  * // ...
67
63
  * }
68
64
  *
69
- * type OrderEntity = {
70
- * kind: 'myPlugin';
71
- * name: 'order';
72
- * recordType: Order;
65
+ * interface Order< C extends Context > {
66
+ * id: number;
67
+ * name: string;
68
+ * // ...
73
69
  * }
74
70
  *
75
71
  * declare module '@wordpress/core-data' {
76
- * export interface PerPackageEntities< C extends Context > {
77
- * myPlugin: OrderEntity | ClientEntity
72
+ * export interface PerPackageEntityRecords< C extends Context > {
73
+ * myPlugin: Client | Order<C>>
78
74
  * }
79
75
  * }
80
76
  *
81
- * const c = getEntityRecord( 'myPlugin', 'order', 15 );
77
+ * const c = getEntityRecord<Order>( 'myPlugin', 'order', 15 );
82
78
  * // c is of the type Order
83
79
  * ```
84
80
  */
85
- export interface PerPackageEntityConfig< C extends Context > {
86
- core: CoreEntities< C >;
81
+ export interface PerPackageEntityRecords< C extends Context > {
82
+ core:
83
+ | Attachment< C >
84
+ | Comment< C >
85
+ | MenuLocation< C >
86
+ | NavMenu< C >
87
+ | NavMenuItem< C >
88
+ | Page< C >
89
+ | Plugin< C >
90
+ | Post< C >
91
+ | Settings< C >
92
+ | Sidebar< C >
93
+ | Taxonomy< C >
94
+ | Theme< C >
95
+ | User< C >
96
+ | Type< C >
97
+ | Widget< C >
98
+ | WidgetType< C >
99
+ | WpTemplate< C >
100
+ | WpTemplatePart< C >;
87
101
  }
88
102
 
89
- /**
90
- * A union of all the registered entities.
91
- */
92
- type EntityConfig< C extends Context = any > =
93
- PerPackageEntityConfig< C >[ keyof PerPackageEntityConfig< C > ];
94
-
95
103
  /**
96
104
  * A union of all known record types.
97
105
  */
98
- export type EntityRecord< C extends Context = any > =
99
- EntityConfig< C >[ 'record' ];
100
-
101
- /**
102
- * An entity corresponding to a specified record type.
103
- */
104
- export type EntityConfigOf<
105
- RecordOrKind extends EntityRecord | Kind,
106
- N extends Name | undefined = undefined
107
- > = RecordOrKind extends EntityRecord
108
- ? Extract< EntityConfig, { record: RecordOrKind } >
109
- : Extract< EntityConfig, { config: { kind: RecordOrKind; name: N } } >;
110
-
111
- /**
112
- * Name of the requested entity.
113
- */
114
- export type NameOf< R extends EntityRecord > =
115
- EntityConfigOf< R >[ 'config' ][ 'name' ];
116
-
117
- /**
118
- * Kind of the requested entity.
119
- */
120
- export type KindOf< R extends EntityRecord > =
121
- EntityConfigOf< R >[ 'config' ][ 'kind' ];
122
-
123
- /**
124
- * Primary key type of the requested entity, sourced from PerPackageEntities.
125
- *
126
- * For core entities, the key type is computed using the entity configuration in entities.js.
127
- */
128
- export type KeyOf<
129
- RecordOrKind extends EntityRecord | Kind,
130
- N extends Name | undefined = undefined,
131
- E extends EntityConfig = EntityConfigOf< RecordOrKind, N >
132
- > = ( E[ 'key' ] extends keyof E[ 'record' ]
133
- ? E[ 'record' ][ E[ 'key' ] ]
134
- : never ) &
135
- ( number | string );
136
-
137
- /**
138
- * Default context of the requested entity, sourced from PerPackageEntities.
139
- *
140
- * For core entities, the default context is extracted from the entity configuration
141
- * in entities.js.
142
- */
143
- export type DefaultContextOf<
144
- RecordOrKind extends EntityRecord | Kind,
145
- N extends Name | undefined = undefined
146
- > = EntityConfigOf< RecordOrKind, N >[ 'defaultContext' ];
147
-
148
- /**
149
- * An entity record type associated with specified kind and name, sourced from PerPackageEntities.
150
- */
151
- export type EntityRecordOf<
152
- K extends Kind,
153
- N extends Name,
154
- C extends Context = DefaultContextOf< K, N >
155
- > = Extract< EntityConfig< C >, { config: { kind: K; name: N } } >[ 'record' ];
156
-
157
- /**
158
- * A union of all known entity kinds.
159
- */
160
- export type Kind = EntityConfig[ 'config' ][ 'kind' ];
161
- /**
162
- * A union of all known entity names.
163
- */
164
- export type Name = EntityConfig[ 'config' ][ 'name' ];
106
+ export type EntityRecord< C extends Context = 'edit' > =
107
+ PerPackageEntityRecords< C >[ keyof PerPackageEntityRecords< C > ];
@@ -4,7 +4,6 @@
4
4
  import type { Context, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -25,6 +24,6 @@ declare module './base-entity-records' {
25
24
  }
26
25
  }
27
26
 
28
- export type MenuLocation<
29
- C extends Context = DefaultContextOf< 'root', 'menuLocation' >
30
- > = OmitNevers< _BaseEntityRecords.MenuLocation< C > >;
27
+ export type MenuLocation< C extends Context = 'edit' > = OmitNevers<
28
+ _BaseEntityRecords.MenuLocation< C >
29
+ >;
@@ -9,7 +9,6 @@ import type {
9
9
  } from './helpers';
10
10
 
11
11
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
12
- import type { DefaultContextOf } from './index';
13
12
 
14
13
  export type NavMenuItemType =
15
14
  | 'taxonomy'
@@ -107,6 +106,6 @@ declare module './base-entity-records' {
107
106
  }
108
107
  }
109
108
 
110
- export type NavMenuItem<
111
- C extends Context = DefaultContextOf< 'root', 'menuItem' >
112
- > = OmitNevers< _BaseEntityRecords.NavMenuItem< C > >;
109
+ export type NavMenuItem< C extends Context = 'edit' > = OmitNevers<
110
+ _BaseEntityRecords.NavMenuItem< C >
111
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, ContextualField, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -49,5 +48,6 @@ declare module './base-entity-records' {
49
48
  }
50
49
  }
51
50
 
52
- export type NavMenu< C extends Context = DefaultContextOf< 'root', 'menu' > > =
53
- OmitNevers< _BaseEntityRecords.NavMenu< C > >;
51
+ export type NavMenu< C extends Context = 'edit' > = OmitNevers<
52
+ _BaseEntityRecords.NavMenu< C >
53
+ >;
@@ -12,7 +12,6 @@ import type {
12
12
  } from './helpers';
13
13
 
14
14
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
15
- import type { DefaultContextOf } from './index';
16
15
 
17
16
  declare module './base-entity-records' {
18
17
  export namespace BaseEntityRecords {
@@ -140,5 +139,6 @@ declare module './base-entity-records' {
140
139
  }
141
140
  }
142
141
 
143
- export type Page< C extends Context = DefaultContextOf< 'postType', 'page' > > =
144
- OmitNevers< _BaseEntityRecords.Page< C > >;
142
+ export type Page< C extends Context = 'edit' > = OmitNevers<
143
+ _BaseEntityRecords.Page< C >
144
+ >;
@@ -9,7 +9,6 @@ import type {
9
9
  } from './helpers';
10
10
 
11
11
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
12
- import type { DefaultContextOf } from './index';
13
12
 
14
13
  declare module './base-entity-records' {
15
14
  export namespace BaseEntityRecords {
@@ -75,5 +74,6 @@ declare module './base-entity-records' {
75
74
  }
76
75
 
77
76
  export type PluginStatus = 'active' | 'inactive';
78
- export type Plugin< C extends Context = DefaultContextOf< 'root', 'plugin' > > =
79
- OmitNevers< _BaseEntityRecords.Plugin< C > >;
77
+ export type Plugin< C extends Context = 'edit' > = OmitNevers<
78
+ _BaseEntityRecords.Plugin< C >
79
+ >;
@@ -13,7 +13,6 @@ import type {
13
13
  } from './helpers';
14
14
 
15
15
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
16
- import type { DefaultContextOf } from './index';
17
16
 
18
17
  declare module './base-entity-records' {
19
18
  export namespace BaseEntityRecords {
@@ -149,5 +148,6 @@ declare module './base-entity-records' {
149
148
  }
150
149
  }
151
150
 
152
- export type Post< C extends Context = DefaultContextOf< 'postType', 'post' > > =
153
- OmitNevers< _BaseEntityRecords.Post< C > >;
151
+ export type Post< C extends Context = 'edit' > = OmitNevers<
152
+ _BaseEntityRecords.Post< C >
153
+ >;