2018-09-17 02:26:34 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { identity } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
2018-11-05 21:02:04 +00:00
|
|
|
* WooCommerce dependencies
|
2018-09-17 02:26:34 +00:00
|
|
|
*/
|
2018-11-05 21:02:04 +00:00
|
|
|
import { getIdsFromQuery, stringifyQuery } from '@woocommerce/navigation';
|
2018-09-17 02:26:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a function that accepts ids as they are found in url parameter and
|
|
|
|
* returns a promise with an optional method applied to results
|
|
|
|
*
|
|
|
|
* @param {string} path - api path
|
|
|
|
* @param {Function} [handleData] - function applied to each iteration of data
|
|
|
|
* @returns {Function} - a function of ids returning a promise
|
|
|
|
*/
|
|
|
|
export function getRequestByIdString( path, handleData = identity ) {
|
|
|
|
return function( queryString = '' ) {
|
|
|
|
const idList = getIdsFromQuery( queryString );
|
|
|
|
if ( idList.length < 1 ) {
|
|
|
|
return Promise.resolve( [] );
|
|
|
|
}
|
|
|
|
const payload = stringifyQuery( {
|
|
|
|
include: idList.join( ',' ),
|
|
|
|
per_page: idList.length,
|
|
|
|
} );
|
|
|
|
return apiFetch( { path: path + payload } ).then( data => data.map( handleData ) );
|
|
|
|
};
|
|
|
|
}
|