2018-11-15 19:48:37 +01:00
|
|
|
import axios, { AxiosInstance, AxiosPromise, AxiosResponse, AxiosInterceptorManager } from 'axios'
|
|
|
|
|
// import LoginModule from '../Modules/Auth/LoginStore'
|
|
|
|
|
import router from '@router'
|
2019-02-20 11:53:56 +01:00
|
|
|
import { clone } from 'lodash'
|
2018-11-15 19:48:37 +01:00
|
|
|
import * as Types from './ApiTypes'
|
2019-02-19 02:33:02 +01:00
|
|
|
import { GlobalStore, UserStore } from '@store'
|
|
|
|
|
import { rescodes } from '@src/store/Modules/rescodes'
|
|
|
|
|
import { serv_constants } from '@src/store/Modules/serv_constants'
|
2018-11-15 19:48:37 +01:00
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
export const API_URL = process.env.MONGODB_HOST
|
2018-11-15 19:48:37 +01:00
|
|
|
export const axiosInstance: AxiosInstance = axios.create({
|
|
|
|
|
baseURL: API_URL,
|
|
|
|
|
headers: {
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
axiosInstance.interceptors.response.use(
|
|
|
|
|
(response) => {
|
|
|
|
|
console.log(response)
|
|
|
|
|
return response
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
2019-02-20 11:53:56 +01:00
|
|
|
if (error.response) {
|
|
|
|
|
console.log(error.response.status)
|
|
|
|
|
console.log('Request Error: ', error.response)
|
|
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const addAuthHeaders = () => {
|
|
|
|
|
// axiosInstance.defaults.headers.Authorization = `Bearer ${LoginModule.state.userInfos.userToken}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const removeAuthHeaders = () => {
|
|
|
|
|
delete axiosInstance.defaults.headers.Authorization
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
async function Request(type: string, path: string, payload: any, setAuthToken?: boolean): Promise<Types.AxiosSuccess | Types.AxiosError> {
|
|
|
|
|
let ricevuto = false
|
2018-11-15 19:48:37 +01:00
|
|
|
try {
|
|
|
|
|
console.log(`Axios Request [${type}]:`, axiosInstance.defaults)
|
|
|
|
|
let response: AxiosResponse
|
2019-02-22 10:23:00 +01:00
|
|
|
if (type === 'post' || type === 'put' || type === 'patch') {
|
2018-11-15 19:48:37 +01:00
|
|
|
response = await axiosInstance[type](path, payload, {
|
|
|
|
|
headers: {
|
2019-02-19 02:33:02 +01:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'x-auth': UserStore.state.x_auth_token
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
})
|
2019-02-19 02:33:02 +01:00
|
|
|
ricevuto = true
|
2019-02-20 11:53:56 +01:00
|
|
|
console.log('response', response)
|
2019-02-19 02:33:02 +01:00
|
|
|
// console.log(new Types.AxiosSuccess(response.data, response.status))
|
|
|
|
|
|
|
|
|
|
const setAuthToken = (path === '/updatepwd')
|
|
|
|
|
|
2019-02-20 11:53:56 +01:00
|
|
|
if (response && (response.status === 200)) {
|
2019-02-19 02:33:02 +01:00
|
|
|
let x_auth_token = ''
|
|
|
|
|
try {
|
|
|
|
|
if (setAuthToken || (path === '/users/login')) {
|
|
|
|
|
x_auth_token = String(response.headers['x-auth'])
|
|
|
|
|
|
|
|
|
|
if (x_auth_token === '') {
|
|
|
|
|
UserStore.mutations.setServerCode(rescodes.ERR_AUTHENTICATION)
|
|
|
|
|
}
|
|
|
|
|
if (setAuthToken) {
|
|
|
|
|
UserStore.mutations.UpdatePwd(x_auth_token)
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.token, x_auth_token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserStore.mutations.setAuth(x_auth_token)
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.token, x_auth_token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GlobalStore.mutations.setStateConnection(ricevuto ? 'online' : 'offline')
|
|
|
|
|
UserStore.mutations.setServerCode(rescodes.OK)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (setAuthToken) {
|
|
|
|
|
UserStore.mutations.setServerCode(rescodes.ERR_AUTHENTICATION)
|
|
|
|
|
UserStore.mutations.setAuth('')
|
|
|
|
|
}
|
|
|
|
|
GlobalStore.mutations.setStateConnection(ricevuto ? 'online' : 'offline')
|
|
|
|
|
return Promise.reject(new Types.AxiosError(serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN, null, rescodes.ERR_AUTHENTICATION))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Types.AxiosSuccess(response.data, response.status)
|
2018-11-15 19:48:37 +01:00
|
|
|
} else if (type === 'get' || type === 'delete') {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
response = await axiosInstance[type](path, {
|
|
|
|
|
params: payload,
|
2019-02-20 11:53:56 +01:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
2019-02-19 02:33:02 +01:00
|
|
|
'x-auth': UserStore.state.x_auth_token
|
|
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
})
|
2019-02-19 02:33:02 +01:00
|
|
|
return new Types.AxiosSuccess(response.data, response.status)
|
2018-11-15 19:48:37 +01:00
|
|
|
} else if (type === 'postFormData') {
|
|
|
|
|
response = await axiosInstance.post(path, payload, {
|
|
|
|
|
headers: {
|
2019-02-19 02:33:02 +01:00
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
|
'x-auth': UserStore.state.x_auth_token
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
})
|
2019-02-19 02:33:02 +01:00
|
|
|
return new Types.AxiosSuccess(response.data, response.status)
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (error) {
|
2019-02-22 10:23:00 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
|
GlobalStore.state.connData.uploading_server = (GlobalStore.state.connData.uploading_server === 1) ? -1 : GlobalStore.state.connData.uploading_server
|
|
|
|
|
GlobalStore.state.connData.downloading_server = (GlobalStore.state.connData.downloading_server === 1) ? -1 : GlobalStore.state.connData.downloading_server
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
if (process.env.DEV) {
|
2019-02-20 11:53:56 +01:00
|
|
|
console.log('ERROR using', path)
|
|
|
|
|
// console.log('Error received: ', error)
|
|
|
|
|
// console.log('ricevuto=', ricevuto)
|
|
|
|
|
console.log('error.response=', error.response)
|
2019-02-19 02:33:02 +01:00
|
|
|
}
|
2019-02-20 11:53:56 +01:00
|
|
|
let mycode = 0
|
2019-02-19 02:33:02 +01:00
|
|
|
if (!ricevuto) {
|
2019-02-20 11:53:56 +01:00
|
|
|
mycode = rescodes.ERR_SERVERFETCH
|
2019-02-19 02:33:02 +01:00
|
|
|
UserStore.mutations.setServerCode(rescodes.ERR_SERVERFETCH)
|
|
|
|
|
} else {
|
2019-02-20 11:53:56 +01:00
|
|
|
mycode = rescodes.ERR_GENERICO
|
2019-02-19 02:33:02 +01:00
|
|
|
UserStore.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
if (error.response) {
|
2019-02-20 11:53:56 +01:00
|
|
|
if (error.response.data && error.response.data.code) {
|
|
|
|
|
mycode = error.response.data.code
|
|
|
|
|
UserStore.mutations.setServerCode(mycode)
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(new Types.AxiosError(error.response.status, error.response.data, error.response.data.code))
|
2018-11-15 19:48:37 +01:00
|
|
|
} else {
|
2019-02-20 11:53:56 +01:00
|
|
|
return Promise.reject(new Types.AxiosError(0, null, mycode, error))
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Request
|