@iel/axios-ext
Version:
Extend extra functionality for axios.
10 lines (9 loc) • 4.5 kB
JavaScript
/*!
* @iel/axios-ext v1.0.5
* Extend extra functionality for axios.
*
* https://github.com/iel-h/axios-ext
*
* Licensed under the MIT license.
*/
var W=Object.defineProperty;var _=Object.getOwnPropertySymbols;var j=Object.prototype.hasOwnProperty,U=Object.prototype.propertyIsEnumerable;var D=(t,o,e)=>o in t?W(t,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[o]=e,N=(t,o)=>{for(var e in o||(o={}))j.call(o,e)&&D(t,e,o[e]);if(_)for(var e of _(o))U.call(o,e)&&D(t,e,o[e]);return t};var f=(t,o,e)=>new Promise((n,s)=>{var x=u=>{try{p(e.next(u))}catch(l){s(l)}},r=u=>{try{p(e.throw(u))}catch(l){s(l)}},p=u=>u.done?n(u.value):Promise.resolve(u.value).then(x,r);p((e=e.apply(t,o)).next())});import{assignSafely as F,isFunction as L,isPlainObject as no,isPromise as so,isString as io}from"@iel/axios-ext-utils";import{bind as $,buildFullPath as X,deepCopy as z,extend as K,pick as B,serialize as G}from"@iel/axios-ext-utils";import P from"axios";var R="$$isShallowInstance",A="$$AXIOS_EXT_EVENT_STORE";function k(t){return[P.constructor,P.Axios].some(o=>t instanceof o)}function Eo(t){let o=k(t)?t:P.create(t),e=M(o),n=J(e,o),s=$(e.use,e),x=function(...r){let p=s(...r);return Promise.resolve(!0).then(()=>{K(n,e.instance)}),p};return e.use=$(x,e),n.$axiosExt=e,n}function J(t,o){let e=(...n)=>t.instance.request.apply(o,n);return K(e,o,e),e[A]=z(e[A]||{}),e[R]=!0,e}function Q(t,o){return X(t.defaults.baseURL,o)}function yo(t,o={}){let{method:e,url:n,data:s={},params:x={}}=o;return G({method:e,url:Q(t,n||""),data:s,params:x})}function go(t){return B(t,["method","url","data","params"])}import{isFunction as Z,noop as a}from"@iel/axios-ext-utils";var H=null,I=null,d=a,q=a,T=a,O=a,S=a;function E(t){return function(o){!Z(o)||H.plugins.appendHookFn(I,t,o.bind(null))}}function oo(){H=null,I=null,d=a,q=a,T=a,O=a,S=a}function V(n,s){return f(this,arguments,function*(t,o,e={}){H=t,I=o,t.plugins.set(o),d=E("onRequest"),q=E("onResponse"),T=E("onResponseError"),O=E("onFinally"),S=E("onDestroy");try{yield o(t,e)}catch(x){console.error("[SetupPlugin] - ",x)}finally{oo()}})}import{isFunction as eo}from"@iel/axios-ext-utils";var to=()=>({onRequest:[],onResponse:[],onResponseError:[],onFinally:[],onDestroy:[]}),y=class{constructor(){this.plugins=new Map}has(o){return this.plugins.has(o)}set(o){return this.has(o)?this.plugins:this.plugins.set(o,to())}get(o){return this.plugins.get(o)}delete(o){return this.plugins.delete(o)}clear(){this.plugins.clear()}appendHookFn(o,e,n){if(!eo(n))return!1;let s=this.plugins.get(o);if(!s||!s[e])return!1;s[e].push(n)}getHooks(o){let e=[];return this.plugins.forEach(n=>{e=e.concat(n[o]||[])}),e}runHooks(o,...e){this.getHooks(o).forEach(n=>n(...e))}};var ro="[AxiosExt] - ",w=class{get _isAxiosExt(){return!0}constructor(o){if(!k(o))throw Error(`${ro}\u53C2\u6570 instance \u5FC5\u987B\u662F Axios \u7684\u5B9E\u4F8B\u3002`);if(this.instance=o,this.rawRequestFn=o.request,this.plugins=new y,R in o)return this;this.init()}destroy(){this.plugins.runHooks("onDestroy"),this.plugins.clear()}init(){let o=this.instance,e=["delete","get","head","options"],n=["post","put","patch"];e.forEach(s=>o[s]=this.proxyRequest(!1)),n.forEach(s=>o[s]=this.proxyRequest(!0)),o.request=this.proxyRequest(!1)}proxyRequest(o=!1){let e=this;return function(){let n=no(this[A])?this[A]:{},[s,x,r]=Array.from(arguments);return o?r=F(r,{url:s,data:x}):io(s||"")?r=F(r,{url:s}):r=F(r,s),new Promise((p,u)=>f(this,null,function*(){let l=i=>{l.resolved||(l.resolved=!0,g(i),p(i))},c=null,g=i=>{c=i},h=e.rawRequestFn,Y=i=>{!L(i)||(h=i)},m=(i={})=>N({$eventStore:n,config:r,resolve:l,reject:u,setReturnValue:g,returnValue:c},i),C=!1;try{let i=e.plugins.getHooks("onRequest");for(let b of i)if(yield b(m({setRequestFn:Y,requestFn:h})),so(c))return l(c);let v=yield h(r);g(v),yield e.plugins.runHooks("onResponse",m({response:v}))}catch(i){C=!0,g(Promise.reject(i)),yield e.plugins.runHooks("onResponseError",m({error:i}))}finally{l(c),yield e.plugins.runHooks("onFinally",{$eventStore:n,isError:C,returnValue:c,config:r})}}))}}use(o,e){return L(o)?(V(this,o,e),this):this}eject(o){return this.plugins.has(o)&&this.plugins.delete(o),this}};function M(t){return t.$axiosExt?t.$axiosExt:new w(t)}export{A as EVENT_STORE_KEY,R as SHALLOW_INSTANCE_KEY,Eo as createAxios,M as createAxiosExt,J as createShallowAxiosInstance,Q as getFullPath,yo as getKeyByConfig,k as isAxiosInstance,S as onDestroy,O as onFinally,d as onRequest,q as onResponse,T as onResponseError,go as pickConfig};