api-fetch.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/define property getters */
  8. /******/ !function() {
  9. /******/ // define getter functions for harmony exports
  10. /******/ __webpack_require__.d = function(exports, definition) {
  11. /******/ for(var key in definition) {
  12. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14. /******/ }
  15. /******/ }
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  20. /******/ !function() {
  21. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22. /******/ }();
  23. /******/
  24. /************************************************************************/
  25. var __webpack_exports__ = {};
  26. // EXPORTS
  27. __webpack_require__.d(__webpack_exports__, {
  28. "default": function() { return /* binding */ build_module; }
  29. });
  30. ;// CONCATENATED MODULE: external ["wp","i18n"]
  31. var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  32. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/nonce.js
  33. /**
  34. * @param {string} nonce
  35. * @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
  36. */
  37. function createNonceMiddleware(nonce) {
  38. /**
  39. * @type {import('../types').APIFetchMiddleware & { nonce: string }}
  40. */
  41. const middleware = (options, next) => {
  42. const {
  43. headers = {}
  44. } = options; // If an 'X-WP-Nonce' header (or any case-insensitive variation
  45. // thereof) was specified, no need to add a nonce header.
  46. for (const headerName in headers) {
  47. if (headerName.toLowerCase() === 'x-wp-nonce' && headers[headerName] === middleware.nonce) {
  48. return next(options);
  49. }
  50. }
  51. return next({ ...options,
  52. headers: { ...headers,
  53. 'X-WP-Nonce': middleware.nonce
  54. }
  55. });
  56. };
  57. middleware.nonce = nonce;
  58. return middleware;
  59. }
  60. /* harmony default export */ var nonce = (createNonceMiddleware);
  61. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/namespace-endpoint.js
  62. /**
  63. * @type {import('../types').APIFetchMiddleware}
  64. */
  65. const namespaceAndEndpointMiddleware = (options, next) => {
  66. let path = options.path;
  67. let namespaceTrimmed, endpointTrimmed;
  68. if (typeof options.namespace === 'string' && typeof options.endpoint === 'string') {
  69. namespaceTrimmed = options.namespace.replace(/^\/|\/$/g, '');
  70. endpointTrimmed = options.endpoint.replace(/^\//, '');
  71. if (endpointTrimmed) {
  72. path = namespaceTrimmed + '/' + endpointTrimmed;
  73. } else {
  74. path = namespaceTrimmed;
  75. }
  76. }
  77. delete options.namespace;
  78. delete options.endpoint;
  79. return next({ ...options,
  80. path
  81. });
  82. };
  83. /* harmony default export */ var namespace_endpoint = (namespaceAndEndpointMiddleware);
  84. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/root-url.js
  85. /**
  86. * Internal dependencies
  87. */
  88. /**
  89. * @param {string} rootURL
  90. * @return {import('../types').APIFetchMiddleware} Root URL middleware.
  91. */
  92. const createRootURLMiddleware = rootURL => (options, next) => {
  93. return namespace_endpoint(options, optionsWithPath => {
  94. let url = optionsWithPath.url;
  95. let path = optionsWithPath.path;
  96. let apiRoot;
  97. if (typeof path === 'string') {
  98. apiRoot = rootURL;
  99. if (-1 !== rootURL.indexOf('?')) {
  100. path = path.replace('?', '&');
  101. }
  102. path = path.replace(/^\//, ''); // API root may already include query parameter prefix if site is
  103. // configured to use plain permalinks.
  104. if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) {
  105. path = path.replace('?', '&');
  106. }
  107. url = apiRoot + path;
  108. }
  109. return next({ ...optionsWithPath,
  110. url
  111. });
  112. });
  113. };
  114. /* harmony default export */ var root_url = (createRootURLMiddleware);
  115. ;// CONCATENATED MODULE: external ["wp","url"]
  116. var external_wp_url_namespaceObject = window["wp"]["url"];
  117. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/preloading.js
  118. /**
  119. * WordPress dependencies
  120. */
  121. /**
  122. * @param {Record<string, any>} preloadedData
  123. * @return {import('../types').APIFetchMiddleware} Preloading middleware.
  124. */
  125. function createPreloadingMiddleware(preloadedData) {
  126. const cache = Object.fromEntries(Object.entries(preloadedData).map(_ref => {
  127. let [path, data] = _ref;
  128. return [(0,external_wp_url_namespaceObject.normalizePath)(path), data];
  129. }));
  130. return (options, next) => {
  131. const {
  132. parse = true
  133. } = options;
  134. /** @type {string | void} */
  135. let rawPath = options.path;
  136. if (!rawPath && options.url) {
  137. const {
  138. rest_route: pathFromQuery,
  139. ...queryArgs
  140. } = (0,external_wp_url_namespaceObject.getQueryArgs)(options.url);
  141. if (typeof pathFromQuery === 'string') {
  142. rawPath = (0,external_wp_url_namespaceObject.addQueryArgs)(pathFromQuery, queryArgs);
  143. }
  144. }
  145. if (typeof rawPath !== 'string') {
  146. return next(options);
  147. }
  148. const method = options.method || 'GET';
  149. const path = (0,external_wp_url_namespaceObject.normalizePath)(rawPath);
  150. if ('GET' === method && cache[path]) {
  151. const cacheData = cache[path]; // Unsetting the cache key ensures that the data is only used a single time.
  152. delete cache[path];
  153. return prepareResponse(cacheData, !!parse);
  154. } else if ('OPTIONS' === method && cache[method] && cache[method][path]) {
  155. const cacheData = cache[method][path]; // Unsetting the cache key ensures that the data is only used a single time.
  156. delete cache[method][path];
  157. return prepareResponse(cacheData, !!parse);
  158. }
  159. return next(options);
  160. };
  161. }
  162. /**
  163. * This is a helper function that sends a success response.
  164. *
  165. * @param {Record<string, any>} responseData
  166. * @param {boolean} parse
  167. * @return {Promise<any>} Promise with the response.
  168. */
  169. function prepareResponse(responseData, parse) {
  170. return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), {
  171. status: 200,
  172. statusText: 'OK',
  173. headers: responseData.headers
  174. }));
  175. }
  176. /* harmony default export */ var preloading = (createPreloadingMiddleware);
  177. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/fetch-all-middleware.js
  178. /**
  179. * WordPress dependencies
  180. */
  181. /**
  182. * Internal dependencies
  183. */
  184. /**
  185. * Apply query arguments to both URL and Path, whichever is present.
  186. *
  187. * @param {import('../types').APIFetchOptions} props
  188. * @param {Record<string, string | number>} queryArgs
  189. * @return {import('../types').APIFetchOptions} The request with the modified query args
  190. */
  191. const modifyQuery = (_ref, queryArgs) => {
  192. let {
  193. path,
  194. url,
  195. ...options
  196. } = _ref;
  197. return { ...options,
  198. url: url && (0,external_wp_url_namespaceObject.addQueryArgs)(url, queryArgs),
  199. path: path && (0,external_wp_url_namespaceObject.addQueryArgs)(path, queryArgs)
  200. };
  201. };
  202. /**
  203. * Duplicates parsing functionality from apiFetch.
  204. *
  205. * @param {Response} response
  206. * @return {Promise<any>} Parsed response json.
  207. */
  208. const parseResponse = response => response.json ? response.json() : Promise.reject(response);
  209. /**
  210. * @param {string | null} linkHeader
  211. * @return {{ next?: string }} The parsed link header.
  212. */
  213. const parseLinkHeader = linkHeader => {
  214. if (!linkHeader) {
  215. return {};
  216. }
  217. const match = linkHeader.match(/<([^>]+)>; rel="next"/);
  218. return match ? {
  219. next: match[1]
  220. } : {};
  221. };
  222. /**
  223. * @param {Response} response
  224. * @return {string | undefined} The next page URL.
  225. */
  226. const getNextPageUrl = response => {
  227. const {
  228. next
  229. } = parseLinkHeader(response.headers.get('link'));
  230. return next;
  231. };
  232. /**
  233. * @param {import('../types').APIFetchOptions} options
  234. * @return {boolean} True if the request contains an unbounded query.
  235. */
  236. const requestContainsUnboundedQuery = options => {
  237. const pathIsUnbounded = !!options.path && options.path.indexOf('per_page=-1') !== -1;
  238. const urlIsUnbounded = !!options.url && options.url.indexOf('per_page=-1') !== -1;
  239. return pathIsUnbounded || urlIsUnbounded;
  240. };
  241. /**
  242. * The REST API enforces an upper limit on the per_page option. To handle large
  243. * collections, apiFetch consumers can pass `per_page=-1`; this middleware will
  244. * then recursively assemble a full response array from all available pages.
  245. *
  246. * @type {import('../types').APIFetchMiddleware}
  247. */
  248. const fetchAllMiddleware = async (options, next) => {
  249. if (options.parse === false) {
  250. // If a consumer has opted out of parsing, do not apply middleware.
  251. return next(options);
  252. }
  253. if (!requestContainsUnboundedQuery(options)) {
  254. // If neither url nor path is requesting all items, do not apply middleware.
  255. return next(options);
  256. } // Retrieve requested page of results.
  257. const response = await build_module({ ...modifyQuery(options, {
  258. per_page: 100
  259. }),
  260. // Ensure headers are returned for page 1.
  261. parse: false
  262. });
  263. const results = await parseResponse(response);
  264. if (!Array.isArray(results)) {
  265. // We have no reliable way of merging non-array results.
  266. return results;
  267. }
  268. let nextPage = getNextPageUrl(response);
  269. if (!nextPage) {
  270. // There are no further pages to request.
  271. return results;
  272. } // Iteratively fetch all remaining pages until no "next" header is found.
  273. let mergedResults =
  274. /** @type {any[]} */
  275. [].concat(results);
  276. while (nextPage) {
  277. const nextResponse = await build_module({ ...options,
  278. // Ensure the URL for the next page is used instead of any provided path.
  279. path: undefined,
  280. url: nextPage,
  281. // Ensure we still get headers so we can identify the next page.
  282. parse: false
  283. });
  284. const nextResults = await parseResponse(nextResponse);
  285. mergedResults = mergedResults.concat(nextResults);
  286. nextPage = getNextPageUrl(nextResponse);
  287. }
  288. return mergedResults;
  289. };
  290. /* harmony default export */ var fetch_all_middleware = (fetchAllMiddleware);
  291. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/http-v1.js
  292. /**
  293. * Set of HTTP methods which are eligible to be overridden.
  294. *
  295. * @type {Set<string>}
  296. */
  297. const OVERRIDE_METHODS = new Set(['PATCH', 'PUT', 'DELETE']);
  298. /**
  299. * Default request method.
  300. *
  301. * "A request has an associated method (a method). Unless stated otherwise it
  302. * is `GET`."
  303. *
  304. * @see https://fetch.spec.whatwg.org/#requests
  305. *
  306. * @type {string}
  307. */
  308. const DEFAULT_METHOD = 'GET';
  309. /**
  310. * API Fetch middleware which overrides the request method for HTTP v1
  311. * compatibility leveraging the REST API X-HTTP-Method-Override header.
  312. *
  313. * @type {import('../types').APIFetchMiddleware}
  314. */
  315. const httpV1Middleware = (options, next) => {
  316. const {
  317. method = DEFAULT_METHOD
  318. } = options;
  319. if (OVERRIDE_METHODS.has(method.toUpperCase())) {
  320. options = { ...options,
  321. headers: { ...options.headers,
  322. 'X-HTTP-Method-Override': method,
  323. 'Content-Type': 'application/json'
  324. },
  325. method: 'POST'
  326. };
  327. }
  328. return next(options);
  329. };
  330. /* harmony default export */ var http_v1 = (httpV1Middleware);
  331. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/user-locale.js
  332. /**
  333. * WordPress dependencies
  334. */
  335. /**
  336. * @type {import('../types').APIFetchMiddleware}
  337. */
  338. const userLocaleMiddleware = (options, next) => {
  339. if (typeof options.url === 'string' && !(0,external_wp_url_namespaceObject.hasQueryArg)(options.url, '_locale')) {
  340. options.url = (0,external_wp_url_namespaceObject.addQueryArgs)(options.url, {
  341. _locale: 'user'
  342. });
  343. }
  344. if (typeof options.path === 'string' && !(0,external_wp_url_namespaceObject.hasQueryArg)(options.path, '_locale')) {
  345. options.path = (0,external_wp_url_namespaceObject.addQueryArgs)(options.path, {
  346. _locale: 'user'
  347. });
  348. }
  349. return next(options);
  350. };
  351. /* harmony default export */ var user_locale = (userLocaleMiddleware);
  352. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/utils/response.js
  353. /**
  354. * WordPress dependencies
  355. */
  356. /**
  357. * Parses the apiFetch response.
  358. *
  359. * @param {Response} response
  360. * @param {boolean} shouldParseResponse
  361. *
  362. * @return {Promise<any> | null | Response} Parsed response.
  363. */
  364. const response_parseResponse = function (response) {
  365. let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  366. if (shouldParseResponse) {
  367. if (response.status === 204) {
  368. return null;
  369. }
  370. return response.json ? response.json() : Promise.reject(response);
  371. }
  372. return response;
  373. };
  374. /**
  375. * Calls the `json` function on the Response, throwing an error if the response
  376. * doesn't have a json function or if parsing the json itself fails.
  377. *
  378. * @param {Response} response
  379. * @return {Promise<any>} Parsed response.
  380. */
  381. const parseJsonAndNormalizeError = response => {
  382. const invalidJsonError = {
  383. code: 'invalid_json',
  384. message: (0,external_wp_i18n_namespaceObject.__)('The response is not a valid JSON response.')
  385. };
  386. if (!response || !response.json) {
  387. throw invalidJsonError;
  388. }
  389. return response.json().catch(() => {
  390. throw invalidJsonError;
  391. });
  392. };
  393. /**
  394. * Parses the apiFetch response properly and normalize response errors.
  395. *
  396. * @param {Response} response
  397. * @param {boolean} shouldParseResponse
  398. *
  399. * @return {Promise<any>} Parsed response.
  400. */
  401. const parseResponseAndNormalizeError = function (response) {
  402. let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  403. return Promise.resolve(response_parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
  404. };
  405. /**
  406. * Parses a response, throwing an error if parsing the response fails.
  407. *
  408. * @param {Response} response
  409. * @param {boolean} shouldParseResponse
  410. * @return {Promise<any>} Parsed response.
  411. */
  412. function parseAndThrowError(response) {
  413. let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  414. if (!shouldParseResponse) {
  415. throw response;
  416. }
  417. return parseJsonAndNormalizeError(response).then(error => {
  418. const unknownError = {
  419. code: 'unknown_error',
  420. message: (0,external_wp_i18n_namespaceObject.__)('An unknown error occurred.')
  421. };
  422. throw error || unknownError;
  423. });
  424. }
  425. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/media-upload.js
  426. /**
  427. * WordPress dependencies
  428. */
  429. /**
  430. * Internal dependencies
  431. */
  432. /**
  433. * @param {import('../types').APIFetchOptions} options
  434. * @return {boolean} True if the request is for media upload.
  435. */
  436. function isMediaUploadRequest(options) {
  437. const isCreateMethod = !!options.method && options.method === 'POST';
  438. const isMediaEndpoint = !!options.path && options.path.indexOf('/wp/v2/media') !== -1 || !!options.url && options.url.indexOf('/wp/v2/media') !== -1;
  439. return isMediaEndpoint && isCreateMethod;
  440. }
  441. /**
  442. * Middleware handling media upload failures and retries.
  443. *
  444. * @type {import('../types').APIFetchMiddleware}
  445. */
  446. const mediaUploadMiddleware = (options, next) => {
  447. if (!isMediaUploadRequest(options)) {
  448. return next(options);
  449. }
  450. let retries = 0;
  451. const maxRetries = 5;
  452. /**
  453. * @param {string} attachmentId
  454. * @return {Promise<any>} Processed post response.
  455. */
  456. const postProcess = attachmentId => {
  457. retries++;
  458. return next({
  459. path: `/wp/v2/media/${attachmentId}/post-process`,
  460. method: 'POST',
  461. data: {
  462. action: 'create-image-subsizes'
  463. },
  464. parse: false
  465. }).catch(() => {
  466. if (retries < maxRetries) {
  467. return postProcess(attachmentId);
  468. }
  469. next({
  470. path: `/wp/v2/media/${attachmentId}?force=true`,
  471. method: 'DELETE'
  472. });
  473. return Promise.reject();
  474. });
  475. };
  476. return next({ ...options,
  477. parse: false
  478. }).catch(response => {
  479. const attachmentId = response.headers.get('x-wp-upload-attachment-id');
  480. if (response.status >= 500 && response.status < 600 && attachmentId) {
  481. return postProcess(attachmentId).catch(() => {
  482. if (options.parse !== false) {
  483. return Promise.reject({
  484. code: 'post_process',
  485. message: (0,external_wp_i18n_namespaceObject.__)('Media upload failed. If this is a photo or a large image, please scale it down and try again.')
  486. });
  487. }
  488. return Promise.reject(response);
  489. });
  490. }
  491. return parseAndThrowError(response, options.parse);
  492. }).then(response => parseResponseAndNormalizeError(response, options.parse));
  493. };
  494. /* harmony default export */ var media_upload = (mediaUploadMiddleware);
  495. ;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/index.js
  496. /**
  497. * WordPress dependencies
  498. */
  499. /**
  500. * Internal dependencies
  501. */
  502. /**
  503. * Default set of header values which should be sent with every request unless
  504. * explicitly provided through apiFetch options.
  505. *
  506. * @type {Record<string, string>}
  507. */
  508. const DEFAULT_HEADERS = {
  509. // The backend uses the Accept header as a condition for considering an
  510. // incoming request as a REST request.
  511. //
  512. // See: https://core.trac.wordpress.org/ticket/44534
  513. Accept: 'application/json, */*;q=0.1'
  514. };
  515. /**
  516. * Default set of fetch option values which should be sent with every request
  517. * unless explicitly provided through apiFetch options.
  518. *
  519. * @type {Object}
  520. */
  521. const DEFAULT_OPTIONS = {
  522. credentials: 'include'
  523. };
  524. /** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
  525. /** @typedef {import('./types').APIFetchOptions} APIFetchOptions */
  526. /**
  527. * @type {import('./types').APIFetchMiddleware[]}
  528. */
  529. const middlewares = [user_locale, namespace_endpoint, http_v1, fetch_all_middleware];
  530. /**
  531. * Register a middleware
  532. *
  533. * @param {import('./types').APIFetchMiddleware} middleware
  534. */
  535. function registerMiddleware(middleware) {
  536. middlewares.unshift(middleware);
  537. }
  538. /**
  539. * Checks the status of a response, throwing the Response as an error if
  540. * it is outside the 200 range.
  541. *
  542. * @param {Response} response
  543. * @return {Response} The response if the status is in the 200 range.
  544. */
  545. const checkStatus = response => {
  546. if (response.status >= 200 && response.status < 300) {
  547. return response;
  548. }
  549. throw response;
  550. };
  551. /** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/
  552. /**
  553. * @type {FetchHandler}
  554. */
  555. const defaultFetchHandler = nextOptions => {
  556. const {
  557. url,
  558. path,
  559. data,
  560. parse = true,
  561. ...remainingOptions
  562. } = nextOptions;
  563. let {
  564. body,
  565. headers
  566. } = nextOptions; // Merge explicitly-provided headers with default values.
  567. headers = { ...DEFAULT_HEADERS,
  568. ...headers
  569. }; // The `data` property is a shorthand for sending a JSON body.
  570. if (data) {
  571. body = JSON.stringify(data);
  572. headers['Content-Type'] = 'application/json';
  573. }
  574. const responsePromise = window.fetch( // Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
  575. url || path || window.location.href, { ...DEFAULT_OPTIONS,
  576. ...remainingOptions,
  577. body,
  578. headers
  579. });
  580. return responsePromise.then(value => Promise.resolve(value).then(checkStatus).catch(response => parseAndThrowError(response, parse)).then(response => parseResponseAndNormalizeError(response, parse)), err => {
  581. // Re-throw AbortError for the users to handle it themselves.
  582. if (err && err.name === 'AbortError') {
  583. throw err;
  584. } // Otherwise, there is most likely no network connection.
  585. // Unfortunately the message might depend on the browser.
  586. throw {
  587. code: 'fetch_error',
  588. message: (0,external_wp_i18n_namespaceObject.__)('You are probably offline.')
  589. };
  590. });
  591. };
  592. /** @type {FetchHandler} */
  593. let fetchHandler = defaultFetchHandler;
  594. /**
  595. * Defines a custom fetch handler for making the requests that will override
  596. * the default one using window.fetch
  597. *
  598. * @param {FetchHandler} newFetchHandler The new fetch handler
  599. */
  600. function setFetchHandler(newFetchHandler) {
  601. fetchHandler = newFetchHandler;
  602. }
  603. /**
  604. * @template T
  605. * @param {import('./types').APIFetchOptions} options
  606. * @return {Promise<T>} A promise representing the request processed via the registered middlewares.
  607. */
  608. function apiFetch(options) {
  609. // creates a nested function chain that calls all middlewares and finally the `fetchHandler`,
  610. // converting `middlewares = [ m1, m2, m3 ]` into:
  611. // ```
  612. // opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );
  613. // ```
  614. const enhancedHandler = middlewares.reduceRight((
  615. /** @type {FetchHandler} */
  616. next, middleware) => {
  617. return workingOptions => middleware(workingOptions, next);
  618. }, fetchHandler);
  619. return enhancedHandler(options).catch(error => {
  620. if (error.code !== 'rest_cookie_invalid_nonce') {
  621. return Promise.reject(error);
  622. } // If the nonce is invalid, refresh it and try again.
  623. return window // @ts-ignore
  624. .fetch(apiFetch.nonceEndpoint).then(checkStatus).then(data => data.text()).then(text => {
  625. // @ts-ignore
  626. apiFetch.nonceMiddleware.nonce = text;
  627. return apiFetch(options);
  628. });
  629. });
  630. }
  631. apiFetch.use = registerMiddleware;
  632. apiFetch.setFetchHandler = setFetchHandler;
  633. apiFetch.createNonceMiddleware = nonce;
  634. apiFetch.createPreloadingMiddleware = preloading;
  635. apiFetch.createRootURLMiddleware = root_url;
  636. apiFetch.fetchAllMiddleware = fetch_all_middleware;
  637. apiFetch.mediaUploadMiddleware = media_upload;
  638. /* harmony default export */ var build_module = (apiFetch);
  639. (window.wp = window.wp || {}).apiFetch = __webpack_exports__["default"];
  640. /******/ })()
  641. ;