woocommerce/packages/js/data/src/controls.ts

48 lines
1.0 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { controls as dataControls } from '@wordpress/data-controls';
import apiFetch, { APIFetchOptions } from '@wordpress/api-fetch';
2022-05-11 15:04:18 +00:00
import { AnyAction } from 'redux';
2022-05-11 15:04:18 +00:00
export const fetchWithHeaders = (
options: APIFetchOptions
): AnyAction & { options: APIFetchOptions } => {
return {
type: 'FETCH_WITH_HEADERS',
options,
};
};
export type FetchWithHeadersResponse< Data > = {
headers: Response[ 'headers' ];
status: Response[ 'status' ];
data: Data;
};
const controls = {
...dataControls,
2022-05-11 15:04:18 +00:00
FETCH_WITH_HEADERS( action: AnyAction ) {
return apiFetch< Response >( { ...action.options, parse: false } )
.then( ( response ) => {
return Promise.all( [
response.headers,
response.status,
response.json(),
] );
} )
.then( ( [ headers, status, data ] ) => ( {
headers,
status,
data,
} ) )
.catch( ( response ) => {
return response.json().then( ( data: unknown ) => {
throw data;
} );
} );
},
};
export default controls;