@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
60 lines (59 loc) • 1.63 kB
JavaScript
// packages/core-data/src/batch/default-processor.js
import apiFetch from "@wordpress/api-fetch";
var maxItems = null;
function chunk(arr, chunkSize) {
const tmp = [...arr];
const cache = [];
while (tmp.length) {
cache.push(tmp.splice(0, chunkSize));
}
return cache;
}
async function defaultProcessor(requests) {
if (maxItems === null) {
const preflightResponse = await apiFetch({
path: "/batch/v1",
method: "OPTIONS"
});
maxItems = preflightResponse.endpoints[0].args.requests.maxItems;
}
const results = [];
for (const batchRequests of chunk(requests, maxItems)) {
const batchResponse = await apiFetch({
path: "/batch/v1",
method: "POST",
data: {
validation: "require-all-validate",
requests: batchRequests.map((request) => ({
path: request.path,
body: request.data,
// Rename 'data' to 'body'.
method: request.method,
headers: request.headers
}))
}
});
let batchResults;
if (batchResponse.failed) {
batchResults = batchResponse.responses.map((response) => ({
error: response?.body
}));
} else {
batchResults = batchResponse.responses.map((response) => {
const result = {};
if (response.status >= 200 && response.status < 300) {
result.output = response.body;
} else {
result.error = response.body;
}
return result;
});
}
results.push(...batchResults);
}
return results;
}
export {
defaultProcessor as default
};
//# sourceMappingURL=default-processor.js.map