Created
September 20, 2018 08:15
-
-
Save akhbar/56e798c9d024eafff5f0819497737c46 to your computer and use it in GitHub Desktop.
axioscall
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 from 'axios'; | |
| import { | |
| isNull | |
| } from 'util'; | |
| import swal from 'sweetalert2'; | |
| import port from '../../../Properties/launchSettings.json'; | |
| import { | |
| store | |
| } from '../../store/store.js'; | |
| import router from '../../router/index.js'; | |
| var url = '/api/', | |
| getStore = store.getters.data, | |
| getToken = () => { | |
| console.log("Token : " + getStore.secureToken.secure_token.token); | |
| return (getStore.secureToken !== undefined && getStore.secureToken.secure_token !== undefined) ? `Bearer ${getStore.secureToken.secure_token.token}` : ''; | |
| }; | |
| var axiosOBJ = axios.create({ | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| Authorization: getToken() | |
| } | |
| }); | |
| axiosOBJ.interceptors.request.use(req => { | |
| req.headers.Authorization = getToken(); | |
| return req; | |
| }); | |
| axiosOBJ.interceptors.response.use(res => { | |
| return Promise.resolve(res.data); | |
| }, err => { | |
| if (err.response.status == "401") { | |
| swal({ | |
| type: 'error', | |
| title: 'Kesalahan', | |
| text: 'Terjadi Kesalahan Pada Sistem', | |
| allowOutsideClick: false, | |
| onClose: () => { | |
| router.replace({ | |
| name: 'Login' | |
| }) | |
| } | |
| }) | |
| } else { | |
| swal({ | |
| type: 'error', | |
| title: 'Kesalahan', | |
| text: 'Terjadi Kesalahan Pada Sistem', | |
| allowOutsideClick: false, | |
| }) | |
| } | |
| return Promise.reject(err); | |
| }) | |
| class AxiosCall { | |
| static Post(payload, path) { | |
| return axiosOBJ.post(url + path, JSON.stringify(payload)); | |
| } | |
| static Get(path) { | |
| return axiosOBJ.get(url + path) | |
| } | |
| } | |
| export default AxiosCall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment