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

98 lines
2.7 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
private silent = false
private showLoadingIndicator () {
2022-05-14 23:37:16 +00:00
NProgress.start()
}
2022-04-15 14:24:30 +00:00
private hideLoadingIndicator () {
2022-05-14 23:37:16 +00:00
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
2024-01-18 11:13:05 +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
public async patch<T> (url: string, data: Record<string, any>) {
return (await this.request<T>('patch', url, data)).data
}
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 => {
this.silent || this.showLoadingIndicator()
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 => {
this.silent || this.hideLoadingIndicator()
this.silent = false
2022-04-15 14:24:30 +00:00
2024-01-18 11:13:05 +00:00
// …get the tokens from the header if exist, and save them
// This occurs during user updating password.
const token = response.headers.authorization
2022-11-16 17:57:38 +00:00
token && authService.setApiToken(token)
2022-04-15 14:24:30 +00:00
return response
}, error => {
this.silent || this.hideLoadingIndicator()
this.silent = false
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' && error.config.url === 'me')) {
2022-04-15 14:24:30 +00:00
// the token must have expired. Log out.
eventBus.emit('LOG_OUT')
}
}
return Promise.reject(error)
})
}
public get silently () {
this.silent = true
return this
}
2022-04-15 14:24:30 +00:00
}
2022-05-14 23:37:16 +00:00
export const http = new Http()