Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default batch processor: Respect the batch endpoint's maxItems #34280

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 57 additions & 26 deletions packages/core-data/src/batch/default-processor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
/**
* External dependencies
*/
import { chunk } from 'lodash';

/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';

/**
* Default batch processor. Sends its input requests to /v1/batch.
* Maximum number of requests to place in a single batch request. Obtained by
* sending a preflight OPTIONS request to /batch/v1/.
*
* @type {number?}
*/
let maxItems = null;

/**
* Default batch processor. Sends its input requests to /batch/v1.
*
* @param {Array} requests List of API requests to perform at once.
*
Expand All @@ -13,33 +26,51 @@ import apiFetch from '@wordpress/api-fetch';
* (if not ).
*/
export default async function defaultProcessor( requests ) {
const batchResponse = await apiFetch( {
path: '/batch/v1',
method: 'POST',
data: {
validation: 'require-all-validate',
requests: requests.map( ( request ) => ( {
path: request.path,
body: request.data, // Rename 'data' to 'body'.
method: request.method,
headers: request.headers,
} ) ),
},
} );

if ( batchResponse.failed ) {
return batchResponse.responses.map( ( response ) => ( {
error: response?.body,
} ) );
if ( maxItems === null ) {
const preflightResponse = await apiFetch( {
path: '/batch/v1',
method: 'OPTIONS',
} );
maxItems = preflightResponse.endpoints[ 0 ].args.requests.maxItems;
}

return batchResponse.responses.map( ( response ) => {
const result = {};
if ( response.status >= 200 && response.status < 300 ) {
result.output = response.body;
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 {
result.error = response.body;
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;
} );
}
return result;
} );

results.push( ...batchResults );
}

return results;
}
79 changes: 53 additions & 26 deletions packages/core-data/src/batch/test/default-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import defaultProcessor from '../default-processor';
jest.mock( '@wordpress/api-fetch' );

describe( 'defaultProcessor', () => {
const preflightResponse = {
endpoints: [
{
args: {
requests: {
maxItems: 25,
},
},
},
],
};

const requests = [
{
path: '/v1/cricketers',
Expand All @@ -26,7 +38,12 @@ describe( 'defaultProcessor', () => {
},
];

const expectedFetchOptions = {
const expectedPreflightOptions = {
path: '/batch/v1',
method: 'OPTIONS',
};

const expectedBatchOptions = {
path: '/batch/v1',
method: 'POST',
data: {
Expand All @@ -49,40 +66,50 @@ describe( 'defaultProcessor', () => {
};

it( 'handles a successful request', async () => {
apiFetch.mockImplementation( async () => ( {
failed: false,
responses: [
{
status: 200,
body: 'Lyon',
},
{
status: 400,
body: 'Error!',
},
],
} ) );
apiFetch.mockImplementation( async ( { method } ) =>
method === 'OPTIONS'
? preflightResponse
: {
failed: false,
responses: [
{
status: 200,
body: 'Lyon',
},
{
status: 400,
body: 'Error!',
},
],
}
);
const results = await defaultProcessor( requests );
expect( apiFetch ).toHaveBeenCalledWith( expectedFetchOptions );
expect( apiFetch ).toHaveBeenCalledWith( expectedPreflightOptions );
expect( apiFetch ).toHaveBeenCalledWith( expectedBatchOptions );
expect( results ).toEqual( [
{ output: 'Lyon' },
{ error: 'Error!' },
] );
} );

it( 'handles a failed request', async () => {
apiFetch.mockImplementation( async () => ( {
failed: true,
responses: [
null,
{
status: 400,
body: 'Error!',
},
],
} ) );
apiFetch.mockImplementation( async ( { method } ) =>
method === 'OPTIONS'
? preflightResponse
: {
failed: true,
responses: [
null,
{
status: 400,
body: 'Error!',
},
],
}
);
const results = await defaultProcessor( requests );
expect( apiFetch ).toHaveBeenCalledWith( expectedFetchOptions );
expect( apiFetch ).toHaveBeenCalledWith( expectedPreflightOptions );
expect( apiFetch ).toHaveBeenCalledWith( expectedBatchOptions );
expect( results ).toEqual( [
{ error: undefined },
{ error: 'Error!' },
Expand Down