UNPKG

@woocommerce/data

Version:
94 lines (93 loc) 3.94 kB
/** * External dependencies */ import { apiFetch } from '@wordpress/data-controls'; import { controls } from '@wordpress/data'; /** * Internal dependencies */ import { cleanQuery, getUrlParameters, getRestPath, parseId } from './utils'; import { getItemError, getItemSuccess, getItemsError, getItemsSuccess, getItemsTotalCountSuccess, getItemsTotalCountError, } from './actions'; import { request } from '../utils'; export const createResolvers = ({ storeName, resourceName, pluralResourceName, namespace, }) => { const getItem = function* (idQuery) { const urlParameters = getUrlParameters(namespace, idQuery); const { id, key } = parseId(idQuery, urlParameters); try { const item = yield apiFetch({ path: getRestPath(`${namespace}/${id}`, {}, urlParameters), method: 'GET', }); yield getItemSuccess(key, item); return item; } catch (error) { yield getItemError(key, error); throw error; } }; const getItems = function* (query) { const urlParameters = getUrlParameters(namespace, query || {}); const resourceQuery = cleanQuery(query || {}, namespace); yield controls.dispatch(storeName, 'startResolution', `get${pluralResourceName}TotalCount`, [query]); // Require ID when requesting specific fields to later update the resource data. if (resourceQuery && resourceQuery._fields && !resourceQuery._fields.includes('id')) { resourceQuery._fields = ['id', ...resourceQuery._fields]; } try { const path = getRestPath(namespace, query || {}, urlParameters); const { items, totalCount } = yield request(path, resourceQuery); yield getItemsTotalCountSuccess(query, totalCount); yield controls.dispatch(storeName, 'finishResolution', `get${pluralResourceName}TotalCount`, [query]); yield getItemsSuccess(query, items, urlParameters); for (const i of items) { if (i.id) { yield controls.dispatch(storeName, 'finishResolution', `get${resourceName}`, [i.id]); } } return items; } catch (error) { yield getItemsTotalCountError(query, error); yield getItemsError(query, error); throw error; } }; const getItemsTotalCount = function* (query) { const startedTotalCountUsingGetItems = yield controls.select(storeName, 'hasStartedResolution', `get${pluralResourceName}`, [query]); // Skip resolver as we get the total count from the getItems query as well with same query parameters. if (startedTotalCountUsingGetItems) { return; } const totalsQuery = { ...(query || {}), page: 1, per_page: 1, }; const urlParameters = getUrlParameters(namespace, totalsQuery); const resourceQuery = cleanQuery(totalsQuery, namespace); // Require ID when requesting specific fields to later update the resource data. if (resourceQuery && resourceQuery._fields && !resourceQuery._fields.includes('id')) { resourceQuery._fields = ['id', ...resourceQuery._fields]; } try { const path = getRestPath(namespace, {}, urlParameters); const { totalCount } = yield request(path, totalsQuery); yield getItemsTotalCountSuccess(query, totalCount); return totalCount; } catch (error) { yield getItemsTotalCountError(query, error); return error; } }; return { [`get${resourceName}`]: getItem, [`get${pluralResourceName}`]: getItems, [`get${pluralResourceName}TotalCount`]: getItemsTotalCount, }; };