UNPKG

wing-axios

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