import axios, {AxiosRequestConfig, AxiosResponse, AxiosError} from 'axios'; import Toasted from 'vue-toasted'; import Vue from 'vue'; import {COMMON_TOAST_DELAY} from '@/config/common-constants'; Vue.use(Toasted); export const domainUrl = 'https://api.takemobi.com/'; export const baseURL = `${domainUrl}`; function showErrorToast() { Vue.toasted.error('Somthing bad happened, please try again later', { position: 'top-right', className: 'toast-error', duration: COMMON_TOAST_DELAY, action: { text: 'CLOSE', onClick: (event, toastObject) => { toastObject.goAway(0); }, }, singleton: true, }); } export const APIService = { get(path: string, options?: AxiosRequestConfig, suspendError: boolean = false): Promise { return new Promise((resolve, reject) => { axios .get(`${baseURL}${path}`, options) .then((response: AxiosResponse) => resolve(response.data)) .catch((e: AxiosError) => { if (!suspendError) { showErrorToast(); } reject(e); }); }); }, post(path: string, data?: any, options?: AxiosRequestConfig): Promise { return new Promise((resolve, reject) => { axios .post(`${baseURL}${path}`, data, options) .then((response: AxiosResponse) => resolve(response.data)) .catch((e: AxiosError) => { showErrorToast(); reject(e); }); }); }, delete(path: string, options?: AxiosRequestConfig, suspendError: boolean = false): Promise { return new Promise((resolve, reject) => { axios .delete(`${baseURL}${path}`, options) .then((response: AxiosResponse) => resolve(response.data)) .catch((e: AxiosError) => { if (!suspendError) { showErrorToast(); } reject(e); }); }); }, };