Created
February 11, 2019 14:38
-
-
Save MariMax/84a1edd3bafe28c1d00a3096045f1c47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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<T>(path: string, options?: AxiosRequestConfig, suspendError: boolean = false): Promise<T> { | |
| return new Promise((resolve, reject) => { | |
| axios | |
| .get<T>(`${baseURL}${path}`, options) | |
| .then((response: AxiosResponse) => resolve(response.data)) | |
| .catch((e: AxiosError) => { | |
| if (!suspendError) { | |
| showErrorToast(); | |
| } | |
| reject(e); | |
| }); | |
| }); | |
| }, | |
| post<T>(path: string, data?: any, options?: AxiosRequestConfig): Promise<T> { | |
| return new Promise((resolve, reject) => { | |
| axios | |
| .post<T>(`${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<any> { | |
| return new Promise((resolve, reject) => { | |
| axios | |
| .delete(`${baseURL}${path}`, options) | |
| .then((response: AxiosResponse) => resolve(response.data)) | |
| .catch((e: AxiosError) => { | |
| if (!suspendError) { | |
| showErrorToast(); | |
| } | |
| reject(e); | |
| }); | |
| }); | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment