@wordpress/api-fetch 7.32.0 → 7.32.1-next.b8c8708f3.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/build/index.js +112 -145
- package/build/index.js.map +7 -1
- package/build/middlewares/fetch-all-middleware.js +48 -81
- package/build/middlewares/fetch-all-middleware.js.map +7 -1
- package/build/middlewares/http-v1.js +29 -38
- package/build/middlewares/http-v1.js.map +7 -1
- package/build/middlewares/media-upload.js +43 -49
- package/build/middlewares/media-upload.js.map +7 -1
- package/build/middlewares/namespace-endpoint.js +27 -14
- package/build/middlewares/namespace-endpoint.js.map +7 -1
- package/build/middlewares/nonce.js +26 -23
- package/build/middlewares/nonce.js.map +7 -1
- package/build/middlewares/preloading.js +63 -60
- package/build/middlewares/preloading.js.map +7 -1
- package/build/middlewares/root-url.js +42 -27
- package/build/middlewares/root-url.js.map +7 -1
- package/build/middlewares/theme-preview.js +49 -38
- package/build/middlewares/theme-preview.js.map +7 -1
- package/build/middlewares/user-locale.js +28 -23
- package/build/middlewares/user-locale.js.map +7 -1
- package/build/types.js +16 -5
- package/build/types.js.map +7 -1
- package/build/utils/response.js +31 -39
- package/build/utils/response.js.map +7 -1
- package/build-module/index.js +76 -121
- package/build-module/index.js.map +7 -1
- package/build-module/middlewares/fetch-all-middleware.js +16 -71
- package/build-module/middlewares/fetch-all-middleware.js.map +7 -1
- package/build-module/middlewares/http-v1.js +11 -34
- package/build-module/middlewares/http-v1.js.map +7 -1
- package/build-module/middlewares/media-upload.js +27 -43
- package/build-module/middlewares/media-upload.js.map +7 -1
- package/build-module/middlewares/namespace-endpoint.js +9 -10
- package/build-module/middlewares/namespace-endpoint.js.map +7 -1
- package/build-module/middlewares/nonce.js +8 -19
- package/build-module/middlewares/nonce.js.map +7 -1
- package/build-module/middlewares/preloading.js +43 -54
- package/build-module/middlewares/preloading.js.map +7 -1
- package/build-module/middlewares/root-url.js +14 -23
- package/build-module/middlewares/root-url.js.map +7 -1
- package/build-module/middlewares/theme-preview.js +29 -31
- package/build-module/middlewares/theme-preview.js.map +7 -1
- package/build-module/middlewares/user-locale.js +10 -19
- package/build-module/middlewares/user-locale.js.map +7 -1
- package/build-module/types.js +1 -2
- package/build-module/types.js.map +7 -1
- package/build-module/utils/response.js +10 -36
- package/build-module/utils/response.js.map +7 -1
- package/package.json +13 -5
|
@@ -1,50 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var theme_preview_exports = {};
|
|
20
|
+
__export(theme_preview_exports, {
|
|
21
|
+
default: () => theme_preview_default
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* This appends a `wp_theme_preview` parameter to the REST API request URL if
|
|
18
|
-
* the admin URL contains a `theme` GET parameter.
|
|
19
|
-
*
|
|
20
|
-
* If the REST API request URL has contained the `wp_theme_preview` parameter as `''`,
|
|
21
|
-
* then bypass this middleware.
|
|
22
|
-
*
|
|
23
|
-
* @param themePath
|
|
24
|
-
* @return Preloading middleware.
|
|
25
|
-
*/
|
|
26
|
-
const createThemePreviewMiddleware = themePath => (options, next) => {
|
|
27
|
-
if (typeof options.url === 'string') {
|
|
28
|
-
const wpThemePreview = (0, _url.getQueryArg)(options.url, 'wp_theme_preview');
|
|
29
|
-
if (wpThemePreview === undefined) {
|
|
30
|
-
options.url = (0, _url.addQueryArgs)(options.url, {
|
|
23
|
+
module.exports = __toCommonJS(theme_preview_exports);
|
|
24
|
+
var import_url = require("@wordpress/url");
|
|
25
|
+
const createThemePreviewMiddleware = (themePath) => (options, next) => {
|
|
26
|
+
if (typeof options.url === "string") {
|
|
27
|
+
const wpThemePreview = (0, import_url.getQueryArg)(
|
|
28
|
+
options.url,
|
|
29
|
+
"wp_theme_preview"
|
|
30
|
+
);
|
|
31
|
+
if (wpThemePreview === void 0) {
|
|
32
|
+
options.url = (0, import_url.addQueryArgs)(options.url, {
|
|
31
33
|
wp_theme_preview: themePath
|
|
32
34
|
});
|
|
33
|
-
} else if (wpThemePreview ===
|
|
34
|
-
options.url = (0,
|
|
35
|
+
} else if (wpThemePreview === "") {
|
|
36
|
+
options.url = (0, import_url.removeQueryArgs)(
|
|
37
|
+
options.url,
|
|
38
|
+
"wp_theme_preview"
|
|
39
|
+
);
|
|
35
40
|
}
|
|
36
41
|
}
|
|
37
|
-
if (typeof options.path ===
|
|
38
|
-
const wpThemePreview = (0,
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
if (typeof options.path === "string") {
|
|
43
|
+
const wpThemePreview = (0, import_url.getQueryArg)(
|
|
44
|
+
options.path,
|
|
45
|
+
"wp_theme_preview"
|
|
46
|
+
);
|
|
47
|
+
if (wpThemePreview === void 0) {
|
|
48
|
+
options.path = (0, import_url.addQueryArgs)(options.path, {
|
|
41
49
|
wp_theme_preview: themePath
|
|
42
50
|
});
|
|
43
|
-
} else if (wpThemePreview ===
|
|
44
|
-
options.path = (0,
|
|
51
|
+
} else if (wpThemePreview === "") {
|
|
52
|
+
options.path = (0, import_url.removeQueryArgs)(
|
|
53
|
+
options.path,
|
|
54
|
+
"wp_theme_preview"
|
|
55
|
+
);
|
|
45
56
|
}
|
|
46
57
|
}
|
|
47
58
|
return next(options);
|
|
48
59
|
};
|
|
49
|
-
var
|
|
50
|
-
//# sourceMappingURL=theme-preview.js.map
|
|
60
|
+
var theme_preview_default = createThemePreviewMiddleware;
|
|
61
|
+
//# sourceMappingURL=theme-preview.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/middlewares/theme-preview.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, getQueryArg, removeQueryArgs } from '@wordpress/url';\n/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\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 * If the REST API request URL has contained the `wp_theme_preview` parameter as `''`,\n * then bypass this middleware.\n *\n * @param themePath\n * @return Preloading middleware.\n */\nconst createThemePreviewMiddleware =\n\t( themePath: Record< string, any > ): APIFetchMiddleware =>\n\t( options, next ) => {\n\t\tif ( typeof options.url === 'string' ) {\n\t\t\tconst wpThemePreview = getQueryArg(\n\t\t\t\toptions.url,\n\t\t\t\t'wp_theme_preview'\n\t\t\t);\n\t\t\tif ( wpThemePreview === undefined ) {\n\t\t\t\toptions.url = addQueryArgs( options.url, {\n\t\t\t\t\twp_theme_preview: themePath,\n\t\t\t\t} );\n\t\t\t} else if ( wpThemePreview === '' ) {\n\t\t\t\toptions.url = removeQueryArgs(\n\t\t\t\t\toptions.url,\n\t\t\t\t\t'wp_theme_preview'\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tif ( typeof options.path === 'string' ) {\n\t\t\tconst wpThemePreview = getQueryArg(\n\t\t\t\toptions.path,\n\t\t\t\t'wp_theme_preview'\n\t\t\t);\n\t\t\tif ( wpThemePreview === undefined ) {\n\t\t\t\toptions.path = addQueryArgs( options.path, {\n\t\t\t\t\twp_theme_preview: themePath,\n\t\t\t\t} );\n\t\t\t} else if ( wpThemePreview === '' ) {\n\t\t\t\toptions.path = removeQueryArgs(\n\t\t\t\t\toptions.path,\n\t\t\t\t\t'wp_theme_preview'\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\treturn next( options );\n\t};\n\nexport default createThemePreviewMiddleware;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA2D;AAgB3D,MAAM,+BACL,CAAE,cACF,CAAE,SAAS,SAAU;AACpB,MAAK,OAAO,QAAQ,QAAQ,UAAW;AACtC,UAAM,qBAAiB;AAAA,MACtB,QAAQ;AAAA,MACR;AAAA,IACD;AACA,QAAK,mBAAmB,QAAY;AACnC,cAAQ,UAAM,yBAAc,QAAQ,KAAK;AAAA,QACxC,kBAAkB;AAAA,MACnB,CAAE;AAAA,IACH,WAAY,mBAAmB,IAAK;AACnC,cAAQ,UAAM;AAAA,QACb,QAAQ;AAAA,QACR;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,MAAK,OAAO,QAAQ,SAAS,UAAW;AACvC,UAAM,qBAAiB;AAAA,MACtB,QAAQ;AAAA,MACR;AAAA,IACD;AACA,QAAK,mBAAmB,QAAY;AACnC,cAAQ,WAAO,yBAAc,QAAQ,MAAM;AAAA,QAC1C,kBAAkB;AAAA,MACnB,CAAE;AAAA,IACH,WAAY,mBAAmB,IAAK;AACnC,cAAQ,WAAO;AAAA,QACd,QAAQ;AAAA,QACR;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,SAAO,KAAM,OAAQ;AACtB;AAED,IAAO,wBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,30 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var user_locale_exports = {};
|
|
20
|
+
__export(user_locale_exports, {
|
|
21
|
+
default: () => user_locale_default
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
/**
|
|
9
|
-
* WordPress dependencies
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Internal dependencies
|
|
14
|
-
*/
|
|
15
|
-
|
|
23
|
+
module.exports = __toCommonJS(user_locale_exports);
|
|
24
|
+
var import_url = require("@wordpress/url");
|
|
16
25
|
const userLocaleMiddleware = (options, next) => {
|
|
17
|
-
if (typeof options.url ===
|
|
18
|
-
options.url = (0,
|
|
19
|
-
_locale: 'user'
|
|
20
|
-
});
|
|
26
|
+
if (typeof options.url === "string" && !(0, import_url.hasQueryArg)(options.url, "_locale")) {
|
|
27
|
+
options.url = (0, import_url.addQueryArgs)(options.url, { _locale: "user" });
|
|
21
28
|
}
|
|
22
|
-
if (typeof options.path ===
|
|
23
|
-
options.path = (0,
|
|
24
|
-
_locale: 'user'
|
|
25
|
-
});
|
|
29
|
+
if (typeof options.path === "string" && !(0, import_url.hasQueryArg)(options.path, "_locale")) {
|
|
30
|
+
options.path = (0, import_url.addQueryArgs)(options.path, { _locale: "user" });
|
|
26
31
|
}
|
|
27
32
|
return next(options);
|
|
28
33
|
};
|
|
29
|
-
var
|
|
30
|
-
//# sourceMappingURL=user-locale.js.map
|
|
34
|
+
var user_locale_default = userLocaleMiddleware;
|
|
35
|
+
//# sourceMappingURL=user-locale.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/middlewares/user-locale.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\nconst userLocaleMiddleware: APIFetchMiddleware = ( 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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA0C;AAO1C,MAAM,uBAA2C,CAAE,SAAS,SAAU;AACrE,MACC,OAAO,QAAQ,QAAQ,YACvB,KAAE,wBAAa,QAAQ,KAAK,SAAU,GACrC;AACD,YAAQ,UAAM,yBAAc,QAAQ,KAAK,EAAE,SAAS,OAAO,CAAE;AAAA,EAC9D;AAEA,MACC,OAAO,QAAQ,SAAS,YACxB,KAAE,wBAAa,QAAQ,MAAM,SAAU,GACtC;AACD,YAAQ,WAAO,yBAAc,QAAQ,MAAM,EAAE,SAAS,OAAO,CAAE;AAAA,EAChE;AAEA,SAAO,KAAM,OAAQ;AACtB;AAEA,IAAO,sBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build/types.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
|
17
|
+
//# sourceMappingURL=types.js.map
|
package/build/types.js.map
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/types.ts"],
|
|
4
|
+
"sourcesContent": ["export interface APIFetchOptions< Parse extends boolean = boolean >\n\textends 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?: Parse;\n\tdata?: any;\n\tnamespace?: string;\n\tendpoint?: string;\n}\n\nexport type FetchHandler< Parse extends boolean = boolean > = (\n\tnextOptions: APIFetchOptions< Parse >\n) => Promise< any >;\n\nexport type APIFetchMiddleware< Parse extends boolean = boolean > = (\n\toptions: APIFetchOptions< Parse >,\n\tnext: FetchHandler< Parse >\n) => Promise< any >;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build/utils/response.js
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var response_exports = {};
|
|
20
|
+
__export(response_exports, {
|
|
21
|
+
parseAndThrowError: () => parseAndThrowError,
|
|
22
|
+
parseResponseAndNormalizeError: () => parseResponseAndNormalizeError
|
|
5
23
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
var _i18n = require("@wordpress/i18n");
|
|
9
|
-
/**
|
|
10
|
-
* WordPress dependencies
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Calls the `json` function on the Response, throwing an error if the response
|
|
15
|
-
* doesn't have a json function or if parsing the json itself fails.
|
|
16
|
-
*
|
|
17
|
-
* @param response
|
|
18
|
-
* @return Parsed response.
|
|
19
|
-
*/
|
|
24
|
+
module.exports = __toCommonJS(response_exports);
|
|
25
|
+
var import_i18n = require("@wordpress/i18n");
|
|
20
26
|
async function parseJsonAndNormalizeError(response) {
|
|
21
27
|
try {
|
|
22
28
|
return await response.json();
|
|
23
29
|
} catch {
|
|
24
30
|
throw {
|
|
25
|
-
code:
|
|
26
|
-
message: (0,
|
|
31
|
+
code: "invalid_json",
|
|
32
|
+
message: (0, import_i18n.__)("The response is not a valid JSON response.")
|
|
27
33
|
};
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Parses the apiFetch response properly and normalize response errors.
|
|
33
|
-
*
|
|
34
|
-
* @param response
|
|
35
|
-
* @param shouldParseResponse
|
|
36
|
-
*
|
|
37
|
-
* @return Parsed response.
|
|
38
|
-
*/
|
|
39
36
|
async function parseResponseAndNormalizeError(response, shouldParseResponse = true) {
|
|
40
37
|
if (!shouldParseResponse) {
|
|
41
38
|
return response;
|
|
@@ -45,20 +42,15 @@ async function parseResponseAndNormalizeError(response, shouldParseResponse = tr
|
|
|
45
42
|
}
|
|
46
43
|
return await parseJsonAndNormalizeError(response);
|
|
47
44
|
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Parses a response, throwing an error if parsing the response fails.
|
|
51
|
-
*
|
|
52
|
-
* @param response
|
|
53
|
-
* @param shouldParseResponse
|
|
54
|
-
* @return Never returns, always throws.
|
|
55
|
-
*/
|
|
56
45
|
async function parseAndThrowError(response, shouldParseResponse = true) {
|
|
57
46
|
if (!shouldParseResponse) {
|
|
58
47
|
throw response;
|
|
59
48
|
}
|
|
60
|
-
|
|
61
|
-
// Parse the response JSON and throw it as an error.
|
|
62
49
|
throw await parseJsonAndNormalizeError(response);
|
|
63
50
|
}
|
|
64
|
-
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
parseAndThrowError,
|
|
54
|
+
parseResponseAndNormalizeError
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/utils/response.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\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\n * @return Parsed response.\n */\nasync function parseJsonAndNormalizeError( response: Response ) {\n\ttry {\n\t\treturn await response.json();\n\t} catch {\n\t\tthrow {\n\t\t\tcode: 'invalid_json',\n\t\t\tmessage: __( 'The response is not a valid JSON response.' ),\n\t\t};\n\t}\n}\n\n/**\n * Parses the apiFetch response properly and normalize response errors.\n *\n * @param response\n * @param shouldParseResponse\n *\n * @return Parsed response.\n */\nexport async function parseResponseAndNormalizeError(\n\tresponse: Response,\n\tshouldParseResponse = true\n) {\n\tif ( ! shouldParseResponse ) {\n\t\treturn response;\n\t}\n\n\tif ( response.status === 204 ) {\n\t\treturn null;\n\t}\n\n\treturn await parseJsonAndNormalizeError( response );\n}\n\n/**\n * Parses a response, throwing an error if parsing the response fails.\n *\n * @param response\n * @param shouldParseResponse\n * @return Never returns, always throws.\n */\nexport async function parseAndThrowError(\n\tresponse: Response,\n\tshouldParseResponse = true\n) {\n\tif ( ! shouldParseResponse ) {\n\t\tthrow response;\n\t}\n\n\t// Parse the response JSON and throw it as an error.\n\tthrow await parseJsonAndNormalizeError( response );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAmB;AASnB,eAAe,2BAA4B,UAAqB;AAC/D,MAAI;AACH,WAAO,MAAM,SAAS,KAAK;AAAA,EAC5B,QAAQ;AACP,UAAM;AAAA,MACL,MAAM;AAAA,MACN,aAAS,gBAAI,4CAA6C;AAAA,IAC3D;AAAA,EACD;AACD;AAUA,eAAsB,+BACrB,UACA,sBAAsB,MACrB;AACD,MAAK,CAAE,qBAAsB;AAC5B,WAAO;AAAA,EACR;AAEA,MAAK,SAAS,WAAW,KAAM;AAC9B,WAAO;AAAA,EACR;AAEA,SAAO,MAAM,2BAA4B,QAAS;AACnD;AASA,eAAsB,mBACrB,UACA,sBAAsB,MACrB;AACD,MAAK,CAAE,qBAAsB;AAC5B,UAAM;AAAA,EACP;AAGA,QAAM,MAAM,2BAA4B,QAAS;AAClD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build-module/index.js
CHANGED
|
@@ -1,151 +1,103 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
import userLocaleMiddleware from './middlewares/user-locale';
|
|
16
|
-
import mediaUploadMiddleware from './middlewares/media-upload';
|
|
17
|
-
import createThemePreviewMiddleware from './middlewares/theme-preview';
|
|
18
|
-
import { parseResponseAndNormalizeError, parseAndThrowError } from './utils/response';
|
|
19
|
-
/**
|
|
20
|
-
* Default set of header values which should be sent with every request unless
|
|
21
|
-
* explicitly provided through apiFetch options.
|
|
22
|
-
*/
|
|
1
|
+
import { __ } from "@wordpress/i18n";
|
|
2
|
+
import createNonceMiddleware from "./middlewares/nonce";
|
|
3
|
+
import createRootURLMiddleware from "./middlewares/root-url";
|
|
4
|
+
import createPreloadingMiddleware from "./middlewares/preloading";
|
|
5
|
+
import fetchAllMiddleware from "./middlewares/fetch-all-middleware";
|
|
6
|
+
import namespaceEndpointMiddleware from "./middlewares/namespace-endpoint";
|
|
7
|
+
import httpV1Middleware from "./middlewares/http-v1";
|
|
8
|
+
import userLocaleMiddleware from "./middlewares/user-locale";
|
|
9
|
+
import mediaUploadMiddleware from "./middlewares/media-upload";
|
|
10
|
+
import createThemePreviewMiddleware from "./middlewares/theme-preview";
|
|
11
|
+
import {
|
|
12
|
+
parseResponseAndNormalizeError,
|
|
13
|
+
parseAndThrowError
|
|
14
|
+
} from "./utils/response";
|
|
23
15
|
const DEFAULT_HEADERS = {
|
|
24
16
|
// The backend uses the Accept header as a condition for considering an
|
|
25
17
|
// incoming request as a REST request.
|
|
26
18
|
//
|
|
27
19
|
// See: https://core.trac.wordpress.org/ticket/44534
|
|
28
|
-
Accept:
|
|
20
|
+
Accept: "application/json, */*;q=0.1"
|
|
29
21
|
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Default set of fetch option values which should be sent with every request
|
|
33
|
-
* unless explicitly provided through apiFetch options.
|
|
34
|
-
*/
|
|
35
22
|
const DEFAULT_OPTIONS = {
|
|
36
|
-
credentials:
|
|
23
|
+
credentials: "include"
|
|
37
24
|
};
|
|
38
|
-
const middlewares = [
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
*/
|
|
25
|
+
const middlewares = [
|
|
26
|
+
userLocaleMiddleware,
|
|
27
|
+
namespaceEndpointMiddleware,
|
|
28
|
+
httpV1Middleware,
|
|
29
|
+
fetchAllMiddleware
|
|
30
|
+
];
|
|
45
31
|
function registerMiddleware(middleware) {
|
|
46
32
|
middlewares.unshift(middleware);
|
|
47
33
|
}
|
|
48
|
-
const defaultFetchHandler = nextOptions => {
|
|
49
|
-
const {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
data,
|
|
53
|
-
parse = true,
|
|
54
|
-
...remainingOptions
|
|
55
|
-
} = nextOptions;
|
|
56
|
-
let {
|
|
57
|
-
body,
|
|
58
|
-
headers
|
|
59
|
-
} = nextOptions;
|
|
60
|
-
|
|
61
|
-
// Merge explicitly-provided headers with default values.
|
|
62
|
-
headers = {
|
|
63
|
-
...DEFAULT_HEADERS,
|
|
64
|
-
...headers
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// The `data` property is a shorthand for sending a JSON body.
|
|
34
|
+
const defaultFetchHandler = (nextOptions) => {
|
|
35
|
+
const { url, path, data, parse = true, ...remainingOptions } = nextOptions;
|
|
36
|
+
let { body, headers } = nextOptions;
|
|
37
|
+
headers = { ...DEFAULT_HEADERS, ...headers };
|
|
68
38
|
if (data) {
|
|
69
39
|
body = JSON.stringify(data);
|
|
70
|
-
headers[
|
|
40
|
+
headers["Content-Type"] = "application/json";
|
|
71
41
|
}
|
|
72
42
|
const responsePromise = globalThis.fetch(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return responsePromise.then(response => {
|
|
81
|
-
// If the response is not 2xx, still parse the response body as JSON
|
|
82
|
-
// but throw the JSON as error.
|
|
83
|
-
if (!response.ok) {
|
|
84
|
-
return parseAndThrowError(response, parse);
|
|
85
|
-
}
|
|
86
|
-
return parseResponseAndNormalizeError(response, parse);
|
|
87
|
-
}, err => {
|
|
88
|
-
// Re-throw AbortError for the users to handle it themselves.
|
|
89
|
-
if (err && err.name === 'AbortError') {
|
|
90
|
-
throw err;
|
|
43
|
+
// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
|
|
44
|
+
url || path || window.location.href,
|
|
45
|
+
{
|
|
46
|
+
...DEFAULT_OPTIONS,
|
|
47
|
+
...remainingOptions,
|
|
48
|
+
body,
|
|
49
|
+
headers
|
|
91
50
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
51
|
+
);
|
|
52
|
+
return responsePromise.then(
|
|
53
|
+
(response) => {
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
return parseAndThrowError(response, parse);
|
|
56
|
+
}
|
|
57
|
+
return parseResponseAndNormalizeError(response, parse);
|
|
58
|
+
},
|
|
59
|
+
(err) => {
|
|
60
|
+
if (err && err.name === "AbortError") {
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
if (!globalThis.navigator.onLine) {
|
|
64
|
+
throw {
|
|
65
|
+
code: "offline_error",
|
|
66
|
+
message: __(
|
|
67
|
+
"Unable to connect. Please check your Internet connection."
|
|
68
|
+
)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
96
71
|
throw {
|
|
97
|
-
code:
|
|
98
|
-
message: __(
|
|
72
|
+
code: "fetch_error",
|
|
73
|
+
message: __(
|
|
74
|
+
"Could not get a valid response from the server."
|
|
75
|
+
)
|
|
99
76
|
};
|
|
100
77
|
}
|
|
101
|
-
|
|
102
|
-
// Hard to diagnose further due to how Window.fetch reports errors.
|
|
103
|
-
throw {
|
|
104
|
-
code: 'fetch_error',
|
|
105
|
-
message: __('Could not get a valid response from the server.')
|
|
106
|
-
};
|
|
107
|
-
});
|
|
78
|
+
);
|
|
108
79
|
};
|
|
109
80
|
let fetchHandler = defaultFetchHandler;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Defines a custom fetch handler for making the requests that will override
|
|
113
|
-
* the default one using window.fetch
|
|
114
|
-
*
|
|
115
|
-
* @param newFetchHandler The new fetch handler
|
|
116
|
-
*/
|
|
117
81
|
function setFetchHandler(newFetchHandler) {
|
|
118
82
|
fetchHandler = newFetchHandler;
|
|
119
83
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
// ```
|
|
130
|
-
// opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );
|
|
131
|
-
// ```
|
|
132
|
-
const enhancedHandler = middlewares.reduceRight((next, middleware) => {
|
|
133
|
-
return workingOptions => middleware(workingOptions, next);
|
|
134
|
-
}, fetchHandler);
|
|
135
|
-
return enhancedHandler(options).catch(error => {
|
|
136
|
-
if (error.code !== 'rest_cookie_invalid_nonce') {
|
|
84
|
+
const apiFetch = (options) => {
|
|
85
|
+
const enhancedHandler = middlewares.reduceRight(
|
|
86
|
+
(next, middleware) => {
|
|
87
|
+
return (workingOptions) => middleware(workingOptions, next);
|
|
88
|
+
},
|
|
89
|
+
fetchHandler
|
|
90
|
+
);
|
|
91
|
+
return enhancedHandler(options).catch((error) => {
|
|
92
|
+
if (error.code !== "rest_cookie_invalid_nonce") {
|
|
137
93
|
return Promise.reject(error);
|
|
138
94
|
}
|
|
139
|
-
|
|
140
|
-
// If the nonce is invalid, refresh it and try again.
|
|
141
|
-
return globalThis.fetch(apiFetch.nonceEndpoint).then(response => {
|
|
142
|
-
// If the nonce refresh fails, it means we failed to recover from the original
|
|
143
|
-
// `rest_cookie_invalid_nonce` error and that it's time to finally re-throw it.
|
|
95
|
+
return globalThis.fetch(apiFetch.nonceEndpoint).then((response) => {
|
|
144
96
|
if (!response.ok) {
|
|
145
97
|
return Promise.reject(error);
|
|
146
98
|
}
|
|
147
99
|
return response.text();
|
|
148
|
-
}).then(text => {
|
|
100
|
+
}).then((text) => {
|
|
149
101
|
apiFetch.nonceMiddleware.nonce = text;
|
|
150
102
|
return apiFetch(options);
|
|
151
103
|
});
|
|
@@ -159,6 +111,9 @@ apiFetch.createRootURLMiddleware = createRootURLMiddleware;
|
|
|
159
111
|
apiFetch.fetchAllMiddleware = fetchAllMiddleware;
|
|
160
112
|
apiFetch.mediaUploadMiddleware = mediaUploadMiddleware;
|
|
161
113
|
apiFetch.createThemePreviewMiddleware = createThemePreviewMiddleware;
|
|
162
|
-
|
|
163
|
-
export * from
|
|
164
|
-
|
|
114
|
+
var index_default = apiFetch;
|
|
115
|
+
export * from "./types";
|
|
116
|
+
export {
|
|
117
|
+
index_default as default
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["__","createNonceMiddleware","createRootURLMiddleware","createPreloadingMiddleware","fetchAllMiddleware","namespaceEndpointMiddleware","httpV1Middleware","userLocaleMiddleware","mediaUploadMiddleware","createThemePreviewMiddleware","parseResponseAndNormalizeError","parseAndThrowError","DEFAULT_HEADERS","Accept","DEFAULT_OPTIONS","credentials","middlewares","registerMiddleware","middleware","unshift","defaultFetchHandler","nextOptions","url","path","data","parse","remainingOptions","body","headers","JSON","stringify","responsePromise","globalThis","fetch","window","location","href","then","response","ok","err","name","navigator","onLine","code","message","fetchHandler","setFetchHandler","newFetchHandler","apiFetch","options","enhancedHandler","reduceRight","next","workingOptions","catch","error","Promise","reject","nonceEndpoint","text","nonceMiddleware","nonce","use"],"sources":["@wordpress/api-fetch/src/index.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport createNonceMiddleware from './middlewares/nonce';\nimport createRootURLMiddleware from './middlewares/root-url';\nimport createPreloadingMiddleware from './middlewares/preloading';\nimport fetchAllMiddleware from './middlewares/fetch-all-middleware';\nimport namespaceEndpointMiddleware from './middlewares/namespace-endpoint';\nimport httpV1Middleware from './middlewares/http-v1';\nimport userLocaleMiddleware from './middlewares/user-locale';\nimport mediaUploadMiddleware from './middlewares/media-upload';\nimport createThemePreviewMiddleware from './middlewares/theme-preview';\nimport {\n\tparseResponseAndNormalizeError,\n\tparseAndThrowError,\n} from './utils/response';\nimport type {\n\tAPIFetchMiddleware,\n\tAPIFetchOptions,\n\tFetchHandler,\n} from './types';\n\n/**\n * Default set of header values which should be sent with every request unless\n * explicitly provided through apiFetch options.\n */\nconst DEFAULT_HEADERS: APIFetchOptions[ 'headers' ] = {\n\t// The backend uses the Accept header as a condition for considering an\n\t// incoming request as a REST request.\n\t//\n\t// See: https://core.trac.wordpress.org/ticket/44534\n\tAccept: 'application/json, */*;q=0.1',\n};\n\n/**\n * Default set of fetch option values which should be sent with every request\n * unless explicitly provided through apiFetch options.\n */\nconst DEFAULT_OPTIONS: APIFetchOptions = {\n\tcredentials: 'include',\n};\n\nconst middlewares: Array< APIFetchMiddleware > = [\n\tuserLocaleMiddleware,\n\tnamespaceEndpointMiddleware,\n\thttpV1Middleware,\n\tfetchAllMiddleware,\n];\n\n/**\n * Register a middleware\n *\n * @param middleware\n */\nfunction registerMiddleware( middleware: APIFetchMiddleware ) {\n\tmiddlewares.unshift( middleware );\n}\n\nconst defaultFetchHandler: FetchHandler = ( nextOptions ) => {\n\tconst { url, path, data, parse = true, ...remainingOptions } = nextOptions;\n\tlet { body, headers } = nextOptions;\n\n\t// Merge explicitly-provided headers with default values.\n\theaders = { ...DEFAULT_HEADERS, ...headers };\n\n\t// The `data` property is a shorthand for sending a JSON body.\n\tif ( data ) {\n\t\tbody = JSON.stringify( data );\n\t\theaders[ 'Content-Type' ] = 'application/json';\n\t}\n\n\tconst responsePromise = globalThis.fetch(\n\t\t// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.\n\t\turl || path || window.location.href,\n\t\t{\n\t\t\t...DEFAULT_OPTIONS,\n\t\t\t...remainingOptions,\n\t\t\tbody,\n\t\t\theaders,\n\t\t}\n\t);\n\n\treturn responsePromise.then(\n\t\t( response ) => {\n\t\t\t// If the response is not 2xx, still parse the response body as JSON\n\t\t\t// but throw the JSON as error.\n\t\t\tif ( ! response.ok ) {\n\t\t\t\treturn parseAndThrowError( response, parse );\n\t\t\t}\n\n\t\t\treturn parseResponseAndNormalizeError( response, parse );\n\t\t},\n\t\t( err ) => {\n\t\t\t// Re-throw AbortError for the users to handle it themselves.\n\t\t\tif ( err && err.name === 'AbortError' ) {\n\t\t\t\tthrow err;\n\t\t\t}\n\n\t\t\t// If the browser reports being offline, we'll just assume that\n\t\t\t// this is why the request failed.\n\t\t\tif ( ! globalThis.navigator.onLine ) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: 'offline_error',\n\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t'Unable to connect. Please check your Internet connection.'\n\t\t\t\t\t),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Hard to diagnose further due to how Window.fetch reports errors.\n\t\t\tthrow {\n\t\t\t\tcode: 'fetch_error',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'Could not get a valid response from the server.'\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\t);\n};\n\nlet fetchHandler = defaultFetchHandler;\n\n/**\n * Defines a custom fetch handler for making the requests that will override\n * the default one using window.fetch\n *\n * @param newFetchHandler The new fetch handler\n */\nfunction setFetchHandler( newFetchHandler: FetchHandler ) {\n\tfetchHandler = newFetchHandler;\n}\n\ninterface apiFetch {\n\t< T, Parse extends boolean = true >(\n\t\toptions: APIFetchOptions< Parse >\n\t): Promise< Parse extends true ? T : Response >;\n\tnonceEndpoint?: string;\n\tnonceMiddleware?: ReturnType< typeof createNonceMiddleware >;\n\tuse: ( middleware: APIFetchMiddleware ) => void;\n\tsetFetchHandler: ( newFetchHandler: FetchHandler ) => void;\n\tcreateNonceMiddleware: typeof createNonceMiddleware;\n\tcreatePreloadingMiddleware: typeof createPreloadingMiddleware;\n\tcreateRootURLMiddleware: typeof createRootURLMiddleware;\n\tfetchAllMiddleware: typeof fetchAllMiddleware;\n\tmediaUploadMiddleware: typeof mediaUploadMiddleware;\n\tcreateThemePreviewMiddleware: typeof createThemePreviewMiddleware;\n}\n\n/**\n * Fetch\n *\n * @param options The options for the fetch.\n * @return A promise representing the request processed via the registered middlewares.\n */\nconst apiFetch: apiFetch = ( options ) => {\n\t// creates a nested function chain that calls all middlewares and finally the `fetchHandler`,\n\t// converting `middlewares = [ m1, m2, m3 ]` into:\n\t// ```\n\t// opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );\n\t// ```\n\tconst enhancedHandler = middlewares.reduceRight< FetchHandler >(\n\t\t( next, middleware ) => {\n\t\t\treturn ( workingOptions ) => middleware( workingOptions, next );\n\t\t},\n\t\tfetchHandler\n\t);\n\n\treturn enhancedHandler( options ).catch( ( error ) => {\n\t\tif ( error.code !== 'rest_cookie_invalid_nonce' ) {\n\t\t\treturn Promise.reject( error );\n\t\t}\n\n\t\t// If the nonce is invalid, refresh it and try again.\n\t\treturn globalThis\n\t\t\t.fetch( apiFetch.nonceEndpoint! )\n\t\t\t.then( ( response ) => {\n\t\t\t\t// If the nonce refresh fails, it means we failed to recover from the original\n\t\t\t\t// `rest_cookie_invalid_nonce` error and that it's time to finally re-throw it.\n\t\t\t\tif ( ! response.ok ) {\n\t\t\t\t\treturn Promise.reject( error );\n\t\t\t\t}\n\n\t\t\t\treturn response.text();\n\t\t\t} )\n\t\t\t.then( ( text ) => {\n\t\t\t\tapiFetch.nonceMiddleware!.nonce = text;\n\t\t\t\treturn apiFetch( options );\n\t\t\t} );\n\t} );\n};\n\napiFetch.use = registerMiddleware;\napiFetch.setFetchHandler = setFetchHandler;\n\napiFetch.createNonceMiddleware = createNonceMiddleware;\napiFetch.createPreloadingMiddleware = createPreloadingMiddleware;\napiFetch.createRootURLMiddleware = createRootURLMiddleware;\napiFetch.fetchAllMiddleware = fetchAllMiddleware;\napiFetch.mediaUploadMiddleware = mediaUploadMiddleware;\napiFetch.createThemePreviewMiddleware = createThemePreviewMiddleware;\n\nexport default apiFetch;\nexport * from './types';\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,EAAE,QAAQ,iBAAiB;;AAEpC;AACA;AACA;AACA,OAAOC,qBAAqB,MAAM,qBAAqB;AACvD,OAAOC,uBAAuB,MAAM,wBAAwB;AAC5D,OAAOC,0BAA0B,MAAM,0BAA0B;AACjE,OAAOC,kBAAkB,MAAM,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M,CAAC;IAC/B;;IAEA;IACA,OAAOxB,UAAU,CACfC,KAAK,CAAEgB,QAAQ,CAACU,aAAe,CAAC,CAChCtB,IAAI,CAAIC,QAAQ,IAAM;MACtB;MACA;MACA,IAAK,CAAEA,QAAQ,CAACC,EAAE,EAAG;QACpB,OAAOkB,OAAO,CAACC,MAAM,CAAEF,KAAM,CAAC;MAC/B;MAEA,OAAOlB,QAAQ,CAACsB,IAAI,CAAC,CAAC;IACvB,CAAE,CAAC,CACFvB,IAAI,CAAIuB,IAAI,IAAM;MAClBX,QAAQ,CAACY,eAAe,CAAEC,KAAK,GAAGF,IAAI;MACtC,OAAOX,QAAQ,CAAEC,OAAQ,CAAC;IAC3B,CAAE,CAAC;EACL,CAAE,CAAC;AACJ,CAAC;AAEDD,QAAQ,CAACc,GAAG,GAAG9C,kBAAkB;AACjCgC,QAAQ,CAACF,eAAe,GAAGA,eAAe;AAE1CE,QAAQ,CAAChD,qBAAqB,GAAGA,qBAAqB;AACtDgD,QAAQ,CAAC9C,0BAA0B,GAAGA,0BAA0B;AAChE8C,QAAQ,CAAC/C,uBAAuB,GAAGA,uBAAuB;AAC1D+C,QAAQ,CAAC7C,kBAAkB,GAAGA,kBAAkB;AAChD6C,QAAQ,CAACzC,qBAAqB,GAAGA,qBAAqB;AACtDyC,QAAQ,CAACxC,4BAA4B,GAAGA,4BAA4B;AAEpE,eAAewC,QAAQ;AACvB,cAAc,SAAS","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport createNonceMiddleware from './middlewares/nonce';\nimport createRootURLMiddleware from './middlewares/root-url';\nimport createPreloadingMiddleware from './middlewares/preloading';\nimport fetchAllMiddleware from './middlewares/fetch-all-middleware';\nimport namespaceEndpointMiddleware from './middlewares/namespace-endpoint';\nimport httpV1Middleware from './middlewares/http-v1';\nimport userLocaleMiddleware from './middlewares/user-locale';\nimport mediaUploadMiddleware from './middlewares/media-upload';\nimport createThemePreviewMiddleware from './middlewares/theme-preview';\nimport {\n\tparseResponseAndNormalizeError,\n\tparseAndThrowError,\n} from './utils/response';\nimport type {\n\tAPIFetchMiddleware,\n\tAPIFetchOptions,\n\tFetchHandler,\n} from './types';\n\n/**\n * Default set of header values which should be sent with every request unless\n * explicitly provided through apiFetch options.\n */\nconst DEFAULT_HEADERS: APIFetchOptions[ 'headers' ] = {\n\t// The backend uses the Accept header as a condition for considering an\n\t// incoming request as a REST request.\n\t//\n\t// See: https://core.trac.wordpress.org/ticket/44534\n\tAccept: 'application/json, */*;q=0.1',\n};\n\n/**\n * Default set of fetch option values which should be sent with every request\n * unless explicitly provided through apiFetch options.\n */\nconst DEFAULT_OPTIONS: APIFetchOptions = {\n\tcredentials: 'include',\n};\n\nconst middlewares: Array< APIFetchMiddleware > = [\n\tuserLocaleMiddleware,\n\tnamespaceEndpointMiddleware,\n\thttpV1Middleware,\n\tfetchAllMiddleware,\n];\n\n/**\n * Register a middleware\n *\n * @param middleware\n */\nfunction registerMiddleware( middleware: APIFetchMiddleware ) {\n\tmiddlewares.unshift( middleware );\n}\n\nconst defaultFetchHandler: FetchHandler = ( nextOptions ) => {\n\tconst { url, path, data, parse = true, ...remainingOptions } = nextOptions;\n\tlet { body, headers } = nextOptions;\n\n\t// Merge explicitly-provided headers with default values.\n\theaders = { ...DEFAULT_HEADERS, ...headers };\n\n\t// The `data` property is a shorthand for sending a JSON body.\n\tif ( data ) {\n\t\tbody = JSON.stringify( data );\n\t\theaders[ 'Content-Type' ] = 'application/json';\n\t}\n\n\tconst responsePromise = globalThis.fetch(\n\t\t// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.\n\t\turl || path || window.location.href,\n\t\t{\n\t\t\t...DEFAULT_OPTIONS,\n\t\t\t...remainingOptions,\n\t\t\tbody,\n\t\t\theaders,\n\t\t}\n\t);\n\n\treturn responsePromise.then(\n\t\t( response ) => {\n\t\t\t// If the response is not 2xx, still parse the response body as JSON\n\t\t\t// but throw the JSON as error.\n\t\t\tif ( ! response.ok ) {\n\t\t\t\treturn parseAndThrowError( response, parse );\n\t\t\t}\n\n\t\t\treturn parseResponseAndNormalizeError( response, parse );\n\t\t},\n\t\t( err ) => {\n\t\t\t// Re-throw AbortError for the users to handle it themselves.\n\t\t\tif ( err && err.name === 'AbortError' ) {\n\t\t\t\tthrow err;\n\t\t\t}\n\n\t\t\t// If the browser reports being offline, we'll just assume that\n\t\t\t// this is why the request failed.\n\t\t\tif ( ! globalThis.navigator.onLine ) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: 'offline_error',\n\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t'Unable to connect. Please check your Internet connection.'\n\t\t\t\t\t),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Hard to diagnose further due to how Window.fetch reports errors.\n\t\t\tthrow {\n\t\t\t\tcode: 'fetch_error',\n\t\t\t\tmessage: __(\n\t\t\t\t\t'Could not get a valid response from the server.'\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\t);\n};\n\nlet fetchHandler = defaultFetchHandler;\n\n/**\n * Defines a custom fetch handler for making the requests that will override\n * the default one using window.fetch\n *\n * @param newFetchHandler The new fetch handler\n */\nfunction setFetchHandler( newFetchHandler: FetchHandler ) {\n\tfetchHandler = newFetchHandler;\n}\n\ninterface apiFetch {\n\t< T, Parse extends boolean = true >(\n\t\toptions: APIFetchOptions< Parse >\n\t): Promise< Parse extends true ? T : Response >;\n\tnonceEndpoint?: string;\n\tnonceMiddleware?: ReturnType< typeof createNonceMiddleware >;\n\tuse: ( middleware: APIFetchMiddleware ) => void;\n\tsetFetchHandler: ( newFetchHandler: FetchHandler ) => void;\n\tcreateNonceMiddleware: typeof createNonceMiddleware;\n\tcreatePreloadingMiddleware: typeof createPreloadingMiddleware;\n\tcreateRootURLMiddleware: typeof createRootURLMiddleware;\n\tfetchAllMiddleware: typeof fetchAllMiddleware;\n\tmediaUploadMiddleware: typeof mediaUploadMiddleware;\n\tcreateThemePreviewMiddleware: typeof createThemePreviewMiddleware;\n}\n\n/**\n * Fetch\n *\n * @param options The options for the fetch.\n * @return A promise representing the request processed via the registered middlewares.\n */\nconst apiFetch: apiFetch = ( options ) => {\n\t// creates a nested function chain that calls all middlewares and finally the `fetchHandler`,\n\t// converting `middlewares = [ m1, m2, m3 ]` into:\n\t// ```\n\t// opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );\n\t// ```\n\tconst enhancedHandler = middlewares.reduceRight< FetchHandler >(\n\t\t( next, middleware ) => {\n\t\t\treturn ( workingOptions ) => middleware( workingOptions, next );\n\t\t},\n\t\tfetchHandler\n\t);\n\n\treturn enhancedHandler( options ).catch( ( error ) => {\n\t\tif ( error.code !== 'rest_cookie_invalid_nonce' ) {\n\t\t\treturn Promise.reject( error );\n\t\t}\n\n\t\t// If the nonce is invalid, refresh it and try again.\n\t\treturn globalThis\n\t\t\t.fetch( apiFetch.nonceEndpoint! )\n\t\t\t.then( ( response ) => {\n\t\t\t\t// If the nonce refresh fails, it means we failed to recover from the original\n\t\t\t\t// `rest_cookie_invalid_nonce` error and that it's time to finally re-throw it.\n\t\t\t\tif ( ! response.ok ) {\n\t\t\t\t\treturn Promise.reject( error );\n\t\t\t\t}\n\n\t\t\t\treturn response.text();\n\t\t\t} )\n\t\t\t.then( ( text ) => {\n\t\t\t\tapiFetch.nonceMiddleware!.nonce = text;\n\t\t\t\treturn apiFetch( options );\n\t\t\t} );\n\t} );\n};\n\napiFetch.use = registerMiddleware;\napiFetch.setFetchHandler = setFetchHandler;\n\napiFetch.createNonceMiddleware = createNonceMiddleware;\napiFetch.createPreloadingMiddleware = createPreloadingMiddleware;\napiFetch.createRootURLMiddleware = createRootURLMiddleware;\napiFetch.fetchAllMiddleware = fetchAllMiddleware;\napiFetch.mediaUploadMiddleware = mediaUploadMiddleware;\napiFetch.createThemePreviewMiddleware = createThemePreviewMiddleware;\n\nexport default apiFetch;\nexport * from './types';\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,UAAU;AAKnB,OAAO,2BAA2B;AAClC,OAAO,6BAA6B;AACpC,OAAO,gCAAgC;AACvC,OAAO,wBAAwB;AAC/B,OAAO,iCAAiC;AACxC,OAAO,sBAAsB;AAC7B,OAAO,0BAA0B;AACjC,OAAO,2BAA2B;AAClC,OAAO,kCAAkC;AACzC;AAAA,EACC;AAAA,EACA;AAAA,OACM;AAWP,MAAM,kBAAgD;AAAA;AAAA;AAAA;AAAA;AAAA,EAKrD,QAAQ;AACT;AAMA,MAAM,kBAAmC;AAAA,EACxC,aAAa;AACd;AAEA,MAAM,cAA2C;AAAA,EAChD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAOA,SAAS,mBAAoB,YAAiC;AAC7D,cAAY,QAAS,UAAW;AACjC;AAEA,MAAM,sBAAoC,CAAE,gBAAiB;AAC5D,QAAM,EAAE,KAAK,MAAM,MAAM,QAAQ,MAAM,GAAG,iBAAiB,IAAI;AAC/D,MAAI,EAAE,MAAM,QAAQ,IAAI;AAGxB,YAAU,EAAE,GAAG,iBAAiB,GAAG,QAAQ;AAG3C,MAAK,MAAO;AACX,WAAO,KAAK,UAAW,IAAK;AAC5B,YAAS,cAAe,IAAI;AAAA,EAC7B;AAEA,QAAM,kBAAkB,WAAW;AAAA;AAAA,IAElC,OAAO,QAAQ,OAAO,SAAS;AAAA,IAC/B;AAAA,MACC,GAAG;AAAA,MACH,GAAG;AAAA,MACH;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,SAAO,gBAAgB;AAAA,IACtB,CAAE,aAAc;AAGf,UAAK,CAAE,SAAS,IAAK;AACpB,eAAO,mBAAoB,UAAU,KAAM;AAAA,MAC5C;AAEA,aAAO,+BAAgC,UAAU,KAAM;AAAA,IACxD;AAAA,IACA,CAAE,QAAS;AAEV,UAAK,OAAO,IAAI,SAAS,c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