@wordpress/api-fetch 7.29.1-next.f34ab90e9.0 → 7.30.1-next.6870dfe5b.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 (97) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/index.js +50 -51
  3. package/build/index.js.map +1 -1
  4. package/build/middlewares/fetch-all-middleware.js +14 -14
  5. package/build/middlewares/fetch-all-middleware.js.map +1 -1
  6. package/build/middlewares/http-v1.js +6 -5
  7. package/build/middlewares/http-v1.js.map +1 -1
  8. package/build/middlewares/media-upload.js +7 -7
  9. package/build/middlewares/media-upload.js.map +1 -1
  10. package/build/middlewares/namespace-endpoint.js +2 -1
  11. package/build/middlewares/namespace-endpoint.js.map +1 -1
  12. package/build/middlewares/nonce.js +7 -5
  13. package/build/middlewares/nonce.js.map +1 -1
  14. package/build/middlewares/preloading.js +10 -7
  15. package/build/middlewares/preloading.js.map +1 -1
  16. package/build/middlewares/root-url.js +2 -2
  17. package/build/middlewares/root-url.js.map +1 -1
  18. package/build/middlewares/theme-preview.js +6 -2
  19. package/build/middlewares/theme-preview.js.map +1 -1
  20. package/build/middlewares/user-locale.js +2 -1
  21. package/build/middlewares/user-locale.js.map +1 -1
  22. package/build/types.js.map +1 -1
  23. package/build/utils/response.js +31 -50
  24. package/build/utils/response.js.map +1 -1
  25. package/build-module/index.js +38 -52
  26. package/build-module/index.js.map +1 -1
  27. package/build-module/middlewares/fetch-all-middleware.js +14 -15
  28. package/build-module/middlewares/fetch-all-middleware.js.map +1 -1
  29. package/build-module/middlewares/http-v1.js +6 -5
  30. package/build-module/middlewares/http-v1.js.map +1 -1
  31. package/build-module/middlewares/media-upload.js +7 -8
  32. package/build-module/middlewares/media-upload.js.map +1 -1
  33. package/build-module/middlewares/namespace-endpoint.js +2 -1
  34. package/build-module/middlewares/namespace-endpoint.js.map +1 -1
  35. package/build-module/middlewares/nonce.js +7 -5
  36. package/build-module/middlewares/nonce.js.map +1 -1
  37. package/build-module/middlewares/preloading.js +10 -7
  38. package/build-module/middlewares/preloading.js.map +1 -1
  39. package/build-module/middlewares/root-url.js +3 -2
  40. package/build-module/middlewares/root-url.js.map +1 -1
  41. package/build-module/middlewares/theme-preview.js +5 -2
  42. package/build-module/middlewares/theme-preview.js.map +1 -1
  43. package/build-module/middlewares/user-locale.js +2 -1
  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 +30 -48
  47. package/build-module/utils/response.js.map +1 -1
  48. package/build-types/index.d.ts +25 -32
  49. package/build-types/index.d.ts.map +1 -1
  50. package/build-types/middlewares/fetch-all-middleware.d.ts +5 -4
  51. package/build-types/middlewares/fetch-all-middleware.d.ts.map +1 -1
  52. package/build-types/middlewares/http-v1.d.ts +8 -3
  53. package/build-types/middlewares/http-v1.d.ts.map +1 -1
  54. package/build-types/middlewares/media-upload.d.ts +5 -4
  55. package/build-types/middlewares/media-upload.d.ts.map +1 -1
  56. package/build-types/middlewares/namespace-endpoint.d.ts +4 -3
  57. package/build-types/middlewares/namespace-endpoint.d.ts.map +1 -1
  58. package/build-types/middlewares/nonce.d.ts +9 -4
  59. package/build-types/middlewares/nonce.d.ts.map +1 -1
  60. package/build-types/middlewares/preloading.d.ts +8 -4
  61. package/build-types/middlewares/preloading.d.ts.map +1 -1
  62. package/build-types/middlewares/root-url.d.ts +8 -4
  63. package/build-types/middlewares/root-url.d.ts.map +1 -1
  64. package/build-types/middlewares/theme-preview.d.ts +8 -4
  65. package/build-types/middlewares/theme-preview.d.ts.map +1 -1
  66. package/build-types/middlewares/user-locale.d.ts +4 -3
  67. package/build-types/middlewares/user-locale.d.ts.map +1 -1
  68. package/build-types/types.d.ts +4 -3
  69. package/build-types/types.d.ts.map +1 -1
  70. package/build-types/utils/response.d.ts +13 -5
  71. package/build-types/utils/response.d.ts.map +1 -1
  72. package/package.json +4 -4
  73. package/src/{index.js → index.ts} +78 -69
  74. package/src/middlewares/{fetch-all-middleware.js → fetch-all-middleware.ts} +24 -20
  75. package/src/middlewares/{http-v1.js → http-v1.ts} +8 -6
  76. package/src/middlewares/{media-upload.js → media-upload.ts} +13 -12
  77. package/src/middlewares/{namespace-endpoint.js → namespace-endpoint.ts} +7 -2
  78. package/src/middlewares/{nonce.js → nonce.ts} +15 -7
  79. package/src/middlewares/{preloading.js → preloading.ts} +21 -11
  80. package/src/middlewares/root-url.ts +47 -0
  81. package/src/middlewares/test/{fetch-all-middleware.js → fetch-all-middleware.ts} +1 -1
  82. package/src/middlewares/test/{http-v1.js → http-v1.ts} +7 -4
  83. package/src/middlewares/test/{media-upload.js → media-upload.ts} +2 -1
  84. package/src/middlewares/test/{namespace-endpoint.js → namespace-endpoint.ts} +2 -1
  85. package/src/middlewares/test/{nonce.js → nonce.ts} +5 -4
  86. package/src/middlewares/test/{preloading.js → preloading.ts} +31 -11
  87. package/src/middlewares/test/{root-url.js → root-url.ts} +2 -1
  88. package/src/middlewares/test/{user-locale.js → user-locale.ts} +9 -8
  89. package/src/middlewares/theme-preview.ts +60 -0
  90. package/src/middlewares/{user-locale.js → user-locale.ts} +4 -2
  91. package/src/test/index.js +218 -147
  92. package/src/types.ts +10 -5
  93. package/src/utils/response.ts +64 -0
  94. package/tsconfig.tsbuildinfo +1 -1
  95. package/src/middlewares/root-url.js +0 -44
  96. package/src/middlewares/theme-preview.js +0 -42
  97. package/src/utils/response.js +0 -85
