2018-12-04 02:58:07 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { stringifyQuery } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-12-04 17:22:46 +00:00
|
|
|
import { getResourceIdentifier, getResourcePrefix } from '../../utils';
|
2018-12-04 02:58:07 +00:00
|
|
|
import { NAMESPACE } from '../../constants';
|
|
|
|
import { SWAGGERNAMESPACE } from 'store/constants';
|
|
|
|
|
2018-12-21 02:13:57 +00:00
|
|
|
const statEndpoints = [ 'orders', 'revenue', 'products', 'taxes', 'coupons' ];
|
2018-12-04 02:58:07 +00:00
|
|
|
// TODO: Remove once swagger endpoints are phased out.
|
2018-12-21 02:13:57 +00:00
|
|
|
const swaggerEndpoints = [ 'categories', 'downloads' ];
|
2018-12-04 02:58:07 +00:00
|
|
|
|
2018-12-04 17:22:46 +00:00
|
|
|
const typeEndpointMap = {
|
|
|
|
'report-stats-query-orders': 'orders',
|
|
|
|
'report-stats-query-revenue': 'revenue',
|
|
|
|
'report-stats-query-products': 'products',
|
|
|
|
'report-stats-query-categories': 'categories',
|
2018-12-19 11:18:43 +00:00
|
|
|
'report-stats-query-downloads': 'downloads',
|
2018-12-04 17:22:46 +00:00
|
|
|
'report-stats-query-coupons': 'coupons',
|
|
|
|
'report-stats-query-taxes': 'taxes',
|
|
|
|
};
|
|
|
|
|
2018-12-04 02:58:07 +00:00
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
2018-12-04 17:22:46 +00:00
|
|
|
const filteredNames = resourceNames.filter( name => {
|
|
|
|
const prefix = getResourcePrefix( name );
|
|
|
|
return Boolean( typeEndpointMap[ prefix ] );
|
|
|
|
} );
|
2018-12-04 02:58:07 +00:00
|
|
|
|
|
|
|
return filteredNames.map( async resourceName => {
|
2018-12-04 17:22:46 +00:00
|
|
|
const prefix = getResourcePrefix( resourceName );
|
|
|
|
const endpoint = typeEndpointMap[ prefix ];
|
|
|
|
const query = getResourceIdentifier( resourceName );
|
2018-12-05 17:05:31 +00:00
|
|
|
|
2018-12-05 19:10:43 +00:00
|
|
|
const fetchArgs = {
|
|
|
|
parse: false,
|
|
|
|
};
|
2018-12-04 02:58:07 +00:00
|
|
|
|
|
|
|
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
|
2018-12-05 19:10:43 +00:00
|
|
|
fetchArgs.url = SWAGGERNAMESPACE + 'reports/' + endpoint + '/stats' + stringifyQuery( query );
|
|
|
|
} else if ( statEndpoints.indexOf( endpoint ) >= 0 ) {
|
|
|
|
fetchArgs.path = NAMESPACE + '/reports/' + endpoint + '/stats' + stringifyQuery( query );
|
|
|
|
} else {
|
|
|
|
fetchArgs.path = endpoint + stringifyQuery( query );
|
2018-12-04 02:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-12-05 19:10:43 +00:00
|
|
|
const response = await fetch( fetchArgs );
|
2018-12-04 02:58:07 +00:00
|
|
|
const report = await response.json();
|
|
|
|
const totalResults = parseInt( response.headers.get( 'x-wp-total' ) );
|
|
|
|
const totalPages = parseInt( response.headers.get( 'x-wp-totalpages' ) );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: report,
|
|
|
|
totalResults,
|
|
|
|
totalPages,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch ( error ) {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
};
|