koel/resources/assets/js/services/http.ts

87 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import Axios, { AxiosInstance, Method } from 'axios'
import NProgress from 'nprogress'
import { eventBus } from '@/utils'
import { authService } from '@/services'
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
class Http {
client: AxiosInstance
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
private static setProgressBar () {
NProgress.start()
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
private static hideProgressBar () {
NProgress.done(true)
}
public request<T> (method: Method, url: string, data: Record<string, any> = {}, onUploadProgress?: any) {
return this.client.request({
2022-04-15 14:24:30 +00:00
url,
data,
method,
onUploadProgress
}) as Promise<{ data: T }>
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
public async get<T> (url: string) {
2022-04-15 14:24:30 +00:00
return (await this.request<T>('get', url)).data
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
public async post<T> (url: string, data: Record<string, any>, onUploadProgress?: any) {
2022-04-15 14:24:30 +00:00
return (await this.request<T>('post', url, data, onUploadProgress)).data
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
public async put<T> (url: string, data: Record<string, any>) {
2022-04-15 14:24:30 +00:00
return (await this.request<T>('put', url, data)).data
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
public async delete<T> (url: string, data: Record<string, any> = {}) {
2022-04-15 14:24:30 +00:00
return (await this.request<T>('delete', url, data)).data
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
constructor () {
2022-04-15 14:24:30 +00:00
this.client = Axios.create({
2022-06-10 10:47:46 +00:00
baseURL: `${window.BASE_URL}api`,
headers: {
'X-Api-Version': 'v6'
}
2022-04-15 14:24:30 +00:00
})
// Intercept the request to make sure the token is injected into the header.
this.client.interceptors.request.use(config => {
2022-05-14 23:37:16 +00:00
Http.setProgressBar()
2022-11-16 17:57:38 +00:00
config.headers.Authorization = `Bearer ${authService.getApiToken()}`
2022-04-15 14:24:30 +00:00
return config
})
// Intercept the response and…
this.client.interceptors.response.use(response => {
2022-05-14 23:37:16 +00:00
Http.hideProgressBar()
2022-04-15 14:24:30 +00:00
2023-08-20 22:35:58 +00:00
// …get the tokens from the header or response data if exist, and save them.
2022-06-10 10:47:46 +00:00
const token = response.headers.authorization || response.data.token
2022-11-16 17:57:38 +00:00
token && authService.setApiToken(token)
const audioToken = response.data['audio-token']
audioToken && authService.setAudioToken(audioToken)
2022-04-15 14:24:30 +00:00
return response
}, error => {
2022-05-14 23:37:16 +00:00
Http.hideProgressBar()
2022-04-15 14:24:30 +00:00
// Also, if we receive a Bad Request / Unauthorized error
2022-07-27 18:29:57 +00:00
if (error.response?.status === 400 || error.response?.status === 401) {
2022-04-15 14:24:30 +00:00
// and we're not trying to log in
if (!(error.config.method === 'post' && /\/api\/me\/?$/.test(error.config.url))) {
// the token must have expired. Log out.
eventBus.emit('LOG_OUT')
}
}
return Promise.reject(error)
})
}
}
2022-05-14 23:37:16 +00:00
export const http = new Http()