| 1 |
function E(t,s=""){let n={};return Object.keys(t).forEach(e=>{t[e]===null||t[e]===!1?delete t[e]:typeof t[e]=="object"?n=Object.assign(n,E(t[e],`${s}[${e}]`)):n[`${s}[${e}]`]=t[e]}),n}const l=function(t=null,s,n,e={},d={}){const c=n.startsWith("/")?n.slice(1):n,a=`${t||window.fluentCartRestVars.rest.url}/${c}`,i={"X-WP-Nonce":window.fluentCartRestVars.rest.nonce};return["PUT","PATCH","DELETE"].indexOf(s.toUpperCase())!==-1&&(i["X-HTTP-Method-Override"]=s,s="POST"),e.query_timestamp=Date.now(),new Promise((O,p)=>{const o=new XMLHttpRequest,w=d.signal;let f=a;if(s.toUpperCase()==="GET"){let r={};Object.keys(e).forEach(u=>{e[u]===null||e[u]===!1?delete e[u]:typeof e[u]=="object"?r=Object.assign(r,E(e[u],u)):r[u]=e[u]});let T=new URLSearchParams(r).toString();f.indexOf("?")!==-1?f+="&"+T:f+="?"+T}if(o.open(s,f,!0),Object.keys(i).forEach(r=>{o.setRequestHeader(r,i[r])}),o.onload=function(){let r;try{r=JSON.parse(o.responseText)}catch{r=null}this.status>=200&&this.status<300?O(r):(r&&(r.code==="rest_cookie_invalid_nonce"||r.code==="rest_cookie_invalid")&&document.dispatchEvent(new CustomEvent("fcart_renew_rest_nonce",{detail:r})),p({data:r,status_code:o.status}))},o.onerror=function(){p({status:o.status,statusText:o.statusText})},o.onabort=function(){p(new DOMException("The request was aborted.","AbortError"))},w){if(w.aborted){o.abort();return}w.addEventListener("abort",()=>o.abort(),{once:!0})}s.toUpperCase()==="GET"?o.send():(o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),o.send(JSON.stringify(e)))})},R={get(t,s={},n=null){return l(n,"GET",t,s)},post(t,s={},n=null,e={}){return l(n,"POST",t,s,e)},delete(t,s={},n=null){return l(n,"DELETE",t,s)},put(t,s={},n=null){return l(n,"PUT",t,s)},patch(t,s={},n=null){return l(n,"PATCH",t,s)},upload(t,s={}){const n=`${window.fluentCartRestVars.rest.url}/${t}`,e={"X-WP-Nonce":window.fluentCartRestVars.rest.nonce};return new Promise((d,c)=>{const a=new XMLHttpRequest;a.open("POST",n,!0),Object.keys(e).forEach(i=>{a.setRequestHeader(i,e[i])}),a.onload=function(){this.status>=200&&this.status<300?d(JSON.parse(a.responseText)):c(JSON.parse(a.responseText))},a.onerror=function(){c({status:a.status,statusText:a.statusText})},a.send(s)})},getNonce(){return window.fluentCartRestVars.rest.nonce}};window.jQuery!==void 0&&jQuery(document).ajaxSuccess((t,s,n)=>{const e=s.getResponseHeader("X-WP-Nonce");e&&(window.fluentCartRestVars,window.fluentCartRestVars.rest_nonce=e)});export{R}; |