@wordpress/api-fetch
Version:
Utility to make WordPress REST API requests.
36 lines (34 loc) • 1.04 kB
JavaScript
/**
* Internal dependencies
*/
import namespaceAndEndpointMiddleware from './namespace-endpoint';
/**
* @param {string} rootURL
* @return {import('../types').APIFetchMiddleware} Root URL middleware.
*/
const createRootURLMiddleware = rootURL => (options, next) => {
return namespaceAndEndpointMiddleware(options, optionsWithPath => {
let url = optionsWithPath.url;
let path = optionsWithPath.path;
let apiRoot;
if (typeof path === 'string') {
apiRoot = rootURL;
if (-1 !== rootURL.indexOf('?')) {
path = path.replace('?', '&');
}
path = path.replace(/^\//, '');
// API root may already include query parameter prefix if site is
// configured to use plain permalinks.
if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) {
path = path.replace('?', '&');
}
url = apiRoot + path;
}
return next({
...optionsWithPath,
url
});
});
};
export default createRootURLMiddleware;
//# sourceMappingURL=root-url.js.map