2019-10-28 13:53:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-04-29 14:47:05 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-02-24 01:36:24 +00:00
|
|
|
import triggerFetch, { APIFetchOptions } from '@wordpress/api-fetch';
|
2021-04-20 15:44:49 +00:00
|
|
|
import DataLoader from 'dataloader';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
assertBatchResponseIsValid,
|
|
|
|
assertResponseIsValid,
|
|
|
|
ApiResponse,
|
|
|
|
} from './types';
|
2021-02-24 01:36:24 +00:00
|
|
|
|
2019-10-28 13:53:09 +00:00
|
|
|
/**
|
|
|
|
* Dispatched a control action for triggering an api fetch call with no parsing.
|
|
|
|
* Typically this would be used in scenarios where headers are needed.
|
|
|
|
*
|
2021-02-24 01:36:24 +00:00
|
|
|
* @param {APIFetchOptions} options The options for the API request.
|
2019-10-28 13:53:09 +00:00
|
|
|
*/
|
2021-03-01 17:47:22 +00:00
|
|
|
export const apiFetchWithHeaders = ( options: APIFetchOptions ) =>
|
|
|
|
( {
|
2019-10-28 13:53:09 +00:00
|
|
|
type: 'API_FETCH_WITH_HEADERS',
|
2020-03-19 11:50:51 +00:00
|
|
|
options,
|
2021-03-01 17:47:22 +00:00
|
|
|
} as const );
|
2019-10-28 13:53:09 +00:00
|
|
|
|
2021-04-20 15:44:49 +00:00
|
|
|
const EMPTY_OBJECT = {};
|
|
|
|
|
2020-04-29 14:47:05 +00:00
|
|
|
/**
|
|
|
|
* Error thrown when JSON cannot be parsed.
|
|
|
|
*/
|
|
|
|
const invalidJsonError = {
|
|
|
|
code: 'invalid_json',
|
|
|
|
message: __(
|
|
|
|
'The response is not a valid JSON response.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2021-03-01 11:22:24 +00:00
|
|
|
const setNonceOnFetch = ( headers: Headers ): void => {
|
|
|
|
if (
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore -- this does exist because it's monkey patched in
|
|
|
|
// middleware/store-api-nonce.
|
|
|
|
triggerFetch.setNonce &&
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore -- this does exist because it's monkey patched in
|
|
|
|
// middleware/store-api-nonce.
|
|
|
|
typeof triggerFetch.setNonce === 'function'
|
|
|
|
) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore -- this does exist because it's monkey patched in
|
|
|
|
// middleware/store-api-nonce.
|
|
|
|
triggerFetch.setNonce( headers );
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(
|
|
|
|
'The monkey patched function on APIFetch, "setNonce", is not present, likely another plugin or some other code has removed this augmentation'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-20 15:44:49 +00:00
|
|
|
/**
|
|
|
|
* Trigger a fetch from the API using the batch endpoint.
|
|
|
|
*/
|
|
|
|
const triggerBatchFetch = ( keys: readonly APIFetchOptions[] ) => {
|
|
|
|
return triggerFetch( {
|
|
|
|
path: `/wc/store/batch`,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
requests: keys.map( ( request: APIFetchOptions ) => {
|
|
|
|
return {
|
|
|
|
...request,
|
|
|
|
body: request?.data,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
},
|
|
|
|
} ).then( ( response: unknown ) => {
|
|
|
|
assertBatchResponseIsValid( response );
|
|
|
|
return keys.map(
|
|
|
|
( key, index: number ) =>
|
|
|
|
response.responses[ index ] || EMPTY_OBJECT
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In ms, how long we should wait for requests to batch.
|
|
|
|
*
|
|
|
|
* DataLoader collects all requests over this window of time (and as a consequence, adds this amount of latency).
|
|
|
|
*/
|
|
|
|
const triggerBatchFetchDelay = 300;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataLoader instance for triggerBatchFetch.
|
|
|
|
*/
|
|
|
|
const triggerBatchFetchLoader = new DataLoader( triggerBatchFetch, {
|
|
|
|
batchScheduleFn: ( callback: () => void ) =>
|
|
|
|
setTimeout( callback, triggerBatchFetchDelay ),
|
|
|
|
cache: false,
|
|
|
|
maxBatchSize: 25,
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a fetch from the API using the batch endpoint.
|
|
|
|
*
|
|
|
|
* @param {APIFetchOptions} request Request object containing API request.
|
|
|
|
*/
|
|
|
|
const batchFetch = async ( request: APIFetchOptions ) => {
|
|
|
|
return await triggerBatchFetchLoader.load( request );
|
|
|
|
};
|
|
|
|
|
2019-10-28 13:53:09 +00:00
|
|
|
/**
|
|
|
|
* Default export for registering the controls with the store.
|
|
|
|
*
|
|
|
|
* @return {Object} An object with the controls to register with the store on
|
|
|
|
* the controls property of the registration object.
|
|
|
|
*/
|
|
|
|
export const controls = {
|
2021-03-01 17:47:22 +00:00
|
|
|
API_FETCH_WITH_HEADERS: ( {
|
2021-02-24 01:36:24 +00:00
|
|
|
options,
|
2021-03-01 17:47:22 +00:00
|
|
|
}: ReturnType< typeof apiFetchWithHeaders > ): Promise< unknown > => {
|
2021-04-20 15:44:49 +00:00
|
|
|
const routes = getSetting( 'restApiRoutes' );
|
|
|
|
|
2019-10-28 13:53:09 +00:00
|
|
|
return new Promise( ( resolve, reject ) => {
|
2021-04-20 15:44:49 +00:00
|
|
|
// GET Requests cannot be batched.
|
|
|
|
if (
|
|
|
|
! options.method ||
|
|
|
|
options.method === 'GET' ||
|
|
|
|
! routes[ '/wc/store' ].includes( '/wc/store/batch' )
|
|
|
|
) {
|
|
|
|
// Parse is disabled here to avoid returning just the body--we also need headers.
|
|
|
|
triggerFetch( { ...options, parse: false } )
|
|
|
|
.then( ( fetchResponse ) => {
|
|
|
|
fetchResponse
|
2020-04-29 14:47:05 +00:00
|
|
|
.json()
|
2021-04-20 15:44:49 +00:00
|
|
|
.then( ( response ) => {
|
|
|
|
resolve( {
|
|
|
|
response,
|
|
|
|
headers: fetchResponse.headers,
|
|
|
|
} );
|
|
|
|
setNonceOnFetch( fetchResponse.headers );
|
2020-04-29 14:47:05 +00:00
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
reject( invalidJsonError );
|
|
|
|
} );
|
2021-04-20 15:44:49 +00:00
|
|
|
} )
|
|
|
|
.catch( ( errorResponse ) => {
|
|
|
|
setNonceOnFetch( errorResponse.headers );
|
|
|
|
if ( typeof errorResponse.json === 'function' ) {
|
|
|
|
// Parse error response before rejecting it.
|
|
|
|
errorResponse
|
|
|
|
.json()
|
|
|
|
.then( ( error: unknown ) => {
|
|
|
|
reject( error );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
reject( invalidJsonError );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
reject( errorResponse.message );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
batchFetch( options )
|
|
|
|
.then( ( response: ApiResponse ) => {
|
|
|
|
assertResponseIsValid( response );
|
|
|
|
|
|
|
|
if ( response.status >= 200 && response.status < 300 ) {
|
|
|
|
resolve( {
|
|
|
|
response: response.body,
|
|
|
|
headers: response.headers,
|
|
|
|
} );
|
|
|
|
setNonceOnFetch( response.headers );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status code indicates error.
|
|
|
|
throw response;
|
|
|
|
} )
|
|
|
|
.catch( ( errorResponse: ApiResponse ) => {
|
|
|
|
if ( errorResponse.headers ) {
|
|
|
|
setNonceOnFetch( errorResponse.headers );
|
|
|
|
}
|
|
|
|
if ( errorResponse.body ) {
|
|
|
|
reject( errorResponse.body );
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2019-10-28 13:53:09 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
};
|