hrms-checkin/src/plugins/http.ts

43 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-07-16 21:27:39 +07:00
import Axios, { type AxiosRequestConfig, type AxiosResponse } from 'axios'
2024-08-28 13:43:21 +07:00
import { getToken } from './auth'
2023-11-15 09:51:52 +07:00
const http = Axios.create({
timeout: 1000000000, // เพิ่มค่า timeout
headers: {
2024-07-16 21:27:39 +07:00
'X-Requested-With': 'XMLHttpRequest',
2023-11-15 09:51:52 +07:00
},
2024-07-16 21:27:39 +07:00
})
2023-11-15 09:51:52 +07:00
http.interceptors.request.use(
async function (config: AxiosRequestConfig<any>) {
2024-07-16 21:27:39 +07:00
config.headers = config.headers ?? {}
2024-08-28 13:43:21 +07:00
const token = await getToken()
2024-07-16 21:27:39 +07:00
if (token) config.headers.Authorization = `Bearer ${token}`
return config
2023-11-15 09:51:52 +07:00
},
function (error: any) {
2024-07-16 21:27:39 +07:00
return Promise.reject(error)
2023-11-15 09:51:52 +07:00
}
2024-07-16 21:27:39 +07:00
)
2023-11-15 09:51:52 +07:00
http.interceptors.response.use(
function (response: AxiosResponse<any, any>) {
2024-07-16 21:27:39 +07:00
return response
2023-11-15 09:51:52 +07:00
},
function (error: any) {
if (typeof error !== undefined) {
// eslint-disable-next-line no-prototype-builtins
2024-07-16 21:27:39 +07:00
if (error.hasOwnProperty('response')) {
2023-11-15 09:51:52 +07:00
if (error.response.status === 401 || error.response.status === 403) {
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
// Store.commit("REMOVE_ACCESS_TOKEN")
}
}
}
2024-07-16 21:27:39 +07:00
return Promise.reject(error)
2023-11-15 09:51:52 +07:00
}
2024-07-16 21:27:39 +07:00
)
2023-11-15 09:51:52 +07:00
2024-07-16 21:27:39 +07:00
export default http