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