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

99 lines
2.8 KiB
TypeScript
Raw Normal View History

import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from 'axios'
import Axios from 'axios'
2022-04-15 14:24:30 +00:00
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
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.
2024-04-04 22:20:42 +00:00
this.client.interceptors.request.use((config: AxiosRequestConfig) => {
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…
2024-04-04 22:20:42 +00:00
this.client.interceptors.response.use((response: AxiosResponse) => {
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
2024-04-04 22:20:42 +00:00
}, (error: AxiosError) => {
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
}
2024-04-23 21:01:27 +00:00
public request<T> (method: Method, url: string, data: Record<string, any> = {}, onUploadProgress?: any) {
return this.client.request({
url,
data,
method,
onUploadProgress,
2024-04-23 21:01:27 +00:00
}) as Promise<{ data: T }>
}
public async get<T> (url: string) {
return (await this.request<T>('get', url)).data
}
public async post<T> (url: string, data: Record<string, any> = {}, onUploadProgress?: any) {
return (await this.request<T>('post', url, data, onUploadProgress)).data
}
public async put<T> (url: string, data: Record<string, any>) {
return (await this.request<T>('put', url, data)).data
}
public async patch<T> (url: string, data: Record<string, any>) {
return (await this.request<T>('patch', url, data)).data
}
public async delete<T> (url: string, data: Record<string, any> = {}) {
return (await this.request<T>('delete', url, data)).data
}
private showLoadingIndicator () {
NProgress.start()
}
private hideLoadingIndicator () {
NProgress.done(true)
}
2022-04-15 14:24:30 +00:00
}
2022-05-14 23:37:16 +00:00
export const http = new Http()