JezK
Edit File: index-824c562b.js
import{g as getQueryArgs,a as addQueryArgs}from"./add-query-args-0e2a8393.js";import{g as getQueryArg,r as removeQueryArgs}from"./remove-query-args-938c53ea.js";function hasQueryArg(e,r){return void 0!==getQueryArg(e,r)}function normalizePath(e){const r=e.split("?"),t=r[1],a=r[0];return t?a+"?"+t.split("&").map((e=>e.split("="))).map((e=>e.map(decodeURIComponent))).sort(((e,r)=>e[0].localeCompare(r[0]))).map((e=>e.map(encodeURIComponent))).map((e=>e.join("="))).join("&"):a}function createNonceMiddleware(e){const r=(e,t)=>{const{headers:a={}}=e;for(const n in a)if("x-wp-nonce"===n.toLowerCase()&&a[n]===r.nonce)return t(e);return t({...e,headers:{...a,"X-WP-Nonce":r.nonce}})};return r.nonce=e,r}const namespaceAndEndpointMiddleware=(e,r)=>{let t,a,n=e.path;return"string"==typeof e.namespace&&"string"==typeof e.endpoint&&(t=e.namespace.replace(/^\/|\/$/g,""),a=e.endpoint.replace(/^\//,""),n=a?t+"/"+a:t),delete e.namespace,delete e.endpoint,r({...e,path:n})},createRootURLMiddleware=e=>(r,t)=>namespaceAndEndpointMiddleware(r,(r=>{let a,n=r.url,o=r.path;return"string"==typeof o&&(a=e,-1!==e.indexOf("?")&&(o=o.replace("?","&")),o=o.replace(/^\//,""),"string"==typeof a&&-1!==a.indexOf("?")&&(o=o.replace("?","&")),n=a+o),t({...r,url:n})}));function createPreloadingMiddleware(e){const r=Object.fromEntries(Object.entries(e).map((([e,r])=>[normalizePath(e),r])));return(e,t)=>{const{parse:a=!0}=e;let n=e.path;if(!n&&e.url){const{rest_route:r,...t}=getQueryArgs(e.url);"string"==typeof r&&(n=addQueryArgs(r,t))}if("string"!=typeof n)return t(e);const o=e.method||"GET",s=normalizePath(n);if("GET"===o&&r[s]){const e=r[s];return delete r[s],prepareResponse(e,!!a)}if("OPTIONS"===o&&r[o]&&r[o][s]){const e=r[o][s];return delete r[o][s],prepareResponse(e,!!a)}return t(e)}}function prepareResponse(e,r){return Promise.resolve(r?e.body:new window.Response(JSON.stringify(e.body),{status:200,statusText:"OK",headers:e.headers}))}const modifyQuery=({path:e,url:r,...t},a)=>({...t,url:r&&addQueryArgs(r,a),path:e&&addQueryArgs(e,a)}),parseResponse$1=e=>e.json?e.json():Promise.reject(e),parseLinkHeader=e=>{if(!e)return{};const r=e.match(/<([^>]+)>; rel="next"/);return r?{next:r[1]}:{}},getNextPageUrl=e=>{const{next:r}=parseLinkHeader(e.headers.get("link"));return r},requestContainsUnboundedQuery=e=>{const r=!!e.path&&-1!==e.path.indexOf("per_page=-1"),t=!!e.url&&-1!==e.url.indexOf("per_page=-1");return r||t},fetchAllMiddleware=async(e,r)=>{if(!1===e.parse)return r(e);if(!requestContainsUnboundedQuery(e))return r(e);const t=await apiFetch({...modifyQuery(e,{per_page:100}),parse:!1}),a=await parseResponse$1(t);if(!Array.isArray(a))return a;let n=getNextPageUrl(t);if(!n)return a;let o=[].concat(a);for(;n;){const r=await apiFetch({...e,path:void 0,url:n,parse:!1}),t=await parseResponse$1(r);o=o.concat(t),n=getNextPageUrl(r)}return o},OVERRIDE_METHODS=new Set(["PATCH","PUT","DELETE"]),DEFAULT_METHOD="GET",httpV1Middleware=(e,r)=>{const{method:t=DEFAULT_METHOD}=e;return OVERRIDE_METHODS.has(t.toUpperCase())&&(e={...e,headers:{...e.headers,"X-HTTP-Method-Override":t,"Content-Type":"application/json"},method:"POST"}),r(e)},userLocaleMiddleware=(e,r)=>("string"!=typeof e.url||hasQueryArg(e.url,"_locale")||(e.url=addQueryArgs(e.url,{_locale:"user"})),"string"!=typeof e.path||hasQueryArg(e.path,"_locale")||(e.path=addQueryArgs(e.path,{_locale:"user"})),r(e)),parseResponse=(e,r=!0)=>r?204===e.status?null:e.json?e.json():Promise.reject(e):e,parseJsonAndNormalizeError=e=>{const r={code:"invalid_json",message:wp.i18n.__("The response is not a valid JSON response.")};if(!e||!e.json)throw r;return e.json().catch((()=>{throw r}))},parseResponseAndNormalizeError=(e,r=!0)=>Promise.resolve(parseResponse(e,r)).catch((e=>parseAndThrowError(e,r)));function parseAndThrowError(e,r=!0){if(!r)throw e;return parseJsonAndNormalizeError(e).then((e=>{const r={code:"unknown_error",message:wp.i18n.__("An unknown error occurred.")};throw e||r}))}function isMediaUploadRequest(e){const r=!!e.method&&"POST"===e.method;return(!!e.path&&-1!==e.path.indexOf("/wp/v2/media")||!!e.url&&-1!==e.url.indexOf("/wp/v2/media"))&&r}const mediaUploadMiddleware=(e,r)=>{if(!isMediaUploadRequest(e))return r(e);let t=0;const a=e=>(t++,r({path:`/wp/v2/media/${e}/post-process`,method:"POST",data:{action:"create-image-subsizes"},parse:!1}).catch((()=>t<5?a(e):(r({path:`/wp/v2/media/${e}?force=true`,method:"DELETE"}),Promise.reject()))));return r({...e,parse:!1}).catch((r=>{const t=r.headers.get("x-wp-upload-attachment-id");return r.status>=500&&r.status<600&&t?a(t).catch((()=>!1!==e.parse?Promise.reject({code:"post_process",message:wp.i18n.__("Media upload failed. If this is a photo or a large image, please scale it down and try again.")}):Promise.reject(r))):parseAndThrowError(r,e.parse)})).then((r=>parseResponseAndNormalizeError(r,e.parse)))},createThemePreviewMiddleware=e=>(r,t)=>{if("string"==typeof r.url){const t=getQueryArg(r.url,"wp_theme_preview");void 0===t?r.url=addQueryArgs(r.url,{wp_theme_preview:e}):""===t&&(r.url=removeQueryArgs(r.url,"wp_theme_preview"))}if("string"==typeof r.path){const t=getQueryArg(r.path,"wp_theme_preview");void 0===t?r.path=addQueryArgs(r.path,{wp_theme_preview:e}):""===t&&(r.path=removeQueryArgs(r.path,"wp_theme_preview"))}return t(r)},DEFAULT_HEADERS={Accept:"application/json, */*;q=0.1"},DEFAULT_OPTIONS={credentials:"include"},middlewares=[userLocaleMiddleware,namespaceAndEndpointMiddleware,httpV1Middleware,fetchAllMiddleware];function registerMiddleware(e){middlewares.unshift(e)}const checkStatus=e=>{if(e.status>=200&&e.status<300)return e;throw e},defaultFetchHandler=e=>{const{url:r,path:t,data:a,parse:n=!0,...o}=e;let{body:s,headers:i}=e;return i={...DEFAULT_HEADERS,...i},a&&(s=JSON.stringify(a),i["Content-Type"]="application/json"),window.fetch(r||t||window.location.href,{...DEFAULT_OPTIONS,...o,body:s,headers:i}).then((e=>Promise.resolve(e).then(checkStatus).catch((e=>parseAndThrowError(e,n))).then((e=>parseResponseAndNormalizeError(e,n)))),(e=>{if(e&&"AbortError"===e.name)throw e;throw{code:"fetch_error",message:wp.i18n.__("You are probably offline.")}}))};let fetchHandler=defaultFetchHandler;function setFetchHandler(e){fetchHandler=e}function apiFetch(e){return middlewares.reduceRight(((e,r)=>t=>r(t,e)),fetchHandler)(e).catch((r=>"rest_cookie_invalid_nonce"!==r.code?Promise.reject(r):window.fetch(apiFetch.nonceEndpoint).then(checkStatus).then((e=>e.text())).then((r=>(apiFetch.nonceMiddleware.nonce=r,apiFetch(e))))))}apiFetch.use=registerMiddleware,apiFetch.setFetchHandler=setFetchHandler,apiFetch.createNonceMiddleware=createNonceMiddleware,apiFetch.createPreloadingMiddleware=createPreloadingMiddleware,apiFetch.createRootURLMiddleware=createRootURLMiddleware,apiFetch.fetchAllMiddleware=fetchAllMiddleware,apiFetch.mediaUploadMiddleware=mediaUploadMiddleware,apiFetch.createThemePreviewMiddleware=createThemePreviewMiddleware;export{apiFetch as a};