@wordpress/api-fetch 6.36.0 → 6.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/index.js +26 -43
  3. package/build/index.js.map +1 -1
  4. package/build/middlewares/fetch-all-middleware.js +16 -32
  5. package/build/middlewares/fetch-all-middleware.js.map +1 -1
  6. package/build/middlewares/http-v1.js +6 -8
  7. package/build/middlewares/http-v1.js.map +1 -1
  8. package/build/middlewares/media-upload.js +4 -14
  9. package/build/middlewares/media-upload.js.map +1 -1
  10. package/build/middlewares/namespace-endpoint.js +2 -6
  11. package/build/middlewares/namespace-endpoint.js.map +1 -1
  12. package/build/middlewares/nonce.js +7 -8
  13. package/build/middlewares/nonce.js.map +1 -1
  14. package/build/middlewares/preloading.js +5 -14
  15. package/build/middlewares/preloading.js.map +1 -1
  16. package/build/middlewares/root-url.js +4 -11
  17. package/build/middlewares/root-url.js.map +1 -1
  18. package/build/middlewares/theme-preview.js +0 -5
  19. package/build/middlewares/theme-preview.js.map +1 -1
  20. package/build/middlewares/user-locale.js +0 -5
  21. package/build/middlewares/user-locale.js.map +1 -1
  22. package/build/types.js.map +1 -1
  23. package/build/utils/response.js +3 -14
  24. package/build/utils/response.js.map +1 -1
  25. package/build-module/index.js +28 -32
  26. package/build-module/index.js.map +1 -1
  27. package/build-module/middlewares/fetch-all-middleware.js +18 -30
  28. package/build-module/middlewares/fetch-all-middleware.js.map +1 -1
  29. package/build-module/middlewares/http-v1.js +6 -7
  30. package/build-module/middlewares/http-v1.js.map +1 -1
  31. package/build-module/middlewares/media-upload.js +6 -13
  32. package/build-module/middlewares/media-upload.js.map +1 -1
  33. package/build-module/middlewares/namespace-endpoint.js +2 -5
  34. package/build-module/middlewares/namespace-endpoint.js.map +1 -1
  35. package/build-module/middlewares/nonce.js +7 -7
  36. package/build-module/middlewares/nonce.js.map +1 -1
  37. package/build-module/middlewares/preloading.js +6 -13
  38. package/build-module/middlewares/preloading.js.map +1 -1
  39. package/build-module/middlewares/root-url.js +5 -9
  40. package/build-module/middlewares/root-url.js.map +1 -1
  41. package/build-module/middlewares/theme-preview.js +1 -4
  42. package/build-module/middlewares/theme-preview.js.map +1 -1
  43. package/build-module/middlewares/user-locale.js +1 -4
  44. package/build-module/middlewares/user-locale.js.map +1 -1
  45. package/build-module/types.js.map +1 -1
  46. package/build-module/utils/response.js +4 -11
  47. package/build-module/utils/response.js.map +1 -1
  48. package/build-types/index.d.ts +6 -6
  49. package/build-types/index.d.ts.map +1 -1
  50. package/package.json +4 -4
  51. package/tsconfig.tsbuildinfo +1 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/middlewares/namespace-endpoint.js"],"names":["namespaceAndEndpointMiddleware","options","next","path","namespaceTrimmed","endpointTrimmed","namespace","endpoint","replace"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA,MAAMA,8BAA8B,GAAG,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AAC3D,MAAIC,IAAI,GAAGF,OAAO,CAACE,IAAnB;AACA,MAAIC,gBAAJ,EAAsBC,eAAtB;;AAEA,MACC,OAAOJ,OAAO,CAACK,SAAf,KAA6B,QAA7B,IACA,OAAOL,OAAO,CAACM,QAAf,KAA4B,QAF7B,EAGE;AACDH,IAAAA,gBAAgB,GAAGH,OAAO,CAACK,SAAR,CAAkBE,OAAlB,CAA2B,UAA3B,EAAuC,EAAvC,CAAnB;AACAH,IAAAA,eAAe,GAAGJ,OAAO,CAACM,QAAR,CAAiBC,OAAjB,CAA0B,KAA1B,EAAiC,EAAjC,CAAlB;;AACA,QAAKH,eAAL,EAAuB;AACtBF,MAAAA,IAAI,GAAGC,gBAAgB,GAAG,GAAnB,GAAyBC,eAAhC;AACA,KAFD,MAEO;AACNF,MAAAA,IAAI,GAAGC,gBAAP;AACA;AACD;;AAED,SAAOH,OAAO,CAACK,SAAf;AACA,SAAOL,OAAO,CAACM,QAAf;AAEA,SAAOL,IAAI,CAAE,EACZ,GAAGD,OADS;AAEZE,IAAAA;AAFY,GAAF,CAAX;AAIA,CAxBD;;eA0BeH,8B","sourcesContent":["/**\n * @type {import('../types').APIFetchMiddleware}\n */\nconst namespaceAndEndpointMiddleware = ( options, next ) => {\n\tlet path = options.path;\n\tlet namespaceTrimmed, endpointTrimmed;\n\n\tif (\n\t\ttypeof options.namespace === 'string' &&\n\t\ttypeof options.endpoint === 'string'\n\t) {\n\t\tnamespaceTrimmed = options.namespace.replace( /^\\/|\\/$/g, '' );\n\t\tendpointTrimmed = options.endpoint.replace( /^\\//, '' );\n\t\tif ( endpointTrimmed ) {\n\t\t\tpath = namespaceTrimmed + '/' + endpointTrimmed;\n\t\t} else {\n\t\t\tpath = namespaceTrimmed;\n\t\t}\n\t}\n\n\tdelete options.namespace;\n\tdelete options.endpoint;\n\n\treturn next( {\n\t\t...options,\n\t\tpath,\n\t} );\n};\n\nexport default namespaceAndEndpointMiddleware;\n"]}
