UNPKG

@alova/adapter-axios

Version:

axios adapter for alova.js

2 lines (1 loc) 1.43 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("axios")):"function"==typeof define&&define.amd?define(["exports","axios"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).AlovaAdapterAxios={},e.axios)}(this,(function(e,o){"use strict";const s="undefined";typeof window===s&&typeof process!==s&&process.browser;const t=()=>{};var a={onMockResponse:({status:e,statusText:s,body:t},a,r)=>{const n={},{config:d}=r,i={baseURL:r.baseURL,url:r.url,data:r.data,...d,headers:new o.AxiosHeaders(d.headers)},u={data:t,status:e,statusText:s,headers:n,config:i};if(e>=400)throw new o.AxiosError(s,"ERR_BAD_REQUEST",i,undefined,u);return{response:u,headers:n}},onMockError:(e,s)=>{const{config:t}=s;return new o.AxiosError(e.message,"ERR_NETWORK",{baseURL:s.baseURL,url:s.url,data:s.data,...t,headers:new o.AxiosHeaders(t.headers)})}};e.axiosMockResponse=a,e.axiosRequestAdapter=function(e={}){const s=e.axios||o;return(e,a)=>{let r=t,n=t;const{config:d}=a,i=new AbortController,{baseURL:u,timeout:l}=s.defaults,f=s({url:a.url,method:a.type,baseURL:u||a.baseURL,timeout:l||a.config.timeout,data:a.data,signal:i.signal,onUploadProgress:e=>{n(e.loaded,e.total||1)},onDownloadProgress:e=>{r(e.loaded,e.total||1)},...d});return{response:()=>f,headers:()=>f.then((e=>(null==e?void 0:e.headers)||((e,...o)=>new e(...o))(o.AxiosHeaders))),abort:()=>{i.abort()},onDownload:e=>{r=e},onUpload:e=>{n=e}}}}}));