2019-05-20 20:50:25 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-06-11 12:47:53 +00:00
|
|
|
import { omit } from 'lodash';
|
2019-05-20 20:50:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { stringifyQuery } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-06-11 12:47:53 +00:00
|
|
|
import { getResourcePrefix, getResourceIdentifier } from '../utils';
|
2019-05-20 20:50:25 +00:00
|
|
|
import { NAMESPACE } from '../constants';
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
const typeEndpointMap = {
|
|
|
|
'import-status': 'reports/import/status',
|
|
|
|
'import-totals': 'reports/import/totals',
|
|
|
|
};
|
|
|
|
|
2019-05-20 20:50:25 +00:00
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
2019-06-11 12:47:53 +00:00
|
|
|
const filteredNames = resourceNames.filter( name => {
|
|
|
|
const prefix = getResourcePrefix( name );
|
|
|
|
return Boolean( typeEndpointMap[ prefix ] );
|
|
|
|
} );
|
2019-05-20 20:50:25 +00:00
|
|
|
|
|
|
|
return filteredNames.map( async resourceName => {
|
2019-06-11 12:47:53 +00:00
|
|
|
const prefix = getResourcePrefix( resourceName );
|
|
|
|
const endpoint = typeEndpointMap[ prefix ];
|
2019-05-20 20:50:25 +00:00
|
|
|
const query = getResourceIdentifier( resourceName );
|
|
|
|
const fetchArgs = {
|
|
|
|
parse: false,
|
2019-06-11 12:47:53 +00:00
|
|
|
path: NAMESPACE + `/${ endpoint }${ stringifyQuery( omit( query, [ 'timestamp' ] ) ) }`,
|
2019-05-20 20:50:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch( fetchArgs );
|
2019-06-11 12:47:53 +00:00
|
|
|
const data = await response.json();
|
2019-05-20 20:50:25 +00:00
|
|
|
|
|
|
|
return {
|
2019-06-11 12:47:53 +00:00
|
|
|
[ resourceName ]: { data },
|
2019-05-20 20:50:25 +00:00
|
|
|
};
|
|
|
|
} catch ( error ) {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
};
|