2019-05-20 20:50:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-07-16 08:56:24 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2019-05-20 20:50:25 +00:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) => {
|
2019-06-11 12:47:53 +00:00
|
|
|
const prefix = getResourcePrefix( name );
|
|
|
|
return Boolean( typeEndpointMap[ prefix ] );
|
|
|
|
} );
|
2019-05-20 20:50:25 +00:00
|
|
|
|
2020-02-14 02:23:21 +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,
|
2020-02-14 02:23:21 +00:00
|
|
|
path: addQueryArgs(
|
|
|
|
`${ NAMESPACE }/${ endpoint }`,
|
|
|
|
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,
|
|
|
|
};
|