|
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}; |
|
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}; |