author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1 |
/******/ (() => { // webpackBootstrap |
19 | 2 |
/******/ "use strict"; |
3 |
/******/ // The require scope |
|
4 |
/******/ var __webpack_require__ = {}; |
|
5 |
/******/ |
|
6 |
/************************************************************************/ |
|
7 |
/******/ /* webpack/runtime/define property getters */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
8 |
/******/ (() => { |
19 | 9 |
/******/ // define getter functions for harmony exports |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
10 |
/******/ __webpack_require__.d = (exports, definition) => { |
19 | 11 |
/******/ for(var key in definition) { |
12 |
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { |
|
13 |
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); |
|
14 |
/******/ } |
|
15 |
/******/ } |
|
9 | 16 |
/******/ }; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
17 |
/******/ })(); |
19 | 18 |
/******/ |
19 |
/******/ /* webpack/runtime/hasOwnProperty shorthand */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
/******/ (() => { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
/******/ })(); |
19 | 23 |
/******/ |
9 | 24 |
/************************************************************************/ |
19 | 25 |
var __webpack_exports__ = {}; |
9 | 26 |
|
19 | 27 |
// EXPORTS |
28 |
__webpack_require__.d(__webpack_exports__, { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
"default": () => (/* binding */ build_module) |
19 | 30 |
}); |
16 | 31 |
|
19 | 32 |
;// CONCATENATED MODULE: external ["wp","i18n"] |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
const external_wp_i18n_namespaceObject = window["wp"]["i18n"]; |
19 | 34 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/nonce.js |
18 | 35 |
/** |
36 |
* @param {string} nonce |
|
37 |
* @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce. |
|
38 |
*/ |
|
9 | 39 |
function createNonceMiddleware(nonce) { |
18 | 40 |
/** |
41 |
* @type {import('../types').APIFetchMiddleware & { nonce: string }} |
|
42 |
*/ |
|
43 |
const middleware = (options, next) => { |
|
44 |
const { |
|
45 |
headers = {} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
46 |
} = options; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
47 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
48 |
// If an 'X-WP-Nonce' header (or any case-insensitive variation |
9 | 49 |
// thereof) was specified, no need to add a nonce header. |
18 | 50 |
for (const headerName in headers) { |
51 |
if (headerName.toLowerCase() === 'x-wp-nonce' && headers[headerName] === middleware.nonce) { |
|
9 | 52 |
return next(options); |
53 |
} |
|
54 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
return next({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
56 |
...options, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
57 |
headers: { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
...headers, |
9 | 59 |
'X-WP-Nonce': middleware.nonce |
18 | 60 |
} |
61 |
}); |
|
62 |
}; |
|
9 | 63 |
middleware.nonce = nonce; |
64 |
return middleware; |
|
65 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
66 |
/* harmony default export */ const nonce = (createNonceMiddleware); |
9 | 67 |
|
19 | 68 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/namespace-endpoint.js |
18 | 69 |
/** |
70 |
* @type {import('../types').APIFetchMiddleware} |
|
71 |
*/ |
|
72 |
const namespaceAndEndpointMiddleware = (options, next) => { |
|
73 |
let path = options.path; |
|
74 |
let namespaceTrimmed, endpointTrimmed; |
|
9 | 75 |
if (typeof options.namespace === 'string' && typeof options.endpoint === 'string') { |
76 |
namespaceTrimmed = options.namespace.replace(/^\/|\/$/g, ''); |
|
77 |
endpointTrimmed = options.endpoint.replace(/^\//, ''); |
|
78 |
if (endpointTrimmed) { |
|
79 |
path = namespaceTrimmed + '/' + endpointTrimmed; |
|
80 |
} else { |
|
81 |
path = namespaceTrimmed; |
|
82 |
} |
|
83 |
} |
|
84 |
delete options.namespace; |
|
85 |
delete options.endpoint; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
86 |
return next({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
87 |
...options, |
18 | 88 |
path |
89 |
}); |
|
9 | 90 |
}; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
/* harmony default export */ const namespace_endpoint = (namespaceAndEndpointMiddleware); |
9 | 92 |
|
19 | 93 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/root-url.js |
9 | 94 |
/** |
95 |
* Internal dependencies |
|
96 |
*/ |
|
97 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
|
18 | 99 |
/** |
100 |
* @param {string} rootURL |
|
101 |
* @return {import('../types').APIFetchMiddleware} Root URL middleware. |
|
102 |
*/ |
|
103 |
const createRootURLMiddleware = rootURL => (options, next) => { |
|
104 |
return namespace_endpoint(options, optionsWithPath => { |
|
105 |
let url = optionsWithPath.url; |
|
106 |
let path = optionsWithPath.path; |
|
107 |
let apiRoot; |
|
108 |
if (typeof path === 'string') { |
|
109 |
apiRoot = rootURL; |
|
110 |
if (-1 !== rootURL.indexOf('?')) { |
|
111 |
path = path.replace('?', '&'); |
|
9 | 112 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
path = path.replace(/^\//, ''); |
9 | 114 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
// API root may already include query parameter prefix if site is |
18 | 116 |
// configured to use plain permalinks. |
117 |
if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) { |
|
118 |
path = path.replace('?', '&'); |
|
119 |
} |
|
120 |
url = apiRoot + path; |
|
121 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
122 |
return next({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
123 |
...optionsWithPath, |
18 | 124 |
url |
9 | 125 |
}); |
18 | 126 |
}); |
9 | 127 |
}; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
128 |
/* harmony default export */ const root_url = (createRootURLMiddleware); |
9 | 129 |
|
19 | 130 |
;// CONCATENATED MODULE: external ["wp","url"] |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
const external_wp_url_namespaceObject = window["wp"]["url"]; |
19 | 132 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/preloading.js |
16 | 133 |
/** |
19 | 134 |
* WordPress dependencies |
16 | 135 |
*/ |
9 | 136 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
|
18 | 138 |
/** |
139 |
* @param {Record<string, any>} preloadedData |
|
140 |
* @return {import('../types').APIFetchMiddleware} Preloading middleware. |
|
141 |
*/ |
|
16 | 142 |
function createPreloadingMiddleware(preloadedData) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
const cache = Object.fromEntries(Object.entries(preloadedData).map(([path, data]) => [(0,external_wp_url_namespaceObject.normalizePath)(path), data])); |
18 | 144 |
return (options, next) => { |
145 |
const { |
|
146 |
parse = true |
|
147 |
} = options; |
|
19 | 148 |
/** @type {string | void} */ |
149 |
let rawPath = options.path; |
|
150 |
if (!rawPath && options.url) { |
|
151 |
const { |
|
152 |
rest_route: pathFromQuery, |
|
153 |
...queryArgs |
|
154 |
} = (0,external_wp_url_namespaceObject.getQueryArgs)(options.url); |
|
155 |
if (typeof pathFromQuery === 'string') { |
|
156 |
rawPath = (0,external_wp_url_namespaceObject.addQueryArgs)(pathFromQuery, queryArgs); |
|
157 |
} |
|
158 |
} |
|
159 |
if (typeof rawPath !== 'string') { |
|
160 |
return next(options); |
|
161 |
} |
|
162 |
const method = options.method || 'GET'; |
|
163 |
const path = (0,external_wp_url_namespaceObject.normalizePath)(rawPath); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
if ('GET' === method && cache[path]) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
const cacheData = cache[path]; |
19 | 166 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
// Unsetting the cache key ensures that the data is only used a single time. |
19 | 168 |
delete cache[path]; |
169 |
return prepareResponse(cacheData, !!parse); |
|
170 |
} else if ('OPTIONS' === method && cache[method] && cache[method][path]) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
171 |
const cacheData = cache[method][path]; |
19 | 172 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
173 |
// Unsetting the cache key ensures that the data is only used a single time. |
19 | 174 |
delete cache[method][path]; |
175 |
return prepareResponse(cacheData, !!parse); |
|
9 | 176 |
} |
177 |
return next(options); |
|
178 |
}; |
|
16 | 179 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
180 |
|
19 | 181 |
/** |
182 |
* This is a helper function that sends a success response. |
|
183 |
* |
|
184 |
* @param {Record<string, any>} responseData |
|
185 |
* @param {boolean} parse |
|
186 |
* @return {Promise<any>} Promise with the response. |
|
187 |
*/ |
|
188 |
function prepareResponse(responseData, parse) { |
|
189 |
return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), { |
|
190 |
status: 200, |
|
191 |
statusText: 'OK', |
|
192 |
headers: responseData.headers |
|
193 |
})); |
|
194 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
195 |
/* harmony default export */ const preloading = (createPreloadingMiddleware); |
9 | 196 |
|
19 | 197 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/fetch-all-middleware.js |
9 | 198 |
/** |
199 |
* WordPress dependencies |
|
200 |
*/ |
|
16 | 201 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
202 |
|
16 | 203 |
/** |
204 |
* Internal dependencies |
|
205 |
*/ |
|
206 |
||
18 | 207 |
|
208 |
/** |
|
209 |
* Apply query arguments to both URL and Path, whichever is present. |
|
210 |
* |
|
211 |
* @param {import('../types').APIFetchOptions} props |
|
19 | 212 |
* @param {Record<string, string | number>} queryArgs |
18 | 213 |
* @return {import('../types').APIFetchOptions} The request with the modified query args |
214 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
215 |
const modifyQuery = ({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
216 |
path, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
217 |
url, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
218 |
...options |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
219 |
}, queryArgs) => ({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
...options, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
221 |
url: url && (0,external_wp_url_namespaceObject.addQueryArgs)(url, queryArgs), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
222 |
path: path && (0,external_wp_url_namespaceObject.addQueryArgs)(path, queryArgs) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
223 |
}); |
9 | 224 |
|
18 | 225 |
/** |
226 |
* Duplicates parsing functionality from apiFetch. |
|
227 |
* |
|
228 |
* @param {Response} response |
|
229 |
* @return {Promise<any>} Parsed response json. |
|
230 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
231 |
const parseResponse = response => response.json ? response.json() : Promise.reject(response); |
9 | 232 |
|
18 | 233 |
/** |
234 |
* @param {string | null} linkHeader |
|
235 |
* @return {{ next?: string }} The parsed link header. |
|
236 |
*/ |
|
237 |
const parseLinkHeader = linkHeader => { |
|
9 | 238 |
if (!linkHeader) { |
239 |
return {}; |
|
240 |
} |
|
18 | 241 |
const match = linkHeader.match(/<([^>]+)>; rel="next"/); |
9 | 242 |
return match ? { |
243 |
next: match[1] |
|
244 |
} : {}; |
|
245 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
246 |
|
18 | 247 |
/** |
248 |
* @param {Response} response |
|
249 |
* @return {string | undefined} The next page URL. |
|
250 |
*/ |
|
251 |
const getNextPageUrl = response => { |
|
252 |
const { |
|
253 |
next |
|
254 |
} = parseLinkHeader(response.headers.get('link')); |
|
9 | 255 |
return next; |
256 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
257 |
|
18 | 258 |
/** |
259 |
* @param {import('../types').APIFetchOptions} options |
|
260 |
* @return {boolean} True if the request contains an unbounded query. |
|
261 |
*/ |
|
262 |
const requestContainsUnboundedQuery = options => { |
|
263 |
const pathIsUnbounded = !!options.path && options.path.indexOf('per_page=-1') !== -1; |
|
264 |
const urlIsUnbounded = !!options.url && options.url.indexOf('per_page=-1') !== -1; |
|
9 | 265 |
return pathIsUnbounded || urlIsUnbounded; |
18 | 266 |
}; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
267 |
|
18 | 268 |
/** |
269 |
* The REST API enforces an upper limit on the per_page option. To handle large |
|
270 |
* collections, apiFetch consumers can pass `per_page=-1`; this middleware will |
|
271 |
* then recursively assemble a full response array from all available pages. |
|
272 |
* |
|
273 |
* @type {import('../types').APIFetchMiddleware} |
|
274 |
*/ |
|
275 |
const fetchAllMiddleware = async (options, next) => { |
|
276 |
if (options.parse === false) { |
|
277 |
// If a consumer has opted out of parsing, do not apply middleware. |
|
278 |
return next(options); |
|
279 |
} |
|
280 |
if (!requestContainsUnboundedQuery(options)) { |
|
281 |
// If neither url nor path is requesting all items, do not apply middleware. |
|
282 |
return next(options); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
283 |
} |
9 | 284 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
285 |
// Retrieve requested page of results. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
286 |
const response = await build_module({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
...modifyQuery(options, { |
18 | 288 |
per_page: 100 |
289 |
}), |
|
290 |
// Ensure headers are returned for page 1. |
|
291 |
parse: false |
|
292 |
}); |
|
293 |
const results = await parseResponse(response); |
|
294 |
if (!Array.isArray(results)) { |
|
295 |
// We have no reliable way of merging non-array results. |
|
296 |
return results; |
|
297 |
} |
|
298 |
let nextPage = getNextPageUrl(response); |
|
299 |
if (!nextPage) { |
|
300 |
// There are no further pages to request. |
|
301 |
return results; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
302 |
} |
18 | 303 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
304 |
// Iteratively fetch all remaining pages until no "next" header is found. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
305 |
let mergedResults = /** @type {any[]} */[].concat(results); |
18 | 306 |
while (nextPage) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
307 |
const nextResponse = await build_module({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
308 |
...options, |
18 | 309 |
// Ensure the URL for the next page is used instead of any provided path. |
310 |
path: undefined, |
|
311 |
url: nextPage, |
|
312 |
// Ensure we still get headers so we can identify the next page. |
|
313 |
parse: false |
|
314 |
}); |
|
315 |
const nextResults = await parseResponse(nextResponse); |
|
316 |
mergedResults = mergedResults.concat(nextResults); |
|
317 |
nextPage = getNextPageUrl(nextResponse); |
|
318 |
} |
|
319 |
return mergedResults; |
|
320 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
321 |
/* harmony default export */ const fetch_all_middleware = (fetchAllMiddleware); |
9 | 322 |
|
19 | 323 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/http-v1.js |
9 | 324 |
/** |
325 |
* Set of HTTP methods which are eligible to be overridden. |
|
326 |
* |
|
18 | 327 |
* @type {Set<string>} |
9 | 328 |
*/ |
18 | 329 |
const OVERRIDE_METHODS = new Set(['PATCH', 'PUT', 'DELETE']); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
330 |
|
9 | 331 |
/** |
332 |
* Default request method. |
|
333 |
* |
|
334 |
* "A request has an associated method (a method). Unless stated otherwise it |
|
335 |
* is `GET`." |
|
336 |
* |
|
337 |
* @see https://fetch.spec.whatwg.org/#requests |
|
338 |
* |
|
339 |
* @type {string} |
|
340 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
341 |
const DEFAULT_METHOD = 'GET'; |
9 | 342 |
|
343 |
/** |
|
344 |
* API Fetch middleware which overrides the request method for HTTP v1 |
|
345 |
* compatibility leveraging the REST API X-HTTP-Method-Override header. |
|
346 |
* |
|
18 | 347 |
* @type {import('../types').APIFetchMiddleware} |
9 | 348 |
*/ |
18 | 349 |
const httpV1Middleware = (options, next) => { |
350 |
const { |
|
351 |
method = DEFAULT_METHOD |
|
352 |
} = options; |
|
9 | 353 |
if (OVERRIDE_METHODS.has(method.toUpperCase())) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
354 |
options = { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
355 |
...options, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
356 |
headers: { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
357 |
...options.headers, |
9 | 358 |
'X-HTTP-Method-Override': method, |
359 |
'Content-Type': 'application/json' |
|
18 | 360 |
}, |
9 | 361 |
method: 'POST' |
18 | 362 |
}; |
9 | 363 |
} |
18 | 364 |
return next(options); |
365 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
366 |
/* harmony default export */ const http_v1 = (httpV1Middleware); |
9 | 367 |
|
19 | 368 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/user-locale.js |
9 | 369 |
/** |
370 |
* WordPress dependencies |
|
371 |
*/ |
|
372 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
373 |
|
18 | 374 |
/** |
375 |
* @type {import('../types').APIFetchMiddleware} |
|
376 |
*/ |
|
377 |
const userLocaleMiddleware = (options, next) => { |
|
19 | 378 |
if (typeof options.url === 'string' && !(0,external_wp_url_namespaceObject.hasQueryArg)(options.url, '_locale')) { |
379 |
options.url = (0,external_wp_url_namespaceObject.addQueryArgs)(options.url, { |
|
9 | 380 |
_locale: 'user' |
381 |
}); |
|
382 |
} |
|
19 | 383 |
if (typeof options.path === 'string' && !(0,external_wp_url_namespaceObject.hasQueryArg)(options.path, '_locale')) { |
384 |
options.path = (0,external_wp_url_namespaceObject.addQueryArgs)(options.path, { |
|
9 | 385 |
_locale: 'user' |
386 |
}); |
|
387 |
} |
|
18 | 388 |
return next(options); |
389 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
390 |
/* harmony default export */ const user_locale = (userLocaleMiddleware); |
9 | 391 |
|
19 | 392 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/utils/response.js |
16 | 393 |
/** |
394 |
* WordPress dependencies |
|
395 |
*/ |
|
396 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
397 |
|
16 | 398 |
/** |
399 |
* Parses the apiFetch response. |
|
400 |
* |
|
401 |
* @param {Response} response |
|
402 |
* @param {boolean} shouldParseResponse |
|
403 |
* |
|
18 | 404 |
* @return {Promise<any> | null | Response} Parsed response. |
16 | 405 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
406 |
const response_parseResponse = (response, shouldParseResponse = true) => { |
16 | 407 |
if (shouldParseResponse) { |
408 |
if (response.status === 204) { |
|
409 |
return null; |
|
410 |
} |
|
411 |
return response.json ? response.json() : Promise.reject(response); |
|
412 |
} |
|
413 |
return response; |
|
414 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
415 |
|
18 | 416 |
/** |
417 |
* Calls the `json` function on the Response, throwing an error if the response |
|
418 |
* doesn't have a json function or if parsing the json itself fails. |
|
419 |
* |
|
420 |
* @param {Response} response |
|
421 |
* @return {Promise<any>} Parsed response. |
|
422 |
*/ |
|
423 |
const parseJsonAndNormalizeError = response => { |
|
424 |
const invalidJsonError = { |
|
16 | 425 |
code: 'invalid_json', |
19 | 426 |
message: (0,external_wp_i18n_namespaceObject.__)('The response is not a valid JSON response.') |
16 | 427 |
}; |
428 |
if (!response || !response.json) { |
|
429 |
throw invalidJsonError; |
|
430 |
} |
|
18 | 431 |
return response.json().catch(() => { |
16 | 432 |
throw invalidJsonError; |
433 |
}); |
|
434 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
435 |
|
16 | 436 |
/** |
437 |
* Parses the apiFetch response properly and normalize response errors. |
|
438 |
* |
|
439 |
* @param {Response} response |
|
440 |
* @param {boolean} shouldParseResponse |
|
441 |
* |
|
18 | 442 |
* @return {Promise<any>} Parsed response. |
16 | 443 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
444 |
const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => { |
18 | 445 |
return Promise.resolve(response_parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse)); |
16 | 446 |
}; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
447 |
|
18 | 448 |
/** |
449 |
* Parses a response, throwing an error if parsing the response fails. |
|
450 |
* |
|
451 |
* @param {Response} response |
|
19 | 452 |
* @param {boolean} shouldParseResponse |
18 | 453 |
* @return {Promise<any>} Parsed response. |
454 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
455 |
function parseAndThrowError(response, shouldParseResponse = true) { |
16 | 456 |
if (!shouldParseResponse) { |
457 |
throw response; |
|
458 |
} |
|
18 | 459 |
return parseJsonAndNormalizeError(response).then(error => { |
460 |
const unknownError = { |
|
16 | 461 |
code: 'unknown_error', |
19 | 462 |
message: (0,external_wp_i18n_namespaceObject.__)('An unknown error occurred.') |
16 | 463 |
}; |
464 |
throw error || unknownError; |
|
465 |
}); |
|
466 |
} |
|
467 |
||
19 | 468 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/media-upload.js |
9 | 469 |
/** |
470 |
* WordPress dependencies |
|
471 |
*/ |
|
472 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
473 |
|
9 | 474 |
/** |
475 |
* Internal dependencies |
|
476 |
*/ |
|
477 |
||
478 |
||
16 | 479 |
/** |
19 | 480 |
* @param {import('../types').APIFetchOptions} options |
481 |
* @return {boolean} True if the request is for media upload. |
|
482 |
*/ |
|
483 |
function isMediaUploadRequest(options) { |
|
484 |
const isCreateMethod = !!options.method && options.method === 'POST'; |
|
485 |
const isMediaEndpoint = !!options.path && options.path.indexOf('/wp/v2/media') !== -1 || !!options.url && options.url.indexOf('/wp/v2/media') !== -1; |
|
486 |
return isMediaEndpoint && isCreateMethod; |
|
487 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
488 |
|
19 | 489 |
/** |
16 | 490 |
* Middleware handling media upload failures and retries. |
491 |
* |
|
18 | 492 |
* @type {import('../types').APIFetchMiddleware} |
16 | 493 |
*/ |
18 | 494 |
const mediaUploadMiddleware = (options, next) => { |
19 | 495 |
if (!isMediaUploadRequest(options)) { |
18 | 496 |
return next(options); |
16 | 497 |
} |
18 | 498 |
let retries = 0; |
499 |
const maxRetries = 5; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
500 |
|
18 | 501 |
/** |
502 |
* @param {string} attachmentId |
|
503 |
* @return {Promise<any>} Processed post response. |
|
504 |
*/ |
|
505 |
const postProcess = attachmentId => { |
|
16 | 506 |
retries++; |
507 |
return next({ |
|
18 | 508 |
path: `/wp/v2/media/${attachmentId}/post-process`, |
16 | 509 |
method: 'POST', |
510 |
data: { |
|
511 |
action: 'create-image-subsizes' |
|
512 |
}, |
|
513 |
parse: false |
|
18 | 514 |
}).catch(() => { |
16 | 515 |
if (retries < maxRetries) { |
516 |
return postProcess(attachmentId); |
|
517 |
} |
|
518 |
next({ |
|
18 | 519 |
path: `/wp/v2/media/${attachmentId}?force=true`, |
16 | 520 |
method: 'DELETE' |
521 |
}); |
|
522 |
return Promise.reject(); |
|
523 |
}); |
|
524 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
525 |
return next({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
...options, |
16 | 527 |
parse: false |
18 | 528 |
}).catch(response => { |
529 |
const attachmentId = response.headers.get('x-wp-upload-attachment-id'); |
|
16 | 530 |
if (response.status >= 500 && response.status < 600 && attachmentId) { |
18 | 531 |
return postProcess(attachmentId).catch(() => { |
16 | 532 |
if (options.parse !== false) { |
533 |
return Promise.reject({ |
|
534 |
code: 'post_process', |
|
19 | 535 |
message: (0,external_wp_i18n_namespaceObject.__)('Media upload failed. If this is a photo or a large image, please scale it down and try again.') |
16 | 536 |
}); |
537 |
} |
|
538 |
return Promise.reject(response); |
|
539 |
}); |
|
540 |
} |
|
541 |
return parseAndThrowError(response, options.parse); |
|
18 | 542 |
}).then(response => parseResponseAndNormalizeError(response, options.parse)); |
543 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
544 |
/* harmony default export */ const media_upload = (mediaUploadMiddleware); |
16 | 545 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
546 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/middlewares/theme-preview.js |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
547 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
548 |
* WordPress dependencies |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
549 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
550 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
551 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
552 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
553 |
* This appends a `wp_theme_preview` parameter to the REST API request URL if |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
554 |
* the admin URL contains a `theme` GET parameter. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
555 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
556 |
* If the REST API request URL has contained the `wp_theme_preview` parameter as `''`, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
557 |
* then bypass this middleware. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
558 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
559 |
* @param {Record<string, any>} themePath |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
560 |
* @return {import('../types').APIFetchMiddleware} Preloading middleware. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
561 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
562 |
const createThemePreviewMiddleware = themePath => (options, next) => { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
563 |
if (typeof options.url === 'string') { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
564 |
const wpThemePreview = (0,external_wp_url_namespaceObject.getQueryArg)(options.url, 'wp_theme_preview'); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
565 |
if (wpThemePreview === undefined) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
566 |
options.url = (0,external_wp_url_namespaceObject.addQueryArgs)(options.url, { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
567 |
wp_theme_preview: themePath |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
568 |
}); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
569 |
} else if (wpThemePreview === '') { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
570 |
options.url = (0,external_wp_url_namespaceObject.removeQueryArgs)(options.url, 'wp_theme_preview'); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
571 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
572 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
573 |
if (typeof options.path === 'string') { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
574 |
const wpThemePreview = (0,external_wp_url_namespaceObject.getQueryArg)(options.path, 'wp_theme_preview'); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
575 |
if (wpThemePreview === undefined) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
576 |
options.path = (0,external_wp_url_namespaceObject.addQueryArgs)(options.path, { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
wp_theme_preview: themePath |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
578 |
}); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
579 |
} else if (wpThemePreview === '') { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
580 |
options.path = (0,external_wp_url_namespaceObject.removeQueryArgs)(options.path, 'wp_theme_preview'); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
581 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
582 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
583 |
return next(options); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
584 |
}; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
585 |
/* harmony default export */ const theme_preview = (createThemePreviewMiddleware); |
16 | 586 |
|
19 | 587 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/api-fetch/build-module/index.js |
16 | 588 |
/** |
589 |
* WordPress dependencies |
|
590 |
*/ |
|
591 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
592 |
|
16 | 593 |
/** |
594 |
* Internal dependencies |
|
595 |
*/ |
|
596 |
||
597 |
||
598 |
||
599 |
||
9 | 600 |
|
601 |
||
602 |
||
603 |
||
604 |
||
605 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
606 |
|
9 | 607 |
/** |
608 |
* Default set of header values which should be sent with every request unless |
|
609 |
* explicitly provided through apiFetch options. |
|
610 |
* |
|
18 | 611 |
* @type {Record<string, string>} |
9 | 612 |
*/ |
18 | 613 |
const DEFAULT_HEADERS = { |
9 | 614 |
// The backend uses the Accept header as a condition for considering an |
615 |
// incoming request as a REST request. |
|
616 |
// |
|
617 |
// See: https://core.trac.wordpress.org/ticket/44534 |
|
618 |
Accept: 'application/json, */*;q=0.1' |
|
619 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
620 |
|
9 | 621 |
/** |
622 |
* Default set of fetch option values which should be sent with every request |
|
623 |
* unless explicitly provided through apiFetch options. |
|
624 |
* |
|
625 |
* @type {Object} |
|
626 |
*/ |
|
18 | 627 |
const DEFAULT_OPTIONS = { |
9 | 628 |
credentials: 'include' |
629 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
630 |
|
18 | 631 |
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */ |
632 |
/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */ |
|
633 |
||
634 |
/** |
|
635 |
* @type {import('./types').APIFetchMiddleware[]} |
|
636 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
637 |
const middlewares = [user_locale, namespace_endpoint, http_v1, fetch_all_middleware]; |
18 | 638 |
|
639 |
/** |
|
640 |
* Register a middleware |
|
641 |
* |
|
642 |
* @param {import('./types').APIFetchMiddleware} middleware |
|
643 |
*/ |
|
9 | 644 |
function registerMiddleware(middleware) { |
645 |
middlewares.unshift(middleware); |
|
646 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
647 |
|
18 | 648 |
/** |
649 |
* Checks the status of a response, throwing the Response as an error if |
|
650 |
* it is outside the 200 range. |
|
651 |
* |
|
652 |
* @param {Response} response |
|
653 |
* @return {Response} The response if the status is in the 200 range. |
|
654 |
*/ |
|
655 |
const checkStatus = response => { |
|
16 | 656 |
if (response.status >= 200 && response.status < 300) { |
657 |
return response; |
|
658 |
} |
|
659 |
throw response; |
|
660 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
661 |
|
18 | 662 |
/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/ |
16 | 663 |
|
18 | 664 |
/** |
665 |
* @type {FetchHandler} |
|
666 |
*/ |
|
667 |
const defaultFetchHandler = nextOptions => { |
|
668 |
const { |
|
669 |
url, |
|
670 |
path, |
|
671 |
data, |
|
672 |
parse = true, |
|
673 |
...remainingOptions |
|
674 |
} = nextOptions; |
|
675 |
let { |
|
676 |
body, |
|
677 |
headers |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
678 |
} = nextOptions; |
9 | 679 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
680 |
// Merge explicitly-provided headers with default values. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
681 |
headers = { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
682 |
...DEFAULT_HEADERS, |
18 | 683 |
...headers |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
684 |
}; |
9 | 685 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
686 |
// The `data` property is a shorthand for sending a JSON body. |
9 | 687 |
if (data) { |
688 |
body = JSON.stringify(data); |
|
689 |
headers['Content-Type'] = 'application/json'; |
|
690 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
691 |
const responsePromise = window.fetch( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
692 |
// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
693 |
url || path || window.location.href, { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
694 |
...DEFAULT_OPTIONS, |
18 | 695 |
...remainingOptions, |
696 |
body, |
|
697 |
headers |
|
698 |
}); |
|
19 | 699 |
return responsePromise.then(value => Promise.resolve(value).then(checkStatus).catch(response => parseAndThrowError(response, parse)).then(response => parseResponseAndNormalizeError(response, parse)), err => { |
700 |
// Re-throw AbortError for the users to handle it themselves. |
|
701 |
if (err && err.name === 'AbortError') { |
|
702 |
throw err; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
703 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
704 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
705 |
// Otherwise, there is most likely no network connection. |
19 | 706 |
// Unfortunately the message might depend on the browser. |
16 | 707 |
throw { |
708 |
code: 'fetch_error', |
|
19 | 709 |
message: (0,external_wp_i18n_namespaceObject.__)('You are probably offline.') |
9 | 710 |
}; |
711 |
}); |
|
712 |
}; |
|
713 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
714 |
/** @type {FetchHandler} */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
715 |
let fetchHandler = defaultFetchHandler; |
18 | 716 |
|
9 | 717 |
/** |
718 |
* Defines a custom fetch handler for making the requests that will override |
|
719 |
* the default one using window.fetch |
|
720 |
* |
|
18 | 721 |
* @param {FetchHandler} newFetchHandler The new fetch handler |
9 | 722 |
*/ |
723 |
function setFetchHandler(newFetchHandler) { |
|
724 |
fetchHandler = newFetchHandler; |
|
725 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
726 |
|
18 | 727 |
/** |
728 |
* @template T |
|
729 |
* @param {import('./types').APIFetchOptions} options |
|
730 |
* @return {Promise<T>} A promise representing the request processed via the registered middlewares. |
|
731 |
*/ |
|
9 | 732 |
function apiFetch(options) { |
18 | 733 |
// creates a nested function chain that calls all middlewares and finally the `fetchHandler`, |
734 |
// converting `middlewares = [ m1, m2, m3 ]` into: |
|
735 |
// ``` |
|
736 |
// opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) ); |
|
737 |
// ``` |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
738 |
const enhancedHandler = middlewares.reduceRight(( /** @type {FetchHandler} */next, middleware) => { |
18 | 739 |
return workingOptions => middleware(workingOptions, next); |
740 |
}, fetchHandler); |
|
741 |
return enhancedHandler(options).catch(error => { |
|
742 |
if (error.code !== 'rest_cookie_invalid_nonce') { |
|
743 |
return Promise.reject(error); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
744 |
} |
16 | 745 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
746 |
// If the nonce is invalid, refresh it and try again. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
747 |
return window |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
748 |
// @ts-ignore |
18 | 749 |
.fetch(apiFetch.nonceEndpoint).then(checkStatus).then(data => data.text()).then(text => { |
750 |
// @ts-ignore |
|
751 |
apiFetch.nonceMiddleware.nonce = text; |
|
752 |
return apiFetch(options); |
|
16 | 753 |
}); |
754 |
}); |
|
9 | 755 |
} |
756 |
apiFetch.use = registerMiddleware; |
|
757 |
apiFetch.setFetchHandler = setFetchHandler; |
|
16 | 758 |
apiFetch.createNonceMiddleware = nonce; |
9 | 759 |
apiFetch.createPreloadingMiddleware = preloading; |
760 |
apiFetch.createRootURLMiddleware = root_url; |
|
761 |
apiFetch.fetchAllMiddleware = fetch_all_middleware; |
|
16 | 762 |
apiFetch.mediaUploadMiddleware = media_upload; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
763 |
apiFetch.createThemePreviewMiddleware = theme_preview; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
764 |
/* harmony default export */ const build_module = (apiFetch); |
9 | 765 |
|
19 | 766 |
(window.wp = window.wp || {}).apiFetch = __webpack_exports__["default"]; |
767 |
/******/ })() |
|
768 |
; |