1
+ {"version":3,"names":["namespaceAndEndpointMiddleware","options","next","path","namespaceTrimmed","endpointTrimmed","namespace","endpoint","replace","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/namespace-endpoint.js"],"sourcesContent":["/**\n * @type {import('../types').APIFetchMiddleware}\n */\nconst namespaceAndEndpointMiddleware = ( options, next ) => {\n\tlet path = options.path;\n\tlet namespaceTrimmed, endpointTrimmed;\n\n\tif (\n\t\ttypeof options.namespace === 'string' &&\n\t\ttypeof options.endpoint === 'string'\n\t) {\n\t\tnamespaceTrimmed = options.namespace.replace( /^\\/|\\/$/g, '' );\n\t\tendpointTrimmed = options.endpoint.replace( /^\\//, '' );\n\t\tif ( endpointTrimmed ) {\n\t\t\tpath = namespaceTrimmed + '/' + endpointTrimmed;\n\t\t} else {\n\t\t\tpath = namespaceTrimmed;\n\t\t}\n\t}\n\n\tdelete options.namespace;\n\tdelete options.endpoint;\n\n\treturn next( {\n\t\t...options,\n\t\tpath,\n\t} );\n};\n\nexport default namespaceAndEndpointMiddleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA,MAAMA,8BAA8B,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;EAC3D,IAAIC,IAAI,GAAGF,OAAO,CAACE,IAAI;EACvB,IAAIC,gBAAgB,EAAEC,eAAe;EAErC,IACC,OAAOJ,OAAO,CAACK,SAAS,KAAK,QAAQ,IACrC,OAAOL,OAAO,CAACM,QAAQ,KAAK,QAAQ,EACnC;IACDH,gBAAgB,GAAGH,OAAO,CAACK,SAAS,CAACE,OAAO,CAAE,UAAU,EAAE,EAAG,CAAC;IAC9DH,eAAe,GAAGJ,OAAO,CAACM,QAAQ,CAACC,OAAO,CAAE,KAAK,EAAE,EAAG,CAAC;IACvD,IAAKH,eAAe,EAAG;MACtBF,IAAI,GAAGC,gBAAgB,GAAG,GAAG,GAAGC,eAAe;IAChD,CAAC,MAAM;MACNF,IAAI,GAAGC,gBAAgB;IACxB;EACD;EAEA,OAAOH,OAAO,CAACK,SAAS;EACxB,OAAOL,OAAO,CAACM,QAAQ;EAEvB,OAAOL,IAAI,CAAE;IACZ,GAAGD,OAAO;IACVE;EACD,CAAE,CAAC;AACJ,CAAC;AAAC,IAAAM,QAAA,GAEaT,8BAA8B;AAAAU,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  /**
9
8
  * @param {string} nonce
10
9
  * @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
@@ -16,26 +15,26 @@ function createNonceMiddleware(nonce) {
16
15
  const middleware = (options, next) => {
17
16
  const {
18
17
  headers = {}
19
- } = options; // If an 'X-WP-Nonce' header (or any case-insensitive variation
20
- // thereof) was specified, no need to add a nonce header.
18
+ } = options;
21
19
 
20
+ // If an 'X-WP-Nonce' header (or any case-insensitive variation
21
+ // thereof) was specified, no need to add a nonce header.
22
22
  for (const headerName in headers) {
23
23
  if (headerName.toLowerCase() === 'x-wp-nonce' && headers[headerName] === middleware.nonce) {
24
24
  return next(options);
25
25
  }
26
26
  }
27
-
28
- return next({ ...options,
29
- headers: { ...headers,
27
+ return next({
28
+ ...options,
29
+ headers: {
30
+ ...headers,
30
31
  'X-WP-Nonce': middleware.nonce
31
32
  }
32
33
  });
33
34
  };
34
-
35
35
  middleware.nonce = nonce;
36
36
  return middleware;
37
37
  }
38
-
39
38
  var _default = createNonceMiddleware;
40
39
  exports.default = _default;
41
40
  //# sourceMappingURL=nonce.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/middlewares/nonce.js"],"names":["createNonceMiddleware","nonce","middleware","options","next","headers","headerName","toLowerCase"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA,SAASA,qBAAT,CAAgCC,KAAhC,EAAwC;AACvC;AACD;AACA;AACC,QAAMC,UAAU,GAAG,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AACvC,UAAM;AAAEC,MAAAA,OAAO,GAAG;AAAZ,QAAmBF,OAAzB,CADuC,CAGvC;AACA;;AACA,SAAM,MAAMG,UAAZ,IAA0BD,OAA1B,EAAoC;AACnC,UACCC,UAAU,CAACC,WAAX,OAA6B,YAA7B,IACAF,OAAO,CAAEC,UAAF,CAAP,KAA0BJ,UAAU,CAACD,KAFtC,EAGE;AACD,eAAOG,IAAI,CAAED,OAAF,CAAX;AACA;AACD;;AAED,WAAOC,IAAI,CAAE,EACZ,GAAGD,OADS;AAEZE,MAAAA,OAAO,EAAE,EACR,GAAGA,OADK;AAER,sBAAcH,UAAU,CAACD;AAFjB;AAFG,KAAF,CAAX;AAOA,GArBD;;AAuBAC,EAAAA,UAAU,CAACD,KAAX,GAAmBA,KAAnB;AAEA,SAAOC,UAAP;AACA;;eAEcF,qB","sourcesContent":["/**\n * @param {string} nonce\n * @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.\n */\nfunction createNonceMiddleware( nonce ) {\n\t/**\n\t * @type {import('../types').APIFetchMiddleware & { nonce: string }}\n\t */\n\tconst middleware = ( options, next ) => {\n\t\tconst { headers = {} } = options;\n\n\t\t// If an 'X-WP-Nonce' header (or any case-insensitive variation\n\t\t// thereof) was specified, no need to add a nonce header.\n\t\tfor ( const headerName in headers ) {\n\t\t\tif (\n\t\t\t\theaderName.toLowerCase() === 'x-wp-nonce' &&\n\t\t\t\theaders[ headerName ] === middleware.nonce\n\t\t\t) {\n\t\t\t\treturn next( options );\n\t\t\t}\n\t\t}\n\n\t\treturn next( {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...headers,\n\t\t\t\t'X-WP-Nonce': middleware.nonce,\n\t\t\t},\n\t\t} );\n\t};\n\n\tmiddleware.nonce = nonce;\n\n\treturn middleware;\n}\n\nexport default createNonceMiddleware;\n"]}
1
+ {"version":3,"names":["createNonceMiddleware","nonce","middleware","options","next","headers","headerName","toLowerCase","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/nonce.js"],"sourcesContent":["/**\n * @param {string} nonce\n * @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.\n */\nfunction createNonceMiddleware( nonce ) {\n\t/**\n\t * @type {import('../types').APIFetchMiddleware & { nonce: string }}\n\t */\n\tconst middleware = ( options, next ) => {\n\t\tconst { headers = {} } = options;\n\n\t\t// If an 'X-WP-Nonce' header (or any case-insensitive variation\n\t\t// thereof) was specified, no need to add a nonce header.\n\t\tfor ( const headerName in headers ) {\n\t\t\tif (\n\t\t\t\theaderName.toLowerCase() === 'x-wp-nonce' &&\n\t\t\t\theaders[ headerName ] === middleware.nonce\n\t\t\t) {\n\t\t\t\treturn next( options );\n\t\t\t}\n\t\t}\n\n\t\treturn next( {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...headers,\n\t\t\t\t'X-WP-Nonce': middleware.nonce,\n\t\t\t},\n\t\t} );\n\t};\n\n\tmiddleware.nonce = nonce;\n\n\treturn middleware;\n}\n\nexport default createNonceMiddleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA,SAASA,qBAAqBA,CAAEC,KAAK,EAAG;EACvC;AACD;AACA;EACC,MAAMC,UAAU,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;IACvC,MAAM;MAAEC,OAAO,GAAG,CAAC;IAAE,CAAC,GAAGF,OAAO;;IAEhC;IACA;IACA,KAAM,MAAMG,UAAU,IAAID,OAAO,EAAG;MACnC,IACCC,UAAU,CAACC,WAAW,CAAC,CAAC,KAAK,YAAY,IACzCF,OAAO,CAAEC,UAAU,CAAE,KAAKJ,UAAU,CAACD,KAAK,EACzC;QACD,OAAOG,IAAI,CAAED,OAAQ,CAAC;MACvB;IACD;IAEA,OAAOC,IAAI,CAAE;MACZ,GAAGD,OAAO;MACVE,OAAO,EAAE;QACR,GAAGA,OAAO;QACV,YAAY,EAAEH,UAAU,CAACD;MAC1B;IACD,CAAE,CAAC;EACJ,CAAC;EAEDC,UAAU,CAACD,KAAK,GAAGA,KAAK;EAExB,OAAOC,UAAU;AAClB;AAAC,IAAAM,QAAA,GAEcR,qBAAqB;AAAAS,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _url = require("@wordpress/url");
9
-
10
8
  /**
11
9
  * WordPress dependencies
12
10
  */
@@ -22,42 +20,38 @@ function createPreloadingMiddleware(preloadedData) {
22
20
  parse = true
23
21
  } = options;
24
22
  /** @type {string | void} */
25
-
26
23
  let rawPath = options.path;
27
-
28
24
  if (!rawPath && options.url) {
29
25
  const {
30
26
  rest_route: pathFromQuery,
31
27
  ...queryArgs
32
28
  } = (0, _url.getQueryArgs)(options.url);
33
-
34
29
  if (typeof pathFromQuery === 'string') {
35
30
  rawPath = (0, _url.addQueryArgs)(pathFromQuery, queryArgs);
36
31
  }
37
32
  }
38
-
39
33
  if (typeof rawPath !== 'string') {
40
34
  return next(options);
41
35
  }
42
-
43
36
  const method = options.method || 'GET';
44
37
  const path = (0, _url.normalizePath)(rawPath);
45
-
46
38
  if ('GET' === method && cache[path]) {
47
- const cacheData = cache[path]; // Unsetting the cache key ensures that the data is only used a single time.
39
+ const cacheData = cache[path];
48
40
 
41
+ // Unsetting the cache key ensures that the data is only used a single time.
49
42
  delete cache[path];
50
43
  return prepareResponse(cacheData, !!parse);
51
44
  } else if ('OPTIONS' === method && cache[method] && cache[method][path]) {
52
- const cacheData = cache[method][path]; // Unsetting the cache key ensures that the data is only used a single time.
45
+ const cacheData = cache[method][path];
53
46
 
47
+ // Unsetting the cache key ensures that the data is only used a single time.
54
48
  delete cache[method][path];
55
49
  return prepareResponse(cacheData, !!parse);
56
50
  }
57
-
58
51
  return next(options);
59
52
  };
60
53
  }
54
+
61
55
  /**
62
56
  * This is a helper function that sends a success response.
63
57
  *
@@ -65,8 +59,6 @@ function createPreloadingMiddleware(preloadedData) {
65
59
  * @param {boolean} parse
66
60
  * @return {Promise<any>} Promise with the response.
67
61
  */
68
-
69
-
70
62
  function prepareResponse(responseData, parse) {
71
63
  return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), {
72
64
  status: 200,
@@ -74,7 +66,6 @@ function prepareResponse(responseData, parse) {
74
66
  headers: responseData.headers
75
67
  }));
76
68
  }
77
-
78
69
  var _default = createPreloadingMiddleware;
79
70
  exports.default = _default;
80
71
  //# sourceMappingURL=preloading.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","method","cacheData","prepareResponse","responseData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,SAASA,0BAAT,CAAqCC,aAArC,EAAqD;AACpD,QAAMC,KAAK,GAAGC,MAAM,CAACC,WAAP,CACbD,MAAM,CAACE,OAAP,CAAgBJ,aAAhB,EAAgCK,GAAhC,CAAqC,CAAE,CAAEC,IAAF,EAAQC,IAAR,CAAF,KAAsB,CAC1D,wBAAeD,IAAf,CAD0D,EAE1DC,IAF0D,CAA3D,CADa,CAAd;AAOA,SAAO,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AAC3B,UAAM;AAAEC,MAAAA,KAAK,GAAG;AAAV,QAAmBF,OAAzB;AACA;;AACA,QAAIG,OAAO,GAAGH,OAAO,CAACF,IAAtB;;AACA,QAAK,CAAEK,OAAF,IAAaH,OAAO,CAACI,GAA1B,EAAgC;AAC/B,YAAM;AAAEC,QAAAA,UAAU,EAAEC,aAAd;AAA6B,WAAGC;AAAhC,UAA8C,uBACnDP,OAAO,CAACI,GAD2C,CAApD;;AAIA,UAAK,OAAOE,aAAP,KAAyB,QAA9B,EAAyC;AACxCH,QAAAA,OAAO,GAAG,uBAAcG,aAAd,EAA6BC,SAA7B,CAAV;AACA;AACD;;AAED,QAAK,OAAOJ,OAAP,KAAmB,QAAxB,EAAmC;AAClC,aAAOF,IAAI,CAAED,OAAF,CAAX;AACA;;AAED,UAAMQ,MAAM,GAAGR,OAAO,CAACQ,MAAR,IAAkB,KAAjC;AACA,UAAMV,IAAI,GAAG,wBAAeK,OAAf,CAAb;;AAEA,QAAK,UAAUK,MAAV,IAAoBf,KAAK,CAAEK,IAAF,CAA9B,EAAyC;AACxC,YAAMW,SAAS,GAAGhB,KAAK,CAAEK,IAAF,CAAvB,CADwC,CAGxC;;AACA,aAAOL,KAAK,CAAEK,IAAF,CAAZ;AAEA,aAAOY,eAAe,CAAED,SAAF,EAAa,CAAC,CAAEP,KAAhB,CAAtB;AACA,KAPD,MAOO,IACN,cAAcM,MAAd,IACAf,KAAK,CAAEe,MAAF,CADL,IAEAf,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAHM,EAIL;AACD,YAAMW,SAAS,GAAGhB,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAAlB,CADC,CAGD;;AACA,aAAOL,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAAP;AAEA,aAAOY,eAAe,CAAED,SAAF,EAAa,CAAC,CAAEP,KAAhB,CAAtB;AACA;;AAED,WAAOD,IAAI,CAAED,OAAF,CAAX;AACA,GA1CD;AA2CA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASU,eAAT,CAA0BC,YAA1B,EAAwCT,KAAxC,EAAgD;AAC/C,SAAOU,OAAO,CAACC,OAAR,CACNX,KAAK,GACFS,YAAY,CAACG,IADX,GAEF,IAAIC,MAAM,CAACC,QAAX,CAAqBC,IAAI,CAACC,SAAL,CAAgBP,YAAY,CAACG,IAA7B,CAArB,EAA0D;AAC1DK,IAAAA,MAAM,EAAE,GADkD;AAE1DC,IAAAA,UAAU,EAAE,IAF8C;AAG1DC,IAAAA,OAAO,EAAEV,YAAY,CAACU;AAHoC,GAA1D,CAHG,CAAP;AASA;;eAEc9B,0B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, getQueryArgs, normalizePath } from '@wordpress/url';\n\n/**\n * @param {Record<string, any>} preloadedData\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nfunction createPreloadingMiddleware( preloadedData ) {\n\tconst cache = Object.fromEntries(\n\t\tObject.entries( preloadedData ).map( ( [ path, data ] ) => [\n\t\t\tnormalizePath( path ),\n\t\t\tdata,\n\t\t] )\n\t);\n\n\treturn ( options, next ) => {\n\t\tconst { parse = true } = options;\n\t\t/** @type {string | void} */\n\t\tlet rawPath = options.path;\n\t\tif ( ! rawPath && options.url ) {\n\t\t\tconst { rest_route: pathFromQuery, ...queryArgs } = getQueryArgs(\n\t\t\t\toptions.url\n\t\t\t);\n\n\t\t\tif ( typeof pathFromQuery === 'string' ) {\n\t\t\t\trawPath = addQueryArgs( pathFromQuery, queryArgs );\n\t\t\t}\n\t\t}\n\n\t\tif ( typeof rawPath !== 'string' ) {\n\t\t\treturn next( options );\n\t\t}\n\n\t\tconst method = options.method || 'GET';\n\t\tconst path = normalizePath( rawPath );\n\n\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t} else if (\n\t\t\t'OPTIONS' === method &&\n\t\t\tcache[ method ] &&\n\t\t\tcache[ method ][ path ]\n\t\t) {\n\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ method ][ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\n/**\n * This is a helper function that sends a success response.\n *\n * @param {Record<string, any>} responseData\n * @param {boolean} parse\n * @return {Promise<any>} Promise with the response.\n */\nfunction prepareResponse( responseData, parse ) {\n\treturn Promise.resolve(\n\t\tparse\n\t\t\t? responseData.body\n\t\t\t: new window.Response( JSON.stringify( responseData.body ), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\theaders: responseData.headers,\n\t\t\t } )\n\t);\n}\n\nexport default createPreloadingMiddleware;\n"]}
1
+ {"version":3,"names":["_url","require","createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","normalizePath","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","getQueryArgs","addQueryArgs","method","cacheData","prepareResponse","responseData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, getQueryArgs, normalizePath } from '@wordpress/url';\n\n/**\n * @param {Record<string, any>} preloadedData\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nfunction createPreloadingMiddleware( preloadedData ) {\n\tconst cache = Object.fromEntries(\n\t\tObject.entries( preloadedData ).map( ( [ path, data ] ) => [\n\t\t\tnormalizePath( path ),\n\t\t\tdata,\n\t\t] )\n\t);\n\n\treturn ( options, next ) => {\n\t\tconst { parse = true } = options;\n\t\t/** @type {string | void} */\n\t\tlet rawPath = options.path;\n\t\tif ( ! rawPath && options.url ) {\n\t\t\tconst { rest_route: pathFromQuery, ...queryArgs } = getQueryArgs(\n\t\t\t\toptions.url\n\t\t\t);\n\n\t\t\tif ( typeof pathFromQuery === 'string' ) {\n\t\t\t\trawPath = addQueryArgs( pathFromQuery, queryArgs );\n\t\t\t}\n\t\t}\n\n\t\tif ( typeof rawPath !== 'string' ) {\n\t\t\treturn next( options );\n\t\t}\n\n\t\tconst method = options.method || 'GET';\n\t\tconst path = normalizePath( rawPath );\n\n\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t} else if (\n\t\t\t'OPTIONS' === method &&\n\t\t\tcache[ method ] &&\n\t\t\tcache[ method ][ path ]\n\t\t) {\n\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ method ][ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\n/**\n * This is a helper function that sends a success response.\n *\n * @param {Record<string, any>} responseData\n * @param {boolean} parse\n * @return {Promise<any>} Promise with the response.\n */\nfunction prepareResponse( responseData, parse ) {\n\treturn Promise.resolve(\n\t\tparse\n\t\t\t? responseData.body\n\t\t\t: new window.Response( JSON.stringify( responseData.body ), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\theaders: responseData.headers,\n\t\t\t } )\n\t);\n}\n\nexport default createPreloadingMiddleware;\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,SAASC,0BAA0BA,CAAEC,aAAa,EAAG;EACpD,MAAMC,KAAK,GAAGC,MAAM,CAACC,WAAW,CAC/BD,MAAM,CAACE,OAAO,CAAEJ,aAAc,CAAC,CAACK,GAAG,CAAE,CAAE,CAAEC,IAAI,EAAEC,IAAI,CAAE,KAAM,CAC1D,IAAAC,kBAAa,EAAEF,IAAK,CAAC,EACrBC,IAAI,CACH,CACH,CAAC;EAED,OAAO,CAAEE,OAAO,EAAEC,IAAI,KAAM;IAC3B,MAAM;MAAEC,KAAK,GAAG;IAAK,CAAC,GAAGF,OAAO;IAChC;IACA,IAAIG,OAAO,GAAGH,OAAO,CAACH,IAAI;IAC1B,IAAK,CAAEM,OAAO,IAAIH,OAAO,CAACI,GAAG,EAAG;MAC/B,MAAM;QAAEC,UAAU,EAAEC,aAAa;QAAE,GAAGC;MAAU,CAAC,GAAG,IAAAC,iBAAY,EAC/DR,OAAO,CAACI,GACT,CAAC;MAED,IAAK,OAAOE,aAAa,KAAK,QAAQ,EAAG;QACxCH,OAAO,GAAG,IAAAM,iBAAY,EAAEH,aAAa,EAAEC,SAAU,CAAC;MACnD;IACD;IAEA,IAAK,OAAOJ,OAAO,KAAK,QAAQ,EAAG;MAClC,OAAOF,IAAI,CAAED,OAAQ,CAAC;IACvB;IAEA,MAAMU,MAAM,GAAGV,OAAO,CAACU,MAAM,IAAI,KAAK;IACtC,MAAMb,IAAI,GAAG,IAAAE,kBAAa,EAAEI,OAAQ,CAAC;IAErC,IAAK,KAAK,KAAKO,MAAM,IAAIlB,KAAK,CAAEK,IAAI,CAAE,EAAG;MACxC,MAAMc,SAAS,GAAGnB,KAAK,CAAEK,IAAI,CAAE;;MAE/B;MACA,OAAOL,KAAK,CAAEK,IAAI,CAAE;MAEpB,OAAOe,eAAe,CAAED,SAAS,EAAE,CAAC,CAAET,KAAM,CAAC;IAC9C,CAAC,MAAM,IACN,SAAS,KAAKQ,MAAM,IACpBlB,KAAK,CAAEkB,MAAM,CAAE,IACflB,KAAK,CAAEkB,MAAM,CAAE,CAAEb,IAAI,CAAE,EACtB;MACD,MAAMc,SAAS,GAAGnB,KAAK,CAAEkB,MAAM,CAAE,CAAEb,IAAI,CAAE;;MAEzC;MACA,OAAOL,KAAK,CAAEkB,MAAM,CAAE,CAAEb,IAAI,CAAE;MAE9B,OAAOe,eAAe,CAAED,SAAS,EAAE,CAAC,CAAET,KAAM,CAAC;IAC9C;IAEA,OAAOD,IAAI,CAAED,OAAQ,CAAC;EACvB,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASY,eAAeA,CAAEC,YAAY,EAAEX,KAAK,EAAG;EAC/C,OAAOY,OAAO,CAACC,OAAO,CACrBb,KAAK,GACFW,YAAY,CAACG,IAAI,GACjB,IAAIC,MAAM,CAACC,QAAQ,CAAEC,IAAI,CAACC,SAAS,CAAEP,YAAY,CAACG,IAAK,CAAC,EAAE;IAC1DK,MAAM,EAAE,GAAG;IACXC,UAAU,EAAE,IAAI;IAChBC,OAAO,EAAEV,YAAY,CAACU;EACtB,CAAE,CACN,CAAC;AACF;AAAC,IAAAC,QAAA,GAEclC,0BAA0B;AAAAmC,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.default = void 0;
9
-
10
8
  var _namespaceEndpoint = _interopRequireDefault(require("./namespace-endpoint"));
11
-
12
9
  /**
13
10
  * Internal dependencies
14
11
  */
@@ -22,30 +19,26 @@ const createRootURLMiddleware = rootURL => (options, next) => {
22
19
  let url = optionsWithPath.url;
23
20
  let path = optionsWithPath.path;
24
21
  let apiRoot;
25
-
26
22
  if (typeof path === 'string') {
27
23
  apiRoot = rootURL;
28
-
29
24
  if (-1 !== rootURL.indexOf('?')) {
30
25
  path = path.replace('?', '&');
31
26
  }
27
+ path = path.replace(/^\//, '');
32
28
 
33
- path = path.replace(/^\//, ''); // API root may already include query parameter prefix if site is
29
+ // API root may already include query parameter prefix if site is
34
30
  // configured to use plain permalinks.
35
-
36
31
  if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) {
37
32
  path = path.replace('?', '&');
38
33
  }
39
-
40
34
  url = apiRoot + path;
41
35
  }
42
-
43
- return next({ ...optionsWithPath,
36
+ return next({
37
+ ...optionsWithPath,
44
38
  url
45
39
  });
46
40
  });
47
41
  };
48
-
49
42
  var _default = createRootURLMiddleware;
50
43
  exports.default = _default;
51
44
  //# sourceMappingURL=root-url.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/middlewares/root-url.js"],"names":["createRootURLMiddleware","rootURL","options","next","optionsWithPath","url","path","apiRoot","indexOf","replace"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,MAAMA,uBAAuB,GAAKC,OAAF,IAAe,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AACnE,SAAO,gCAAgCD,OAAhC,EAA2CE,eAAF,IAAuB;AACtE,QAAIC,GAAG,GAAGD,eAAe,CAACC,GAA1B;AACA,QAAIC,IAAI,GAAGF,eAAe,CAACE,IAA3B;AACA,QAAIC,OAAJ;;AAEA,QAAK,OAAOD,IAAP,KAAgB,QAArB,EAAgC;AAC/BC,MAAAA,OAAO,GAAGN,OAAV;;AAEA,UAAK,CAAC,CAAD,KAAOA,OAAO,CAACO,OAAR,CAAiB,GAAjB,CAAZ,EAAqC;AACpCF,QAAAA,IAAI,GAAGA,IAAI,CAACG,OAAL,CAAc,GAAd,EAAmB,GAAnB,CAAP;AACA;;AAEDH,MAAAA,IAAI,GAAGA,IAAI,CAACG,OAAL,CAAc,KAAd,EAAqB,EAArB,CAAP,CAP+B,CAS/B;AACA;;AACA,UACC,aAAa,OAAOF,OAApB,IACA,CAAC,CAAD,KAAOA,OAAO,CAACC,OAAR,CAAiB,GAAjB,CAFR,EAGE;AACDF,QAAAA,IAAI,GAAGA,IAAI,CAACG,OAAL,CAAc,GAAd,EAAmB,GAAnB,CAAP;AACA;;AAEDJ,MAAAA,GAAG,GAAGE,OAAO,GAAGD,IAAhB;AACA;;AAED,WAAOH,IAAI,CAAE,EACZ,GAAGC,eADS;AAEZC,MAAAA;AAFY,KAAF,CAAX;AAIA,GA9BM,CAAP;AA+BA,CAhCD;;eAkCeL,uB","sourcesContent":["/**\n * Internal dependencies\n */\nimport namespaceAndEndpointMiddleware from './namespace-endpoint';\n\n/**\n * @param {string} rootURL\n * @return {import('../types').APIFetchMiddleware} Root URL middleware.\n */\nconst createRootURLMiddleware = ( rootURL ) => ( options, next ) => {\n\treturn namespaceAndEndpointMiddleware( options, ( optionsWithPath ) => {\n\t\tlet url = optionsWithPath.url;\n\t\tlet path = optionsWithPath.path;\n\t\tlet apiRoot;\n\n\t\tif ( typeof path === 'string' ) {\n\t\t\tapiRoot = rootURL;\n\n\t\t\tif ( -1 !== rootURL.indexOf( '?' ) ) {\n\t\t\t\tpath = path.replace( '?', '&' );\n\t\t\t}\n\n\t\t\tpath = path.replace( /^\\//, '' );\n\n\t\t\t// API root may already include query parameter prefix if site is\n\t\t\t// configured to use plain permalinks.\n\t\t\tif (\n\t\t\t\t'string' === typeof apiRoot &&\n\t\t\t\t-1 !== apiRoot.indexOf( '?' )\n\t\t\t) {\n\t\t\t\tpath = path.replace( '?', '&' );\n\t\t\t}\n\n\t\t\turl = apiRoot + path;\n\t\t}\n\n\t\treturn next( {\n\t\t\t...optionsWithPath,\n\t\t\turl,\n\t\t} );\n\t} );\n};\n\nexport default createRootURLMiddleware;\n"]}
1
+ {"version":3,"names":["_namespaceEndpoint","_interopRequireDefault","require","createRootURLMiddleware","rootURL","options","next","namespaceAndEndpointMiddleware","optionsWithPath","url","path","apiRoot","indexOf","replace","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/root-url.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport namespaceAndEndpointMiddleware from './namespace-endpoint';\n\n/**\n * @param {string} rootURL\n * @return {import('../types').APIFetchMiddleware} Root URL middleware.\n */\nconst createRootURLMiddleware = ( rootURL ) => ( options, next ) => {\n\treturn namespaceAndEndpointMiddleware( options, ( optionsWithPath ) => {\n\t\tlet url = optionsWithPath.url;\n\t\tlet path = optionsWithPath.path;\n\t\tlet apiRoot;\n\n\t\tif ( typeof path === 'string' ) {\n\t\t\tapiRoot = rootURL;\n\n\t\t\tif ( -1 !== rootURL.indexOf( '?' ) ) {\n\t\t\t\tpath = path.replace( '?', '&' );\n\t\t\t}\n\n\t\t\tpath = path.replace( /^\\//, '' );\n\n\t\t\t// API root may already include query parameter prefix if site is\n\t\t\t// configured to use plain permalinks.\n\t\t\tif (\n\t\t\t\t'string' === typeof apiRoot &&\n\t\t\t\t-1 !== apiRoot.indexOf( '?' )\n\t\t\t) {\n\t\t\t\tpath = path.replace( '?', '&' );\n\t\t\t}\n\n\t\t\turl = apiRoot + path;\n\t\t}\n\n\t\treturn next( {\n\t\t\t...optionsWithPath,\n\t\t\turl,\n\t\t} );\n\t} );\n};\n\nexport default createRootURLMiddleware;\n"],"mappings":";;;;;;;AAGA,IAAAA,kBAAA,GAAAC,sBAAA,CAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,MAAMC,uBAAuB,GAAKC,OAAO,IAAM,CAAEC,OAAO,EAAEC,IAAI,KAAM;EACnE,OAAO,IAAAC,0BAA8B,EAAEF,OAAO,EAAIG,eAAe,IAAM;IACtE,IAAIC,GAAG,GAAGD,eAAe,CAACC,GAAG;IAC7B,IAAIC,IAAI,GAAGF,eAAe,CAACE,IAAI;IAC/B,IAAIC,OAAO;IAEX,IAAK,OAAOD,IAAI,KAAK,QAAQ,EAAG;MAC/BC,OAAO,GAAGP,OAAO;MAEjB,IAAK,CAAC,CAAC,KAAKA,OAAO,CAACQ,OAAO,CAAE,GAAI,CAAC,EAAG;QACpCF,IAAI,GAAGA,IAAI,CAACG,OAAO,CAAE,GAAG,EAAE,GAAI,CAAC;MAChC;MAEAH,IAAI,GAAGA,IAAI,CAACG,OAAO,CAAE,KAAK,EAAE,EAAG,CAAC;;MAEhC;MACA;MACA,IACC,QAAQ,KAAK,OAAOF,OAAO,IAC3B,CAAC,CAAC,KAAKA,OAAO,CAACC,OAAO,CAAE,GAAI,CAAC,EAC5B;QACDF,IAAI,GAAGA,IAAI,CAACG,OAAO,CAAE,GAAG,EAAE,GAAI,CAAC;MAChC;MAEAJ,GAAG,GAAGE,OAAO,GAAGD,IAAI;IACrB;IAEA,OAAOJ,IAAI,CAAE;MACZ,GAAGE,eAAe;MAClBC;IACD,CAAE,CAAC;EACJ,CAAE,CAAC;AACJ,CAAC;AAAC,IAAAK,QAAA,GAEaX,uBAAuB;AAAAY,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _url = require("@wordpress/url");
9
-
10
8
  /**
11
9
  * WordPress dependencies
12
10
  */
@@ -24,16 +22,13 @@ const createThemePreviewMiddleware = themePath => (options, next) => {
24
22
  wp_theme_preview: themePath
25
23
  });
26
24
  }
27
-
28
25
  if (typeof options.path === 'string' && !(0, _url.hasQueryArg)(options.path, 'wp_theme_preview')) {
29
26
  options.path = (0, _url.addQueryArgs)(options.path, {
30
27
  wp_theme_preview: themePath
31
28
  });
32
29
  }
33
-
34
30
  return next(options);
35
31
  };
36
-
37
32
  var _default = createThemePreviewMiddleware;
38
33
  exports.default = _default;
39
34
  //# sourceMappingURL=theme-preview.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/middlewares/theme-preview.js"],"names":["createThemePreviewMiddleware","themePath","options","next","url","wp_theme_preview","path"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,4BAA4B,GAAKC,SAAF,IAAiB,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AAC1E,MACC,OAAOD,OAAO,CAACE,GAAf,KAAuB,QAAvB,IACA,CAAE,sBAAaF,OAAO,CAACE,GAArB,EAA0B,kBAA1B,CAFH,EAGE;AACDF,IAAAA,OAAO,CAACE,GAAR,GAAc,uBAAcF,OAAO,CAACE,GAAtB,EAA2B;AACxCC,MAAAA,gBAAgB,EAAEJ;AADsB,KAA3B,CAAd;AAGA;;AAED,MACC,OAAOC,OAAO,CAACI,IAAf,KAAwB,QAAxB,IACA,CAAE,sBAAaJ,OAAO,CAACI,IAArB,EAA2B,kBAA3B,CAFH,EAGE;AACDJ,IAAAA,OAAO,CAACI,IAAR,GAAe,uBAAcJ,OAAO,CAACI,IAAtB,EAA4B;AAC1CD,MAAAA,gBAAgB,EAAEJ;AADwB,KAA5B,CAAf;AAGA;;AAED,SAAOE,IAAI,CAAED,OAAF,CAAX;AACA,CApBD;;eAsBeF,4B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * This appends a `wp_theme_preview` parameter to the REST API request URL if\n * the admin URL contains a `theme` GET parameter.\n *\n * @param {Record<string, any>} themePath\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nconst createThemePreviewMiddleware = ( themePath ) => ( options, next ) => {\n\tif (\n\t\ttypeof options.url === 'string' &&\n\t\t! hasQueryArg( options.url, 'wp_theme_preview' )\n\t) {\n\t\toptions.url = addQueryArgs( options.url, {\n\t\t\twp_theme_preview: themePath,\n\t\t} );\n\t}\n\n\tif (\n\t\ttypeof options.path === 'string' &&\n\t\t! hasQueryArg( options.path, 'wp_theme_preview' )\n\t) {\n\t\toptions.path = addQueryArgs( options.path, {\n\t\t\twp_theme_preview: themePath,\n\t\t} );\n\t}\n\n\treturn next( options );\n};\n\nexport default createThemePreviewMiddleware;\n"]}
1
+ {"version":3,"names":["_url","require","createThemePreviewMiddleware","themePath","options","next","url","hasQueryArg","addQueryArgs","wp_theme_preview","path","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/theme-preview.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * This appends a `wp_theme_preview` parameter to the REST API request URL if\n * the admin URL contains a `theme` GET parameter.\n *\n * @param {Record<string, any>} themePath\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nconst createThemePreviewMiddleware = ( themePath ) => ( options, next ) => {\n\tif (\n\t\ttypeof options.url === 'string' &&\n\t\t! hasQueryArg( options.url, 'wp_theme_preview' )\n\t) {\n\t\toptions.url = addQueryArgs( options.url, {\n\t\t\twp_theme_preview: themePath,\n\t\t} );\n\t}\n\n\tif (\n\t\ttypeof options.path === 'string' &&\n\t\t! hasQueryArg( options.path, 'wp_theme_preview' )\n\t) {\n\t\toptions.path = addQueryArgs( options.path, {\n\t\t\twp_theme_preview: themePath,\n\t\t} );\n\t}\n\n\treturn next( options );\n};\n\nexport default createThemePreviewMiddleware;\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,4BAA4B,GAAKC,SAAS,IAAM,CAAEC,OAAO,EAAEC,IAAI,KAAM;EAC1E,IACC,OAAOD,OAAO,CAACE,GAAG,KAAK,QAAQ,IAC/B,CAAE,IAAAC,gBAAW,EAAEH,OAAO,CAACE,GAAG,EAAE,kBAAmB,CAAC,EAC/C;IACDF,OAAO,CAACE,GAAG,GAAG,IAAAE,iBAAY,EAAEJ,OAAO,CAACE,GAAG,EAAE;MACxCG,gBAAgB,EAAEN;IACnB,CAAE,CAAC;EACJ;EAEA,IACC,OAAOC,OAAO,CAACM,IAAI,KAAK,QAAQ,IAChC,CAAE,IAAAH,gBAAW,EAAEH,OAAO,CAACM,IAAI,EAAE,kBAAmB,CAAC,EAChD;IACDN,OAAO,CAACM,IAAI,GAAG,IAAAF,iBAAY,EAAEJ,OAAO,CAACM,IAAI,EAAE;MAC1CD,gBAAgB,EAAEN;IACnB,CAAE,CAAC;EACJ;EAEA,OAAOE,IAAI,CAAED,OAAQ,CAAC;AACvB,CAAC;AAAC,IAAAO,QAAA,GAEaT,4BAA4B;AAAAU,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _url = require("@wordpress/url");
9
-
10
8
  /**
11
9
  * WordPress dependencies
12
10
  */
@@ -20,16 +18,13 @@ const userLocaleMiddleware = (options, next) => {
20
18
  _locale: 'user'
21
19
  });
22
20
  }
23
-
24
21
  if (typeof options.path === 'string' && !(0, _url.hasQueryArg)(options.path, '_locale')) {
25
22
  options.path = (0, _url.addQueryArgs)(options.path, {
26
23
  _locale: 'user'
27
24
  });
28
25
  }
29
-
30
26
  return next(options);
31
27
  };
32
-
33
28
  var _default = userLocaleMiddleware;
34
29
  exports.default = _default;
35
30
  //# sourceMappingURL=user-locale.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/middlewares/user-locale.js"],"names":["userLocaleMiddleware","options","next","url","_locale","path"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA,MAAMA,oBAAoB,GAAG,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AACjD,MACC,OAAOD,OAAO,CAACE,GAAf,KAAuB,QAAvB,IACA,CAAE,sBAAaF,OAAO,CAACE,GAArB,EAA0B,SAA1B,CAFH,EAGE;AACDF,IAAAA,OAAO,CAACE,GAAR,GAAc,uBAAcF,OAAO,CAACE,GAAtB,EAA2B;AAAEC,MAAAA,OAAO,EAAE;AAAX,KAA3B,CAAd;AACA;;AAED,MACC,OAAOH,OAAO,CAACI,IAAf,KAAwB,QAAxB,IACA,CAAE,sBAAaJ,OAAO,CAACI,IAArB,EAA2B,SAA3B,CAFH,EAGE;AACDJ,IAAAA,OAAO,CAACI,IAAR,GAAe,uBAAcJ,OAAO,CAACI,IAAtB,EAA4B;AAAED,MAAAA,OAAO,EAAE;AAAX,KAA5B,CAAf;AACA;;AAED,SAAOF,IAAI,CAAED,OAAF,CAAX;AACA,CAhBD;;eAkBeD,oB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * @type {import('../types').APIFetchMiddleware}\n */\nconst userLocaleMiddleware = ( options, next ) => {\n\tif (\n\t\ttypeof options.url === 'string' &&\n\t\t! hasQueryArg( options.url, '_locale' )\n\t) {\n\t\toptions.url = addQueryArgs( options.url, { _locale: 'user' } );\n\t}\n\n\tif (\n\t\ttypeof options.path === 'string' &&\n\t\t! hasQueryArg( options.path, '_locale' )\n\t) {\n\t\toptions.path = addQueryArgs( options.path, { _locale: 'user' } );\n\t}\n\n\treturn next( options );\n};\n\nexport default userLocaleMiddleware;\n"]}
1
+ {"version":3,"names":["_url","require","userLocaleMiddleware","options","next","url","hasQueryArg","addQueryArgs","_locale","path","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/user-locale.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * @type {import('../types').APIFetchMiddleware}\n */\nconst userLocaleMiddleware = ( options, next ) => {\n\tif (\n\t\ttypeof options.url === 'string' &&\n\t\t! hasQueryArg( options.url, '_locale' )\n\t) {\n\t\toptions.url = addQueryArgs( options.url, { _locale: 'user' } );\n\t}\n\n\tif (\n\t\ttypeof options.path === 'string' &&\n\t\t! hasQueryArg( options.path, '_locale' )\n\t) {\n\t\toptions.path = addQueryArgs( options.path, { _locale: 'user' } );\n\t}\n\n\treturn next( options );\n};\n\nexport default userLocaleMiddleware;\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA,MAAMC,oBAAoB,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;EACjD,IACC,OAAOD,OAAO,CAACE,GAAG,KAAK,QAAQ,IAC/B,CAAE,IAAAC,gBAAW,EAAEH,OAAO,CAACE,GAAG,EAAE,SAAU,CAAC,EACtC;IACDF,OAAO,CAACE,GAAG,GAAG,IAAAE,iBAAY,EAAEJ,OAAO,CAACE,GAAG,EAAE;MAAEG,OAAO,EAAE;IAAO,CAAE,CAAC;EAC/D;EAEA,IACC,OAAOL,OAAO,CAACM,IAAI,KAAK,QAAQ,IAChC,CAAE,IAAAH,gBAAW,EAAEH,OAAO,CAACM,IAAI,EAAE,SAAU,CAAC,EACvC;IACDN,OAAO,CAACM,IAAI,GAAG,IAAAF,iBAAY,EAAEJ,OAAO,CAACM,IAAI,EAAE;MAAED,OAAO,EAAE;IAAO,CAAE,CAAC;EACjE;EAEA,OAAOJ,IAAI,CAAED,OAAQ,CAAC;AACvB,CAAC;AAAC,IAAAO,QAAA,GAEaR,oBAAoB;AAAAS,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
1
+ {"version":3,"names":[],"sources":["@wordpress/api-fetch/src/types.ts"],"sourcesContent":["export interface APIFetchOptions extends RequestInit {\n\t// Override headers, we only accept it as an object due to the `nonce` middleware\n\theaders?: Record< string, string >;\n\tpath?: string;\n\turl?: string;\n\t/**\n\t * @default true\n\t */\n\tparse?: boolean;\n\tdata?: any;\n\tnamespace?: string;\n\tendpoint?: string;\n}\n\nexport type APIFetchMiddleware = (\n\toptions: APIFetchOptions,\n\tnext: ( nextOptions: APIFetchOptions ) => Promise< any >\n) => Promise< any >;\n"],"mappings":""}
@@ -5,9 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.parseAndThrowError = parseAndThrowError;
7
7
  exports.parseResponseAndNormalizeError = void 0;
8
-
9
8
  var _i18n = require("@wordpress/i18n");
10
-
11
9
  /**
12
10
  * WordPress dependencies
13
11
  */
@@ -25,12 +23,11 @@ const parseResponse = (response, shouldParseResponse = true) => {
25
23
  if (response.status === 204) {
26
24
  return null;
27
25
  }
28
-
29
26
  return response.json ? response.json() : Promise.reject(response);
30
27
  }
31
-
32
28
  return response;
33
29
  };
30
+
34
31
  /**
35
32
  * Calls the `json` function on the Response, throwing an error if the response
36
33
  * doesn't have a json function or if parsing the json itself fails.
@@ -38,22 +35,19 @@ const parseResponse = (response, shouldParseResponse = true) => {
38
35
  * @param {Response} response
39
36
  * @return {Promise<any>} Parsed response.
40
37
  */
41
-
42
-
43
38
  const parseJsonAndNormalizeError = response => {
44
39
  const invalidJsonError = {
45
40
  code: 'invalid_json',
46
41
  message: (0, _i18n.__)('The response is not a valid JSON response.')
47
42
  };
48
-
49
43
  if (!response || !response.json) {
50
44
  throw invalidJsonError;
51
45
  }
52
-
53
46
  return response.json().catch(() => {
54
47
  throw invalidJsonError;
55
48
  });
56
49
  };
50
+
57
51
  /**
58
52
  * Parses the apiFetch response properly and normalize response errors.
59
53
  *
@@ -62,11 +56,10 @@ const parseJsonAndNormalizeError = response => {
62
56
  *
63
57
  * @return {Promise<any>} Parsed response.
64
58
  */
65
-
66
-
67
59
  const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => {
68
60
  return Promise.resolve(parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
69
61
  };
62
+
70
63
  /**
71
64
  * Parses a response, throwing an error if parsing the response fails.
72
65
  *
@@ -74,15 +67,11 @@ const parseResponseAndNormalizeError = (response, shouldParseResponse = true) =>
74
67
  * @param {boolean} shouldParseResponse
75
68
  * @return {Promise<any>} Parsed response.
76
69
  */
77
-
78
-
79
70
  exports.parseResponseAndNormalizeError = parseResponseAndNormalizeError;
80
-
81
71
  function parseAndThrowError(response, shouldParseResponse = true) {
82
72
  if (!shouldParseResponse) {
83
73
  throw response;
84
74
  }
85
-
86
75
  return parseJsonAndNormalizeError(response).then(error => {
87
76
  const unknownError = {
88
77
  code: 'unknown_error',
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/api-fetch/src/utils/response.js"],"names":["parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","catch","parseResponseAndNormalizeError","resolve","res","parseAndThrowError","then","error","unknownError"],"mappings":";;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,aAAa,GAAG,CAAEC,QAAF,EAAYC,mBAAmB,GAAG,IAAlC,KAA4C;AACjE,MAAKA,mBAAL,EAA2B;AAC1B,QAAKD,QAAQ,CAACE,MAAT,KAAoB,GAAzB,EAA+B;AAC9B,aAAO,IAAP;AACA;;AAED,WAAOF,QAAQ,CAACG,IAAT,GAAgBH,QAAQ,CAACG,IAAT,EAAhB,GAAkCC,OAAO,CAACC,MAAR,CAAgBL,QAAhB,CAAzC;AACA;;AAED,SAAOA,QAAP;AACA,CAVD;AAYA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,MAAMM,0BAA0B,GAAKN,QAAF,IAAgB;AAClD,QAAMO,gBAAgB,GAAG;AACxBC,IAAAA,IAAI,EAAE,cADkB;AAExBC,IAAAA,OAAO,EAAE,cAAI,4CAAJ;AAFe,GAAzB;;AAKA,MAAK,CAAET,QAAF,IAAc,CAAEA,QAAQ,CAACG,IAA9B,EAAqC;AACpC,UAAMI,gBAAN;AACA;;AAED,SAAOP,QAAQ,CAACG,IAAT,GAAgBO,KAAhB,CAAuB,MAAM;AACnC,UAAMH,gBAAN;AACA,GAFM,CAAP;AAGA,CAbD;AAeA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,MAAMI,8BAA8B,GAAG,CAC7CX,QAD6C,EAE7CC,mBAAmB,GAAG,IAFuB,KAGzC;AACJ,SAAOG,OAAO,CAACQ,OAAR,CACNb,aAAa,CAAEC,QAAF,EAAYC,mBAAZ,CADP,EAELS,KAFK,CAEIG,GAAF,IAAWC,kBAAkB,CAAED,GAAF,EAAOZ,mBAAP,CAF/B,CAAP;AAGA,CAPM;AASP;AACA;AACA;AACA;AACA;AACA;AACA;;;;;AACO,SAASa,kBAAT,CAA6Bd,QAA7B,EAAuCC,mBAAmB,GAAG,IAA7D,EAAoE;AAC1E,MAAK,CAAEA,mBAAP,EAA6B;AAC5B,UAAMD,QAAN;AACA;;AAED,SAAOM,0BAA0B,CAAEN,QAAF,CAA1B,CAAuCe,IAAvC,CAA+CC,KAAF,IAAa;AAChE,UAAMC,YAAY,GAAG;AACpBT,MAAAA,IAAI,EAAE,eADc;AAEpBC,MAAAA,OAAO,EAAE,cAAI,4BAAJ;AAFW,KAArB;AAKA,UAAMO,KAAK,IAAIC,YAAf;AACA,GAPM,CAAP;AAQA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Parses the apiFetch response.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any> | null | Response} Parsed response.\n */\nconst parseResponse = ( response, shouldParseResponse = true ) => {\n\tif ( shouldParseResponse ) {\n\t\tif ( response.status === 204 ) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn response.json ? response.json() : Promise.reject( response );\n\t}\n\n\treturn response;\n};\n\n/**\n * Calls the `json` function on the Response, throwing an error if the response\n * doesn't have a json function or if parsing the json itself fails.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response.\n */\nconst parseJsonAndNormalizeError = ( response ) => {\n\tconst invalidJsonError = {\n\t\tcode: 'invalid_json',\n\t\tmessage: __( 'The response is not a valid JSON response.' ),\n\t};\n\n\tif ( ! response || ! response.json ) {\n\t\tthrow invalidJsonError;\n\t}\n\n\treturn response.json().catch( () => {\n\t\tthrow invalidJsonError;\n\t} );\n};\n\n/**\n * Parses the apiFetch response properly and normalize response errors.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any>} Parsed response.\n */\nexport const parseResponseAndNormalizeError = (\n\tresponse,\n\tshouldParseResponse = true\n) => {\n\treturn Promise.resolve(\n\t\tparseResponse( response, shouldParseResponse )\n\t).catch( ( res ) => parseAndThrowError( res, shouldParseResponse ) );\n};\n\n/**\n * Parses a response, throwing an error if parsing the response fails.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n * @return {Promise<any>} Parsed response.\n */\nexport function parseAndThrowError( response, shouldParseResponse = true ) {\n\tif ( ! shouldParseResponse ) {\n\t\tthrow response;\n\t}\n\n\treturn parseJsonAndNormalizeError( response ).then( ( error ) => {\n\t\tconst unknownError = {\n\t\t\tcode: 'unknown_error',\n\t\t\tmessage: __( 'An unknown error occurred.' ),\n\t\t};\n\n\t\tthrow error || unknownError;\n\t} );\n}\n"]}
1
+ {"version":3,"names":["_i18n","require","parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","__","catch","parseResponseAndNormalizeError","resolve","res","parseAndThrowError","exports","then","error","unknownError"],"sources":["@wordpress/api-fetch/src/utils/response.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Parses the apiFetch response.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any> | null | Response} Parsed response.\n */\nconst parseResponse = ( response, shouldParseResponse = true ) => {\n\tif ( shouldParseResponse ) {\n\t\tif ( response.status === 204 ) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn response.json ? response.json() : Promise.reject( response );\n\t}\n\n\treturn response;\n};\n\n/**\n * Calls the `json` function on the Response, throwing an error if the response\n * doesn't have a json function or if parsing the json itself fails.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response.\n */\nconst parseJsonAndNormalizeError = ( response ) => {\n\tconst invalidJsonError = {\n\t\tcode: 'invalid_json',\n\t\tmessage: __( 'The response is not a valid JSON response.' ),\n\t};\n\n\tif ( ! response || ! response.json ) {\n\t\tthrow invalidJsonError;\n\t}\n\n\treturn response.json().catch( () => {\n\t\tthrow invalidJsonError;\n\t} );\n};\n\n/**\n * Parses the apiFetch response properly and normalize response errors.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any>} Parsed response.\n */\nexport const parseResponseAndNormalizeError = (\n\tresponse,\n\tshouldParseResponse = true\n) => {\n\treturn Promise.resolve(\n\t\tparseResponse( response, shouldParseResponse )\n\t).catch( ( res ) => parseAndThrowError( res, shouldParseResponse ) );\n};\n\n/**\n * Parses a response, throwing an error if parsing the response fails.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n * @return {Promise<any>} Parsed response.\n */\nexport function parseAndThrowError( response, shouldParseResponse = true ) {\n\tif ( ! shouldParseResponse ) {\n\t\tthrow response;\n\t}\n\n\treturn parseJsonAndNormalizeError( response ).then( ( error ) => {\n\t\tconst unknownError = {\n\t\t\tcode: 'unknown_error',\n\t\t\tmessage: __( 'An unknown error occurred.' ),\n\t\t};\n\n\t\tthrow error || unknownError;\n\t} );\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,aAAa,GAAGA,CAAEC,QAAQ,EAAEC,mBAAmB,GAAG,IAAI,KAAM;EACjE,IAAKA,mBAAmB,EAAG;IAC1B,IAAKD,QAAQ,CAACE,MAAM,KAAK,GAAG,EAAG;MAC9B,OAAO,IAAI;IACZ;IAEA,OAAOF,QAAQ,CAACG,IAAI,GAAGH,QAAQ,CAACG,IAAI,CAAC,CAAC,GAAGC,OAAO,CAACC,MAAM,CAAEL,QAAS,CAAC;EACpE;EAEA,OAAOA,QAAQ;AAChB,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMM,0BAA0B,GAAKN,QAAQ,IAAM;EAClD,MAAMO,gBAAgB,GAAG;IACxBC,IAAI,EAAE,cAAc;IACpBC,OAAO,EAAE,IAAAC,QAAE,EAAE,4CAA6C;EAC3D,CAAC;EAED,IAAK,CAAEV,QAAQ,IAAI,CAAEA,QAAQ,CAACG,IAAI,EAAG;IACpC,MAAMI,gBAAgB;EACvB;EAEA,OAAOP,QAAQ,CAACG,IAAI,CAAC,CAAC,CAACQ,KAAK,CAAE,MAAM;IACnC,MAAMJ,gBAAgB;EACvB,CAAE,CAAC;AACJ,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMK,8BAA8B,GAAGA,CAC7CZ,QAAQ,EACRC,mBAAmB,GAAG,IAAI,KACtB;EACJ,OAAOG,OAAO,CAACS,OAAO,CACrBd,aAAa,CAAEC,QAAQ,EAAEC,mBAAoB,CAC9C,CAAC,CAACU,KAAK,CAAIG,GAAG,IAAMC,kBAAkB,CAAED,GAAG,EAAEb,mBAAoB,CAAE,CAAC;AACrE,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AANAe,OAAA,CAAAJ,8BAAA,GAAAA,8BAAA;AAOO,SAASG,kBAAkBA,CAAEf,QAAQ,EAAEC,mBAAmB,GAAG,IAAI,EAAG;EAC1E,IAAK,CAAEA,mBAAmB,EAAG;IAC5B,MAAMD,QAAQ;EACf;EAEA,OAAOM,0BAA0B,CAAEN,QAAS,CAAC,CAACiB,IAAI,CAAIC,KAAK,IAAM;IAChE,MAAMC,YAAY,GAAG;MACpBX,IAAI,EAAE,eAAe;MACrBC,OAAO,EAAE,IAAAC,QAAE,EAAE,4BAA6B;IAC3C,CAAC;IAED,MAAMQ,KAAK,IAAIC,YAAY;EAC5B,CAAE,CAAC;AACJ"}
@@ -2,10 +2,10 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { __ } from '@wordpress/i18n';
5
+
5
6
  /**
6
7
  * Internal dependencies
7
8
  */
8
-
9
9
  import createNonceMiddleware from './middlewares/nonce';
10
10
  import createRootURLMiddleware from './middlewares/root-url';
11
11
  import createPreloadingMiddleware from './middlewares/preloading';
@@ -16,13 +16,13 @@ import userLocaleMiddleware from './middlewares/user-locale';
16
16
  import mediaUploadMiddleware from './middlewares/media-upload';
17
17
  import createThemePreviewMiddleware from './middlewares/theme-preview';
18
18
  import { parseResponseAndNormalizeError, parseAndThrowError } from './utils/response';
19
+
19
20
  /**
20
21
  * Default set of header values which should be sent with every request unless
21
22
  * explicitly provided through apiFetch options.
22
23
  *
23
24
  * @type {Record<string, string>}
24
25
  */
25
-
26
26
  const DEFAULT_HEADERS = {
27
27
  // The backend uses the Accept header as a condition for considering an
28
28
  // incoming request as a REST request.
@@ -30,34 +30,34 @@ const DEFAULT_HEADERS = {
30
30
  // See: https://core.trac.wordpress.org/ticket/44534
31
31
  Accept: 'application/json, */*;q=0.1'
32
32
  };
33
+
33
34
  /**
34
35
  * Default set of fetch option values which should be sent with every request
35
36
  * unless explicitly provided through apiFetch options.
36
37
  *
37
38
  * @type {Object}
38
39
  */
39
-
40
40
  const DEFAULT_OPTIONS = {
41
41
  credentials: 'include'
42
42
  };
43
- /** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
44
43
 
44
+ /** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
45
45
  /** @typedef {import('./types').APIFetchOptions} APIFetchOptions */
46
46
 
47
47
  /**
48
48
  * @type {import('./types').APIFetchMiddleware[]}
49
49
  */
50
-
51
50
  const middlewares = [userLocaleMiddleware, namespaceEndpointMiddleware, httpV1Middleware, fetchAllMiddleware];
51
+
52
52
  /**
53
53
  * Register a middleware
54
54
  *
55
55
  * @param {import('./types').APIFetchMiddleware} middleware
56
56
  */
57
-
58
57
  function registerMiddleware(middleware) {
59
58
  middlewares.unshift(middleware);
60
59
  }
60
+
61
61
  /**
62
62
  * Checks the status of a response, throwing the Response as an error if
63
63
  * it is outside the 200 range.
@@ -65,22 +65,18 @@ function registerMiddleware(middleware) {
65
65
  * @param {Response} response
66
66
  * @return {Response} The response if the status is in the 200 range.
67
67
  */
68
-
69
-
70
68
  const checkStatus = response => {
71
69
  if (response.status >= 200 && response.status < 300) {
72
70
  return response;
73
71
  }
74
-
75
72
  throw response;
76
73
  };
74
+
77
75
  /** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/
78
76
 
79
77
  /**
80
78
  * @type {FetchHandler}
81
79
  */
82
-
83
-
84
80
  const defaultFetchHandler = nextOptions => {
85
81
  const {
86
82
  url,
@@ -92,19 +88,23 @@ const defaultFetchHandler = nextOptions => {
92
88
  let {
93
89
  body,
94
90
  headers
95
- } = nextOptions; // Merge explicitly-provided headers with default values.
91
+ } = nextOptions;
96
92
 
97
- headers = { ...DEFAULT_HEADERS,
93
+ // Merge explicitly-provided headers with default values.
94
+ headers = {
95
+ ...DEFAULT_HEADERS,
98
96
  ...headers
99
- }; // The `data` property is a shorthand for sending a JSON body.
97
+ };
100
98
 
99
+ // The `data` property is a shorthand for sending a JSON body.
101
100
  if (data) {
102
101
  body = JSON.stringify(data);
103
102
  headers['Content-Type'] = 'application/json';
104
103
  }
105
-
106
- const responsePromise = window.fetch( // Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
107
- url || path || window.location.href, { ...DEFAULT_OPTIONS,
104
+ const responsePromise = window.fetch(
105
+ // Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
106
+ url || path || window.location.href, {
107
+ ...DEFAULT_OPTIONS,
108
108
  ...remainingOptions,
109
109
  body,
110
110
  headers
@@ -113,55 +113,52 @@ const defaultFetchHandler = nextOptions => {
113
113
  // Re-throw AbortError for the users to handle it themselves.
114
114
  if (err && err.name === 'AbortError') {
115
115
  throw err;
116
- } // Otherwise, there is most likely no network connection.
117
- // Unfortunately the message might depend on the browser.
118
-
116
+ }
119
117
 
118
+ // Otherwise, there is most likely no network connection.
119
+ // Unfortunately the message might depend on the browser.
120
120
  throw {
121
121
  code: 'fetch_error',
122
122
  message: __('You are probably offline.')
123
123
  };
124
124
  });
125
125
  };
126
- /** @type {FetchHandler} */
127
-
128
126
 
127
+ /** @type {FetchHandler} */
129
128
  let fetchHandler = defaultFetchHandler;
129
+
130
130
  /**
131
131
  * Defines a custom fetch handler for making the requests that will override
132
132
  * the default one using window.fetch
133
133
  *
134
134
  * @param {FetchHandler} newFetchHandler The new fetch handler
135
135
  */
136
-
137
136
  function setFetchHandler(newFetchHandler) {
138
137
  fetchHandler = newFetchHandler;
139
138
  }
139
+
140
140
  /**
141
141
  * @template T
142
142
  * @param {import('./types').APIFetchOptions} options
143
143
  * @return {Promise<T>} A promise representing the request processed via the registered middlewares.
144
144
  */
145
-
146
-
147
145
  function apiFetch(options) {
148
146
  // creates a nested function chain that calls all middlewares and finally the `fetchHandler`,
149
147
  // converting `middlewares = [ m1, m2, m3 ]` into:
150
148
  // ```
151
149
  // opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );
152
150
  // ```
153
- const enhancedHandler = middlewares.reduceRight((
154
- /** @type {FetchHandler} */
155
- next, middleware) => {
151
+ const enhancedHandler = middlewares.reduceRight(( /** @type {FetchHandler} */next, middleware) => {
156
152
  return workingOptions => middleware(workingOptions, next);
157
153
  }, fetchHandler);
158
154
  return enhancedHandler(options).catch(error => {
159
155
  if (error.code !== 'rest_cookie_invalid_nonce') {
160
156
  return Promise.reject(error);
161
- } // If the nonce is invalid, refresh it and try again.
157
+ }
162
158
 
163
-
164
- return window // @ts-ignore
159
+ // If the nonce is invalid, refresh it and try again.
160
+ return window
161
+ // @ts-ignore
165
162
  .fetch(apiFetch.nonceEndpoint).then(checkStatus).then(data => data.text()).then(text => {
166
163
  // @ts-ignore
167
164
  apiFetch.nonceMiddleware.nonce = text;
@@ -169,7 +166,6 @@ function apiFetch(options) {
169
166
  });
170
167
  });
171
168
  }
172
-
173
169
  apiFetch.use = registerMiddleware;
174
170
  apiFetch.setFetchHandler = setFetchHandler;
175
171
  apiFetch.createNonceMiddleware = createNonceMiddleware;