@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.
- package/CHANGELOG.md +2 -0
- package/build/index.js +50 -51
- package/build/index.js.map +1 -1
- package/build/middlewares/fetch-all-middleware.js +14 -14
- package/build/middlewares/fetch-all-middleware.js.map +1 -1
- package/build/middlewares/http-v1.js +6 -5
- package/build/middlewares/http-v1.js.map +1 -1
- package/build/middlewares/media-upload.js +7 -7
- package/build/middlewares/media-upload.js.map +1 -1
- package/build/middlewares/namespace-endpoint.js +2 -1
- package/build/middlewares/namespace-endpoint.js.map +1 -1
- package/build/middlewares/nonce.js +7 -5
- package/build/middlewares/nonce.js.map +1 -1
- package/build/middlewares/preloading.js +10 -7
- package/build/middlewares/preloading.js.map +1 -1
- package/build/middlewares/root-url.js +2 -2
- package/build/middlewares/root-url.js.map +1 -1
- package/build/middlewares/theme-preview.js +6 -2
- package/build/middlewares/theme-preview.js.map +1 -1
- package/build/middlewares/user-locale.js +2 -1
- package/build/middlewares/user-locale.js.map +1 -1
- package/build/types.js.map +1 -1
- package/build/utils/response.js +31 -50
- package/build/utils/response.js.map +1 -1
- package/build-module/index.js +38 -52
- package/build-module/index.js.map +1 -1
- package/build-module/middlewares/fetch-all-middleware.js +14 -15
- package/build-module/middlewares/fetch-all-middleware.js.map +1 -1
- package/build-module/middlewares/http-v1.js +6 -5
- package/build-module/middlewares/http-v1.js.map +1 -1
- package/build-module/middlewares/media-upload.js +7 -8
- package/build-module/middlewares/media-upload.js.map +1 -1
- package/build-module/middlewares/namespace-endpoint.js +2 -1
- package/build-module/middlewares/namespace-endpoint.js.map +1 -1
- package/build-module/middlewares/nonce.js +7 -5
- package/build-module/middlewares/nonce.js.map +1 -1
- package/build-module/middlewares/preloading.js +10 -7
- package/build-module/middlewares/preloading.js.map +1 -1
- package/build-module/middlewares/root-url.js +3 -2
- package/build-module/middlewares/root-url.js.map +1 -1
- package/build-module/middlewares/theme-preview.js +5 -2
- package/build-module/middlewares/theme-preview.js.map +1 -1
- package/build-module/middlewares/user-locale.js +2 -1
- package/build-module/middlewares/user-locale.js.map +1 -1
- package/build-module/types.js.map +1 -1
- package/build-module/utils/response.js +30 -48
- package/build-module/utils/response.js.map +1 -1
- package/build-types/index.d.ts +25 -32
- package/build-types/index.d.ts.map +1 -1
- package/build-types/middlewares/fetch-all-middleware.d.ts +5 -4
- package/build-types/middlewares/fetch-all-middleware.d.ts.map +1 -1
- package/build-types/middlewares/http-v1.d.ts +8 -3
- package/build-types/middlewares/http-v1.d.ts.map +1 -1
- package/build-types/middlewares/media-upload.d.ts +5 -4
- package/build-types/middlewares/media-upload.d.ts.map +1 -1
- package/build-types/middlewares/namespace-endpoint.d.ts +4 -3
- package/build-types/middlewares/namespace-endpoint.d.ts.map +1 -1
- package/build-types/middlewares/nonce.d.ts +9 -4
- package/build-types/middlewares/nonce.d.ts.map +1 -1
- package/build-types/middlewares/preloading.d.ts +8 -4
- package/build-types/middlewares/preloading.d.ts.map +1 -1
- package/build-types/middlewares/root-url.d.ts +8 -4
- package/build-types/middlewares/root-url.d.ts.map +1 -1
- package/build-types/middlewares/theme-preview.d.ts +8 -4
- package/build-types/middlewares/theme-preview.d.ts.map +1 -1
- package/build-types/middlewares/user-locale.d.ts +4 -3
- package/build-types/middlewares/user-locale.d.ts.map +1 -1
- package/build-types/types.d.ts +4 -3
- package/build-types/types.d.ts.map +1 -1
- package/build-types/utils/response.d.ts +13 -5
- package/build-types/utils/response.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/{index.js → index.ts} +78 -69
- package/src/middlewares/{fetch-all-middleware.js → fetch-all-middleware.ts} +24 -20
- package/src/middlewares/{http-v1.js → http-v1.ts} +8 -6
- package/src/middlewares/{media-upload.js → media-upload.ts} +13 -12
- package/src/middlewares/{namespace-endpoint.js → namespace-endpoint.ts} +7 -2
- package/src/middlewares/{nonce.js → nonce.ts} +15 -7
- package/src/middlewares/{preloading.js → preloading.ts} +21 -11
- package/src/middlewares/root-url.ts +47 -0
- package/src/middlewares/test/{fetch-all-middleware.js → fetch-all-middleware.ts} +1 -1
- package/src/middlewares/test/{http-v1.js → http-v1.ts} +7 -4
- package/src/middlewares/test/{media-upload.js → media-upload.ts} +2 -1
- package/src/middlewares/test/{namespace-endpoint.js → namespace-endpoint.ts} +2 -1
- package/src/middlewares/test/{nonce.js → nonce.ts} +5 -4
- package/src/middlewares/test/{preloading.js → preloading.ts} +31 -11
- package/src/middlewares/test/{root-url.js → root-url.ts} +2 -1
- package/src/middlewares/test/{user-locale.js → user-locale.ts} +9 -8
- package/src/middlewares/theme-preview.ts +60 -0
- package/src/middlewares/{user-locale.js → user-locale.ts} +4 -2
- package/src/test/index.js +218 -147
- package/src/types.ts +10 -5
- package/src/utils/response.ts +64 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/src/middlewares/root-url.js +0 -44
- package/src/middlewares/theme-preview.js +0 -42
- package/src/utils/response.js +0 -85
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import type { APIFetchMiddleware } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Middleware handling media upload failures and retries.
|
|
4
|
-
*
|
|
5
|
-
* @
|
|
4
|
+
* @param options
|
|
5
|
+
* @param next
|
|
6
6
|
*/
|
|
7
|
-
declare const mediaUploadMiddleware:
|
|
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.
|
|
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
|
-
*
|
|
2
|
+
* Internal dependencies
|
|
4
3
|
*/
|
|
5
|
-
|
|
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.
|
|
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
|
-
*
|
|
4
|
-
|
|
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):
|
|
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.
|
|
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
|
-
*
|
|
4
|
-
|
|
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>):
|
|
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.
|
|
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
|
-
*
|
|
4
|
-
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { APIFetchMiddleware } from '../types';
|
|
5
|
+
/**
|
|
6
|
+
* @param rootURL
|
|
7
|
+
* @return Root URL middleware.
|
|
5
8
|
*/
|
|
6
|
-
declare
|
|
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.
|
|
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
|
-
|
|
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
|
|
10
|
-
* @return
|
|
12
|
+
* @param themePath
|
|
13
|
+
* @return Preloading middleware.
|
|
11
14
|
*/
|
|
12
|
-
declare
|
|
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.
|
|
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
|
-
*
|
|
2
|
+
* Internal dependencies
|
|
4
3
|
*/
|
|
5
|
-
|
|
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.
|
|
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"}
|
package/build-types/types.d.ts
CHANGED
|
@@ -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?:
|
|
8
|
+
parse?: Parse;
|
|
9
9
|
data?: any;
|
|
10
10
|
namespace?: string;
|
|
11
11
|
endpoint?: string;
|
|
12
12
|
}
|
|
13
|
-
export type
|
|
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,
|
|
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
|
|
5
|
-
* @param
|
|
6
|
-
* @return
|
|
13
|
+
* @param response
|
|
14
|
+
* @param shouldParseResponse
|
|
15
|
+
* @return Never returns, always throws.
|
|
7
16
|
*/
|
|
8
|
-
export function parseAndThrowError(response: Response, shouldParseResponse?: boolean): Promise<
|
|
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.
|
|
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.
|
|
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.
|
|
34
|
-
"@wordpress/url": "^4.
|
|
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": "
|
|
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
|
-
|
|
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
|
|
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 =
|
|
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
|
-
(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
//
|
|
128
|
-
//
|
|
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: __(
|
|
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
|
|
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
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* @
|
|
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
|
-
|
|
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
|
-
(
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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 {
|
|
15
|
-
* @param {Record<string, string | number>}
|
|
16
|
-
* @return
|
|
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 = (
|
|
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
|
|
28
|
-
* @return
|
|
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
|
|
35
|
-
* @return
|
|
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
|
|
51
|
-
* @return
|
|
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
|
|
60
|
-
* @return
|
|
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
|
-
* @
|
|
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 =
|
|
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
|
-
* @
|
|
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 = {
|