bazarr/frontend/src/apis/raw/client.ts

98 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-08-23 15:13:46 +00:00
import socketio from "@/modules/socketio";
2022-06-14 14:13:28 +00:00
import { notification } from "@/modules/task";
import { LOG } from "@/utilities/console";
2022-08-23 15:13:46 +00:00
import { setAuthenticated } from "@/utilities/event";
2022-06-14 14:13:28 +00:00
import { showNotification } from "@mantine/notifications";
2021-03-25 14:22:43 +00:00
import Axios, { AxiosError, AxiosInstance, CancelTokenSource } from "axios";
2022-05-31 15:49:04 +00:00
import { Environment } from "../../utilities";
2022-06-14 14:13:28 +00:00
function GetErrorMessage(data: unknown, defaultMsg = "Unknown error"): string {
if (typeof data === "string") {
return data;
} else {
return defaultMsg;
}
}
class BazarrClient {
2021-03-25 14:22:43 +00:00
axios!: AxiosInstance;
source!: CancelTokenSource;
2022-08-23 15:13:46 +00:00
bIsAuthenticated: boolean;
2021-03-25 14:22:43 +00:00
constructor() {
2022-08-23 15:13:46 +00:00
this.bIsAuthenticated = false;
const baseUrl = `${Environment.baseUrl}/api/`;
this.initialize(baseUrl, Environment.apiKey);
2022-08-23 15:13:46 +00:00
socketio.initialize();
2021-03-25 14:22:43 +00:00
}
initialize(url: string, apikey?: string) {
2022-08-23 15:13:46 +00:00
LOG("info", "initializing BazarrClient with baseUrl", url);
2021-03-25 14:22:43 +00:00
this.axios = Axios.create({
baseURL: url,
});
this.axios.defaults.headers.post["Content-Type"] = "application/json";
this.axios.defaults.headers.common["X-API-KEY"] = apikey ?? "AUTH_NEEDED";
2021-03-25 14:22:43 +00:00
this.source = Axios.CancelToken.source();
this.axios.interceptors.request.use((config) => {
config.cancelToken = this.source.token;
return config;
});
this.axios.interceptors.response.use(
(resp) => {
if (resp.status >= 200 && resp.status < 300) {
2022-08-23 15:13:46 +00:00
if (!this.bIsAuthenticated) {
this.bIsAuthenticated = true;
setAuthenticated(true);
}
2021-03-25 14:22:43 +00:00
return Promise.resolve(resp);
} else {
2022-06-14 14:13:28 +00:00
const error: BackendError = {
code: resp.status,
message: GetErrorMessage(resp.data),
};
this.handleError(error);
2021-03-25 14:22:43 +00:00
return Promise.reject(resp);
}
},
(error: AxiosError) => {
2022-06-14 14:13:28 +00:00
const message = GetErrorMessage(
error.response?.data,
"You have disconnected from the server"
);
const backendError: BackendError = {
code: error.response?.status ?? 500,
message,
};
error.message = backendError.message;
this.handleError(backendError);
2021-03-25 14:22:43 +00:00
return Promise.reject(error);
}
);
}
2022-06-14 14:13:28 +00:00
handleError(error: BackendError) {
const { code, message } = error;
2021-03-25 14:22:43 +00:00
switch (code) {
case 401:
2022-08-23 15:13:46 +00:00
this.bIsAuthenticated = false;
setAuthenticated(false);
2022-07-26 13:27:01 +00:00
return;
2021-03-25 14:22:43 +00:00
}
2022-06-14 14:13:28 +00:00
LOG("error", "A error has occurred", code);
showNotification(notification.error(`Error ${code}`, message));
2021-03-25 14:22:43 +00:00
}
}
export default new BazarrClient();