github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/web/src/plugins/http.ts (about) 1 import _Vue from 'vue'; 2 import Axios, { AxiosStatic, AxiosError } from 'axios'; 3 4 export function reasonToError(reason: Error | AxiosError): Error { 5 if ((reason as AxiosError).response) { 6 const response = (reason as AxiosError).response; 7 if (response) { 8 return response.data 9 ? new Error(response.data) 10 : new Error(`Server return status code ${response.status}`); 11 } 12 } else if (reason.message) { 13 return new Error(reason.message); 14 } 15 return new Error('Unknown Error'); 16 } 17 18 // eslint-disable-next-line 19 export function AxiosPlugin(Vue: typeof _Vue, options?: any): void { 20 Vue.prototype.$http = Axios; 21 Vue.prototype.$httpError = reasonToError; 22 } 23 24 declare module 'vue/types/vue' { 25 interface Vue { 26 readonly $http: AxiosStatic; 27 /** 28 * convert http reject reason to error object 29 */ 30 $httpError: typeof reasonToError; 31 } 32 }