@wordpress/core-data 6.16.0 → 6.17.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 (236) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/actions.js +69 -134
  3. package/build/actions.js.map +1 -1
  4. package/build/batch/create-batch.js +0 -22
  5. package/build/batch/create-batch.js.map +1 -1
  6. package/build/batch/default-processor.js +3 -15
  7. package/build/batch/default-processor.js.map +1 -1
  8. package/build/batch/index.js +0 -3
  9. package/build/batch/index.js.map +1 -1
  10. package/build/entities.js +109 -36
  11. package/build/entities.js.map +1 -1
  12. package/build/entity-provider.js +34 -53
  13. package/build/entity-provider.js.map +1 -1
  14. package/build/entity-types/attachment.js.map +1 -1
  15. package/build/entity-types/base-entity-records.js +0 -3
  16. package/build/entity-types/base-entity-records.js.map +1 -1
  17. package/build/entity-types/comment.js.map +1 -1
  18. package/build/entity-types/helpers.js.map +1 -1
  19. package/build/entity-types/index.js.map +1 -1
  20. package/build/entity-types/menu-location.js.map +1 -1
  21. package/build/entity-types/nav-menu-item.js.map +1 -1
  22. package/build/entity-types/nav-menu.js.map +1 -1
  23. package/build/entity-types/page.js.map +1 -1
  24. package/build/entity-types/plugin.js.map +1 -1
  25. package/build/entity-types/post.js.map +1 -1
  26. package/build/entity-types/settings.js.map +1 -1
  27. package/build/entity-types/sidebar.js.map +1 -1
  28. package/build/entity-types/taxonomy.js.map +1 -1
  29. package/build/entity-types/theme.js.map +1 -1
  30. package/build/entity-types/type.js.map +1 -1
  31. package/build/entity-types/user.js.map +1 -1
  32. package/build/entity-types/widget-type.js.map +1 -1
  33. package/build/entity-types/widget.js.map +1 -1
  34. package/build/entity-types/wp-template-part.js.map +1 -1
  35. package/build/entity-types/wp-template.js.map +1 -1
  36. package/build/fetch/__experimental-fetch-link-suggestions.js +14 -20
  37. package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
  38. package/build/fetch/__experimental-fetch-url-data.js +4 -13
  39. package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
  40. package/build/fetch/index.js +0 -3
  41. package/build/fetch/index.js.map +1 -1
  42. package/build/hooks/constants.js +4 -5
  43. package/build/hooks/constants.js.map +1 -1
  44. package/build/hooks/index.js +0 -5
  45. package/build/hooks/index.js.map +1 -1
  46. package/build/hooks/memoize.js +0 -3
  47. package/build/hooks/memoize.js.map +1 -1
  48. package/build/hooks/use-entity-record.js +0 -9
  49. package/build/hooks/use-entity-record.js.map +1 -1
  50. package/build/hooks/use-entity-records.js +2 -9
  51. package/build/hooks/use-entity-records.js.map +1 -1
  52. package/build/hooks/use-query-select.js +1 -13
  53. package/build/hooks/use-query-select.js.map +1 -1
  54. package/build/hooks/use-resource-permissions.js +0 -15
  55. package/build/hooks/use-resource-permissions.js.map +1 -1
  56. package/build/index.js +9 -41
  57. package/build/index.js.map +1 -1
  58. package/build/locks/actions.js +1 -6
  59. package/build/locks/actions.js.map +1 -1
  60. package/build/locks/engine.js +1 -9
  61. package/build/locks/engine.js.map +1 -1
  62. package/build/locks/reducer.js +8 -9
  63. package/build/locks/reducer.js.map +1 -1
  64. package/build/locks/selectors.js +8 -11
  65. package/build/locks/selectors.js.map +1 -1
  66. package/build/locks/utils.js +4 -19
  67. package/build/locks/utils.js.map +1 -1
  68. package/build/name.js +0 -1
  69. package/build/name.js.map +1 -1
  70. package/build/private-apis.js +1 -2
  71. package/build/private-apis.js.map +1 -1
  72. package/build/private-selectors.js +2 -5
  73. package/build/private-selectors.js.map +1 -1
  74. package/build/queried-data/actions.js +4 -6
  75. package/build/queried-data/actions.js.map +1 -1
  76. package/build/queried-data/get-query-parts.js +11 -24
  77. package/build/queried-data/get-query-parts.js.map +1 -1
  78. package/build/queried-data/index.js +0 -6
  79. package/build/queried-data/index.js.map +1 -1
  80. package/build/queried-data/reducer.js +33 -51
  81. package/build/queried-data/reducer.js.map +1 -1
  82. package/build/queried-data/selectors.js +4 -25
  83. package/build/queried-data/selectors.js.map +1 -1
  84. package/build/reducer.js +76 -128
  85. package/build/reducer.js.map +1 -1
  86. package/build/resolvers.js +91 -133
  87. package/build/resolvers.js.map +1 -1
  88. package/build/selectors.js +51 -145
  89. package/build/selectors.js.map +1 -1
  90. package/build/sync.js +19 -0
  91. package/build/sync.js.map +1 -0
  92. package/build/types.js.map +1 -1
  93. package/build/utils/conservative-map-item.js +3 -10
  94. package/build/utils/conservative-map-item.js.map +1 -1
  95. package/build/utils/forward-resolver.js +0 -2
  96. package/build/utils/forward-resolver.js.map +1 -1
  97. package/build/utils/get-normalized-comma-separable.js +0 -3
  98. package/build/utils/get-normalized-comma-separable.js.map +1 -1
  99. package/build/utils/if-matching-action.js +0 -3
  100. package/build/utils/if-matching-action.js.map +1 -1
  101. package/build/utils/index.js +0 -10
  102. package/build/utils/index.js.map +1 -1
  103. package/build/utils/is-raw-attribute.js +0 -1
  104. package/build/utils/is-raw-attribute.js.map +1 -1
  105. package/build/utils/on-sub-key.js +5 -9
  106. package/build/utils/on-sub-key.js.map +1 -1
  107. package/build/utils/replace-action.js +0 -2
  108. package/build/utils/replace-action.js.map +1 -1
  109. package/build/utils/set-nested-value.js +0 -4
  110. package/build/utils/set-nested-value.js.map +1 -1
  111. package/build/utils/with-weak-map-cache.js +3 -6
  112. package/build/utils/with-weak-map-cache.js.map +1 -1
  113. package/build-module/actions.js +72 -95
  114. package/build-module/actions.js.map +1 -1
  115. package/build-module/batch/create-batch.js +1 -20
  116. package/build-module/batch/create-batch.js.map +1 -1
  117. package/build-module/batch/default-processor.js +4 -13
  118. package/build-module/batch/default-processor.js.map +1 -1
  119. package/build-module/batch/index.js.map +1 -1
  120. package/build-module/entities.js +109 -25
  121. package/build-module/entities.js.map +1 -1
  122. package/build-module/entity-provider.js +35 -45
  123. package/build-module/entity-provider.js.map +1 -1
  124. package/build-module/entity-types/attachment.js.map +1 -1
  125. package/build-module/entity-types/base-entity-records.js +0 -2
  126. package/build-module/entity-types/base-entity-records.js.map +1 -1
  127. package/build-module/entity-types/comment.js.map +1 -1
  128. package/build-module/entity-types/helpers.js.map +1 -1
  129. package/build-module/entity-types/index.js.map +1 -1
  130. package/build-module/entity-types/menu-location.js.map +1 -1
  131. package/build-module/entity-types/nav-menu-item.js.map +1 -1
  132. package/build-module/entity-types/nav-menu.js.map +1 -1
  133. package/build-module/entity-types/page.js.map +1 -1
  134. package/build-module/entity-types/plugin.js.map +1 -1
  135. package/build-module/entity-types/post.js.map +1 -1
  136. package/build-module/entity-types/settings.js.map +1 -1
  137. package/build-module/entity-types/sidebar.js.map +1 -1
  138. package/build-module/entity-types/taxonomy.js.map +1 -1
  139. package/build-module/entity-types/theme.js.map +1 -1
  140. package/build-module/entity-types/type.js.map +1 -1
  141. package/build-module/entity-types/user.js.map +1 -1
  142. package/build-module/entity-types/widget-type.js.map +1 -1
  143. package/build-module/entity-types/widget.js.map +1 -1
  144. package/build-module/entity-types/wp-template-part.js.map +1 -1
  145. package/build-module/entity-types/wp-template.js.map +1 -1
  146. package/build-module/fetch/__experimental-fetch-link-suggestions.js +15 -15
  147. package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
  148. package/build-module/fetch/__experimental-fetch-url-data.js +5 -10
  149. package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
  150. package/build-module/fetch/index.js.map +1 -1
  151. package/build-module/hooks/constants.js +3 -4
  152. package/build-module/hooks/constants.js.map +1 -1
  153. package/build-module/hooks/index.js.map +1 -1
  154. package/build-module/hooks/memoize.js +2 -1
  155. package/build-module/hooks/memoize.js.map +1 -1
  156. package/build-module/hooks/use-entity-record.js +1 -3
  157. package/build-module/hooks/use-entity-record.js.map +1 -1
  158. package/build-module/hooks/use-entity-records.js +2 -3
  159. package/build-module/hooks/use-entity-records.js.map +1 -1
  160. package/build-module/hooks/use-query-select.js +1 -9
  161. package/build-module/hooks/use-query-select.js.map +1 -1
  162. package/build-module/hooks/use-resource-permissions.js +1 -10
  163. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  164. package/build-module/index.js +11 -19
  165. package/build-module/index.js.map +1 -1
  166. package/build-module/locks/actions.js +0 -3
  167. package/build-module/locks/actions.js.map +1 -1
  168. package/build-module/locks/engine.js +0 -5
  169. package/build-module/locks/engine.js.map +1 -1
  170. package/build-module/locks/reducer.js +7 -7
  171. package/build-module/locks/reducer.js.map +1 -1
  172. package/build-module/locks/selectors.js +7 -8
  173. package/build-module/locks/selectors.js.map +1 -1
  174. package/build-module/locks/utils.js +4 -14
  175. package/build-module/locks/utils.js.map +1 -1
  176. package/build-module/name.js.map +1 -1
  177. package/build-module/private-apis.js.map +1 -1
  178. package/build-module/private-selectors.js +2 -2
  179. package/build-module/private-selectors.js.map +1 -1
  180. package/build-module/queried-data/actions.js +4 -3
  181. package/build-module/queried-data/actions.js.map +1 -1
  182. package/build-module/queried-data/get-query-parts.js +13 -21
  183. package/build-module/queried-data/get-query-parts.js.map +1 -1
  184. package/build-module/queried-data/index.js.map +1 -1
  185. package/build-module/queried-data/reducer.js +33 -42
  186. package/build-module/queried-data/reducer.js.map +1 -1
  187. package/build-module/queried-data/selectors.js +6 -21
  188. package/build-module/queried-data/selectors.js.map +1 -1
  189. package/build-module/reducer.js +79 -104
  190. package/build-module/reducer.js.map +1 -1
  191. package/build-module/resolvers.js +94 -96
  192. package/build-module/resolvers.js.map +1 -1
  193. package/build-module/selectors.js +56 -92
  194. package/build-module/selectors.js.map +1 -1
  195. package/build-module/sync.js +12 -0
  196. package/build-module/sync.js.map +1 -0
  197. package/build-module/types.js.map +1 -1
  198. package/build-module/utils/conservative-map-item.js +4 -8
  199. package/build-module/utils/conservative-map-item.js.map +1 -1
  200. package/build-module/utils/forward-resolver.js +0 -1
  201. package/build-module/utils/forward-resolver.js.map +1 -1
  202. package/build-module/utils/get-normalized-comma-separable.js +0 -2
  203. package/build-module/utils/get-normalized-comma-separable.js.map +1 -1
  204. package/build-module/utils/if-matching-action.js +0 -2
  205. package/build-module/utils/if-matching-action.js.map +1 -1
  206. package/build-module/utils/index.js.map +1 -1
  207. package/build-module/utils/is-raw-attribute.js.map +1 -1
  208. package/build-module/utils/on-sub-key.js +5 -7
  209. package/build-module/utils/on-sub-key.js.map +1 -1
  210. package/build-module/utils/replace-action.js +0 -1
  211. package/build-module/utils/replace-action.js.map +1 -1
  212. package/build-module/utils/set-nested-value.js +0 -3
  213. package/build-module/utils/set-nested-value.js.map +1 -1
  214. package/build-module/utils/with-weak-map-cache.js +3 -5
  215. package/build-module/utils/with-weak-map-cache.js.map +1 -1
  216. package/build-types/actions.d.ts.map +1 -1
  217. package/build-types/entities.d.ts +53 -0
  218. package/build-types/entities.d.ts.map +1 -1
  219. package/build-types/entity-provider.d.ts +1 -1
  220. package/build-types/entity-provider.d.ts.map +1 -1
  221. package/build-types/hooks/memoize.d.ts +1 -1
  222. package/build-types/hooks/memoize.d.ts.map +1 -1
  223. package/build-types/index.d.ts +3 -3
  224. package/build-types/index.d.ts.map +1 -1
  225. package/build-types/resolvers.d.ts.map +1 -1
  226. package/build-types/sync.d.ts +2 -0
  227. package/build-types/sync.d.ts.map +1 -0
  228. package/package.json +17 -15
  229. package/src/actions.js +24 -14
  230. package/src/entities.js +91 -0
  231. package/src/entity-provider.js +11 -2
  232. package/src/resolvers.js +89 -41
  233. package/src/sync.js +18 -0
  234. package/src/test/entity-provider.js +272 -0
  235. package/tsconfig.json +1 -0
  236. package/tsconfig.tsbuildinfo +1 -1
