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';
|
|
|
|
|
|
|
|
export interface ApiFetchWithHeadersAction {
|
|
|
|
type: string;
|
|
|
|
options: APIFetchOptions;
|
|
|
|
}
|
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-02-24 01:36:24 +00:00
|
|
|
* @return {ApiFetchWithHeadersAction} The control action descriptor.
|
2019-10-28 13:53:09 +00:00
|
|
|
*/
|
2021-02-24 01:36:24 +00:00
|
|
|
export const apiFetchWithHeaders = (
|
|
|
|
options: APIFetchOptions
|
|
|
|
): ApiFetchWithHeadersAction => {
|
2019-10-28 13:53:09 +00:00
|
|
|
return {
|
|
|
|
type: 'API_FETCH_WITH_HEADERS',
|
2020-03-19 11:50:51 +00:00
|
|
|
options,
|
2019-10-28 13:53:09 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
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'
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
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-02-24 01:36:24 +00:00
|
|
|
API_FETCH_WITH_HEADERS( {
|
|
|
|
options,
|
|
|
|
}: {
|
|
|
|
options: APIFetchOptions;
|
|
|
|
} ): Promise< unknown > {
|
2019-10-28 13:53:09 +00:00
|
|
|
return new Promise( ( resolve, reject ) => {
|
2020-03-19 11:50:51 +00:00
|
|
|
triggerFetch( { ...options, parse: false } )
|
|
|
|
.then( ( fetchResponse ) => {
|
2020-04-29 14:47:05 +00:00
|
|
|
fetchResponse
|
|
|
|
.json()
|
|
|
|
.then( ( response ) => {
|
|
|
|
resolve( {
|
|
|
|
response,
|
|
|
|
headers: fetchResponse.headers,
|
|
|
|
} );
|
2021-02-24 01:36:24 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore -- this does exist but doesn't appear to be typed in the api-fetch types.
|
2020-04-29 14:47:05 +00:00
|
|
|
triggerFetch.setNonce( fetchResponse.headers );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
reject( invalidJsonError );
|
|
|
|
} );
|
2019-10-28 13:53:09 +00:00
|
|
|
} )
|
2020-03-31 16:00:03 +00:00
|
|
|
.catch( ( errorResponse ) => {
|
2021-02-24 01:36:24 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore -- this does exist but doesn't appear to be typed in the api-fetch types.
|
2020-12-24 11:00:51 +00:00
|
|
|
triggerFetch.setNonce( errorResponse.headers );
|
2020-04-22 10:09:20 +00:00
|
|
|
if ( typeof errorResponse.json === 'function' ) {
|
|
|
|
// Parse error response before rejecting it.
|
2020-04-29 14:47:05 +00:00
|
|
|
errorResponse
|
|
|
|
.json()
|
2021-02-24 01:36:24 +00:00
|
|
|
.then( ( error: unknown ) => {
|
2020-04-29 14:47:05 +00:00
|
|
|
reject( error );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
reject( invalidJsonError );
|
|
|
|
} );
|
2020-04-22 10:09:20 +00:00
|
|
|
} else {
|
|
|
|
reject( errorResponse.message );
|
|
|
|
}
|
2019-10-28 13:53:09 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
};
|