import axios from 'axios'; import {AXIOS} from '../common/config/env.config'; import {AxiosInstance} from 'axios'; import {AUTH} from '../common/config/app.config'; export class AxiosService { private headers: any = { 'Accept': AXIOS.ACCEPT, 'Content-type': AXIOS.CONTENT_TYPE, }; private axios: AxiosInstance; constructor(baseURL: any) { this.axios = axios.create({ baseURL, timeout: AXIOS.TIMEOUT, }); } get(url: string, config?: any) { const headers = this.getHeader(); return this.axios .get(url, Object.assign(config || {}, {headers})) .then(_handleSuccess) .catch(_handleError); } post(url: string, config?: any) { const headers = this.getHeader(); return this.axios .post(url, config, {headers}) .then(_handleSuccess) .catch(_handleError); } private getHeader() { return Object.assign({ Authorization: `Bearer ${sessionStorage.getItem(AUTH.GET_AUTH_KEY)}`, }, this.headers); } } function _handleSuccess(result: any) { return result.data; } function _handleError(e: any) { if (e.response.data.error) { throw e.response.data.error; } else { throw Error(e); } }