xior
Version:
A lite http request lib based on fetch with plugins support and similar API to axios.
2 lines (1 loc) • 4.05 kB
JavaScript
import{A as M,B as Y,C as Z,D as me,a as V,b as D,c as x,d as P,e as E,f as S,g as u,h as H,i as T,k as $,l as G,m as q,n as b,o as U,p as F,q as J,r as B,s as K,t as W,u as j,v as Re,w as he,x as O,y as v,z}from"../chunk-C6PKHUO4.mjs";import{a as m,b as C,c as Q,d}from"../chunk-BOVQDIMN.mjs";var A="application/",ee=`${A}x-www-form-urlencoded`,te=RegExp,ne=new te(`^${ee}`,"i"),ie=`${A}${P}`,se=new te(`^${A}.*${P}.*`,"i");function ae(r=b){return[q,b,U].includes(r)}var ce=typeof URLSearchParams!=`${T}`;function L(r){return d(this,null,function*(){let e=r[E]||O,t=r.encodeURI!==!1,o=r[G]&&r[G].toUpperCase(),a=r.url,l=a,n=ce&&r.data instanceof URLSearchParams,i=n?H.fromEntries(r.data.entries()):r.data,X=i,R=r!=null&&r[u]?m({},r[u]):{},h=r.params,I=ae(o);if(i&&typeof i.append!==D){let f="",c="content-type";if(r!=null&&r[u]){let p=$(r[u]).find(y=>y.toLowerCase()===c);p&&(c=p,f=r[u][p])}(!f||n)&&(f=I||n?ee:ie,R[c]=f),typeof i===V&&(I&&h&&(h=j(i,h)),se.test(f)?X=JSON.stringify(v(i)):!I&&ne.test(f)&&(X=e(i)))}if(h&&$(h).length>0){let f=e(h,t);a+=a.includes("?")?`&${f}`:`?${f}`}return C(m({},r),{_data:X,_url:a,data:i,url:l,method:o,[u]:R,isGet:I})})}var fe=typeof AbortController!=`${T}`;function ue(r,e){return d(this,null,function*(){let t;if(!e||!r.ok||[x,P].includes(e)){if(t=yield r[x](),t&&e!==x)try{t=JSON.parse(t)}catch(o){}}else{if(e==="blob")return r.blob();if(e==="arraybuffer")return r.arrayBuffer()}return t})}var le=r=>new g(r),g=class{constructor(e){this.REQI=[];this.RESI=[];this.P=[];this.config=e,this.defaults={params:{},[u]:{}}}get interceptors(){return{request:{use:(e,t,o)=>(this.REQI.push(e),e),eject:e=>{this.REQI=this.REQI.filter(t=>t!==e)},clear:()=>{this.REQI=[]}},response:{use:(e,t)=>(this.RESI.push({fn:e,onRejected:t}),e),eject:e=>{this.RESI=this.RESI.filter(t=>t.fn!==e)},clear:()=>{this.RESI=[]}}}}get plugins(){return{use:e=>(this.P.push(e),e),eject:e=>{this.P=this.P.filter(t=>t!==e)},clear:()=>{this.P=[]}}}request(e){return d(this,null,function*(){let t=j(this.config||{},this.defaults,typeof e=="string"?{url:e}:e),o="credentials";t.withCredentials&&!t[o]&&(t[o]="include"),t[E]||(t[E]=O);for(let n of this.REQI)t=yield n(t);let a=this._.bind(this);this.P.forEach(n=>{a=n(a,this)});let l=a(t);if(!t._did){let n=0,i=[];for(this.RESI.forEach(function(R){i.push(R.fn,R.onRejected)});i.length>n;)l=l.then(i[n++],i[n++])}return l})}_(e){return d(this,null,function*(){let N=yield L(e),{url:t,method:o,headers:a,timeout:l,signal:n,data:i,_data:X,_url:R,isGet:h,fetch:I}=N,f=Q(N,["url","method","headers","timeout","signal","data","_data","_url","isGet","fetch"]);e._url=R;let c,p=[],y=T;if(l&&fe){let s=new AbortController;y=setTimeout(()=>{s.abort(new Z(`timeout of ${l}ms exceeded`,e))},l),p.push(s.signal)}n&&p.push(n),c=p[0],p.length>1&&(c=W(p,()=>{clearTimeout(y)}));let w=R||t,_="baseURL";return e[_]&&!z(w)&&(w=M(e[_],w)),(I||fetch)(w,C(m({body:h?T:X},f),{signal:c,method:o,headers:a})).then(s=>d(this,null,function*(){let{responseType:re}=e,k={data:yield ue(s,re),response:s,config:e,request:e,[S]:s[S],statusText:s.statusText,[u]:s[u]};if(!s.ok){let oe=new Y(s[S]?`Request failed with status code ${s[S]}`:"Network error",e,k);return Promise.reject(oe)}return k})).finally(()=>{var s;y&&clearTimeout(y),(s=c==null?void 0:c.clear)==null||s.call(c)})})}cG(e){return(t,o)=>this.request(o?C(m({},o),{method:e,url:t}):{method:e,url:t})}cP(e){return(t,o,a)=>this.request(a?C(m({},a),{method:e,url:t,data:o}):{method:e,url:t,data:o})}get(e,t){return this.cG(b)(e,t)}head(e,t){return this.cG(q)(e,t)}post(e,t,o){return this.cP(F)(e,t,o)}put(e,t,o){return this.cP(J)(e,t,o)}patch(e,t,o){return this.cP(B)(e,t,o)}delete(e,t){return this.cG(K)(e,t)}options(e,t){return this.cG(U)(e,t)}};g.create=le,g.VERSION="0.7.8";var pe=Object.assign(g.create(),{create:g.create,VERSION:g.VERSION}),xe=pe;export{g as Xior,Y as XiorError,Z as XiorTimeoutError,W as anySignal,Re as buildSortedURL,xe as default,he as delay,O as encodeParams,z as isAbsoluteURL,me as isXiorError,M as joinPath,j as merge,v as trimUndefined};