wing-axios
Version:
a http tool based on axios
2 lines (1 loc) • 2.04 kB
JavaScript
define(["exports","axios","qs"],(function(t,e,n){"use strict";function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var o=r(e),i=r(n);function s({baseURL:t="",timeout:e=8e3,withCredentials:n=!1,contentType:r,loadingTips:o={start:()=>{},finish:()=>{},error:()=>{}}}={}){let i={baseURL:t,timeout:e,withCredentials:n};r&&(i.headers={"Content-Type":r}),this.options=i,this.loadingTips=o}function a(t){if(!t)return"";let e=[];for(const n in t)Object.hasOwnProperty.call(t,n)&&e.push(`${n}=${t[n]}`);return e.length>0?"?"+e.join("&"):""}function u(t){return"function"==typeof t}function c(t){return"object"==typeof t}s.prototype.create=function(){let t=o.default.create(this.options);const{start:e,finish:n,error:r}=this.loadingTips;return t.interceptors.request.use((function(t){const{params:n,data:r,url:o}=t,s=n||i.default.stringify(r)||{};return t.url=o.replace(/\{\w+\}/gi,(t=>{const e=t.replace(/\{?\}?/gi,"");return s[e]})),e&&e(t),t}),(function(t){return r&&r(t),Promise.reject(t)})),t.interceptors.response.use((function(t){return n&&n(t),t}),(function(t){return r&&r(t),Promise.reject(t)})),t},t.WingAxios=s,t.createWingService=function({host:t="",baseURL:e="",timeout:n=8e3,withCredentials:r=!0,contentType:o,loadingTips:i={start:()=>{},finish:()=>{},error:()=>{}},apiUrls:f={},token:l,commonParams:p={},commonQuery:d={},onError:h}){let m=new s({baseURL:e,timeout:n,withCredentials:r,contentType:o,loadingTips:i}).create(),y={};for(let e in f){let n=f[e].split(",");const r=n[0]?n[0].toLowerCase():"get";y[e]=(e={},o)=>{let i="";u(d)?i=a(d()):c(d)&&(i=a(d));const s=`${t}${n[1]}${i}`;let f={};u(p)?f=p():c(p)&&(f=p);let y,g={...f,...e};return y="post"===r?o?e:JSON.stringify(g):"put"===r||"patch"===r?JSON.stringify(g):{params:g},l&&("string"==typeof l&&(m.defaults.headers.common.Authorization=`Bearer ${l}`),"function"==typeof l&&(m.defaults.headers.common.Authorization=`Bearer ${l()}`)),m[r](s,y).then((t=>t.data)).catch((t=>h&&h(t)))}}return y},t.default=s,Object.defineProperty(t,"__esModule",{value:!0})}));