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