index of
/
home
/
thefkyzp
/
hulmvisaadvisor.com
/
wp-content
/
plugins
/
surecart
/
dist
/
components
/
components
/
File: /home/thefkyzp/hulmvisaadvisor.com/wp-content/plugins/surecart/dist/components/components/fetch.js
import{g as getQueryArgs,a as addQueryArgs}from"./add-query-args.js";import{g as getQueryArg,r as removeQueryArgs}from"./remove-query-args.js";function hasQueryArg(e,r){return void 0!==getQueryArg(e,r)}function normalizePath(e){const r=e.split("?"),t=r[1],n=r[0];return t?n+"?"+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("&"):n}function createNonceMiddleware(e){const r=(e,t)=>{const{headers:n={}}=e;for(const a in n)if("x-wp-nonce"===a.toLowerCase()&&n[a]===r.nonce)return t(e);return t({...e,headers:{...n,"X-WP-Nonce":r.nonce}})};return r.nonce=e,r}const namespaceAndEndpointMiddleware=(e,r)=>{let t,n,a=e.path;return"string"==typeof e.namespace&&"string"==typeof e.endpoint&&(t=e.namespace.replace(/^\/|\/$/g,""),n=e.endpoint.replace(/^\//,""),a=n?t+"/"+n:t),delete e.namespace,delete e.endpoint,r({...e,path:a})},createRootURLMiddleware=e=>(r,t)=>namespaceAndEndpointMiddleware(r,(r=>{let n,a=r.url,o=r.path;return"string"==typeof o&&(n=e,-1!==e.indexOf("?")&&(o=o.replace("?","&")),o=o.replace(/^\//,""),"string"==typeof n&&-1!==n.indexOf("?")&&(o=o.replace("?","&")),a=n+o),t({...r,url:a})}));function createPreloadingMiddleware(e){const r=Object.fromEntries(Object.entries(e).map((([e,r])=>[normalizePath(e),r])));return(e,t)=>{const{parse:n=!0}=e;let a=e.path;if(!a&&e.url){const{rest_route:r,...t}=getQueryArgs(e.url);"string"==typeof r&&(a=addQueryArgs(r,t))}if("string"!=typeof a)return t(e);const o=e.method||"GET",i=normalizePath(a);if("GET"===o&&r[i]){const e=r[i];return delete r[i],prepareResponse(e,!!n)}if("OPTIONS"===o&&r[o]&&r[o][i]){const e=r[o][i];return delete r[o][i],prepareResponse(e,!!n)}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},n)=>({...t,url:r&&addQueryArgs(r,n),path:e&&addQueryArgs(e,n)}),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}),n=await parseResponse$1(t);if(!Array.isArray(n))return n;let a=getNextPageUrl(t);if(!a)return n;let o=[].concat(n);for(;a;){const r=await apiFetch({...e,path:void 0,url:a,parse:!1}),t=await parseResponse$1(r);o=o.concat(t),a=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$1=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$1(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 n=e=>(t++,r({path:`/wp/v2/media/${e}/post-process`,method:"POST",data:{action:"create-image-subsizes"},parse:!1}).catch((()=>t<5?n(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?n(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:n,parse:a=!0,...o}=e;let{body:i,headers:s}=e;return s={...DEFAULT_HEADERS,...s},n&&(i=JSON.stringify(n),s["Content-Type"]="application/json"),window.fetch(r||t||window.location.href,{...DEFAULT_OPTIONS,...o,body:i,headers:s}).then((e=>Promise.resolve(e).then(checkStatus).catch((e=>parseAndThrowError(e,a))).then((e=>parseResponseAndNormalizeError(e,a)))),(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))))))}var _a,_b,_c,_d,_e,_f,_g;apiFetch.use=registerMiddleware,apiFetch.setFetchHandler=setFetchHandler,apiFetch.createNonceMiddleware=createNonceMiddleware,apiFetch.createPreloadingMiddleware=createPreloadingMiddleware,apiFetch.createRootURLMiddleware=createRootURLMiddleware,apiFetch.fetchAllMiddleware=fetchAllMiddleware,apiFetch.mediaUploadMiddleware=mediaUploadMiddleware,apiFetch.createThemePreviewMiddleware=createThemePreviewMiddleware,apiFetch.fetchAllMiddleware=null,apiFetch.use(apiFetch.createRootURLMiddleware((null===(_b=null===(_a=null===window||void 0===window?void 0:window.parent)||void 0===_a?void 0:_a.scData)||void 0===_b?void 0:_b.root_url)||(null===(_c=null===window||void 0===window?void 0:window.scData)||void 0===_c?void 0:_c.root_url))),(null===(_d=null===window||void 0===window?void 0:window.scData)||void 0===_d?void 0:_d.nonce)&&(apiFetch.nonceMiddleware=apiFetch.createNonceMiddleware(null===(_e=null===window||void 0===window?void 0:window.scData)||void 0===_e?void 0:_e.nonce),apiFetch.use(apiFetch.nonceMiddleware)),(null===(_f=null===window||void 0===window?void 0:window.scData)||void 0===_f?void 0:_f.nonce_endpoint)&&(apiFetch.nonceEndpoint=null===(_g=null===window||void 0===window?void 0:window.scData)||void 0===_g?void 0:_g.nonce_endpoint),apiFetch.use(((e,r)=>(e.path=addQueryArgs(e.path,{t:Date.now()}),r(e)))),apiFetch.use(((e,r)=>{const t=r(e);return t.catch((e=>{if("invalid_json"===e.code){e.message=wp.i18n.__("The response is not a valid JSON response.","surecart");const r="https://surecart.com/docs/is-not-a-valid-json-response/";e.additional_errors=[{code:"invalid_json",message:wp.i18n.sprintf( /* translators: %s: URL to debug settings page */ wp.i18n.__("Please ensure that your site is not in debug mode as this may interfere with API responses. %s","surecart"),`<a href="${r}" target="_blank" rel="noopener noreferrer">${wp.i18n.__("More Information","surecart")}</a>`)}]}return"checkout.finalize_error"===e.code&&(e.additional_errors=[{code:"checkout.finalize_error",message:e.message}],e.message=wp.i18n.__("We were not able to process this order","surecart")),Promise.reject(e)})),t}));const parseJsonAndNormalizeError=e=>{const r={code:"invalid_json",message:wp.i18n.__("The response is not a valid JSON response.","surecart")};if((null==e?void 0:e.code)&&(null==e?void 0:e.message))throw e;if(!e||!e.json)throw r;return e.json().catch((()=>{throw r}))},handleNonceError=async e=>{const r=await parseJsonAndNormalizeError(e);if("rest_cookie_invalid_nonce"!==r.code)throw r;return window.fetch(apiFetch.nonceEndpoint).then((e=>{if(e.status>=200&&e.status<300)return e;throw e})).then((e=>e.text())).then((e=>{apiFetch.nonceMiddleware.nonce=e}))};export{apiFetch as a,handleNonceError as h};