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