@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
package/CHANGELOG.md
CHANGED
package/build/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
+
var _exportNames = {};
|
|
7
8
|
exports.default = void 0;
|
|
8
9
|
var _i18n = require("@wordpress/i18n");
|
|
9
10
|
var _nonce = _interopRequireDefault(require("./middlewares/nonce"));
|
|
@@ -16,6 +17,18 @@ var _userLocale = _interopRequireDefault(require("./middlewares/user-locale"));
|
|
|
16
17
|
var _mediaUpload = _interopRequireDefault(require("./middlewares/media-upload"));
|
|
17
18
|
var _themePreview = _interopRequireDefault(require("./middlewares/theme-preview"));
|
|
18
19
|
var _response = require("./utils/response");
|
|
20
|
+
var _types = require("./types");
|
|
21
|
+
Object.keys(_types).forEach(function (key) {
|
|
22
|
+
if (key === "default" || key === "__esModule") return;
|
|
23
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
24
|
+
if (key in exports && exports[key] === _types[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _types[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
19
32
|
/**
|
|
20
33
|
* WordPress dependencies
|
|
21
34
|
*/
|
|
@@ -27,8 +40,6 @@ var _response = require("./utils/response");
|
|
|
27
40
|
/**
|
|
28
41
|
* Default set of header values which should be sent with every request unless
|
|
29
42
|
* explicitly provided through apiFetch options.
|
|
30
|
-
*
|
|
31
|
-
* @type {Record<string, string>}
|
|
32
43
|
*/
|
|
33
44
|
const DEFAULT_HEADERS = {
|
|
34
45
|
// The backend uses the Accept header as a condition for considering an
|
|
@@ -41,49 +52,20 @@ const DEFAULT_HEADERS = {
|
|
|
41
52
|
/**
|
|
42
53
|
* Default set of fetch option values which should be sent with every request
|
|
43
54
|
* unless explicitly provided through apiFetch options.
|
|
44
|
-
*
|
|
45
|
-
* @type {Object}
|
|
46
55
|
*/
|
|
47
56
|
const DEFAULT_OPTIONS = {
|
|
48
57
|
credentials: 'include'
|
|
49
58
|
};
|
|
50
|
-
|
|
51
|
-
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
|
|
52
|
-
/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @type {import('./types').APIFetchMiddleware[]}
|
|
56
|
-
*/
|
|
57
59
|
const middlewares = [_userLocale.default, _namespaceEndpoint.default, _httpV.default, _fetchAllMiddleware.default];
|
|
58
60
|
|
|
59
61
|
/**
|
|
60
62
|
* Register a middleware
|
|
61
63
|
*
|
|
62
|
-
* @param
|
|
64
|
+
* @param middleware
|
|
63
65
|
*/
|
|
64
66
|
function registerMiddleware(middleware) {
|
|
65
67
|
middlewares.unshift(middleware);
|
|
66
68
|
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Checks the status of a response, throwing the Response as an error if
|
|
70
|
-
* it is outside the 200 range.
|
|
71
|
-
*
|
|
72
|
-
* @param {Response} response
|
|
73
|
-
* @return {Response} The response if the status is in the 200 range.
|
|
74
|
-
*/
|
|
75
|
-
const checkStatus = response => {
|
|
76
|
-
if (response.status >= 200 && response.status < 300) {
|
|
77
|
-
return response;
|
|
78
|
-
}
|
|
79
|
-
throw response;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @type {FetchHandler}
|
|
86
|
-
*/
|
|
87
69
|
const defaultFetchHandler = nextOptions => {
|
|
88
70
|
const {
|
|
89
71
|
url,
|
|
@@ -108,7 +90,7 @@ const defaultFetchHandler = nextOptions => {
|
|
|
108
90
|
body = JSON.stringify(data);
|
|
109
91
|
headers['Content-Type'] = 'application/json';
|
|
110
92
|
}
|
|
111
|
-
const responsePromise =
|
|
93
|
+
const responsePromise = globalThis.fetch(
|
|
112
94
|
// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
|
|
113
95
|
url || path || window.location.href, {
|
|
114
96
|
...DEFAULT_OPTIONS,
|
|
@@ -116,46 +98,59 @@ const defaultFetchHandler = nextOptions => {
|
|
|
116
98
|
body,
|
|
117
99
|
headers
|
|
118
100
|
});
|
|
119
|
-
return responsePromise.then(
|
|
101
|
+
return responsePromise.then(response => {
|
|
102
|
+
// If the response is not 2xx, still parse the response body as JSON
|
|
103
|
+
// but throw the JSON as error.
|
|
104
|
+
if (!response.ok) {
|
|
105
|
+
return (0, _response.parseAndThrowError)(response, parse);
|
|
106
|
+
}
|
|
107
|
+
return (0, _response.parseResponseAndNormalizeError)(response, parse);
|
|
108
|
+
}, err => {
|
|
120
109
|
// Re-throw AbortError for the users to handle it themselves.
|
|
121
110
|
if (err && err.name === 'AbortError') {
|
|
122
111
|
throw err;
|
|
123
112
|
}
|
|
124
113
|
|
|
125
|
-
//
|
|
126
|
-
//
|
|
114
|
+
// If the browser reports being offline, we'll just assume that
|
|
115
|
+
// this is why the request failed.
|
|
116
|
+
if (!globalThis.navigator.onLine) {
|
|
117
|
+
throw {
|
|
118
|
+
code: 'offline_error',
|
|
119
|
+
message: (0, _i18n.__)('Unable to connect. Please check your Internet connection.')
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Hard to diagnose further due to how Window.fetch reports errors.
|
|
127
124
|
throw {
|
|
128
125
|
code: 'fetch_error',
|
|
129
|
-
message: (0, _i18n.__)('
|
|
126
|
+
message: (0, _i18n.__)('Could not get a valid response from the server.')
|
|
130
127
|
};
|
|
131
128
|
});
|
|
132
129
|
};
|
|
133
|
-
|
|
134
|
-
/** @type {FetchHandler} */
|
|
135
130
|
let fetchHandler = defaultFetchHandler;
|
|
136
131
|
|
|
137
132
|
/**
|
|
138
133
|
* Defines a custom fetch handler for making the requests that will override
|
|
139
134
|
* the default one using window.fetch
|
|
140
135
|
*
|
|
141
|
-
* @param
|
|
136
|
+
* @param newFetchHandler The new fetch handler
|
|
142
137
|
*/
|
|
143
138
|
function setFetchHandler(newFetchHandler) {
|
|
144
139
|
fetchHandler = newFetchHandler;
|
|
145
140
|
}
|
|
146
|
-
|
|
147
141
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
* @
|
|
142
|
+
* Fetch
|
|
143
|
+
*
|
|
144
|
+
* @param options The options for the fetch.
|
|
145
|
+
* @return A promise representing the request processed via the registered middlewares.
|
|
151
146
|
*/
|
|
152
|
-
|
|
147
|
+
const apiFetch = options => {
|
|
153
148
|
// creates a nested function chain that calls all middlewares and finally the `fetchHandler`,
|
|
154
149
|
// converting `middlewares = [ m1, m2, m3 ]` into:
|
|
155
150
|
// ```
|
|
156
151
|
// opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );
|
|
157
152
|
// ```
|
|
158
|
-
const enhancedHandler = middlewares.reduceRight((
|
|
153
|
+
const enhancedHandler = middlewares.reduceRight((next, middleware) => {
|
|
159
154
|
return workingOptions => middleware(workingOptions, next);
|
|
160
155
|
}, fetchHandler);
|
|
161
156
|
return enhancedHandler(options).catch(error => {
|
|
@@ -164,15 +159,19 @@ function apiFetch(options) {
|
|
|
164
159
|
}
|
|
165
160
|
|
|
166
161
|
// If the nonce is invalid, refresh it and try again.
|
|
167
|
-
return
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
162
|
+
return globalThis.fetch(apiFetch.nonceEndpoint).then(response => {
|
|
163
|
+
// If the nonce refresh fails, it means we failed to recover from the original
|
|
164
|
+
// `rest_cookie_invalid_nonce` error and that it's time to finally re-throw it.
|
|
165
|
+
if (!response.ok) {
|
|
166
|
+
return Promise.reject(error);
|
|
167
|
+
}
|
|
168
|
+
return response.text();
|
|
169
|
+
}).then(text => {
|
|
171
170
|
apiFetch.nonceMiddleware.nonce = text;
|
|
172
171
|
return apiFetch(options);
|
|
173
172
|
});
|
|
174
173
|
});
|
|
175
|
-
}
|
|
174
|
+
};
|
|
176
175
|
apiFetch.use = registerMiddleware;
|
|
177
176
|
apiFetch.setFetchHandler = setFetchHandler;
|
|
178
177
|
apiFetch.createNonceMiddleware = _nonce.default;
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_i18n","require","_nonce","_interopRequireDefault","_rootUrl","_preloading","_fetchAllMiddleware","_namespaceEndpoint","_httpV","_userLocale","_mediaUpload","_themePreview","_response","DEFAULT_HEADERS","Accept","DEFAULT_OPTIONS","credentials","middlewares","userLocaleMiddleware","namespaceEndpointMiddleware","httpV1Middleware","fetchAllMiddleware","registerMiddleware","middleware","unshift","checkStatus","response","status","defaultFetchHandler","nextOptions","url","path","data","parse","remainingOptions","body","headers","JSON","stringify","responsePromise","window","fetch","location","href","then","value","Promise","resolve","catch","parseAndThrowError","parseResponseAndNormalizeError","err","name","code","message","__","fetchHandler","setFetchHandler","newFetchHandler","apiFetch","options","enhancedHandler","reduceRight","next","workingOptions","error","reject","nonceEndpoint","text","nonceMiddleware","nonce","use","createNonceMiddleware","createPreloadingMiddleware","createRootURLMiddleware","mediaUploadMiddleware","createThemePreviewMiddleware","_default","exports","default"],"sources":["@wordpress/api-fetch/src/index.js"],"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';\n\n/**\n * Default set of header values which should be sent with every request unless\n * explicitly provided through apiFetch options.\n *\n * @type {Record<string, string>}\n */\nconst DEFAULT_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 *\n * @type {Object}\n */\nconst DEFAULT_OPTIONS = {\n\tcredentials: 'include',\n};\n\n/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */\n/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */\n\n/**\n * @type {import('./types').APIFetchMiddleware[]}\n */\nconst middlewares = [\n\tuserLocaleMiddleware,\n\tnamespaceEndpointMiddleware,\n\thttpV1Middleware,\n\tfetchAllMiddleware,\n];\n\n/**\n * Register a middleware\n *\n * @param {import('./types').APIFetchMiddleware} middleware\n */\nfunction registerMiddleware( middleware ) {\n\tmiddlewares.unshift( middleware );\n}\n\n/**\n * Checks the status of a response, throwing the Response as an error if\n * it is outside the 200 range.\n *\n * @param {Response} response\n * @return {Response} The response if the status is in the 200 range.\n */\nconst checkStatus = ( response ) => {\n\tif ( response.status >= 200 && response.status < 300 ) {\n\t\treturn response;\n\t}\n\n\tthrow response;\n};\n\n/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/\n\n/**\n * @type {FetchHandler}\n */\nconst defaultFetchHandler = ( 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 = window.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( value ) =>\n\t\t\tPromise.resolve( value )\n\t\t\t\t.then( checkStatus )\n\t\t\t\t.catch( ( response ) => parseAndThrowError( response, parse ) )\n\t\t\t\t.then( ( response ) =>\n\t\t\t\t\tparseResponseAndNormalizeError( response, parse )\n\t\t\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// Otherwise, there is most likely no network connection.\n\t\t\t// Unfortunately the message might depend on the browser.\n\t\t\tthrow {\n\t\t\t\tcode: 'fetch_error',\n\t\t\t\tmessage: __( 'You are probably offline.' ),\n\t\t\t};\n\t\t}\n\t);\n};\n\n/** @type {FetchHandler} */\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 {FetchHandler} newFetchHandler The new fetch handler\n */\nfunction setFetchHandler( newFetchHandler ) {\n\tfetchHandler = newFetchHandler;\n}\n\n/**\n * @template T\n * @param {import('./types').APIFetchOptions} options\n * @return {Promise<T>} A promise representing the request processed via the registered middlewares.\n */\nfunction 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(\n\t\t( /** @type {FetchHandler} */ 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 (\n\t\t\twindow\n\t\t\t\t// @ts-ignore\n\t\t\t\t.fetch( apiFetch.nonceEndpoint )\n\t\t\t\t.then( checkStatus )\n\t\t\t\t.then( ( data ) => data.text() )\n\t\t\t\t.then( ( text ) => {\n\t\t\t\t\t// @ts-ignore\n\t\t\t\t\tapiFetch.nonceMiddleware.nonce = text;\n\t\t\t\t\treturn apiFetch( options );\n\t\t\t\t} )\n\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;\n"],"mappings":";;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,QAAA,GAAAD,sBAAA,CAAAF,OAAA;AACA,IAAAI,WAAA,GAAAF,sBAAA,CAAAF,OAAA;AACA,IAAAK,mBAAA,GAAAH,sBAAA,CAAAF,OAAA;AACA,IAAAM,kBAAA,GAAAJ,sBAAA,CAAAF,OAAA;AACA,IAAAO,MAAA,GAAAL,sBAAA,CAAAF,OAAA;AACA,IAAAQ,WAAA,GAAAN,sBAAA,CAAAF,OAAA;AACA,IAAAS,YAAA,GAAAP,sBAAA,CAAAF,OAAA;AACA,IAAAU,aAAA,GAAAR,sBAAA,CAAAF,OAAA;AACA,IAAAW,SAAA,GAAAX,OAAA;AAjBA;AACA;AACA;;AAGA;AACA;AACA;;AAeA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMY,eAAe,GAAG;EACvB;EACA;EACA;EACA;EACAC,MAAM,EAAE;AACT,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,eAAe,GAAG;EACvBC,WAAW,EAAE;AACd,CAAC;;AAED;AACA;;AAEA;AACA;AACA;AACA,MAAMC,WAAW,GAAG,CACnBC,mBAAoB,EACpBC,0BAA2B,EAC3BC,cAAgB,EAChBC,2BAAkB,CAClB;;AAED;AACA;AACA;AACA;AACA;AACA,SAASC,kBAAkBA,CAAEC,UAAU,EAAG;EACzCN,WAAW,CAACO,OAAO,CAAED,UAAW,CAAC;AAClC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAME,WAAW,GAAKC,QAAQ,IAAM;EACnC,IAAKA,QAAQ,CAACC,MAAM,IAAI,GAAG,IAAID,QAAQ,CAACC,MAAM,GAAG,GAAG,EAAG;IACtD,OAAOD,QAAQ;EAChB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A,2BAAkB;AAChDsC,QAAQ,CAACgB,qBAAqB,GAAGA,oBAAqB;AACtDhB,QAAQ,CAACiB,4BAA4B,GAAGA,qBAA4B;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEtDpB,QAAQ","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_i18n","require","_nonce","_interopRequireDefault","_rootUrl","_preloading","_fetchAllMiddleware","_namespaceEndpoint","_httpV","_userLocale","_mediaUpload","_themePreview","_response","_types","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","DEFAULT_HEADERS","Accept","DEFAULT_OPTIONS","credentials","middlewares","userLocaleMiddleware","namespaceEndpointMiddleware","httpV1Middleware","fetchAllMiddleware","registerMiddleware","middleware","unshift","defaultFetchHandler","nextOptions","url","path","data","parse","remainingOptions","body","headers","JSON","stringify","responsePromise","globalThis","fetch","window","location","href","then","response","ok","parseAndThrowError","parseResponseAndNormalizeError","err","name","navigator","onLine","code","message","__","fetchHandler","setFetchHandler","newFetchHandler","apiFetch","options","enhancedHandler","reduceRight","next","workingOptions","catch","error","Promise","reject","nonceEndpoint","text","nonceMiddleware","nonce","use","createNonceMiddleware","createPreloadingMiddleware","createRootURLMiddleware","mediaUploadMiddleware","createThemePreviewMiddleware","_default","default"],"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":";;;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,MAA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ACF,eAAe,GAAGA,eAAe;AAE1CE,QAAQ,CAACe,qBAAqB,GAAGA,cAAqB;AACtDf,QAAQ,CAACgB,0BAA0B,GAAGA,mBAA0B;AAChEhB,QAAQ,CAACiB,uBAAuB,GAAGA,gBAAuB;AAC1DjB,QAAQ,CAACpC,kBAAkB,GAAGA,2BAAkB;AAChDoC,QAAQ,CAACkB,qBAAqB,GAAGA,oBAAqB;AACtDlB,QAAQ,CAACmB,4BAA4B,GAAGA,qBAA4B;AAAC,IAAAC,QAAA,GAAApE,OAAA,CAAAqE,OAAA,GAEtDrB,QAAQ","ignoreList":[]}
|
|
@@ -18,9 +18,9 @@ var _ = _interopRequireDefault(require(".."));
|
|
|
18
18
|
/**
|
|
19
19
|
* Apply query arguments to both URL and Path, whichever is present.
|
|
20
20
|
*
|
|
21
|
-
* @param {
|
|
22
|
-
* @param {Record<string, string | number>}
|
|
23
|
-
* @return
|
|
21
|
+
* @param {APIFetchOptions} props The request options
|
|
22
|
+
* @param {Record< string, string | number >} queryArgs
|
|
23
|
+
* @return The request with the modified query args
|
|
24
24
|
*/
|
|
25
25
|
const modifyQuery = ({
|
|
26
26
|
path,
|
|
@@ -35,14 +35,14 @@ const modifyQuery = ({
|
|
|
35
35
|
/**
|
|
36
36
|
* Duplicates parsing functionality from apiFetch.
|
|
37
37
|
*
|
|
38
|
-
* @param
|
|
39
|
-
* @return
|
|
38
|
+
* @param response
|
|
39
|
+
* @return Parsed response json.
|
|
40
40
|
*/
|
|
41
41
|
const parseResponse = response => response.json ? response.json() : Promise.reject(response);
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
* @param
|
|
45
|
-
* @return
|
|
44
|
+
* @param linkHeader
|
|
45
|
+
* @return The parsed link header.
|
|
46
46
|
*/
|
|
47
47
|
const parseLinkHeader = linkHeader => {
|
|
48
48
|
if (!linkHeader) {
|
|
@@ -55,8 +55,8 @@ const parseLinkHeader = linkHeader => {
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
* @param
|
|
59
|
-
* @return
|
|
58
|
+
* @param response
|
|
59
|
+
* @return The next page URL.
|
|
60
60
|
*/
|
|
61
61
|
const getNextPageUrl = response => {
|
|
62
62
|
const {
|
|
@@ -66,8 +66,8 @@ const getNextPageUrl = response => {
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
* @param
|
|
70
|
-
* @return
|
|
69
|
+
* @param options
|
|
70
|
+
* @return True if the request contains an unbounded query.
|
|
71
71
|
*/
|
|
72
72
|
const requestContainsUnboundedQuery = options => {
|
|
73
73
|
const pathIsUnbounded = !!options.path && options.path.indexOf('per_page=-1') !== -1;
|
|
@@ -79,8 +79,8 @@ const requestContainsUnboundedQuery = options => {
|
|
|
79
79
|
* The REST API enforces an upper limit on the per_page option. To handle large
|
|
80
80
|
* collections, apiFetch consumers can pass `per_page=-1`; this middleware will
|
|
81
81
|
* then recursively assemble a full response array from all available pages.
|
|
82
|
-
*
|
|
83
|
-
* @
|
|
82
|
+
* @param options
|
|
83
|
+
* @param next
|
|
84
84
|
*/
|
|
85
85
|
const fetchAllMiddleware = async (options, next) => {
|
|
86
86
|
if (options.parse === false) {
|
|
@@ -112,7 +112,7 @@ const fetchAllMiddleware = async (options, next) => {
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
// Iteratively fetch all remaining pages until no "next" header is found.
|
|
115
|
-
let mergedResults =
|
|
115
|
+
let mergedResults = [].concat(results);
|
|
116
116
|
while (nextPage) {
|
|
117
117
|
const nextResponse = await (0, _.default)({
|
|
118
118
|
...options,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_url","require","_","_interopRequireDefault","modifyQuery","path","url","options","queryArgs","addQueryArgs","parseResponse","response","json","Promise","reject","parseLinkHeader","linkHeader","match","next","getNextPageUrl","headers","get","requestContainsUnboundedQuery","pathIsUnbounded","indexOf","urlIsUnbounded","fetchAllMiddleware","parse","apiFetch","per_page","results","Array","isArray","nextPage","mergedResults","concat","nextResponse","undefined","nextResults","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/fetch-all-middleware.
|
|
1
|
+
{"version":3,"names":["_url","require","_","_interopRequireDefault","modifyQuery","path","url","options","queryArgs","addQueryArgs","parseResponse","response","json","Promise","reject","parseLinkHeader","linkHeader","match","next","getNextPageUrl","headers","get","requestContainsUnboundedQuery","pathIsUnbounded","indexOf","urlIsUnbounded","fetchAllMiddleware","parse","apiFetch","per_page","results","Array","isArray","nextPage","mergedResults","concat","nextResponse","undefined","nextResults","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/fetch-all-middleware.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport apiFetch from '..';\nimport type { APIFetchMiddleware, APIFetchOptions } from '../types';\n\n/**\n * Apply query arguments to both URL and Path, whichever is present.\n *\n * @param {APIFetchOptions} props The request options\n * @param {Record< string, string | number >} queryArgs\n * @return The request with the modified query args\n */\nconst modifyQuery = (\n\t{ path, url, ...options }: APIFetchOptions,\n\tqueryArgs: Record< string, string | number >\n): APIFetchOptions => ( {\n\t...options,\n\turl: url && addQueryArgs( url, queryArgs ),\n\tpath: path && addQueryArgs( path, queryArgs ),\n} );\n\n/**\n * Duplicates parsing functionality from apiFetch.\n *\n * @param response\n * @return Parsed response json.\n */\nconst parseResponse = ( response: Response ) =>\n\tresponse.json ? response.json() : Promise.reject( response );\n\n/**\n * @param linkHeader\n * @return The parsed link header.\n */\nconst parseLinkHeader = ( linkHeader: string | null ) => {\n\tif ( ! linkHeader ) {\n\t\treturn {};\n\t}\n\tconst match = linkHeader.match( /<([^>]+)>; rel=\"next\"/ );\n\treturn match\n\t\t? {\n\t\t\t\tnext: match[ 1 ],\n\t\t }\n\t\t: {};\n};\n\n/**\n * @param response\n * @return The next page URL.\n */\nconst getNextPageUrl = ( response: Response ) => {\n\tconst { next } = parseLinkHeader( response.headers.get( 'link' ) );\n\treturn next;\n};\n\n/**\n * @param options\n * @return True if the request contains an unbounded query.\n */\nconst requestContainsUnboundedQuery = ( options: APIFetchOptions ) => {\n\tconst pathIsUnbounded =\n\t\t!! options.path && options.path.indexOf( 'per_page=-1' ) !== -1;\n\tconst urlIsUnbounded =\n\t\t!! options.url && options.url.indexOf( 'per_page=-1' ) !== -1;\n\treturn pathIsUnbounded || urlIsUnbounded;\n};\n\n/**\n * The REST API enforces an upper limit on the per_page option. To handle large\n * collections, apiFetch consumers can pass `per_page=-1`; this middleware will\n * then recursively assemble a full response array from all available pages.\n * @param options\n * @param next\n */\nconst fetchAllMiddleware: APIFetchMiddleware = async ( options, next ) => {\n\tif ( options.parse === false ) {\n\t\t// If a consumer has opted out of parsing, do not apply middleware.\n\t\treturn next( options );\n\t}\n\tif ( ! requestContainsUnboundedQuery( options ) ) {\n\t\t// If neither url nor path is requesting all items, do not apply middleware.\n\t\treturn next( options );\n\t}\n\n\t// Retrieve requested page of results.\n\tconst response = await apiFetch( {\n\t\t...modifyQuery( options, {\n\t\t\tper_page: 100,\n\t\t} ),\n\t\t// Ensure headers are returned for page 1.\n\t\tparse: false,\n\t} );\n\n\tconst results = await parseResponse( response );\n\n\tif ( ! Array.isArray( results ) ) {\n\t\t// We have no reliable way of merging non-array results.\n\t\treturn results;\n\t}\n\n\tlet nextPage = getNextPageUrl( response );\n\n\tif ( ! nextPage ) {\n\t\t// There are no further pages to request.\n\t\treturn results;\n\t}\n\n\t// Iteratively fetch all remaining pages until no \"next\" header is found.\n\tlet mergedResults = ( [] as Array< any > ).concat( results );\n\twhile ( nextPage ) {\n\t\tconst nextResponse = await apiFetch( {\n\t\t\t...options,\n\t\t\t// Ensure the URL for the next page is used instead of any provided path.\n\t\t\tpath: undefined,\n\t\t\turl: nextPage,\n\t\t\t// Ensure we still get headers so we can identify the next page.\n\t\t\tparse: false,\n\t\t} );\n\t\tconst nextResults = await parseResponse( nextResponse );\n\t\tmergedResults = mergedResults.concat( nextResults );\n\t\tnextPage = getNextPageUrl( nextResponse );\n\t}\n\treturn mergedResults;\n};\n\nexport default fetchAllMiddleware;\n"],"mappings":";;;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAKA,IAAAC,CAAA,GAAAC,sBAAA,CAAAF,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMG,WAAW,GAAGA,CACnB;EAAEC,IAAI;EAAEC,GAAG;EAAE,GAAGC;AAAyB,CAAC,EAC1CC,SAA4C,MACrB;EACvB,GAAGD,OAAO;EACVD,GAAG,EAAEA,GAAG,IAAI,IAAAG,iBAAY,EAAEH,GAAG,EAAEE,SAAU,CAAC;EAC1CH,IAAI,EAAEA,IAAI,IAAI,IAAAI,iBAAY,EAAEJ,IAAI,EAAEG,SAAU;AAC7C,CAAC,CAAE;;AAEH;AACA;AACA;AACA;AACA;AACA;AACA,MAAME,aAAa,GAAKC,QAAkB,IACzCA,QAAQ,CAACC,IAAI,GAAGD,QAAQ,CAACC,IAAI,CAAC,CAAC,GAAGC,OAAO,CAACC,MAAM,CAAEH,QAAS,CAAC;;AAE7D;AACA;AACA;AACA;AACA,MAAMI,eAAe,GAAKC,UAAyB,IAAM;EACxD,IAAK,CAAEA,UAAU,EAAG;IACnB,OAAO,CAAC,CAAC;EACV;EACA,MAAMC,KAAK,GAAGD,UAAU,CAACC,KAAK,CAAE,uBAAwB,CAAC;EACzD,OAAOA,KAAK,GACT;IACAC,IAAI,EAAED,KAAK,CAAE,CAAC;EACd,CAAC,GACD,CAAC,CAAC;AACN,CAAC;;AAED;AACA;AACA;AACA;AACA,MAAME,cAAc,GAAKR,QAAkB,IAAM;EAChD,MAAM;IAAEO;EAAK,CAAC,GAAGH,eAAe,CAAEJ,QAAQ,CAACS,OAAO,CAACC,GAAG,CAAE,MAAO,CAAE,CAAC;EAClE,OAAOH,IAAI;AACZ,CAAC;;AAED;AACA;AACA;AACA;AACA,MAAMI,6BAA6B,G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AE;MACpC,GAAGrB,OAAO;MACV;MACAF,IAAI,EAAEgC,SAAS;MACf/B,GAAG,EAAE2B,QAAQ;MACb;MACAN,KAAK,EAAE;IACR,CAAE,CAAC;IACH,MAAMW,WAAW,GAAG,MAAM5B,aAAa,CAAE0B,YAAa,CAAC;IACvDF,aAAa,GAAGA,aAAa,CAACC,MAAM,CAAEG,WAAY,CAAC;IACnDL,QAAQ,GAAGd,cAAc,CAAEiB,YAAa,CAAC;EAC1C;EACA,OAAOF,aAAa;AACrB,CAAC;AAAC,IAAAK,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaf,kBAAkB","ignoreList":[]}
|
|
@@ -4,10 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
|
|
7
11
|
/**
|
|
8
12
|
* Set of HTTP methods which are eligible to be overridden.
|
|
9
|
-
*
|
|
10
|
-
* @type {Set<string>}
|
|
11
13
|
*/
|
|
12
14
|
const OVERRIDE_METHODS = new Set(['PATCH', 'PUT', 'DELETE']);
|
|
13
15
|
|
|
@@ -18,8 +20,6 @@ const OVERRIDE_METHODS = new Set(['PATCH', 'PUT', 'DELETE']);
|
|
|
18
20
|
* is `GET`."
|
|
19
21
|
*
|
|
20
22
|
* @see https://fetch.spec.whatwg.org/#requests
|
|
21
|
-
*
|
|
22
|
-
* @type {string}
|
|
23
23
|
*/
|
|
24
24
|
const DEFAULT_METHOD = 'GET';
|
|
25
25
|
|
|
@@ -27,7 +27,8 @@ const DEFAULT_METHOD = 'GET';
|
|
|
27
27
|
* API Fetch middleware which overrides the request method for HTTP v1
|
|
28
28
|
* compatibility leveraging the REST API X-HTTP-Method-Override header.
|
|
29
29
|
*
|
|
30
|
-
* @
|
|
30
|
+
* @param options
|
|
31
|
+
* @param next
|
|
31
32
|
*/
|
|
32
33
|
const httpV1Middleware = (options, next) => {
|
|
33
34
|
const {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["OVERRIDE_METHODS","Set","DEFAULT_METHOD","httpV1Middleware","options","next","method","has","toUpperCase","headers","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/http-v1.
|
|
1
|
+
{"version":3,"names":["OVERRIDE_METHODS","Set","DEFAULT_METHOD","httpV1Middleware","options","next","method","has","toUpperCase","headers","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/http-v1.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\n/**\n * Set of HTTP methods which are eligible to be overridden.\n */\nconst OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );\n\n/**\n * Default request method.\n *\n * \"A request has an associated method (a method). Unless stated otherwise it\n * is `GET`.\"\n *\n * @see https://fetch.spec.whatwg.org/#requests\n */\nconst DEFAULT_METHOD = 'GET';\n\n/**\n * API Fetch middleware which overrides the request method for HTTP v1\n * compatibility leveraging the REST API X-HTTP-Method-Override header.\n *\n * @param options\n * @param next\n */\nconst httpV1Middleware: APIFetchMiddleware = ( options, next ) => {\n\tconst { method = DEFAULT_METHOD } = options;\n\tif ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {\n\t\toptions = {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...options.headers,\n\t\t\t\t'X-HTTP-Method-Override': method,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t},\n\t\t\tmethod: 'POST',\n\t\t};\n\t}\n\n\treturn next( options );\n};\n\nexport default httpV1Middleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA,MAAMA,gBAAgB,GAAG,IAAIC,GAAG,CAAE,CAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAG,CAAC;;AAEhE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,cAAc,GAAG,KAAK;;AAE5B;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,gBAAoC,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;EACjE,MAAM;IAAEC,MAAM,GAAGJ;EAAe,CAAC,GAAGE,OAAO;EAC3C,IAAKJ,gBAAgB,CAACO,GAAG,CAAED,MAAM,CAACE,WAAW,CAAC,CAAE,CAAC,EAAG;IACnDJ,OAAO,GAAG;MACT,GAAGA,OAAO;MACVK,OAAO,EAAE;QACR,GAAGL,OAAO,CAACK,OAAO;QAClB,wBAAwB,EAAEH,MAAM;QAChC,cAAc,EAAE;MACjB,CAAC;MACDA,MAAM,EAAE;IACT,CAAC;EACF;EAEA,OAAOD,IAAI,CAAED,OAAQ,CAAC;AACvB,CAAC;AAAC,IAAAM,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaT,gBAAgB","ignoreList":[]}
|
|
@@ -15,8 +15,8 @@ var _response = require("../utils/response");
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* @param
|
|
19
|
-
* @return
|
|
18
|
+
* @param options
|
|
19
|
+
* @return True if the request is for media upload.
|
|
20
20
|
*/
|
|
21
21
|
function isMediaUploadRequest(options) {
|
|
22
22
|
const isCreateMethod = !!options.method && options.method === 'POST';
|
|
@@ -26,8 +26,8 @@ function isMediaUploadRequest(options) {
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Middleware handling media upload failures and retries.
|
|
29
|
-
*
|
|
30
|
-
* @
|
|
29
|
+
* @param options
|
|
30
|
+
* @param next
|
|
31
31
|
*/
|
|
32
32
|
const mediaUploadMiddleware = (options, next) => {
|
|
33
33
|
if (!isMediaUploadRequest(options)) {
|
|
@@ -37,8 +37,8 @@ const mediaUploadMiddleware = (options, next) => {
|
|
|
37
37
|
const maxRetries = 5;
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* @param
|
|
41
|
-
* @return
|
|
40
|
+
* @param attachmentId
|
|
41
|
+
* @return Processed post response.
|
|
42
42
|
*/
|
|
43
43
|
const postProcess = attachmentId => {
|
|
44
44
|
retries++;
|
|
@@ -65,7 +65,7 @@ const mediaUploadMiddleware = (options, next) => {
|
|
|
65
65
|
parse: false
|
|
66
66
|
}).catch(response => {
|
|
67
67
|
// `response` could actually be an error thrown by `defaultFetchHandler`.
|
|
68
|
-
if (!response.
|
|
68
|
+
if (!(response instanceof globalThis.Response)) {
|
|
69
69
|
return Promise.reject(response);
|
|
70
70
|
}
|
|
71
71
|
const attachmentId = response.headers.get('x-wp-upload-attachment-id');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_i18n","require","_response","isMediaUploadRequest","options","isCreateMethod","method","isMediaEndpoint","path","indexOf","url","mediaUploadMiddleware","next","retries","maxRetries","postProcess","attachmentId","data","action","parse","catch","Promise","reject","response","headers","get","status","code","message","__","parseAndThrowError","then","parseResponseAndNormalizeError","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/media-upload.
|
|
1
|
+
{"version":3,"names":["_i18n","require","_response","isMediaUploadRequest","options","isCreateMethod","method","isMediaEndpoint","path","indexOf","url","mediaUploadMiddleware","next","retries","maxRetries","postProcess","attachmentId","data","action","parse","catch","Promise","reject","response","globalThis","Response","headers","get","status","code","message","__","parseAndThrowError","then","parseResponseAndNormalizeError","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/media-upload.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport {\n\tparseAndThrowError,\n\tparseResponseAndNormalizeError,\n} from '../utils/response';\nimport type { APIFetchOptions, APIFetchMiddleware } from '../types';\n\n/**\n * @param options\n * @return True if the request is for media upload.\n */\nfunction isMediaUploadRequest( options: APIFetchOptions ) {\n\tconst isCreateMethod = !! options.method && options.method === 'POST';\n\tconst isMediaEndpoint =\n\t\t( !! options.path && options.path.indexOf( '/wp/v2/media' ) !== -1 ) ||\n\t\t( !! options.url && options.url.indexOf( '/wp/v2/media' ) !== -1 );\n\n\treturn isMediaEndpoint && isCreateMethod;\n}\n\n/**\n * Middleware handling media upload failures and retries.\n * @param options\n * @param next\n */\nconst mediaUploadMiddleware: APIFetchMiddleware = ( options, next ) => {\n\tif ( ! isMediaUploadRequest( options ) ) {\n\t\treturn next( options );\n\t}\n\n\tlet retries = 0;\n\tconst maxRetries = 5;\n\n\t/**\n\t * @param attachmentId\n\t * @return Processed post response.\n\t */\n\tconst postProcess = ( attachmentId: string ): Promise< any > => {\n\t\tretries++;\n\t\treturn next( {\n\t\t\tpath: `/wp/v2/media/${ attachmentId }/post-process`,\n\t\t\tmethod: 'POST',\n\t\t\tdata: { action: 'create-image-subsizes' },\n\t\t\tparse: false,\n\t\t} ).catch( () => {\n\t\t\tif ( retries < maxRetries ) {\n\t\t\t\treturn postProcess( attachmentId );\n\t\t\t}\n\t\t\tnext( {\n\t\t\t\tpath: `/wp/v2/media/${ attachmentId }?force=true`,\n\t\t\t\tmethod: 'DELETE',\n\t\t\t} );\n\n\t\t\treturn Promise.reject();\n\t\t} );\n\t};\n\n\treturn next( { ...options, parse: false } )\n\t\t.catch( ( response: Response ) => {\n\t\t\t// `response` could actually be an error thrown by `defaultFetchHandler`.\n\t\t\tif ( ! ( response instanceof globalThis.Response ) ) {\n\t\t\t\treturn Promise.reject( response );\n\t\t\t}\n\n\t\t\tconst attachmentId = response.headers.get(\n\t\t\t\t'x-wp-upload-attachment-id'\n\t\t\t);\n\t\t\tif (\n\t\t\t\tresponse.status >= 500 &&\n\t\t\t\tresponse.status < 600 &&\n\t\t\t\tattachmentId\n\t\t\t) {\n\t\t\t\treturn postProcess( attachmentId ).catch( () => {\n\t\t\t\t\tif ( options.parse !== false ) {\n\t\t\t\t\t\treturn Promise.reject( {\n\t\t\t\t\t\t\tcode: 'post_process',\n\t\t\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t\t\t'Media upload failed. If this is a photo or a large image, please scale it down and try again.'\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\n\t\t\t\t\treturn Promise.reject( response );\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn parseAndThrowError( response, options.parse );\n\t\t} )\n\t\t.then( ( response: Response ) =>\n\t\t\tparseResponseAndNormalizeError( response, options.parse )\n\t\t);\n};\n\nexport default mediaUploadMiddleware;\n"],"mappings":";;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,SAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAOA;AACA;AACA;AACA;AACA,SAASE,oBAAoBA,CAAEC,OAAwB,EAAG;EACzD,MAAMC,cAAc,GAAG,CAAC,CAAED,OAAO,CAACE,MAAM,IAAIF,OAAO,CAACE,MAAM,KAAK,MAAM;EACrE,MAAMC,eAAe,GAClB,CAAC,CAAEH,OAAO,CAACI,IAAI,IAAIJ,OAAO,CAACI,IAAI,CAACC,OAAO,CAAE,cAAe,CAAC,KAAK,CAAC,CAAC,IAChE,CAAC,CAAEL,OAAO,CAACM,GAAG,IAAIN,OAAO,CAACM,GAAG,CAACD,OAAO,CAAE,cAAe,CAAC,KAAK,CAAC,CAAG;EAEnE,OAAOF,eAAe,IAAIF,cAAc;AACzC;;AAEA;AACA;AACA;AACA;AACA;AACA,MAAMM,qBAAyC,GAAGA,CAAEP,OAAO,EAAEQ,IAAI,KAAM;EACtE,IAAK,CAAET,oBAAoB,CAAEC,OAAQ,CAAC,EAAG;IACxC,OAAOQ,IAAI,CAAER,OAAQ,CAAC;EACvB;EAEA,IAAIS,OAAO,GAAG,CAAC;EACf,MAAMC,UAAU,GAAG,CAAC;;EAEpB;AACD;AACA;AACA;EACC,MAAMC,WAAW,GAAKC,YAAoB,IAAsB;IAC/DH,OAAO,EAAE;IACT,OAAOD,IAAI,CAAE;MACZJ,IAAI,EAAE,gBAAiBQ,YAAY,eAAgB;MACnDV,MAAM,EAAE,MAAM;MACdW,IAAI,EAAE;QAAE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,EAAEnB,OAAO,CAACe,KAAM,CACzD,CAAC;AACH,CAAC;AAAC,IAAAgB,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEa1B,qBAAqB","ignoreList":[]}
|
|
@@ -5,8 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Internal dependencies
|
|
9
9
|
*/
|
|
10
|
+
|
|
10
11
|
const namespaceAndEndpointMiddleware = (options, next) => {
|
|
11
12
|
let path = options.path;
|
|
12
13
|
let namespaceTrimmed, endpointTrimmed;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["namespaceAndEndpointMiddleware","options","next","path","namespaceTrimmed","endpointTrimmed","namespace","endpoint","replace","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/namespace-endpoint.
|
|
1
|
+
{"version":3,"names":["namespaceAndEndpointMiddleware","options","next","path","namespaceTrimmed","endpointTrimmed","namespace","endpoint","replace","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/namespace-endpoint.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\nconst namespaceAndEndpointMiddleware: APIFetchMiddleware = (\n\toptions,\n\tnext\n) => {\n\tlet path = options.path;\n\tlet namespaceTrimmed, endpointTrimmed;\n\n\tif (\n\t\ttypeof options.namespace === 'string' &&\n\t\ttypeof options.endpoint === 'string'\n\t) {\n\t\tnamespaceTrimmed = options.namespace.replace( /^\\/|\\/$/g, '' );\n\t\tendpointTrimmed = options.endpoint.replace( /^\\//, '' );\n\t\tif ( endpointTrimmed ) {\n\t\t\tpath = namespaceTrimmed + '/' + endpointTrimmed;\n\t\t} else {\n\t\t\tpath = namespaceTrimmed;\n\t\t}\n\t}\n\n\tdelete options.namespace;\n\tdelete options.endpoint;\n\n\treturn next( {\n\t\t...options,\n\t\tpath,\n\t} );\n};\n\nexport default namespaceAndEndpointMiddleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;;AAGA,MAAMA,8BAAkD,GAAGA,CAC1DC,OAAO,EACPC,IAAI,KACA;EACJ,IAAIC,IAAI,GAAGF,OAAO,CAACE,IAAI;EACvB,IAAIC,gBAAgB,EAAEC,eAAe;EAErC,IACC,OAAOJ,OAAO,CAACK,SAAS,KAAK,QAAQ,IACrC,OAAOL,OAAO,CAACM,QAAQ,KAAK,QAAQ,EACnC;IACDH,gBAAgB,GAAGH,OAAO,CAACK,SAAS,CAACE,OAAO,CAAE,UAAU,EAAE,EAAG,CAAC;IAC9DH,eAAe,GAAGJ,OAAO,CAACM,QAAQ,CAACC,OAAO,CAAE,KAAK,EAAE,EAAG,CAAC;IACvD,IAAKH,eAAe,EAAG;MACtBF,IAAI,GAAGC,gBAAgB,GAAG,GAAG,GAAGC,eAAe;IAChD,CAAC,MAAM;MACNF,IAAI,GAAGC,gBAAgB;IACxB;EACD;EAEA,OAAOH,OAAO,CAACK,SAAS;EACxB,OAAOL,OAAO,CAACM,QAAQ;EAEvB,OAAOL,IAAI,CAAE;IACZ,GAAGD,OAAO;IACVE;EACD,CAAE,CAAC;AACJ,CAAC;AAAC,IAAAM,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaX,8BAA8B","ignoreList":[]}
|
|
@@ -5,13 +5,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param nonce
|
|
13
|
+
*
|
|
14
|
+
* @return A middleware to enhance a request with a nonce.
|
|
10
15
|
*/
|
|
11
16
|
function createNonceMiddleware(nonce) {
|
|
12
|
-
/**
|
|
13
|
-
* @type {import('../types').APIFetchMiddleware & { nonce: string }}
|
|
14
|
-
*/
|
|
15
17
|
const middleware = (options, next) => {
|
|
16
18
|
const {
|
|
17
19
|
headers = {}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createNonceMiddleware","nonce","middleware","options","next","headers","headerName","toLowerCase","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/nonce.
|
|
1
|
+
{"version":3,"names":["createNonceMiddleware","nonce","middleware","options","next","headers","headerName","toLowerCase","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/nonce.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\n/**\n * @param nonce\n *\n * @return A middleware to enhance a request with a nonce.\n */\nfunction createNonceMiddleware(\n\tnonce: string\n): APIFetchMiddleware & { nonce: string } {\n\tconst middleware: APIFetchMiddleware & { nonce: string } = (\n\t\toptions,\n\t\tnext\n\t) => {\n\t\tconst { headers = {} } = options;\n\n\t\t// If an 'X-WP-Nonce' header (or any case-insensitive variation\n\t\t// thereof) was specified, no need to add a nonce header.\n\t\tfor ( const headerName in headers ) {\n\t\t\tif (\n\t\t\t\theaderName.toLowerCase() === 'x-wp-nonce' &&\n\t\t\t\theaders[ headerName ] === middleware.nonce\n\t\t\t) {\n\t\t\t\treturn next( options );\n\t\t\t}\n\t\t}\n\n\t\treturn next( {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...headers,\n\t\t\t\t'X-WP-Nonce': middleware.nonce,\n\t\t\t},\n\t\t} );\n\t};\n\n\tmiddleware.nonce = nonce;\n\n\treturn middleware;\n}\n\nexport default createNonceMiddleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA,SAASA,qBAAqBA,CAC7BC,KAAa,EAC4B;EACzC,MAAMC,UAAkD,GAAGA,CAC1DC,OAAO,EACPC,IAAI,KACA;IACJ,MAAM;MAAEC,OAAO,GAAG,CAAC;IAAE,CAAC,GAAGF,OAAO;;IAEhC;IACA;IACA,KAAM,MAAMG,UAAU,IAAID,OAAO,EAAG;MACnC,IACCC,UAAU,CAACC,WAAW,CAAC,CAAC,KAAK,YAAY,IACzCF,OAAO,CAAEC,UAAU,CAAE,KAAKJ,UAAU,CAACD,KAAK,EACzC;QACD,OAAOG,IAAI,CAAED,OAAQ,CAAC;MACvB;IACD;IAEA,OAAOC,IAAI,CAAE;MACZ,GAAGD,OAAO;MACVE,OAAO,EAAE;QACR,GAAGA,OAAO;QACV,YAAY,EAAEH,UAAU,CAACD;MAC1B;IACD,CAAE,CAAC;EACJ,CAAC;EAEDC,UAAU,CAACD,KAAK,GAAGA,KAAK;EAExB,OAAOC,UAAU;AAClB;AAAC,IAAAM,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEcV,qBAAqB","ignoreList":[]}
|
|
@@ -10,8 +10,12 @@ var _url = require("@wordpress/url");
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
|
|
13
|
+
* Internal dependencies
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param preloadedData
|
|
18
|
+
* @return Preloading middleware.
|
|
15
19
|
*/
|
|
16
20
|
function createPreloadingMiddleware(preloadedData) {
|
|
17
21
|
const cache = Object.fromEntries(Object.entries(preloadedData).map(([path, data]) => [(0, _url.normalizePath)(path), data]));
|
|
@@ -19,7 +23,6 @@ function createPreloadingMiddleware(preloadedData) {
|
|
|
19
23
|
const {
|
|
20
24
|
parse = true
|
|
21
25
|
} = options;
|
|
22
|
-
/** @type {string | void} */
|
|
23
26
|
let rawPath = options.path;
|
|
24
27
|
if (!rawPath && options.url) {
|
|
25
28
|
const {
|
|
@@ -55,9 +58,9 @@ function createPreloadingMiddleware(preloadedData) {
|
|
|
55
58
|
/**
|
|
56
59
|
* This is a helper function that sends a success response.
|
|
57
60
|
*
|
|
58
|
-
* @param
|
|
59
|
-
* @param
|
|
60
|
-
* @return
|
|
61
|
+
* @param responseData
|
|
62
|
+
* @param parse
|
|
63
|
+
* @return Promise with the response.
|
|
61
64
|
*/
|
|
62
65
|
function prepareResponse(responseData, parse) {
|
|
63
66
|
if (parse) {
|
|
@@ -73,7 +76,7 @@ function prepareResponse(responseData, parse) {
|
|
|
73
76
|
// See: https://github.com/WordPress/gutenberg/issues/67358#issuecomment-2621163926.
|
|
74
77
|
Object.entries(responseData.headers).forEach(([key, value]) => {
|
|
75
78
|
if (key.toLowerCase() === 'link') {
|
|
76
|
-
responseData.headers[key] = value.replace(/<([^>]+)>/, (
|
|
79
|
+
responseData.headers[key] = value.replace(/<([^>]+)>/, (_, url) => `<${encodeURI(url)}>`);
|
|
77
80
|
}
|
|
78
81
|
});
|
|
79
82
|
return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), {
|