@@ -1,24 +1,17 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.getThemeSupports = exports.getRawEntityRecord = exports.getNavigationFallbackId = exports.getEntityRecords = exports.getEntityRecord = exports.getEmbedPreview = exports.getEditedEntityRecord = exports.getCurrentUser = exports.getCurrentThemeGlobalStylesRevisions = exports.getCurrentTheme = exports.getBlockPatterns = exports.getBlockPatternCategories = exports.getAutosaves = exports.getAutosave = exports.getAuthors = exports.canUserEditEntityRecord = exports.canUser = exports.__experimentalGetTemplateForLink = exports.__experimentalGetCurrentThemeGlobalStylesVariations = exports.__experimentalGetCurrentThemeBaseGlobalStyles = exports.__experimentalGetCurrentGlobalStylesId = void 0;
9
-
10
8
  var _changeCase = require("change-case");
11
-
12
9
  var _url = require("@wordpress/url");
13
-
14
10
  var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
15
-
16
11
  var _name = require("./name");
17
-
18
12
  var _entities = require("./entities");
19
-
20
13
  var _utils = require("./utils");
21
-
14
+ var _sync = require("./sync");
22
15
  /**
23
16
  * External dependencies
24
17
  */
@@ -46,13 +39,11 @@ const getAuthors = query => async ({
46
39
  });
47
40
  dispatch.receiveUserQuery(path, users);
48
41
  };
42
+
49
43
  /**
50
44
  * Requests the current user from the REST API.
51
45
  */
52
-
53
-
54
46
  exports.getAuthors = getAuthors;
55
-
56
47
  const getCurrentUser = () => async ({
57
48
  dispatch
58
49
  }) => {
@@ -61,6 +52,7 @@ const getCurrentUser = () => async ({
61
52
  });
62
53
  dispatch.receiveCurrentUser(currentUser);
63
54
  };
55
+
64
56
  /**
65
57
  * Requests an entity's record from the REST API.
66
58
  *
@@ -71,80 +63,101 @@ const getCurrentUser = () => async ({
71
63
  * include with request. If requesting specific
72
64
  * fields, fields must always include the ID.
73
65
  */
74
-
75
-
76
66
  exports.getCurrentUser = getCurrentUser;
77
-
78
67
  const getEntityRecord = (kind, name, key = '', query) => async ({
79
68
  select,
80
69
  dispatch
81
70
  }) => {
82
71
  const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(kind));
83
72
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
84
-
85
73
  if (!entityConfig || entityConfig?.__experimentalNoFetch) {
86
74
  return;
87
75
  }
88
-
89
76
  const lock = await dispatch.__unstableAcquireStoreLock(_name.STORE_NAME, ['entities', 'records', kind, name, key], {
90
77
  exclusive: false
91
78
  });
