UNPKG

axios-server

Version:
2 lines (1 loc) 1.93 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("axios")):"function"==typeof define&&define.amd?define(["exports","axios"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["Axios-Server"]={},e.axios)}(this,(function(e,t){"use strict";function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=s(t);e.default=class{constructor(e,t,s=""){this.axiosInstance=a.default.create(e),e.useDefaultInterceptor&&(this.axiosInstance.interceptors.request.use((e=>e),(e=>Promise.reject(e))),this.axiosInstance.interceptors.response.use((e=>e.data),(e=>{var t;return Promise.reject(null===(t=e.response)||void 0===t?void 0:t.data)})));Object.keys(t).forEach((e=>{this[e]=(a={})=>{a=this._getOptions(a,t[e]);const{method:o,path:n="",query:r,body:i}=a;return this.axiosInstance({url:s+n,method:o,params:r,data:i,headers:Object.assign({},a.headers)})}}))}commit(e){return this.axiosInstance(e)}get(e,t){return this.axiosInstance.get(e,t)}post(e,t,s){return this.axiosInstance.post(e,t,s)}put(e,t,s){return this.axiosInstance.put(e,t,s)}delete(e,t){return this.axiosInstance.delete(e,t)}_getOptions(e={},t={}){const s=["api","path","params","headers","query","body","method","dataType","noAlert","mock","preProcess","process","download"],a=Object.assign({method:"get"},t,e),o="get"===a.method?"query":"body";!a[o]&&(a[o]={}),e.query&&t.query&&Object.assign(a.query,t.query,e.query),a.action&&(a.api=a.api||"",a.api=a.action,delete a.action),a.path=this._replacePath(a);for(const e in a)s.includes(e)?a[e]=a[e]:(a[o][e]=a[e],delete a[e]);return Object.keys(a[o]).length||a[o]instanceof FormData||delete a[o],a}_replacePath(e){const{api:t,path:s=""}=e;return s+("/"==t.substr(0,1)?t:"/"+t)}},e.handleService=function(e,t){return Object.keys(e).forEach((s=>{e[s].method||(e[s].method="get"),e[s].path||Object.assign(e[s],{path:t})})),e},Object.defineProperty(e,"__esModule",{value:!0})}));