Created
October 27, 2018 06:22
-
-
Save 197291/f5d1c1fe4cd5086134c599ae6b7b08e7 to your computer and use it in GitHub Desktop.
Api
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 } from 'axios'; | |
import queryString from 'query-string'; | |
import { Store } from 'redux'; | |
import { isNil, omitBy, flow } from 'lodash'; | |
import { BASE_URL } from 'redux/constants'; | |
import { showError } from 'redux/actions'; | |
import { isIe } from 'helpers'; | |
const processConfig = (config: AxiosRequestConfig): AxiosRequestConfig => { | |
let _config = { | |
...config, | |
withCredentials: true, | |
paramsSerializer: flow((params) => omitBy(params, isNil), queryString.stringify), | |
} as AxiosRequestConfig; | |
if (isIe()) { | |
_config.headers = { | |
..._config.headers, | |
'Cache-Control': 'no-cache', | |
'Pragma': 'no-cache', | |
}; | |
} | |
return _config; | |
}; | |
class Api { | |
static _instance: Api = new Api(); | |
store: Store<any> = null; | |
static getInstance(): Api { | |
return Api._instance; | |
} | |
constructor() { | |
if (Api._instance) { | |
throw new Error('Instantiation failed: use Api.getInstance() instead of new.'); | |
} | |
} | |
get(url: string, config?: AxiosRequestConfig) { | |
return new Promise((resolve, reject) => { | |
axios.get(this.getUrl(url), processConfig(config)) | |
.then(({data}) => resolve(data)) | |
.catch((error: any) => { | |
this.store.dispatch(showError(error)); | |
return reject(error); | |
}); | |
}); | |
} | |
put(url: string, payload: any = {}, config?: AxiosRequestConfig) { | |
return new Promise((resolve, reject) => { | |
axios.put(this.getUrl(url), payload, processConfig(config)) | |
.then(({data}) => resolve(data)) | |
.catch((error: any) => { | |
this.store.dispatch(showError(error)); | |
return reject(error); | |
}); | |
}); | |
} | |
post(url: string, payload: any = {}, config?: AxiosRequestConfig) { | |
return new Promise((resolve, reject) => { | |
axios.post(this.getUrl(url), payload, processConfig(config)) | |
.then(({data}) => resolve(data)) | |
.catch((error: any) => { | |
this.store.dispatch(showError(error)); | |
return reject(error); | |
}); | |
}); | |
} | |
delete(url: string, config?: AxiosRequestConfig) { | |
return new Promise((resolve, reject) => { | |
axios.delete(this.getUrl(url), processConfig(config)) | |
.then(({data}) => resolve(data)) | |
.catch((error: any) => { | |
this.store.dispatch(showError(error)); | |
return reject(error); | |
}); | |
}); | |
} | |
get CancelToken() { | |
return axios.CancelToken; | |
} | |
private getUrl(url: string): string { | |
return (!(url.includes('.json') || url.includes('' + BASE_URL)) ? BASE_URL : '') + url; | |
} | |
} | |
export default Api.getInstance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment