@wordpress/core-data 3.1.9 → 3.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,7 +42,7 @@ The following set of dispatching action creators are available on the object ret
42
42
 
43
43
  <!-- START TOKEN(Autogenerated actions|src/actions.js) -->
44
44
 
45
- <a name="addEntities" href="#addEntities">#</a> **addEntities**
45
+ ### addEntities
46
46
 
47
47
  Returns an action object used in adding new entities.
48
48
 
@@ -54,7 +54,7 @@ _Returns_
54
54
 
55
55
  - `Object`: Action object.
56
56
 
57
- <a name="deleteEntityRecord" href="#deleteEntityRecord">#</a> **deleteEntityRecord**
57
+ ### deleteEntityRecord
58
58
 
59
59
  Action triggered to delete an entity record.
60
60
 
@@ -67,7 +67,7 @@ _Parameters_
67
67
  - _options_ `[Object]`: Delete options.
68
68
  - _options.\_\_unstableFetch_ `[Function]`: Internal use only. Function to call instead of `apiFetch()`. Must return a control descriptor.
69
69
 
70
- <a name="editEntityRecord" href="#editEntityRecord">#</a> **editEntityRecord**
70
+ ### editEntityRecord
71
71
 
72
72
  Returns an action object that triggers an
73
73
  edit to an entity record.
@@ -85,7 +85,7 @@ _Returns_
85
85
 
86
86
  - `Object`: Action object.
87
87
 
88
- <a name="receiveAutosaves" href="#receiveAutosaves">#</a> **receiveAutosaves**
88
+ ### receiveAutosaves
89
89
 
90
90
  Returns an action object used in signalling that the autosaves for a
91
91
  post have been received.
@@ -99,7 +99,7 @@ _Returns_
99
99
 
100
100
  - `Object`: Action object.
101
101
 
102
- <a name="receiveCurrentTheme" href="#receiveCurrentTheme">#</a> **receiveCurrentTheme**
102
+ ### receiveCurrentTheme
103
103
 
104
104
  Returns an action object used in signalling that the current theme has been received.
105
105
 
@@ -111,7 +111,7 @@ _Returns_
111
111
 
112
112
  - `Object`: Action object.
113
113
 
114
- <a name="receiveCurrentUser" href="#receiveCurrentUser">#</a> **receiveCurrentUser**
114
+ ### receiveCurrentUser
115
115
 
116
116
  Returns an action used in signalling that the current user has been received.
117
117
 
@@ -123,7 +123,7 @@ _Returns_
123
123
 
124
124
  - `Object`: Action object.
125
125
 
126
- <a name="receiveEmbedPreview" href="#receiveEmbedPreview">#</a> **receiveEmbedPreview**
126
+ ### receiveEmbedPreview
127
127
 
128
128
  Returns an action object used in signalling that the preview data for
129
129
  a given URl has been received.
@@ -137,7 +137,7 @@ _Returns_
137
137
 
138
138
  - `Object`: Action object.
139
139
 
140
- <a name="receiveEntityRecords" href="#receiveEntityRecords">#</a> **receiveEntityRecords**
140
+ ### receiveEntityRecords
141
141
 
142
142
  Returns an action object used in signalling that entity records have been received.
143
143
 
@@ -154,7 +154,7 @@ _Returns_
154
154
 
155
155
  - `Object`: Action object.
156
156
 
157
- <a name="receiveThemeSupports" href="#receiveThemeSupports">#</a> **receiveThemeSupports**
157
+ ### receiveThemeSupports
158
158
 
159
159
  Returns an action object used in signalling that the index has been received.
160
160
 
@@ -166,7 +166,7 @@ _Returns_
166
166
 
167
167
  - `Object`: Action object.
168
168
 
169
- <a name="receiveUploadPermissions" href="#receiveUploadPermissions">#</a> **receiveUploadPermissions**
169
+ ### receiveUploadPermissions
170
170
 
171
171
  Returns an action object used in signalling that Upload permissions have been received.
172
172
 
@@ -178,7 +178,7 @@ _Returns_
178
178
 
179
179
  - `Object`: Action object.
180
180
 
181
- <a name="receiveUserPermission" href="#receiveUserPermission">#</a> **receiveUserPermission**
181
+ ### receiveUserPermission
182
182
 
183
183
  Returns an action object used in signalling that the current user has
184
184
  permission to perform an action on a REST resource.
@@ -192,7 +192,7 @@ _Returns_
192
192
 
193
193
  - `Object`: Action object.
194
194
 
195
- <a name="receiveUserQuery" href="#receiveUserQuery">#</a> **receiveUserQuery**
195
+ ### receiveUserQuery
196
196
 
197
197
  Returns an action object used in signalling that authors have been received.
198
198
 
@@ -205,12 +205,12 @@ _Returns_
205
205
 
206
206
  - `Object`: Action object.
207
207
 
208
- <a name="redo" href="#redo">#</a> **redo**
208
+ ### redo
209
209
 
210
210
  Action triggered to redo the last undoed
211
211
  edit to an entity record, if any.
212
212
 
213
- <a name="saveEditedEntityRecord" href="#saveEditedEntityRecord">#</a> **saveEditedEntityRecord**
213
+ ### saveEditedEntityRecord
214
214
 
215
215
  Action triggered to save an entity record's edits.
216
216
 
@@ -221,7 +221,7 @@ _Parameters_
221
221
  - _recordId_ `Object`: ID of the record.
222
222
  - _options_ `Object`: Saving options.
223
223
 
224
- <a name="saveEntityRecord" href="#saveEntityRecord">#</a> **saveEntityRecord**
224
+ ### saveEntityRecord
225
225
 
226
226
  Action triggered to save an entity record.
227
227
 
@@ -234,7 +234,7 @@ _Parameters_
234
234
  - _options.isAutosave_ `[boolean]`: Whether this is an autosave.
235
235
  - _options.\_\_unstableFetch_ `[Function]`: Internal use only. Function to call instead of `apiFetch()`. Must return a control descriptor.
236
236
 
237
- <a name="undo" href="#undo">#</a> **undo**
237
+ ### undo
238
238
 
239
239
  Action triggered to undo the last edit to
240
240
  an entity record, if any.
