UNPKG

@bennetgallein/mande

Version:

Some awesome description

7 lines (6 loc) 1.56 kB
/*! * @bennetgallein/mande v3.0.0-rc5 * (c) 2024 Eduardo San Martin Morote * @license MIT */ let e=/\/+$/,n=/^\/+/;const t={responseAs:"json",headers:{Accept:"application/json","Content-Type":"application/json"}};function o(o,r={},s){function l(r,s,l,i={}){let a,p;"object"==typeof s?(a="",p=s,i=l||{}):(a=s,p=l);let f={...t,...c,method:r,...i,headers:(d={...t.headers,...c.headers,...i.headers},Object.keys(d).reduce(((e,n)=>(null!=d[n]&&(e[n]=d[n]),e)),{}))};var d;let h={...t.query,...c.query,...i.query},{responseAs:y}=f;return a=function(t,o){return t.replace(e,"")+"/"+o.replace(n,"")}(o,"number"==typeof a?""+a:a||""),a+=function(e){let n=Object.keys(e).map((n=>[n,e[n]].map(encodeURIComponent).join("="))).join("&");return n?"?"+n:""}(h),p&&(f.body=JSON.stringify(p)),u(a,f).then((e=>Promise.all([e,"response"===y?e:e[y]().catch((()=>null))]))).then((([e,n])=>{if(e.status>=200&&e.status<300)return"response"!==y&&204==e.status?null:n;let t=new Error(e.statusText);throw t.response=e,t.body=n,t}))}const u="undefined"!=typeof fetch?fetch:s;if(!u)throw new Error("No fetch function exists. Make sure to include a polyfill on Node.js.");const c={query:{},headers:{},...r};return{options:c,post:l.bind(null,"POST"),put:l.bind(null,"PUT"),patch:l.bind(null,"PATCH"),get:(e,n)=>l("GET",e,null,n),delete:(e,n)=>l("DELETE",e,null,n)}}function r(e,n){const t=n.length;return function(){let o=e,r=Array.from(arguments);if(arguments.length===t){o={...e};const[n]=r.splice(0,1);n(o)}return n.call(null,o,...r)}}export{t as defaults,o as mande,r as nuxtWrap};