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