@@ -247,7 +247,7 @@ The following selectors are available on the object returned by `wp.data.select(
247
247
 
248
248
  <!-- START TOKEN(Autogenerated selectors|src/selectors.js) -->
249
249
 
250
- <a name="canUser" href="#canUser">#</a> **canUser**
250
+ ### canUser
251
251
 
252
252
  Returns whether the current user can perform the given action on the given
253
253
  REST resource.
@@ -268,7 +268,7 @@ _Returns_
268
268
 
269
269
  - `boolean|undefined`: Whether or not the user can perform the action, or `undefined` if the OPTIONS request is still being made.
270
270
 
271
- <a name="canUserEditEntityRecord" href="#canUserEditEntityRecord">#</a> **canUserEditEntityRecord**
271
+ ### canUserEditEntityRecord
272
272
 
273
273
  Returns whether the current user can edit the given entity.
274
274
 
@@ -288,7 +288,7 @@ _Returns_
288
288
 
289
289
  - `boolean|undefined`: Whether or not the user can edit, or `undefined` if the OPTIONS request is still being made.
290
290
 
291
- <a name="getAuthors" href="#getAuthors">#</a> **getAuthors**
291
+ ### getAuthors
292
292
 
293
293
  Returns all available authors.
294
294
 
@@ -301,7 +301,7 @@ _Returns_
301
301
 
302
302
  - `Array`: Authors list.
303
303
 
304
- <a name="getAutosave" href="#getAutosave">#</a> **getAutosave**
304
+ ### getAutosave
305
305
 
306
306
  Returns the autosave for the post and author.
307
307
 
@@ -316,7 +316,7 @@ _Returns_
316
316
 
317
317
  - `?Object`: The autosave for the post and author.
318
318
 
319
- <a name="getAutosaves" href="#getAutosaves">#</a> **getAutosaves**
319
+ ### getAutosaves
320
320
 
321
321
  Returns the latest autosaves for the post.
322
322
 
@@ -333,7 +333,7 @@ _Returns_
333
333
 
334
334
  - `?Array`: An array of autosaves for the post, or undefined if there is none.
335
335
 
336
- <a name="getCurrentTheme" href="#getCurrentTheme">#</a> **getCurrentTheme**
336
+ ### getCurrentTheme
337
337
 
338
338
  Return the current theme.
339
339
 
@@ -345,7 +345,7 @@ _Returns_
345
345
 
346
346
  - `Object`: The current theme.
347
347
 
348
- <a name="getCurrentUser" href="#getCurrentUser">#</a> **getCurrentUser**
348
+ ### getCurrentUser
349
349
 
350
350
  Returns the current user.
351
351
 
@@ -357,7 +357,7 @@ _Returns_
357
357
 
358
358
  - `Object`: Current user object.
359
359
 
360
- <a name="getEditedEntityRecord" href="#getEditedEntityRecord">#</a> **getEditedEntityRecord**
360
+ ### getEditedEntityRecord
361
361
 
362
362
  Returns the specified entity record, merged with its edits.
363
363
 
@@ -372,7 +372,7 @@ _Returns_
372
372
 
373
373
  - `Object?`: The entity record, merged with its edits.
374
374
 
375
- <a name="getEmbedPreview" href="#getEmbedPreview">#</a> **getEmbedPreview**
375
+ ### getEmbedPreview
376
376
 
377
377
  Returns the embed preview for the given URL.
378
378
 
@@ -385,7 +385,7 @@ _Returns_
385
385
 
386
386
  - `*`: Undefined if the preview has not been fetched, otherwise, the preview fetched from the embed preview API.
387
387
 
388
- <a name="getEntitiesByKind" href="#getEntitiesByKind">#</a> **getEntitiesByKind**
388
+ ### getEntitiesByKind
389
389
 
390
390
  Returns whether the entities for the give kind are loaded.
391
391
 
@@ -398,7 +398,7 @@ _Returns_
398
398
 
399
399
  - `Array<Object>`: Array of entities with config matching kind.
400
400
 
401
- <a name="getEntity" href="#getEntity">#</a> **getEntity**
401
+ ### getEntity
402
402
 
403
403
  Returns the entity object given its kind and name.
404
404
 
@@ -412,7 +412,7 @@ _Returns_
412
412
 
413
413
  - `Object`: Entity
414
414
 
415
- <a name="getEntityRecord" href="#getEntityRecord">#</a> **getEntityRecord**
415
+ ### getEntityRecord
416
416
 
417
417
  Returns the Entity's record object by key. Returns `null` if the value is not
418
418
  yet received, undefined if the value entity is known to not exist, or the
@@ -430,7 +430,7 @@ _Returns_
430
430
 
431
431
  - `Object?`: Record.
432
432
 
433
- <a name="getEntityRecordEdits" href="#getEntityRecordEdits">#</a> **getEntityRecordEdits**
433
+ ### getEntityRecordEdits
434
434
 
435
435
  Returns the specified entity record's edits.
436
436
 
@@ -445,7 +445,7 @@ _Returns_
445
445
 
446
446
  - `Object?`: The entity record's edits.
447
447
 
448
- <a name="getEntityRecordNonTransientEdits" href="#getEntityRecordNonTransientEdits">#</a> **getEntityRecordNonTransientEdits**
448
+ ### getEntityRecordNonTransientEdits
449
449
 
450
450
  Returns the specified entity record's non transient edits.
451
451
 
@@ -464,7 +464,7 @@ _Returns_
464
464
 
465
465
  - `Object?`: The entity record's non transient edits.
466
466
 
467
- <a name="getEntityRecords" href="#getEntityRecords">#</a> **getEntityRecords**
467
+ ### getEntityRecords
468
468
 
469
469
  Returns the Entity's records.
470
470
 
@@ -479,7 +479,7 @@ _Returns_
479
479
 
480
480
  - `?Array`: Records.
481
481
 
482
- <a name="getLastEntityDeleteError" href="#getLastEntityDeleteError">#</a> **getLastEntityDeleteError**
482
+ ### getLastEntityDeleteError
483
483
 
484
484
  Returns the specified entity record's last delete error.
485
485
 
@@ -494,7 +494,7 @@ _Returns_
494
494
 
495
495
  - `Object?`: The entity record's save error.
496
496
 
497
- <a name="getLastEntitySaveError" href="#getLastEntitySaveError">#</a> **getLastEntitySaveError**
497
+ ### getLastEntitySaveError
498
498
 
499
499
  Returns the specified entity record's last save error.
500
500
 
@@ -509,7 +509,7 @@ _Returns_
509
509
 
510
510
  - `Object?`: The entity record's save error.
511
511
 
512
- <a name="getRawEntityRecord" href="#getRawEntityRecord">#</a> **getRawEntityRecord**
512
+ ### getRawEntityRecord
513
513
 
514
514
  Returns the entity's record object by key,
515
515
  with its attributes mapped to their raw values.
@@ -525,7 +525,7 @@ _Returns_
525
525
 
526
526
  - `Object?`: Object with the entity's raw attributes.
527
527
 
528
- <a name="getRedoEdit" href="#getRedoEdit">#</a> **getRedoEdit**
528
+ ### getRedoEdit
529
529
 
530
530
  Returns the next edit from the current undo offset
531
531
  for the entity records edits history, if any.
@@ -538,7 +538,7 @@ _Returns_
538
538
 
539
539
  - `Object?`: The edit.
540
540
 
541
- <a name="getReferenceByDistinctEdits" href="#getReferenceByDistinctEdits">#</a> **getReferenceByDistinctEdits**
541
+ ### getReferenceByDistinctEdits
542
542
 
543
543
  Returns a new reference when edited values have changed. This is useful in
544
544
  inferring where an edit has been made between states by comparison of the
@@ -559,7 +559,7 @@ _Returns_
559
559
 
560
560
  - `*`: A value whose reference will change only when an edit occurs.
561
561
 
562
- <a name="getThemeSupports" href="#getThemeSupports">#</a> **getThemeSupports**
562
+ ### getThemeSupports
563
563
 
564
564
  Return theme supports data in the index.
565
565
 
@@ -571,7 +571,7 @@ _Returns_
571
571
 
572
572
  - `*`: Index data.
573
573
 
574
- <a name="getUndoEdit" href="#getUndoEdit">#</a> **getUndoEdit**
574
+ ### getUndoEdit
575
575
 
576
576
  Returns the previous edit from the current undo offset
577
577
  for the entity records edits history, if any.
@@ -584,7 +584,7 @@ _Returns_
584
584
 
585
585
  - `Object?`: The edit.
586
586
 
587
- <a name="getUserQueryResults" href="#getUserQueryResults">#</a> **getUserQueryResults**
587
+ ### getUserQueryResults
588
588
 
589
589
  Returns all the users returned by a query ID.
590
590
 
@@ -597,7 +597,7 @@ _Returns_
597
597
 
598
598
  - `Array`: Users list.
599
599
 
600
- <a name="hasEditsForEntityRecord" href="#hasEditsForEntityRecord">#</a> **hasEditsForEntityRecord**
600
+ ### hasEditsForEntityRecord
601
601
 
602
602
  Returns true if the specified entity record has edits,
603
603
  and false otherwise.
@@ -613,7 +613,7 @@ _Returns_
613
613
 
614
614
  - `boolean`: Whether the entity record has edits or not.
615
615
 
616
- <a name="hasEntityRecords" href="#hasEntityRecords">#</a> **hasEntityRecords**
616
+ ### hasEntityRecords
617
617
 
618
618
  Returns true if records have been received for the given set of parameters,
619
619
  or false otherwise.
@@ -629,7 +629,7 @@ _Returns_
629
629
 
630
630
  - `boolean`: Whether entity records have been received.
631
631
 
632
- <a name="hasFetchedAutosaves" href="#hasFetchedAutosaves">#</a> **hasFetchedAutosaves**
632
+ ### hasFetchedAutosaves
633
633
 
634
634
  Returns true if the REST request for autosaves has completed.
635
635
 
@@ -643,7 +643,7 @@ _Returns_
643
643
 
644
644
  - `boolean`: True if the REST request was completed. False otherwise.
645
645
 
646
- <a name="hasRedo" href="#hasRedo">#</a> **hasRedo**
646
+ ### hasRedo
647
647
 
648
648
  Returns true if there is a next edit from the current undo offset
649
649
  for the entity records edits history, and false otherwise.
@@ -656,7 +656,7 @@ _Returns_
656
656
 
657
657
  - `boolean`: Whether there is a next edit or not.
658
658
 
659
- <a name="hasUndo" href="#hasUndo">#</a> **hasUndo**
659
+ ### hasUndo
660
660
 
661
661
  Returns true if there is a previous edit from the current undo offset
662
662
  for the entity records edits history, and false otherwise.
@@ -669,7 +669,7 @@ _Returns_
669
669
 
670
670
  - `boolean`: Whether there is a previous edit or not.
671
671
 
672
- <a name="isAutosavingEntityRecord" href="#isAutosavingEntityRecord">#</a> **isAutosavingEntityRecord**
672
+ ### isAutosavingEntityRecord
673
673
 
674
674
  Returns true if the specified entity record is autosaving, and false otherwise.
675
675
 
@@ -684,7 +684,7 @@ _Returns_
684
684
 
685
685
  - `boolean`: Whether the entity record is autosaving or not.
686
686
 
687
- <a name="isDeletingEntityRecord" href="#isDeletingEntityRecord">#</a> **isDeletingEntityRecord**
687
+ ### isDeletingEntityRecord
688
688
 
689
689
  Returns true if the specified entity record is deleting, and false otherwise.
690
690
 
@@ -699,7 +699,7 @@ _Returns_
699
699
 
700
700
  - `boolean`: Whether the entity record is deleting or not.
701
701
 
702
- <a name="isPreviewEmbedFallback" href="#isPreviewEmbedFallback">#</a> **isPreviewEmbedFallback**
702
+ ### isPreviewEmbedFallback
703
703
 
704
704
  Determines if the returned preview is an oEmbed link fallback.
705
705
 
@@ -716,7 +716,7 @@ _Returns_
716
716
 
717
717
  - `boolean`: Is the preview for the URL an oEmbed link fallback.
718
718
 
719
- <a name="isRequestingEmbedPreview" href="#isRequestingEmbedPreview">#</a> **isRequestingEmbedPreview**
719
+ ### isRequestingEmbedPreview
720
720
 
721
721
  Returns true if a request is in progress for embed preview data, or false
722
722
  otherwise.
@@ -730,7 +730,7 @@ _Returns_
730
730
 
731
731
  - `boolean`: Whether a request is in progress for an embed preview.
732
732
 
733
- <a name="isSavingEntityRecord" href="#isSavingEntityRecord">#</a> **isSavingEntityRecord**
733
+ ### isSavingEntityRecord
734
734
 
735
735
  Returns true if the specified entity record is saving, and false otherwise.
736
736
 
@@ -745,7 +745,6 @@ _Returns_
745
745
 
746
746
  - `boolean`: Whether the entity record is saving or not.
747
747
 
748
-
749
748
  <!-- END TOKEN(Autogenerated selectors|src/selectors.js) -->
750
749
 
751
750
  <br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
@@ -7,14 +7,27 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = defaultProcessor;
9
9
 
10
+ var _lodash = require("lodash");
11
+
10
12
  var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
11
13
 
14
+ /**
15
+ * External dependencies
16
+ */
17
+
12
18
  /**
13
19
  * WordPress dependencies
14
20
  */
15
21
 
16
22
  /**
17
- * Default batch processor. Sends its input requests to /v1/batch.
23
+ * Maximum number of requests to place in a single batch request. Obtained by
24
+ * sending a preflight OPTIONS request to /batch/v1/.
25
+ *
26
+ * @type {number?}
27
+ */
28
+ let maxItems = null;
29
+ /**
30
+ * Default batch processor. Sends its input requests to /batch/v1.
18
31
  *
19
32
  * @param {Array} requests List of API requests to perform at once.
20
33
  *
@@ -22,38 +35,56 @@ var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
22
35
  * either `output` (if that request was succesful) or `error`
23
36
  * (if not ).
24
37
  */
25
- async function defaultProcessor(requests) {
26
- const batchResponse = await (0, _apiFetch.default)({
27
- path: '/batch/v1',
28
- method: 'POST',
29
- data: {
30
- validation: 'require-all-validate',
31
- requests: requests.map(request => ({
32
- path: request.path,
33
- body: request.data,
34
- // Rename 'data' to 'body'.
35
- method: request.method,
36
- headers: request.headers
37
- }))
38
- }
39
- });
40
38
 
41
- if (batchResponse.failed) {
42
- return batchResponse.responses.map(response => ({
43
- error: response === null || response === void 0 ? void 0 : response.body
44
- }));
39
+ async function defaultProcessor(requests) {
40
+ if (maxItems === null) {
41
+ const preflightResponse = await (0, _apiFetch.default)({
42
+ path: '/batch/v1',
43
+ method: 'OPTIONS'
44
+ });
45
+ maxItems = preflightResponse.endpoints[0].args.requests.maxItems;
45
46
  }
46
47
 
47
- return batchResponse.responses.map(response => {
48
- const result = {};
48
+ const results = [];
49
49
 
50
- if (response.status >= 200 && response.status < 300) {
51
- result.output = response.body;
50
+ for (const batchRequests of (0, _lodash.chunk)(requests, maxItems)) {
51
+ const batchResponse = await (0, _apiFetch.default)({
52
+ path: '/batch/v1',
53
+ method: 'POST',
54
+ data: {
55
+ validation: 'require-all-validate',
56
+ requests: batchRequests.map(request => ({
57
+ path: request.path,
58
+ body: request.data,
59
+ // Rename 'data' to 'body'.
60
+ method: request.method,
61
+ headers: request.headers
62
+ }))
63
+ }
64
+ });
65
+ let batchResults;
66
+
67
+ if (batchResponse.failed) {
68
+ batchResults = batchResponse.responses.map(response => ({
69
+ error: response === null || response === void 0 ? void 0 : response.body
70
+ }));
52
71
  } else {
53
- result.error = response.body;
72
+ batchResults = batchResponse.responses.map(response => {
73
+ const result = {};
74
+
75
+ if (response.status >= 200 && response.status < 300) {
76
+ result.output = response.body;
77
+ } else {
78
+ result.error = response.body;
79
+ }
80
+
81
+ return result;
82
+ });
54
83
  }
55
84
 
56
- return result;
57
- });
85
+ results.push(...batchResults);
86
+ }
87
+
88
+ return results;
58
89
  }
59
90
  //# sourceMappingURL=default-processor.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/batch/default-processor.js"],"names":["defaultProcessor","requests","batchResponse","path","method","data","validation","map","request","body","headers","failed","responses","response","error","result","status","output"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,eAAeA,gBAAf,CAAiCC,QAAjC,EAA4C;AAC1D,QAAMC,aAAa,GAAG,MAAM,uBAAU;AACrCC,IAAAA,IAAI,EAAE,WAD+B;AAErCC,IAAAA,MAAM,EAAE,MAF6B;AAGrCC,IAAAA,IAAI,EAAE;AACLC,MAAAA,UAAU,EAAE,sBADP;AAELL,MAAAA,QAAQ,EAAEA,QAAQ,CAACM,GAAT,CAAgBC,OAAF,KAAiB;AACxCL,QAAAA,IAAI,EAAEK,OAAO,CAACL,IAD0B;AAExCM,QAAAA,IAAI,EAAED,OAAO,CAACH,IAF0B;AAEpB;AACpBD,QAAAA,MAAM,EAAEI,OAAO,CAACJ,MAHwB;AAIxCM,QAAAA,OAAO,EAAEF,OAAO,CAACE;AAJuB,OAAjB,CAAd;AAFL;AAH+B,GAAV,CAA5B;;AAcA,MAAKR,aAAa,CAACS,MAAnB,EAA4B;AAC3B,WAAOT,aAAa,CAACU,SAAd,CAAwBL,GAAxB,CAA+BM,QAAF,KAAkB;AACrDC,MAAAA,KAAK,EAAED,QAAF,aAAEA,QAAF,uBAAEA,QAAQ,CAAEJ;AADoC,KAAlB,CAA7B,CAAP;AAGA;;AAED,SAAOP,aAAa,CAACU,SAAd,CAAwBL,GAAxB,CAA+BM,QAAF,IAAgB;AACnD,UAAME,MAAM,GAAG,EAAf;;AACA,QAAKF,QAAQ,CAACG,MAAT,IAAmB,GAAnB,IAA0BH,QAAQ,CAACG,MAAT,GAAkB,GAAjD,EAAuD;AACtDD,MAAAA,MAAM,CAACE,MAAP,GAAgBJ,QAAQ,CAACJ,IAAzB;AACA,KAFD,MAEO;AACNM,MAAAA,MAAM,CAACD,KAAP,GAAeD,QAAQ,CAACJ,IAAxB;AACA;;AACD,WAAOM,MAAP;AACA,GARM,CAAP;AASA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Default batch processor. Sends its input requests to /v1/batch.\n *\n * @param {Array} requests List of API requests to perform at once.\n *\n * @return {Promise} Promise that resolves to a list of objects containing\n * either `output` (if that request was succesful) or `error`\n * (if not ).\n */\nexport default async function defaultProcessor( requests ) {\n\tconst batchResponse = await apiFetch( {\n\t\tpath: '/batch/v1',\n\t\tmethod: 'POST',\n\t\tdata: {\n\t\t\tvalidation: 'require-all-validate',\n\t\t\trequests: requests.map( ( request ) => ( {\n\t\t\t\tpath: request.path,\n\t\t\t\tbody: request.data, // Rename 'data' to 'body'.\n\t\t\t\tmethod: request.method,\n\t\t\t\theaders: request.headers,\n\t\t\t} ) ),\n\t\t},\n\t} );\n\n\tif ( batchResponse.failed ) {\n\t\treturn batchResponse.responses.map( ( response ) => ( {\n\t\t\terror: response?.body,\n\t\t} ) );\n\t}\n\n\treturn batchResponse.responses.map( ( response ) => {\n\t\tconst result = {};\n\t\tif ( response.status >= 200 && response.status < 300 ) {\n\t\t\tresult.output = response.body;\n\t\t} else {\n\t\t\tresult.error = response.body;\n\t\t}\n\t\treturn result;\n\t} );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/batch/default-processor.js"],"names":["maxItems","defaultProcessor","requests","preflightResponse","path","method","endpoints","args","results","batchRequests","batchResponse","data","validation","map","request","body","headers","batchResults","failed","responses","response","error","result","status","output","push"],"mappings":";;;;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,IAAIA,QAAQ,GAAG,IAAf;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,eAAeC,gBAAf,CAAiCC,QAAjC,EAA4C;AAC1D,MAAKF,QAAQ,KAAK,IAAlB,EAAyB;AACxB,UAAMG,iBAAiB,GAAG,MAAM,uBAAU;AACzCC,MAAAA,IAAI,EAAE,WADmC;AAEzCC,MAAAA,MAAM,EAAE;AAFiC,KAAV,CAAhC;AAIAL,IAAAA,QAAQ,GAAGG,iBAAiB,CAACG,SAAlB,CAA6B,CAA7B,EAAiCC,IAAjC,CAAsCL,QAAtC,CAA+CF,QAA1D;AACA;;AAED,QAAMQ,OAAO,GAAG,EAAhB;;AAEA,OAAM,MAAMC,aAAZ,IAA6B,mBAAOP,QAAP,EAAiBF,QAAjB,CAA7B,EAA2D;AAC1D,UAAMU,aAAa,GAAG,MAAM,uBAAU;AACrCN,MAAAA,IAAI,EAAE,WAD+B;AAErCC,MAAAA,MAAM,EAAE,MAF6B;AAGrCM,MAAAA,IAAI,EAAE;AACLC,QAAAA,UAAU,EAAE,sBADP;AAELV,QAAAA,QAAQ,EAAEO,aAAa,CAACI,GAAd,CAAqBC,OAAF,KAAiB;AAC7CV,UAAAA,IAAI,EAAEU,OAAO,CAACV,IAD+B;AAE7CW,UAAAA,IAAI,EAAED,OAAO,CAACH,IAF+B;AAEzB;AACpBN,UAAAA,MAAM,EAAES,OAAO,CAACT,MAH6B;AAI7CW,UAAAA,OAAO,EAAEF,OAAO,CAACE;AAJ4B,SAAjB,CAAnB;AAFL;AAH+B,KAAV,CAA5B;AAcA,QAAIC,YAAJ;;AAEA,QAAKP,aAAa,CAACQ,MAAnB,EAA4B;AAC3BD,MAAAA,YAAY,GAAGP,aAAa,CAACS,SAAd,CAAwBN,GAAxB,CAA+BO,QAAF,KAAkB;AAC7DC,QAAAA,KAAK,EAAED,QAAF,aAAEA,QAAF,uBAAEA,QAAQ,CAAEL;AAD4C,OAAlB,CAA7B,CAAf;AAGA,KAJD,MAIO;AACNE,MAAAA,YAAY,GAAGP,aAAa,CAACS,SAAd,CAAwBN,GAAxB,CAA+BO,QAAF,IAAgB;AAC3D,cAAME,MAAM,GAAG,EAAf;;AACA,YAAKF,QAAQ,CAACG,MAAT,IAAmB,GAAnB,IAA0BH,QAAQ,CAACG,MAAT,GAAkB,GAAjD,EAAuD;AACtDD,UAAAA,MAAM,CAACE,MAAP,GAAgBJ,QAAQ,CAACL,IAAzB;AACA,SAFD,MAEO;AACNO,UAAAA,MAAM,CAACD,KAAP,GAAeD,QAAQ,CAACL,IAAxB;AACA;;AACD,eAAOO,MAAP;AACA,OARc,CAAf;AASA;;AAEDd,IAAAA,OAAO,CAACiB,IAAR,CAAc,GAAGR,YAAjB;AACA;;AAED,SAAOT,OAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { chunk } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Maximum number of requests to place in a single batch request. Obtained by\n * sending a preflight OPTIONS request to /batch/v1/.\n *\n * @type {number?}\n */\nlet maxItems = null;\n\n/**\n * Default batch processor. Sends its input requests to /batch/v1.\n *\n * @param {Array} requests List of API requests to perform at once.\n *\n * @return {Promise} Promise that resolves to a list of objects containing\n * either `output` (if that request was succesful) or `error`\n * (if not ).\n */\nexport default async function defaultProcessor( requests ) {\n\tif ( maxItems === null ) {\n\t\tconst preflightResponse = await apiFetch( {\n\t\t\tpath: '/batch/v1',\n\t\t\tmethod: 'OPTIONS',\n\t\t} );\n\t\tmaxItems = preflightResponse.endpoints[ 0 ].args.requests.maxItems;\n\t}\n\n\tconst results = [];\n\n\tfor ( const batchRequests of chunk( requests, maxItems ) ) {\n\t\tconst batchResponse = await apiFetch( {\n\t\t\tpath: '/batch/v1',\n\t\t\tmethod: 'POST',\n\t\t\tdata: {\n\t\t\t\tvalidation: 'require-all-validate',\n\t\t\t\trequests: batchRequests.map( ( request ) => ( {\n\t\t\t\t\tpath: request.path,\n\t\t\t\t\tbody: request.data, // Rename 'data' to 'body'.\n\t\t\t\t\tmethod: request.method,\n\t\t\t\t\theaders: request.headers,\n\t\t\t\t} ) ),\n\t\t\t},\n\t\t} );\n\n\t\tlet batchResults;\n\n\t\tif ( batchResponse.failed ) {\n\t\t\tbatchResults = batchResponse.responses.map( ( response ) => ( {\n\t\t\t\terror: response?.body,\n\t\t\t} ) );\n\t\t} else {\n\t\t\tbatchResults = batchResponse.responses.map( ( response ) => {\n\t\t\t\tconst result = {};\n\t\t\t\tif ( response.status >= 200 && response.status < 300 ) {\n\t\t\t\t\tresult.output = response.body;\n\t\t\t\t} else {\n\t\t\t\t\tresult.error = response.body;\n\t\t\t\t}\n\t\t\t\treturn result;\n\t\t\t} );\n\t\t}\n\n\t\tresults.push( ...batchResults );\n\t}\n\n\treturn results;\n}\n"]}
@@ -1,9 +1,22 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { chunk } from 'lodash';
1
5
  /**
2
6
  * WordPress dependencies
3
7
  */
8
+
4
9
  import apiFetch from '@wordpress/api-fetch';
5
10
  /**
6
- * Default batch processor. Sends its input requests to /v1/batch.
11
+ * Maximum number of requests to place in a single batch request. Obtained by
12
+ * sending a preflight OPTIONS request to /batch/v1/.
13
+ *
14
+ * @type {number?}
15
+ */
16
+
17
+ let maxItems = null;
18
+ /**
19
+ * Default batch processor. Sends its input requests to /batch/v1.
7
20
  *
8
21
  * @param {Array} requests List of API requests to perform at once.
9
22
  *
@@ -13,37 +26,54 @@ import apiFetch from '@wordpress/api-fetch';
13
26
  */
14
27
 
15
28
  export default async function defaultProcessor(requests) {
16
- const batchResponse = await apiFetch({
17
- path: '/batch/v1',
18
- method: 'POST',
19
- data: {
20
- validation: 'require-all-validate',
21
- requests: requests.map(request => ({
22
- path: request.path,
23
- body: request.data,
24
- // Rename 'data' to 'body'.
25
- method: request.method,
26
- headers: request.headers
27
- }))
28
- }
29
- });
30
-
31
- if (batchResponse.failed) {
32
- return batchResponse.responses.map(response => ({
33
- error: response === null || response === void 0 ? void 0 : response.body
34
- }));
29
+ if (maxItems === null) {
30
+ const preflightResponse = await apiFetch({
31
+ path: '/batch/v1',
32
+ method: 'OPTIONS'
33
+ });
34
+ maxItems = preflightResponse.endpoints[0].args.requests.maxItems;
35
35
  }
36
36
 
37
- return batchResponse.responses.map(response => {
38
- const result = {};
37
+ const results = [];
39
38
 
40
- if (response.status >= 200 && response.status < 300) {
41
- result.output = response.body;
39
+ for (const batchRequests of chunk(requests, maxItems)) {
40
+ const batchResponse = await apiFetch({
41
+ path: '/batch/v1',
42
+ method: 'POST',
43
+ data: {
44
+ validation: 'require-all-validate',
45
+ requests: batchRequests.map(request => ({
46
+ path: request.path,
47
+ body: request.data,
48
+ // Rename 'data' to 'body'.
49
+ method: request.method,
50
+ headers: request.headers
51
+ }))
52
+ }
53
+ });
54
+ let batchResults;
55
+
56
+ if (batchResponse.failed) {
57
+ batchResults = batchResponse.responses.map(response => ({
58
+ error: response === null || response === void 0 ? void 0 : response.body
59
+ }));
42
60
  } else {
43
- result.error = response.body;
61
+ batchResults = batchResponse.responses.map(response => {
62
+ const result = {};
63
+
64
+ if (response.status >= 200 && response.status < 300) {
65
+ result.output = response.body;
66
+ } else {
67
+ result.error = response.body;
68
+ }
69
+
70
+ return result;
71
+ });
44
72
  }
45
73
 
46
- return result;
47
- });
74
+ results.push(...batchResults);
75
+ }
76
+
77
+ return results;
48
78
  }
49
79
  //# sourceMappingURL=default-processor.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/batch/default-processor.js"],"names":["apiFetch","defaultProcessor","requests","batchResponse","path","method","data","validation","map","request","body","headers","failed","responses","response","error","result","status","output"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,eAAeC,gBAAf,CAAiCC,QAAjC,EAA4C;AAC1D,QAAMC,aAAa,GAAG,MAAMH,QAAQ,CAAE;AACrCI,IAAAA,IAAI,EAAE,WAD+B;AAErCC,IAAAA,MAAM,EAAE,MAF6B;AAGrCC,IAAAA,IAAI,EAAE;AACLC,MAAAA,UAAU,EAAE,sBADP;AAELL,MAAAA,QAAQ,EAAEA,QAAQ,CAACM,GAAT,CAAgBC,OAAF,KAAiB;AACxCL,QAAAA,IAAI,EAAEK,OAAO,CAACL,IAD0B;AAExCM,QAAAA,IAAI,EAAED,OAAO,CAACH,IAF0B;AAEpB;AACpBD,QAAAA,MAAM,EAAEI,OAAO,CAACJ,MAHwB;AAIxCM,QAAAA,OAAO,EAAEF,OAAO,CAACE;AAJuB,OAAjB,CAAd;AAFL;AAH+B,GAAF,CAApC;;AAcA,MAAKR,aAAa,CAACS,MAAnB,EAA4B;AAC3B,WAAOT,aAAa,CAACU,SAAd,CAAwBL,GAAxB,CAA+BM,QAAF,KAAkB;AACrDC,MAAAA,KAAK,EAAED,QAAF,aAAEA,QAAF,uBAAEA,QAAQ,CAAEJ;AADoC,KAAlB,CAA7B,CAAP;AAGA;;AAED,SAAOP,aAAa,CAACU,SAAd,CAAwBL,GAAxB,CAA+BM,QAAF,IAAgB;AACnD,UAAME,MAAM,GAAG,EAAf;;AACA,QAAKF,QAAQ,CAACG,MAAT,IAAmB,GAAnB,IAA0BH,QAAQ,CAACG,MAAT,GAAkB,GAAjD,EAAuD;AACtDD,MAAAA,MAAM,CAACE,MAAP,GAAgBJ,QAAQ,CAACJ,IAAzB;AACA,KAFD,MAEO;AACNM,MAAAA,MAAM,CAACD,KAAP,GAAeD,QAAQ,CAACJ,IAAxB;AACA;;AACD,WAAOM,MAAP;AACA,GARM,CAAP;AASA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Default batch processor. Sends its input requests to /v1/batch.\n *\n * @param {Array} requests List of API requests to perform at once.\n *\n * @return {Promise} Promise that resolves to a list of objects containing\n * either `output` (if that request was succesful) or `error`\n * (if not ).\n */\nexport default async function defaultProcessor( requests ) {\n\tconst batchResponse = await apiFetch( {\n\t\tpath: '/batch/v1',\n\t\tmethod: 'POST',\n\t\tdata: {\n\t\t\tvalidation: 'require-all-validate',\n\t\t\trequests: requests.map( ( request ) => ( {\n\t\t\t\tpath: request.path,\n\t\t\t\tbody: request.data, // Rename 'data' to 'body'.\n\t\t\t\tmethod: request.method,\n\t\t\t\theaders: request.headers,\n\t\t\t} ) ),\n\t\t},\n\t} );\n\n\tif ( batchResponse.failed ) {\n\t\treturn batchResponse.responses.map( ( response ) => ( {\n\t\t\terror: response?.body,\n\t\t} ) );\n\t}\n\n\treturn batchResponse.responses.map( ( response ) => {\n\t\tconst result = {};\n\t\tif ( response.status >= 200 && response.status < 300 ) {\n\t\t\tresult.output = response.body;\n\t\t} else {\n\t\t\tresult.error = response.body;\n\t\t}\n\t\treturn result;\n\t} );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/batch/default-processor.js"],"names":["chunk","apiFetch","maxItems","defaultProcessor","requests","preflightResponse","path","method","endpoints","args","results","batchRequests","batchResponse","data","validation","map","request","body","headers","batchResults","failed","responses","response","error","result","status","output","push"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,KAAT,QAAsB,QAAtB;AAEA;AACA;AACA;;AACA,OAAOC,QAAP,MAAqB,sBAArB;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,IAAIC,QAAQ,GAAG,IAAf;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,eAAeC,gBAAf,CAAiCC,QAAjC,EAA4C;AAC1D,MAAKF,QAAQ,KAAK,IAAlB,EAAyB;AACxB,UAAMG,iBAAiB,GAAG,MAAMJ,QAAQ,CAAE;AACzCK,MAAAA,IAAI,EAAE,WADmC;AAEzCC,MAAAA,MAAM,EAAE;AAFiC,KAAF,CAAxC;AAIAL,IAAAA,QAAQ,GAAGG,iBAAiB,CAACG,SAAlB,CAA6B,CAA7B,EAAiCC,IAAjC,CAAsCL,QAAtC,CAA+CF,QAA1D;AACA;;AAED,QAAMQ,OAAO,GAAG,EAAhB;;AAEA,OAAM,MAAMC,aAAZ,IAA6BX,KAAK,CAAEI,QAAF,EAAYF,QAAZ,CAAlC,EAA2D;AAC1D,UAAMU,aAAa,GAAG,MAAMX,QAAQ,CAAE;AACrCK,MAAAA,IAAI,EAAE,WAD+B;AAErCC,MAAAA,MAAM,EAAE,MAF6B;AAGrCM,MAAAA,IAAI,EAAE;AACLC,QAAAA,UAAU,EAAE,sBADP;AAELV,QAAAA,QAAQ,EAAEO,aAAa,CAACI,GAAd,CAAqBC,OAAF,KAAiB;AAC7CV,UAAAA,IAAI,EAAEU,OAAO,CAACV,IAD+B;AAE7CW,UAAAA,IAAI,EAAED,OAAO,CAACH,IAF+B;AAEzB;AACpBN,UAAAA,MAAM,EAAES,OAAO,CAACT,MAH6B;AAI7CW,UAAAA,OAAO,EAAEF,OAAO,CAACE;AAJ4B,SAAjB,CAAnB;AAFL;AAH+B,KAAF,CAApC;AAcA,QAAIC,YAAJ;;AAEA,QAAKP,aAAa,CAACQ,MAAnB,EAA4B;AAC3BD,MAAAA,YAAY,GAAGP,aAAa,CAACS,SAAd,CAAwBN,GAAxB,CAA+BO,QAAF,KAAkB;AAC7DC,QAAAA,KAAK,EAAED,QAAF,aAAEA,QAAF,uBAAEA,QAAQ,CAAEL;AAD4C,OAAlB,CAA7B,CAAf;AAGA,KAJD,MAIO;AACNE,MAAAA,YAAY,GAAGP,aAAa,CAACS,SAAd,CAAwBN,GAAxB,CAA+BO,QAAF,IAAgB;AAC3D,cAAME,MAAM,GAAG,EAAf;;AACA,YAAKF,QAAQ,CAACG,MAAT,IAAmB,GAAnB,IAA0BH,QAAQ,CAACG,MAAT,GAAkB,GAAjD,EAAuD;AACtDD,UAAAA,MAAM,CAACE,MAAP,GAAgBJ,QAAQ,CAACL,IAAzB;AACA,SAFD,MAEO;AACNO,UAAAA,MAAM,CAACD,KAAP,GAAeD,QAAQ,CAACL,IAAxB;AACA;;AACD,eAAOO,MAAP;AACA,OARc,CAAf;AASA;;AAEDd,IAAAA,OAAO,CAACiB,IAAR,CAAc,GAAGR,YAAjB;AACA;;AAED,SAAOT,OAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { chunk } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Maximum number of requests to place in a single batch request. Obtained by\n * sending a preflight OPTIONS request to /batch/v1/.\n *\n * @type {number?}\n */\nlet maxItems = null;\n\n/**\n * Default batch processor. Sends its input requests to /batch/v1.\n *\n * @param {Array} requests List of API requests to perform at once.\n *\n * @return {Promise} Promise that resolves to a list of objects containing\n * either `output` (if that request was succesful) or `error`\n * (if not ).\n */\nexport default async function defaultProcessor( requests ) {\n\tif ( maxItems === null ) {\n\t\tconst preflightResponse = await apiFetch( {\n\t\t\tpath: '/batch/v1',\n\t\t\tmethod: 'OPTIONS',\n\t\t} );\n\t\tmaxItems = preflightResponse.endpoints[ 0 ].args.requests.maxItems;\n\t}\n\n\tconst results = [];\n\n\tfor ( const batchRequests of chunk( requests, maxItems ) ) {\n\t\tconst batchResponse = await apiFetch( {\n\t\t\tpath: '/batch/v1',\n\t\t\tmethod: 'POST',\n\t\t\tdata: {\n\t\t\t\tvalidation: 'require-all-validate',\n\t\t\t\trequests: batchRequests.map( ( request ) => ( {\n\t\t\t\t\tpath: request.path,\n\t\t\t\t\tbody: request.data, // Rename 'data' to 'body'.\n\t\t\t\t\tmethod: request.method,\n\t\t\t\t\theaders: request.headers,\n\t\t\t\t} ) ),\n\t\t\t},\n\t\t} );\n\n\t\tlet batchResults;\n\n\t\tif ( batchResponse.failed ) {\n\t\t\tbatchResults = batchResponse.responses.map( ( response ) => ( {\n\t\t\t\terror: response?.body,\n\t\t\t} ) );\n\t\t} else {\n\t\t\tbatchResults = batchResponse.responses.map( ( response ) => {\n\t\t\t\tconst result = {};\n\t\t\t\tif ( response.status >= 200 && response.status < 300 ) {\n\t\t\t\t\tresult.output = response.body;\n\t\t\t\t} else {\n\t\t\t\t\tresult.error = response.body;\n\t\t\t\t}\n\t\t\t\treturn result;\n\t\t\t} );\n\t\t}\n\n\t\tresults.push( ...batchResults );\n\t}\n\n\treturn results;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/core-data",
3
- "version": "3.1.9",
3
+ "version": "3.1.13",
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",
@@ -30,15 +30,15 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "@babel/runtime": "^7.13.10",
33
- "@wordpress/api-fetch": "^5.1.1",
34
- "@wordpress/blocks": "^9.1.5",
35
- "@wordpress/data": "^5.1.3",
36
- "@wordpress/data-controls": "^2.1.3",
37
- "@wordpress/element": "^3.1.1",
38
- "@wordpress/html-entities": "^3.1.1",
39
- "@wordpress/i18n": "^4.1.1",
33
+ "@wordpress/api-fetch": "^5.1.3",
34
+ "@wordpress/blocks": "^9.1.8",
35
+ "@wordpress/data": "^5.1.6",
36
+ "@wordpress/data-controls": "^2.1.7",
37
+ "@wordpress/element": "^3.1.2",
38
+ "@wordpress/html-entities": "^3.1.2",
39
+ "@wordpress/i18n": "^4.1.2",
40
40
  "@wordpress/is-shallow-equal": "^4.1.1",
41
- "@wordpress/url": "^3.1.1",
41
+ "@wordpress/url": "^3.1.3",
42
42
  "equivalent-key-map": "^0.2.2",
43
43
  "lodash": "^4.17.21",
44
44
  "rememo": "^3.0.0",
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "a39d5605fcea63a9e02a9ae9d3f3d4f1685df14e"
50
+ "gitHead": "6464883c6c546233543a1ee01428059dee3560fc"
51
51
  }
@@ -1,10 +1,23 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { chunk } from 'lodash';
5
+
1
6
  /**
2
7
  * WordPress dependencies
3
8
  */
4
9
  import apiFetch from '@wordpress/api-fetch';
5
10
 
6
11
  /**
7
- * Default batch processor. Sends its input requests to /v1/batch.
12
+ * Maximum number of requests to place in a single batch request. Obtained by
13
+ * sending a preflight OPTIONS request to /batch/v1/.
14
+ *
15
+ * @type {number?}
16
+ */
17
+ let maxItems = null;
18
+
19
+ /**
20
+ * Default batch processor. Sends its input requests to /batch/v1.
8
21
  *
9
22
  * @param {Array} requests List of API requests to perform at once.
10
23
  *
@@ -13,33 +26,51 @@ import apiFetch from '@wordpress/api-fetch';
13
26
  * (if not ).
14
27
  */
15
28
  export default async function defaultProcessor( requests ) {
16
- const batchResponse = await apiFetch( {
17
- path: '/batch/v1',
18
- method: 'POST',
19
- data: {
20
- validation: 'require-all-validate',
21
- requests: requests.map( ( request ) => ( {
22
- path: request.path,
23
- body: request.data, // Rename 'data' to 'body'.
24
- method: request.method,
25
- headers: request.headers,
26
- } ) ),
27
- },
28
- } );
29
-
30
- if ( batchResponse.failed ) {
31
- return batchResponse.responses.map( ( response ) => ( {
32
- error: response?.body,
33
- } ) );
29
+ if ( maxItems === null ) {
30
+ const preflightResponse = await apiFetch( {
31
+ path: '/batch/v1',
32
+ method: 'OPTIONS',
33
+ } );
34
+ maxItems = preflightResponse.endpoints[ 0 ].args.requests.maxItems;
34
35
  }
35
36
 
36
- return batchResponse.responses.map( ( response ) => {
37
- const result = {};
38
- if ( response.status >= 200 && response.status < 300 ) {
39
- result.output = response.body;
37
+ const results = [];
38
+
39
+ for ( const batchRequests of chunk( requests, maxItems ) ) {
40
+ const batchResponse = await apiFetch( {
41
+ path: '/batch/v1',
42
+ method: 'POST',
43
+ data: {
44
+ validation: 'require-all-validate',
45
+ requests: batchRequests.map( ( request ) => ( {
46
+ path: request.path,
47
+ body: request.data, // Rename 'data' to 'body'.
48
+ method: request.method,
49
+ headers: request.headers,
50
+ } ) ),
51
+ },
52
+ } );
53
+
54
+ let batchResults;
55
+
56
+ if ( batchResponse.failed ) {
57
+ batchResults = batchResponse.responses.map( ( response ) => ( {
58
+ error: response?.body,
59
+ } ) );
40
60
  } else {
41
- result.error = response.body;
61
+ batchResults = batchResponse.responses.map( ( response ) => {
62
+ const result = {};
63
+ if ( response.status >= 200 && response.status < 300 ) {
64
+ result.output = response.body;
65
+ } else {
66
+ result.error = response.body;
67
+ }
68
+ return result;
69
+ } );
42
70
  }
43
- return result;
44
- } );
71
+
72
+ results.push( ...batchResults );
73
+ }
74
+
75
+ return results;
45
76
  }
@@ -11,6 +11,18 @@ import defaultProcessor from '../default-processor';
11
11
  jest.mock( '@wordpress/api-fetch' );
12
12
 
13
13
  describe( 'defaultProcessor', () => {
14
+ const preflightResponse = {
15
+ endpoints: [
16
+ {
17
+ args: {
18
+ requests: {
19
+ maxItems: 25,
20
+ },
21
+ },
22
+ },
23
+ ],
24
+ };
25
+
14
26
  const requests = [
15
27
  {
16
28
  path: '/v1/cricketers',
@@ -26,7 +38,12 @@ describe( 'defaultProcessor', () => {
26
38
  },
27
39
  ];
28
40
 
29
- const expectedFetchOptions = {
41
+ const expectedPreflightOptions = {
42
+ path: '/batch/v1',
43
+ method: 'OPTIONS',
44
+ };
45
+
46
+ const expectedBatchOptions = {
30
47
  path: '/batch/v1',
31
48
  method: 'POST',
32
49
  data: {
@@ -49,21 +66,26 @@ describe( 'defaultProcessor', () => {
49
66
  };
50
67
 
51
68
  it( 'handles a successful request', async () => {
52
- apiFetch.mockImplementation( async () => ( {
53
- failed: false,
54
- responses: [
55
- {
56
- status: 200,
57
- body: 'Lyon',
58
- },
59
- {
60
- status: 400,
61
- body: 'Error!',
62
- },
63
- ],
64
- } ) );
69
+ apiFetch.mockImplementation( async ( { method } ) =>
70
+ method === 'OPTIONS'
71
+ ? preflightResponse
72
+ : {
73
+ failed: false,
74
+ responses: [
75
+ {
76
+ status: 200,
77
+ body: 'Lyon',
78
+ },
79
+ {
80
+ status: 400,
81
+ body: 'Error!',
82
+ },
83
+ ],
84
+ }
85
+ );
65
86
  const results = await defaultProcessor( requests );
66
- expect( apiFetch ).toHaveBeenCalledWith( expectedFetchOptions );
87
+ expect( apiFetch ).toHaveBeenCalledWith( expectedPreflightOptions );
88
+ expect( apiFetch ).toHaveBeenCalledWith( expectedBatchOptions );
67
89
  expect( results ).toEqual( [
68
90
  { output: 'Lyon' },
69
91
  { error: 'Error!' },
@@ -71,18 +93,23 @@ describe( 'defaultProcessor', () => {
71
93
  } );
72
94
 
73
95
  it( 'handles a failed request', async () => {
74
- apiFetch.mockImplementation( async () => ( {
75
- failed: true,
76
- responses: [
77
- null,
78
- {
79
- status: 400,
80
- body: 'Error!',
81
- },
82
- ],
83
- } ) );
96
+ apiFetch.mockImplementation( async ( { method } ) =>
97
+ method === 'OPTIONS'
98
+ ? preflightResponse
99
+ : {
100
+ failed: true,
101
+ responses: [
102
+ null,
103
+ {
104
+ status: 400,
105
+ body: 'Error!',
106
+ },
107
+ ],
108
+ }
109
+ );
84
110
  const results = await defaultProcessor( requests );
85
- expect( apiFetch ).toHaveBeenCalledWith( expectedFetchOptions );
111
+ expect( apiFetch ).toHaveBeenCalledWith( expectedPreflightOptions );
112
+ expect( apiFetch ).toHaveBeenCalledWith( expectedBatchOptions );
86
113
  expect( results ).toEqual( [
87
114
  { error: undefined },
88
115
  { error: 'Error!' },