@@ -1,8 +1,9 @@
1
- export default mediaUploadMiddleware;
1
+ import type { APIFetchMiddleware } from '../types';
2
2
  /**
3
3
  * Middleware handling media upload failures and retries.
4
- *
5
- * @type {import('../types').APIFetchMiddleware}
4
+ * @param options
5
+ * @param next
6
6
  */
7
- declare const mediaUploadMiddleware: import("../types").APIFetchMiddleware;
7
+ declare const mediaUploadMiddleware: APIFetchMiddleware;
8
+ export default mediaUploadMiddleware;
8
9
  //# sourceMappingURL=media-upload.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"media-upload.d.ts","sourceRoot":"","sources":["../../src/middlewares/media-upload.js"],"names":[],"mappings":";AA0BA;;;;GAIG;AACH,qCAFU,OAAO,UAAU,EAAE,kBAAkB,CAmE7C"}
1
+ {"version":3,"file":"media-upload.d.ts","sourceRoot":"","sources":["../../src/middlewares/media-upload.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAmB,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAepE;;;;GAIG;AACH,QAAA,MAAM,qBAAqB,EAAE,kBAiE5B,CAAC;AAEF,eAAe,qBAAqB,CAAC"}
@@ -1,6 +1,7 @@
1
- export default namespaceAndEndpointMiddleware;
2
1
  /**
3
- * @type {import('../types').APIFetchMiddleware}
2
+ * Internal dependencies
4
3
  */
5
- declare const namespaceAndEndpointMiddleware: import("../types").APIFetchMiddleware;
4
+ import type { APIFetchMiddleware } from '../types';
5
+ declare const namespaceAndEndpointMiddleware: APIFetchMiddleware;
6
+ export default namespaceAndEndpointMiddleware;
6
7
  //# sourceMappingURL=namespace-endpoint.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"namespace-endpoint.d.ts","sourceRoot":"","sources":["../../src/middlewares/namespace-endpoint.js"],"names":[],"mappings":";AAAA;;GAEG;AACH,8CAFU,OAAO,UAAU,EAAE,kBAAkB,CA0B7C"}
1
+ {"version":3,"file":"namespace-endpoint.d.ts","sourceRoot":"","sources":["../../src/middlewares/namespace-endpoint.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD,QAAA,MAAM,8BAA8B,EAAE,kBA2BrC,CAAC;AAEF,eAAe,8BAA8B,CAAC"}
@@ -1,9 +1,14 @@
1
- export default createNonceMiddleware;
2
1
  /**
3
- * @param {string} nonce
4
- * @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
2
+ * Internal dependencies
3
+ */
4
+ import type { APIFetchMiddleware } from '../types';
5
+ /**
6
+ * @param nonce
7
+ *
8
+ * @return A middleware to enhance a request with a nonce.
5
9
  */
6
- declare function createNonceMiddleware(nonce: string): import("../types").APIFetchMiddleware & {
10
+ declare function createNonceMiddleware(nonce: string): APIFetchMiddleware & {
7
11
  nonce: string;
8
12
  };
13
+ export default createNonceMiddleware;
9
14
  //# sourceMappingURL=nonce.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"nonce.d.ts","sourceRoot":"","sources":["../../src/middlewares/nonce.js"],"names":[],"mappings":";AAAA;;;GAGG;AACH,8CAHW,MAAM,GACL,OAAO,UAAU,EAAE,kBAAkB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAgCpE"}
1
+ {"version":3,"file":"nonce.d.ts","sourceRoot":"","sources":["../../src/middlewares/nonce.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;GAIG;AACH,iBAAS,qBAAqB,CAC7B,KAAK,EAAE,MAAM,GACX,kBAAkB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CA8BxC;AAED,eAAe,qBAAqB,CAAC"}
@@ -1,7 +1,11 @@
1
- export default createPreloadingMiddleware;
2
1
  /**
3
- * @param {Record<string, any>} preloadedData
4
- * @return {import('../types').APIFetchMiddleware} Preloading middleware.
2
+ * Internal dependencies
3
+ */
4
+ import type { APIFetchMiddleware } from '../types';
5
+ /**
6
+ * @param preloadedData
7
+ * @return Preloading middleware.
5
8
  */
6
- declare function createPreloadingMiddleware(preloadedData: Record<string, any>): import("../types").APIFetchMiddleware;
9
+ declare function createPreloadingMiddleware(preloadedData: Record<string, any>): APIFetchMiddleware;
10
+ export default createPreloadingMiddleware;
7
11
  //# sourceMappingURL=preloading.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"preloading.d.ts","sourceRoot":"","sources":["../../src/middlewares/preloading.js"],"names":[],"mappings":";AAKA;;;GAGG;AACH,2DAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAClB,OAAO,UAAU,EAAE,kBAAkB,CAqDhD"}
1
+ {"version":3,"file":"preloading.d.ts","sourceRoot":"","sources":["../../src/middlewares/preloading.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD;;;GAGG;AACH,iBAAS,0BAA0B,CAClC,aAAa,EAAE,MAAM,CAAE,MAAM,EAAE,GAAG,CAAE,GAClC,kBAAkB,CAkDpB;AAkDD,eAAe,0BAA0B,CAAC"}
@@ -1,7 +1,11 @@
1
- export default createRootURLMiddleware;
2
1
  /**
3
- * @param {string} rootURL
4
- * @return {import('../types').APIFetchMiddleware} Root URL middleware.
2
+ * Internal dependencies
3
+ */
4
+ import type { APIFetchMiddleware } from '../types';
5
+ /**
6
+ * @param rootURL
7
+ * @return Root URL middleware.
5
8
  */
6
- declare function createRootURLMiddleware(rootURL: string): import("../types").APIFetchMiddleware;
9
+ declare const createRootURLMiddleware: (rootURL: string) => APIFetchMiddleware;
10
+ export default createRootURLMiddleware;
7
11
  //# sourceMappingURL=root-url.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"root-url.d.ts","sourceRoot":"","sources":["../../src/middlewares/root-url.js"],"names":[],"mappings":";AAKA;;;GAGG;AACH,kDAHW,MAAM,GACL,OAAO,UAAU,EAAE,kBAAkB,CAkChD"}
1
+ {"version":3,"file":"root-url.d.ts","sourceRoot":"","sources":["../../src/middlewares/root-url.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAGnD;;;GAGG;AACH,QAAA,MAAM,uBAAuB,YACjB,MAAM,KAAI,kBAiCpB,CAAC;AAEH,eAAe,uBAAuB,CAAC"}
@@ -1,4 +1,7 @@
1
- export default createThemePreviewMiddleware;
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { APIFetchMiddleware } from '../types';
2
5
  /**
3
6
  * This appends a `wp_theme_preview` parameter to the REST API request URL if
4
7
  * the admin URL contains a `theme` GET parameter.
@@ -6,8 +9,9 @@ export default createThemePreviewMiddleware;
6
9
  * If the REST API request URL has contained the `wp_theme_preview` parameter as `''`,
7
10
  * then bypass this middleware.
8
11
  *
9
- * @param {Record<string, any>} themePath
10
- * @return {import('../types').APIFetchMiddleware} Preloading middleware.
12
+ * @param themePath
13
+ * @return Preloading middleware.
11
14
  */
12
- declare function createThemePreviewMiddleware(themePath: Record<string, any>): import("../types").APIFetchMiddleware;
15
+ declare const createThemePreviewMiddleware: (themePath: Record<string, any>) => APIFetchMiddleware;
16
+ export default createThemePreviewMiddleware;
13
17
  //# sourceMappingURL=theme-preview.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"theme-preview.d.ts","sourceRoot":"","sources":["../../src/middlewares/theme-preview.js"],"names":[],"mappings":";AAKA;;;;;;;;;GASG;AACH,yDAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAClB,OAAO,UAAU,EAAE,kBAAkB,CA0BhD"}
1
+ {"version":3,"file":"theme-preview.d.ts","sourceRoot":"","sources":["../../src/middlewares/theme-preview.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;;;;;;GASG;AACH,QAAA,MAAM,4BAA4B,cACpB,MAAM,CAAE,MAAM,EAAE,GAAG,CAAE,KAAI,kBAqCrC,CAAC;AAEH,eAAe,4BAA4B,CAAC"}
@@ -1,6 +1,7 @@
1
- export default userLocaleMiddleware;
2
1
  /**
3
- * @type {import('../types').APIFetchMiddleware}
2
+ * Internal dependencies
4
3
  */
5
- declare const userLocaleMiddleware: import("../types").APIFetchMiddleware;
4
+ import type { APIFetchMiddleware } from '../types';
5
+ declare const userLocaleMiddleware: APIFetchMiddleware;
6
+ export default userLocaleMiddleware;
6
7
  //# sourceMappingURL=user-locale.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"user-locale.d.ts","sourceRoot":"","sources":["../../src/middlewares/user-locale.js"],"names":[],"mappings":";AAKA;;GAEG;AACH,oCAFU,OAAO,UAAU,EAAE,kBAAkB,CAkB7C"}
1
+ {"version":3,"file":"user-locale.d.ts","sourceRoot":"","sources":["../../src/middlewares/user-locale.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnD,QAAA,MAAM,oBAAoB,EAAE,kBAgB3B,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
@@ -1,14 +1,15 @@
1
- export interface APIFetchOptions extends RequestInit {
1
+ export interface APIFetchOptions<Parse extends boolean = boolean> extends RequestInit {
2
2
  headers?: Record<string, string>;
3
3
  path?: string;
4
4
  url?: string;
5
5
  /**
6
6
  * @default true
7
7
  */
8
- parse?: boolean;
8
+ parse?: Parse;
9
9
  data?: any;
10
10
  namespace?: string;
11
11
  endpoint?: string;
12
12
  }
13
- export type APIFetchMiddleware = (options: APIFetchOptions, next: (nextOptions: APIFetchOptions) => Promise<any>) => Promise<any>;
13
+ export type FetchHandler<Parse extends boolean = boolean> = (nextOptions: APIFetchOptions<Parse>) => Promise<any>;
14
+ export type APIFetchMiddleware<Parse extends boolean = boolean> = (options: APIFetchOptions<Parse>, next: FetchHandler<Parse>) => Promise<any>;
14
15
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAgB,SAAQ,WAAW;IAEnD,OAAO,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,CAAC;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,MAAM,kBAAkB,GAAG,CAChC,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,CAAE,WAAW,EAAE,eAAe,KAAM,OAAO,CAAE,GAAG,CAAE,KACpD,OAAO,CAAE,GAAG,CAAE,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe,CAAE,KAAK,SAAS,OAAO,GAAG,OAAO,CAChE,SAAQ,WAAW;IAEnB,OAAO,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,CAAC;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,MAAM,YAAY,CAAE,KAAK,SAAS,OAAO,GAAG,OAAO,IAAK,CAC7D,WAAW,EAAE,eAAe,CAAE,KAAK,CAAE,KACjC,OAAO,CAAE,GAAG,CAAE,CAAC;AAEpB,MAAM,MAAM,kBAAkB,CAAE,KAAK,SAAS,OAAO,GAAG,OAAO,IAAK,CACnE,OAAO,EAAE,eAAe,CAAE,KAAK,CAAE,EACjC,IAAI,EAAE,YAAY,CAAE,KAAK,CAAE,KACvB,OAAO,CAAE,GAAG,CAAE,CAAC"}
@@ -1,10 +1,18 @@
1
+ /**
2
+ * Parses the apiFetch response properly and normalize response errors.
3
+ *
4
+ * @param response
5
+ * @param shouldParseResponse
6
+ *
7
+ * @return Parsed response.
8
+ */
9
+ export declare function parseResponseAndNormalizeError(response: Response, shouldParseResponse?: boolean): Promise<any>;
1
10
  /**
2
11
  * Parses a response, throwing an error if parsing the response fails.
3
12
  *
4
- * @param {Response} response
5
- * @param {boolean} shouldParseResponse
6
- * @return {Promise<any>} Parsed response.
13
+ * @param response
14
+ * @param shouldParseResponse
15
+ * @return Never returns, always throws.
7
16
  */
8
- export function parseAndThrowError(response: Response, shouldParseResponse?: boolean): Promise<any>;
9
- export function parseResponseAndNormalizeError(response: Response, shouldParseResponse?: boolean): Promise<any>;
17
+ export declare function parseAndThrowError(response: Response, shouldParseResponse?: boolean): Promise<void>;
10
18
  //# sourceMappingURL=response.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/utils/response.js"],"names":[],"mappings":"AAgEA;;;;;;GAMG;AACH,6CAJW,QAAQ,wBACR,OAAO,GACN,OAAO,CAAC,GAAG,CAAC,CAevB;AA7BM,yDALI,QAAQ,wBACR,OAAO,GAEN,OAAO,CAAC,GAAG,CAAC,CASvB"}
1
+ {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/utils/response.ts"],"names":[],"mappings":"AAuBA;;;;;;;GAOG;AACH,wBAAsB,8BAA8B,CACnD,QAAQ,EAAE,QAAQ,EAClB,mBAAmB,UAAO,gBAW1B;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,QAAQ,EAClB,mBAAmB,UAAO,iBAQ1B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/api-fetch",
3
- "version": "7.29.1-next.f34ab90e9.0",
3
+ "version": "7.30.1-next.6870dfe5b.0",
4
4
  "description": "Utility to make WordPress REST API requests.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -30,11 +30,11 @@
30
30
  "types": "build-types",
31
31
  "dependencies": {
32
32
  "@babel/runtime": "7.25.7",
33
- "@wordpress/i18n": "^6.2.1-next.f34ab90e9.0",
34
- "@wordpress/url": "^4.29.1-next.f34ab90e9.0"
33
+ "@wordpress/i18n": "^6.3.1-next.6870dfe5b.0",
34
+ "@wordpress/url": "^4.30.1-next.6870dfe5b.0"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "91f58004986ec6faf118825f925ac7873d171986"
39
+ "gitHead": "c8637da9df499cd7b6b07c9fad918f6d45f4de3d"
40
40
  }
@@ -19,14 +19,17 @@ import {
19
19
  parseResponseAndNormalizeError,
20
20
  parseAndThrowError,
21
21
  } from './utils/response';
22
+ import type {
23
+ APIFetchMiddleware,
24
+ APIFetchOptions,
25
+ FetchHandler,
26
+ } from './types';
22
27
 
23
28
  /**
24
29
  * Default set of header values which should be sent with every request unless
25
30
  * explicitly provided through apiFetch options.
26
- *
27
- * @type {Record<string, string>}
28
31
  */
29
- const DEFAULT_HEADERS = {
32
+ const DEFAULT_HEADERS: APIFetchOptions[ 'headers' ] = {
30
33
  // The backend uses the Accept header as a condition for considering an
31
34
  // incoming request as a REST request.
32
35
  //
@@ -37,20 +40,12 @@ const DEFAULT_HEADERS = {
37
40
  /**
38
41
  * Default set of fetch option values which should be sent with every request
39
42
  * unless explicitly provided through apiFetch options.
40
- *
41
- * @type {Object}
42
43
  */
43
- const DEFAULT_OPTIONS = {
44
+ const DEFAULT_OPTIONS: APIFetchOptions = {
44
45
  credentials: 'include',
45
46
  };
46
47
 
47
- /** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
48
- /** @typedef {import('./types').APIFetchOptions} APIFetchOptions */
49
-
50
- /**
51
- * @type {import('./types').APIFetchMiddleware[]}
52
- */
53
- const middlewares = [
48
+ const middlewares: Array< APIFetchMiddleware > = [
54
49
  userLocaleMiddleware,
55
50
  namespaceEndpointMiddleware,
56
51
  httpV1Middleware,
@@ -60,33 +55,13 @@ const middlewares = [
60
55
  /**
61
56
  * Register a middleware
62
57
  *
63
- * @param {import('./types').APIFetchMiddleware} middleware
58
+ * @param middleware
64
59
  */
65
- function registerMiddleware( middleware ) {
60
+ function registerMiddleware( middleware: APIFetchMiddleware ) {
66
61
  middlewares.unshift( middleware );
67
62
  }
68
63
 
69
- /**
70
- * Checks the status of a response, throwing the Response as an error if
71
- * it is outside the 200 range.
72
- *
73
- * @param {Response} response
74
- * @return {Response} The response if the status is in the 200 range.
75
- */
76
- const checkStatus = ( response ) => {
77
- if ( response.status >= 200 && response.status < 300 ) {
78
- return response;
79
- }
80
-
81
- throw response;
82
- };
83
-
84
- /** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/
85
-
86
- /**
87
- * @type {FetchHandler}
88
- */
89
- const defaultFetchHandler = ( nextOptions ) => {
64
+ const defaultFetchHandler: FetchHandler = ( nextOptions ) => {
90
65
  const { url, path, data, parse = true, ...remainingOptions } = nextOptions;
91
66
  let { body, headers } = nextOptions;
92
67
 
@@ -99,7 +74,7 @@ const defaultFetchHandler = ( nextOptions ) => {
99
74
  headers[ 'Content-Type' ] = 'application/json';
100
75
  }
101
76
 
102
- const responsePromise = window.fetch(
77
+ const responsePromise = globalThis.fetch(
103
78
  // Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
104
79
  url || path || window.location.href,
105
80
  {
@@ -111,55 +86,85 @@ const defaultFetchHandler = ( nextOptions ) => {
111
86
  );
112
87
 
113
88
  return responsePromise.then(
114
- ( value ) =>
115
- Promise.resolve( value )
116
- .then( checkStatus )
117
- .catch( ( response ) => parseAndThrowError( response, parse ) )
118
- .then( ( response ) =>
119
- parseResponseAndNormalizeError( response, parse )
120
- ),
89
+ ( response ) => {
90
+ // If the response is not 2xx, still parse the response body as JSON
91
+ // but throw the JSON as error.
92
+ if ( ! response.ok ) {
93
+ return parseAndThrowError( response, parse );
94
+ }
95
+
96
+ return parseResponseAndNormalizeError( response, parse );
97
+ },
121
98
  ( err ) => {
122
99
  // Re-throw AbortError for the users to handle it themselves.
123
100
  if ( err && err.name === 'AbortError' ) {
124
101
  throw err;
125
102
  }
126
103
 
127
- // Otherwise, there is most likely no network connection.
128
- // Unfortunately the message might depend on the browser.
104
+ // If the browser reports being offline, we'll just assume that
105
+ // this is why the request failed.
106
+ if ( ! globalThis.navigator.onLine ) {
107
+ throw {
108
+ code: 'offline_error',
109
+ message: __(
110
+ 'Unable to connect. Please check your Internet connection.'
111
+ ),
112
+ };
113
+ }
114
+
115
+ // Hard to diagnose further due to how Window.fetch reports errors.
129
116
  throw {
130
117
  code: 'fetch_error',
131
- message: __( 'You are probably offline.' ),
118
+ message: __(
119
+ 'Could not get a valid response from the server.'
120
+ ),
132
121
  };
133
122
  }
134
123
  );
135
124
  };
136
125
 
137
- /** @type {FetchHandler} */
138
126
  let fetchHandler = defaultFetchHandler;
139
127
 
140
128
  /**
141
129
  * Defines a custom fetch handler for making the requests that will override
142
130
  * the default one using window.fetch
143
131
  *
144
- * @param {FetchHandler} newFetchHandler The new fetch handler
132
+ * @param newFetchHandler The new fetch handler
145
133
  */
146
- function setFetchHandler( newFetchHandler ) {
134
+ function setFetchHandler( newFetchHandler: FetchHandler ) {
147
135
  fetchHandler = newFetchHandler;
148
136
  }
149
137
 
138
+ interface apiFetch {
139
+ < T, Parse extends boolean = true >(
140
+ options: APIFetchOptions< Parse >
141
+ ): Promise< Parse extends true ? T : Response >;
142
+ nonceEndpoint?: string;
143
+ nonceMiddleware?: ReturnType< typeof createNonceMiddleware >;
144
+ use: ( middleware: APIFetchMiddleware ) => void;
145
+ setFetchHandler: ( newFetchHandler: FetchHandler ) => void;
146
+ createNonceMiddleware: typeof createNonceMiddleware;
147
+ createPreloadingMiddleware: typeof createPreloadingMiddleware;
148
+ createRootURLMiddleware: typeof createRootURLMiddleware;
149
+ fetchAllMiddleware: typeof fetchAllMiddleware;
150
+ mediaUploadMiddleware: typeof mediaUploadMiddleware;
151
+ createThemePreviewMiddleware: typeof createThemePreviewMiddleware;
152
+ }
153
+
150
154
  /**
151
- * @template T
152
- * @param {import('./types').APIFetchOptions} options
153
- * @return {Promise<T>} A promise representing the request processed via the registered middlewares.
155
+ * Fetch
156
+ *
157
+ * @param options The options for the fetch.
158
+ * @return A promise representing the request processed via the registered middlewares.
154
159
  */
155
- function apiFetch( options ) {
160
+ const apiFetch: apiFetch = ( options ) => {
156
161
  // creates a nested function chain that calls all middlewares and finally the `fetchHandler`,
157
162
  // converting `middlewares = [ m1, m2, m3 ]` into:
158
163
  // ```
159
164
  // opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );
160
165
  // ```
161
- const enhancedHandler = middlewares.reduceRight(
162
- ( /** @type {FetchHandler} */ next, middleware ) => {
166
+ const enhancedHandler = middlewares.reduceRight< FetchHandler >(
167
+ ( next, middleware ) => {
163
168
  return ( workingOptions ) => middleware( workingOptions, next );
164
169
  },
165
170
  fetchHandler
@@ -171,20 +176,23 @@ function apiFetch( options ) {
171
176
  }
172
177
 
173
178
  // If the nonce is invalid, refresh it and try again.
174
- return (
175
- window
176
- // @ts-ignore
177
- .fetch( apiFetch.nonceEndpoint )
178
- .then( checkStatus )
179
- .then( ( data ) => data.text() )
180
- .then( ( text ) => {
181
- // @ts-ignore
182
- apiFetch.nonceMiddleware.nonce = text;
183
- return apiFetch( options );
184
- } )
185
- );
179
+ return globalThis
180
+ .fetch( apiFetch.nonceEndpoint! )
181
+ .then( ( response ) => {
182
+ // If the nonce refresh fails, it means we failed to recover from the original
183
+ // `rest_cookie_invalid_nonce` error and that it's time to finally re-throw it.
184
+ if ( ! response.ok ) {
185
+ return Promise.reject( error );
186
+ }
187
+
188
+ return response.text();
189
+ } )
190
+ .then( ( text ) => {
191
+ apiFetch.nonceMiddleware!.nonce = text;
192
+ return apiFetch( options );
193
+ } );
186
194
  } );
187
- }
195
+ };
188
196
 
189
197
  apiFetch.use = registerMiddleware;
190
198
  apiFetch.setFetchHandler = setFetchHandler;
@@ -197,3 +205,4 @@ apiFetch.mediaUploadMiddleware = mediaUploadMiddleware;
197
205
  apiFetch.createThemePreviewMiddleware = createThemePreviewMiddleware;
198
206
 
199
207
  export default apiFetch;
208
+ export * from './types';
@@ -7,15 +7,19 @@ import { addQueryArgs } from '@wordpress/url';
7
7
  * Internal dependencies
8
8
  */
9
9
  import apiFetch from '..';
10
+ import type { APIFetchMiddleware, APIFetchOptions } from '../types';
10
11
 
11
12
  /**
12
13
  * Apply query arguments to both URL and Path, whichever is present.
13
14
  *
14
- * @param {import('../types').APIFetchOptions} props
15
- * @param {Record<string, string | number>} queryArgs
16
- * @return {import('../types').APIFetchOptions} The request with the modified query args
15
+ * @param {APIFetchOptions} props The request options
16
+ * @param {Record< string, string | number >} queryArgs
17
+ * @return The request with the modified query args
17
18
  */
18
- const modifyQuery = ( { path, url, ...options }, queryArgs ) => ( {
19
+ const modifyQuery = (
20
+ { path, url, ...options }: APIFetchOptions,
21
+ queryArgs: Record< string, string | number >
22
+ ): APIFetchOptions => ( {
19
23
  ...options,
20
24
  url: url && addQueryArgs( url, queryArgs ),
21
25
  path: path && addQueryArgs( path, queryArgs ),
@@ -24,17 +28,17 @@ const modifyQuery = ( { path, url, ...options }, queryArgs ) => ( {
24
28
  /**
25
29
  * Duplicates parsing functionality from apiFetch.
26
30
  *
27
- * @param {Response} response
28
- * @return {Promise<any>} Parsed response json.
31
+ * @param response
32
+ * @return Parsed response json.
29
33
  */
30
- const parseResponse = ( response ) =>
34
+ const parseResponse = ( response: Response ) =>
31
35
  response.json ? response.json() : Promise.reject( response );
32
36
 
33
37
  /**
34
- * @param {string | null} linkHeader
35
- * @return {{ next?: string }} The parsed link header.
38
+ * @param linkHeader
39
+ * @return The parsed link header.
36
40
  */
37
- const parseLinkHeader = ( linkHeader ) => {
41
+ const parseLinkHeader = ( linkHeader: string | null ) => {
38
42
  if ( ! linkHeader ) {
39
43
  return {};
40
44
  }
@@ -47,19 +51,19 @@ const parseLinkHeader = ( linkHeader ) => {
47
51
  };
48
52
 
49
53
  /**
50
- * @param {Response} response
51
- * @return {string | undefined} The next page URL.
54
+ * @param response
55
+ * @return The next page URL.
52
56
  */
53
- const getNextPageUrl = ( response ) => {
57
+ const getNextPageUrl = ( response: Response ) => {
54
58
  const { next } = parseLinkHeader( response.headers.get( 'link' ) );
55
59
  return next;
56
60
  };
57
61
 
58
62
  /**
59
- * @param {import('../types').APIFetchOptions} options
60
- * @return {boolean} True if the request contains an unbounded query.
63
+ * @param options
64
+ * @return True if the request contains an unbounded query.
61
65
  */
62
- const requestContainsUnboundedQuery = ( options ) => {
66
+ const requestContainsUnboundedQuery = ( options: APIFetchOptions ) => {
63
67
  const pathIsUnbounded =
64
68
  !! options.path && options.path.indexOf( 'per_page=-1' ) !== -1;
65
69
  const urlIsUnbounded =
@@ -71,10 +75,10 @@ const requestContainsUnboundedQuery = ( options ) => {
71
75
  * The REST API enforces an upper limit on the per_page option. To handle large
72
76
  * collections, apiFetch consumers can pass `per_page=-1`; this middleware will
73
77
  * then recursively assemble a full response array from all available pages.
74
- *
75
- * @type {import('../types').APIFetchMiddleware}
78
+ * @param options
79
+ * @param next
76
80
  */
77
- const fetchAllMiddleware = async ( options, next ) => {
81
+ const fetchAllMiddleware: APIFetchMiddleware = async ( options, next ) => {
78
82
  if ( options.parse === false ) {
79
83
  // If a consumer has opted out of parsing, do not apply middleware.
80
84
  return next( options );
@@ -108,7 +112,7 @@ const fetchAllMiddleware = async ( options, next ) => {
108
112
  }
109
113
 
110
114
  // Iteratively fetch all remaining pages until no "next" header is found.
111
- let mergedResults = /** @type {any[]} */ ( [] ).concat( results );
115
+ let mergedResults = ( [] as Array< any > ).concat( results );
112
116
  while ( nextPage ) {
113
117
  const nextResponse = await apiFetch( {
114
118
  ...options,
@@ -1,7 +1,10 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { APIFetchMiddleware } from '../types';
5
+
1
6
  /**
2
7
  * Set of HTTP methods which are eligible to be overridden.
3
- *
4
- * @type {Set<string>}
5
8
  */
6
9
  const OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );
7
10
 
@@ -12,8 +15,6 @@ const OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );
12
15
  * is `GET`."
13
16
  *
14
17
  * @see https://fetch.spec.whatwg.org/#requests
15
- *
16
- * @type {string}
17
18
  */
18
19
  const DEFAULT_METHOD = 'GET';
19
20
 
@@ -21,9 +22,10 @@ const DEFAULT_METHOD = 'GET';
21
22
  * API Fetch middleware which overrides the request method for HTTP v1
22
23
  * compatibility leveraging the REST API X-HTTP-Method-Override header.
23
24
  *
24
- * @type {import('../types').APIFetchMiddleware}
25
+ * @param options
26
+ * @param next
25
27
  */
26
- const httpV1Middleware = ( options, next ) => {
28
+ const httpV1Middleware: APIFetchMiddleware = ( options, next ) => {
27
29
  const { method = DEFAULT_METHOD } = options;
28
30
  if ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {
29
31
  options = {