92
-
93
79
  try {
94
- if (query !== undefined && query._fields) {
95
- // If requesting specific fields, items and query association to said
96
- // records are stored by ID reference. Thus, fields must always include
97
- // the ID.
98
- query = { ...query,
99
- _fields: [...new Set([...((0, _utils.getNormalizedCommaSeparable)(query._fields) || []), entityConfig.key || _entities.DEFAULT_ENTITY_KEY])].join()
100
- };
101
- } // Disable reason: While true that an early return could leave `path`
102
- // unused, it's important that path is derived using the query prior to
103
- // additional query modifications in the condition below, since those
104
- // modifications are relevant to how the data is tracked in state, and not
105
- // for how the request is made to the REST API.
106
- // eslint-disable-next-line @wordpress/no-unused-vars-before-return
107
-
108
-
109
- const path = (0, _url.addQueryArgs)(entityConfig.baseURL + (key ? '/' + key : ''), { ...entityConfig.baseURLParams,
110
- ...query
111
- });
80
+ // Entity supports configs,
81
+ // use the sync algorithm instead of the old fetch behavior.
82
+ if (window.__experimentalEnableSync && entityConfig.syncConfig && !query) {
83
+ const objectId = entityConfig.getSyncObjectId(key);
84
+
85
+ // Loads the persisted document.
86
+ await (0, _sync.getSyncProvider)().bootstrap(entityConfig.syncObjectType, objectId, record => {
87
+ dispatch.receiveEntityRecords(kind, name, record, query);
88
+ });
112
89
 
113
- if (query !== undefined) {
114
- query = { ...query,
115
- include: [key]
116
- }; // The resolution cache won't consider query as reusable based on the
117
- // fields, so it's tested here, prior to initiating the REST request,
118
- // and without causing `getEntityRecords` resolution to occur.
90
+ // Boostraps the edited document as well (and load from peers).
91
+ await (0, _sync.getSyncProvider)().bootstrap(entityConfig.syncObjectType + '--edit', objectId, record => {
92
+ dispatch({
93
+ type: 'EDIT_ENTITY_RECORD',
94
+ kind,
95
+ name,
96
+ recordId: key,
97
+ edits: record,
98
+ meta: {
99
+ undo: undefined
100
+ }
101
+ });
102
+ });
103
+ } else {
104
+ if (query !== undefined && query._fields) {
105
+ // If requesting specific fields, items and query association to said
106
+ // records are stored by ID reference. Thus, fields must always include
107
+ // the ID.
108
+ query = {
109
+ ...query,
110
+ _fields: [...new Set([...((0, _utils.getNormalizedCommaSeparable)(query._fields) || []), entityConfig.key || _entities.DEFAULT_ENTITY_KEY])].join()
111
+ };
112
+ }
119
113
 
120
- const hasRecords = select.hasEntityRecords(kind, name, query);
114
+ // Disable reason: While true that an early return could leave `path`
115
+ // unused, it's important that path is derived using the query prior to
116
+ // additional query modifications in the condition below, since those
117
+ // modifications are relevant to how the data is tracked in state, and not
118
+ // for how the request is made to the REST API.
121
119
 
122
- if (hasRecords) {
123
- return;
120
+ // eslint-disable-next-line @wordpress/no-unused-vars-before-return
121
+ const path = (0, _url.addQueryArgs)(entityConfig.baseURL + (key ? '/' + key : ''), {
122
+ ...entityConfig.baseURLParams,
123
+ ...query
124
+ });
125
+ if (query !== undefined) {
126
+ query = {
127
+ ...query,
128
+ include: [key]
129
+ };
130
+
131
+ // The resolution cache won't consider query as reusable based on the
132
+ // fields, so it's tested here, prior to initiating the REST request,
133
+ // and without causing `getEntityRecords` resolution to occur.
134
+ const hasRecords = select.hasEntityRecords(kind, name, query);
135
+ if (hasRecords) {
136
+ return;
137
+ }
124
138
  }
139
+ const record = await (0, _apiFetch.default)({
140
+ path
141
+ });
142
+ dispatch.receiveEntityRecords(kind, name, record, query);
125
143
  }
126
-
127
- const record = await (0, _apiFetch.default)({
128
- path
129
- });
130
- dispatch.receiveEntityRecords(kind, name, record, query);
131
144
  } finally {
132
145
  dispatch.__unstableReleaseStoreLock(lock);
133
146
  }
134
147
  };
148
+
135
149
  /**
136
150
  * Requests an entity's record from the REST API.
137
151
  */
138
-
139
-
140
152
  exports.getEntityRecord = getEntityRecord;
141
153
  const getRawEntityRecord = (0, _utils.forwardResolver)('getEntityRecord');
154
+
142
155
  /**
143
156
  * Requests an entity's record from the REST API.
144
157
  */
145
-
146
158
  exports.getRawEntityRecord = getRawEntityRecord;
147
159
  const getEditedEntityRecord = (0, _utils.forwardResolver)('getEntityRecord');
160
+
148
161
  /**
149
162
  * Requests the entity's records from the REST API.
150
163
  *
@@ -153,42 +166,38 @@ const getEditedEntityRecord = (0, _utils.forwardResolver)('getEntityRecord');
153
166
  * @param {Object?} query Query Object. If requesting specific fields, fields
154
167
  * must always include the ID.
155
168
  */
156
-
157
169
  exports.getEditedEntityRecord = getEditedEntityRecord;
158
-
159
170
  const getEntityRecords = (kind, name, query = {}) => async ({
160
171
  dispatch
161
172
  }) => {
162
173
  const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(kind));
163
174
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
164
-
165
175
  if (!entityConfig || entityConfig?.__experimentalNoFetch) {
166
176
  return;
167
177
  }
168
-
169
178
  const lock = await dispatch.__unstableAcquireStoreLock(_name.STORE_NAME, ['entities', 'records', kind, name], {
170
179
  exclusive: false
171
180
  });
172
-
173
181
  try {
174
182
  if (query._fields) {
175
183
  // If requesting specific fields, items and query association to said
176
184
  // records are stored by ID reference. Thus, fields must always include
177
185
  // the ID.
178
- query = { ...query,
186
+ query = {
187
+ ...query,
179
188
  _fields: [...new Set([...((0, _utils.getNormalizedCommaSeparable)(query._fields) || []), entityConfig.key || _entities.DEFAULT_ENTITY_KEY])].join()
180
189
  };
181
190
  }
182
-
183
- const path = (0, _url.addQueryArgs)(entityConfig.baseURL, { ...entityConfig.baseURLParams,
191
+ const path = (0, _url.addQueryArgs)(entityConfig.baseURL, {
192
+ ...entityConfig.baseURLParams,
184
193
  ...query
185
194
  });
186
195
  let records = Object.values(await (0, _apiFetch.default)({
187
196
  path
188
- })); // If we request fields but the result doesn't contain the fields,
197
+ }));
198
+ // If we request fields but the result doesn't contain the fields,
189
199
  // explicitly set these fields as "undefined"
190
200
  // that way we consider the query "fullfilled".
191
-
192
201
  if (query._fields) {
193
202
  records = records.map(record => {
194
203
  query._fields.split(',').forEach(field => {
@@ -196,15 +205,14 @@ const getEntityRecords = (kind, name, query = {}) => async ({
196
205
  record[field] = undefined;
197
206
  }
198
207
  });
199
-
200
208
  return record;
201
209
  });
202
210
  }
211
+ dispatch.receiveEntityRecords(kind, name, records, query);
203
212
 
204
- dispatch.receiveEntityRecords(kind, name, records, query); // When requesting all fields, the list of results can be used to
213
+ // When requesting all fields, the list of results can be used to
205
214
  // resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
206
215
  // See https://github.com/WordPress/gutenberg/pull/26575
207
-
208
216
  if (!query?._fields && !query.context) {
209
217
  const key = entityConfig.key || _entities.DEFAULT_ENTITY_KEY;
210
218
  const resolutionsArgs = records.filter(record => record[key]).map(record => [kind, name, record[key]]);
@@ -223,17 +231,14 @@ const getEntityRecords = (kind, name, query = {}) => async ({
223
231
  dispatch.__unstableReleaseStoreLock(lock);
224
232
  }
225
233
  };
226
-
227
234
  exports.getEntityRecords = getEntityRecords;
228
-
229
235
  getEntityRecords.shouldInvalidate = (action, kind, name) => {
230
236
  return (action.type === 'RECEIVE_ITEMS' || action.type === 'REMOVE_ITEMS') && action.invalidateCache && kind === action.kind && name === action.name;
231
237
  };
238
+
232
239
  /**
233
240
  * Requests the current theme.
234
241
  */
235
-
236
-
237
242
  const getCurrentTheme = () => async ({
238
243
  dispatch,
239
244
  resolveSelect
@@ -243,21 +248,19 @@ const getCurrentTheme = () => async ({
243
248
  });
244
249
  dispatch.receiveCurrentTheme(activeThemes[0]);
245
250
  };
251
+
246
252
  /**
247
253
  * Requests theme supports data from the index.
248
254
  */
249
-
250
-
251
255
  exports.getCurrentTheme = getCurrentTheme;
252
256
  const getThemeSupports = (0, _utils.forwardResolver)('getCurrentTheme');
257
+
253
258
  /**
254
259
  * Requests a preview from the from the Embed API.
255
260
  *
256
261
  * @param {string} url URL to get the preview for.
257
262
  */
258
-
259
263
  exports.getThemeSupports = getThemeSupports;
260
-
261
264
  const getEmbedPreview = url => async ({
262
265
  dispatch
263
266
  }) => {
@@ -273,6 +276,7 @@ const getEmbedPreview = url => async ({
273
276
  dispatch.receiveEmbedPreview(url, false);
274
277
  }
275
278
  };
279
+
276
280
  /**
277
281
  * Checks whether the current user can perform the given action on the given
278
282
  * REST resource.
@@ -282,10 +286,7 @@ const getEmbedPreview = url => async ({
282
286
  * @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
283
287
  * @param {?string} id ID of the rest resource to check.
284
288
  */
285
-
286
-
287
289
  exports.getEmbedPreview = getEmbedPreview;
288
-
289
290
  const canUser = (requestedAction, resource, id) => async ({
290
291
  dispatch,
291
292
  registry
@@ -295,26 +296,21 @@ const canUser = (requestedAction, resource, id) => async ({
295
296
  } = registry.select(_name.STORE_NAME);
296
297
  const resourcePath = id ? `${resource}/${id}` : resource;
297
298
  const retrievedActions = ['create', 'read', 'update', 'delete'];
298
-
299
299
  if (!retrievedActions.includes(requestedAction)) {
300
300
  throw new Error(`'${requestedAction}' is not a valid action.`);
301
- } // Prevent resolving the same resource twice.
302
-
301
+ }
303
302
 
303
+ // Prevent resolving the same resource twice.
304
304
  for (const relatedAction of retrievedActions) {
305
305
  if (relatedAction === requestedAction) {
306
306
  continue;
307
307
  }
308
-
309
308
  const isAlreadyResolving = hasStartedResolution('canUser', [relatedAction, resource, id]);
310
-
311
309
  if (isAlreadyResolving) {
312
310
  return;
313
311
  }
314
312
  }
315
-
316
313
  let response;
317
-
318
314
  try {
319
315
  response = await (0, _apiFetch.default)({
320
316
  path: `/wp/v2/${resourcePath}`,
@@ -325,11 +321,11 @@ const canUser = (requestedAction, resource, id) => async ({
325
321
  // Do nothing if our OPTIONS request comes back with an API error (4xx or
326
322
  // 5xx). The previously determined isAllowed value will remain in the store.
327
323
  return;
328
- } // Optional chaining operator is used here because the API requests don't
324
+ }
325
+
326
+ // Optional chaining operator is used here because the API requests don't
329
327
  // return the expected result in the native version. Instead, API requests
330
328
  // only return the result, without including response properties like the headers.
331
-
332
-
333
329
  const allowHeader = response.headers?.get('allow');
334
330
  const allowedMethods = allowHeader?.allow || allowHeader || '';
335
331
  const permissions = {};
@@ -339,15 +335,14 @@ const canUser = (requestedAction, resource, id) => async ({
339
335
  update: 'PUT',
340
336
  delete: 'DELETE'
341
337
  };
342
-
343
338
  for (const [actionName, methodName] of Object.entries(methods)) {
344
339
  permissions[actionName] = allowedMethods.includes(methodName);
345
340
  }
346
-
347
341
  for (const action of retrievedActions) {
348
342
  dispatch.receiveUserPermission(`${action}/${resourcePath}`, permissions[action]);
349
343
  }
350
344
  };
345
+
351
346
  /**
352
347
  * Checks whether the current user can perform the given action on the given
353
348
  * REST resource.
@@ -356,33 +351,26 @@ const canUser = (requestedAction, resource, id) => async ({
356
351
  * @param {string} name Entity name.
357
352
  * @param {string} recordId Record's id.
358
353
  */
359
-
360
-
361
354
  exports.canUser = canUser;
362
-
363
355
  const canUserEditEntityRecord = (kind, name, recordId) => async ({
364
356
  dispatch
365
357
  }) => {
366
358
  const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(kind));
367
359
  const entityConfig = configs.find(config => config.name === name && config.kind === kind);
368
-
369
360
  if (!entityConfig) {
370
361
  return;
371
362
  }
372
-
373
363
  const resource = entityConfig.__unstable_rest_base;
374
364
  await dispatch(canUser('update', resource, recordId));
375
365
  };
366
+
376
367
  /**
377
368
  * Request autosave data from the REST API.
378
369
  *
379
370
  * @param {string} postType The type of the parent post.
380
371
  * @param {number} postId The id of the parent post.
381
372
  */
382
-
383
-
384
373
  exports.canUserEditEntityRecord = canUserEditEntityRecord;
385
-
386
374
  const getAutosaves = (postType, postId) => async ({
387
375
  dispatch,
388
376
  resolveSelect
@@ -394,11 +382,11 @@ const getAutosaves = (postType, postId) => async ({
394
382
  const autosaves = await (0, _apiFetch.default)({
395
383
  path: `/${restNamespace}/${restBase}/${postId}/autosaves?context=edit`
396
384
  });
397
-
398
385
  if (autosaves && autosaves.length) {
399
386
  dispatch.receiveAutosaves(postId, autosaves);
400
387
  }
401
388
  };
389
+
402
390
  /**
403
391
  * Request autosave data from the REST API.
404
392
  *
@@ -408,30 +396,24 @@ const getAutosaves = (postType, postId) => async ({
408
396
  * @param {string} postType The type of the parent post.
409
397
  * @param {number} postId The id of the parent post.
410
398
  */
411
-
412
-
413
399
  exports.getAutosaves = getAutosaves;
414
-
415
400
  const getAutosave = (postType, postId) => async ({
416
401
  resolveSelect
417
402
  }) => {
418
403
  await resolveSelect.getAutosaves(postType, postId);
419
404
  };
405
+
420
406
  /**
421
407
  * Retrieve the frontend template used for a given link.
422
408
  *
423
409
  * @param {string} link Link.
424
410
  */
425
-
426
-
427
411
  exports.getAutosave = getAutosave;
428
-
429
412
  const __experimentalGetTemplateForLink = link => async ({
430
413
  dispatch,
431
414
  resolveSelect
432
415
  }) => {
433
416
  let template;
434
-
435
417
  try {
436
418
  // This is NOT calling a REST endpoint but rather ends up with a response from
437
419
  // an Ajax function which has a different shape from a WP_REST_Response.
@@ -442,28 +424,23 @@ const __experimentalGetTemplateForLink = link => async ({
442
424
  }).then(({
443
425
  data
444
426
  }) => data);
445
- } catch (e) {// For non-FSE themes, it is possible that this request returns an error.
427
+ } catch (e) {
428
+ // For non-FSE themes, it is possible that this request returns an error.
446
429
  }
447
-
448
430
  if (!template) {
449
431
  return;
450
432
  }
451
-
452
433
  const record = await resolveSelect.getEntityRecord('postType', 'wp_template', template.id);
453
-
454
434
  if (record) {
455
435
  dispatch.receiveEntityRecords('postType', 'wp_template', [record], {
456
436
  'find-template': link
457
437
  });
458
438
  }
459
439
  };
460
-
461
440
  exports.__experimentalGetTemplateForLink = __experimentalGetTemplateForLink;
462
-
463
441
  __experimentalGetTemplateForLink.shouldInvalidate = action => {
464
442
  return (action.type === 'RECEIVE_ITEMS' || action.type === 'REMOVE_ITEMS') && action.invalidateCache && action.kind === 'postType' && action.name === 'wp_template';
465
443
  };
466
-
467
444
  const __experimentalGetCurrentGlobalStylesId = () => async ({
468
445
  dispatch,
469
446
  resolveSelect
@@ -472,18 +449,14 @@ const __experimentalGetCurrentGlobalStylesId = () => async ({
472
449
  status: 'active'
473
450
  });
474
451
  const globalStylesURL = activeThemes?.[0]?._links?.['wp:user-global-styles']?.[0]?.href;
475
-
476
452
  if (globalStylesURL) {
477
453
  const globalStylesObject = await (0, _apiFetch.default)({
478
454
  url: globalStylesURL
479
455
  });
480
-
481
456
  dispatch.__experimentalReceiveCurrentGlobalStylesId(globalStylesObject.id);
482
457
  }
483
458
  };
484
-
485
459
  exports.__experimentalGetCurrentGlobalStylesId = __experimentalGetCurrentGlobalStylesId;
486
-
487
460
  const __experimentalGetCurrentThemeBaseGlobalStyles = () => async ({
488
461
  resolveSelect,
489
462
  dispatch
@@ -492,12 +465,9 @@ const __experimentalGetCurrentThemeBaseGlobalStyles = () => async ({
492
465
  const themeGlobalStyles = await (0, _apiFetch.default)({
493
466
  path: `/wp/v2/global-styles/themes/${currentTheme.stylesheet}`
494
467
  });
495
-
496
468
  dispatch.__experimentalReceiveThemeBaseGlobalStyles(currentTheme.stylesheet, themeGlobalStyles);
497
469
  };
498
-
499
470
  exports.__experimentalGetCurrentThemeBaseGlobalStyles = __experimentalGetCurrentThemeBaseGlobalStyles;
500
-
501
471
  const __experimentalGetCurrentThemeGlobalStylesVariations = () => async ({
502
472
  resolveSelect,
503
473
  dispatch
@@ -506,16 +476,13 @@ const __experimentalGetCurrentThemeGlobalStylesVariations = () => async ({
506
476
  const variations = await (0, _apiFetch.default)({
507
477
  path: `/wp/v2/global-styles/themes/${currentTheme.stylesheet}/variations`
508
478
  });
509
-
510
479
  dispatch.__experimentalReceiveThemeGlobalStyleVariations(currentTheme.stylesheet, variations);
511
480
  };
481
+
512
482
  /**
513
483
  * Fetches and returns the revisions of the current global styles theme.
514
484
  */
515
-
516
-
517
485
  exports.__experimentalGetCurrentThemeGlobalStylesVariations = __experimentalGetCurrentThemeGlobalStylesVariations;
518
-
519
486
  const getCurrentThemeGlobalStylesRevisions = () => async ({
520
487
  resolveSelect,
521
488
  dispatch
@@ -523,7 +490,6 @@ const getCurrentThemeGlobalStylesRevisions = () => async ({
523
490
  const globalStylesId = await resolveSelect.__experimentalGetCurrentGlobalStylesId();
524
491
  const record = globalStylesId ? await resolveSelect.getEntityRecord('root', 'globalStyles', globalStylesId) : undefined;
525
492
  const revisionsURL = record?._links?.['version-history']?.[0]?.href;
526
-
527
493
  if (revisionsURL) {
528
494
  const resetRevisions = await (0, _apiFetch.default)({
529
495
  url: revisionsURL
@@ -532,13 +498,10 @@ const getCurrentThemeGlobalStylesRevisions = () => async ({
532
498
  dispatch.receiveThemeGlobalStyleRevisions(globalStylesId, revisions);
533
499
  }
534
500
  };
535
-
536
501
  exports.getCurrentThemeGlobalStylesRevisions = getCurrentThemeGlobalStylesRevisions;
537
-
538
502
  getCurrentThemeGlobalStylesRevisions.shouldInvalidate = action => {
539
503
  return action.type === 'SAVE_ENTITY_RECORD_FINISH' && action.kind === 'root' && !action.error && action.name === 'globalStyles';
540
504
  };
541
-
542
505
  const getBlockPatterns = () => async ({
543
506
  dispatch
544
507
  }) => {
@@ -551,9 +514,7 @@ const getBlockPatterns = () => async ({
551
514
  patterns
552
515
  });
553
516
  };
554
-
555
517
  exports.getBlockPatterns = getBlockPatterns;
556
-
557
518
  const getBlockPatternCategories = () => async ({
558
519
  dispatch
559
520
  }) => {
@@ -565,9 +526,7 @@ const getBlockPatternCategories = () => async ({
565
526
  categories
566
527
  });
567
528
  };
568
-
569
529
  exports.getBlockPatternCategories = getBlockPatternCategories;
570
-
571
530
  const getNavigationFallbackId = () => async ({
572
531
  dispatch,
573
532
  select
@@ -579,18 +538,17 @@ const getNavigationFallbackId = () => async ({
579
538
  });
580
539
  const record = fallback?._embedded?.self;
581
540
  dispatch.receiveNavigationFallbackId(fallback?.id);
582
-
583
541
  if (record) {
584
542
  // If the fallback is already in the store, don't invalidate navigation queries.
585
543
  // Otherwise, invalidate the cache for the scenario where there were no Navigation
586
544
  // posts in the state and the fallback created one.
587
545
  const existingFallbackEntityRecord = select.getEntityRecord('postType', 'wp_navigation', fallback?.id);
588
546
  const invalidateNavigationQueries = !existingFallbackEntityRecord;
589
- dispatch.receiveEntityRecords('postType', 'wp_navigation', record, undefined, invalidateNavigationQueries); // Resolve to avoid further network requests.
547
+ dispatch.receiveEntityRecords('postType', 'wp_navigation', record, undefined, invalidateNavigationQueries);
590
548
 
549
+ // Resolve to avoid further network requests.
591
550
  dispatch.finishResolution('getEntityRecord', ['postType', 'wp_navigation', fallback?.id]);
592
551
  }
593
552
  };
594
-
595
553
  exports.getNavigationFallbackId = getNavigationFallbackId;
596
554
  //# sourceMappingURL=resolvers.js.map