mirror of
https://github.com/khairul169/vaulterm.git
synced 2025-04-28 16:49:39 +07:00
32 lines
940 B
TypeScript
32 lines
940 B
TypeScript
import authStore from "@/stores/auth";
|
|
import { QueryClient } from "@tanstack/react-query";
|
|
import { ofetch } from "ofetch";
|
|
|
|
export const BASE_API_URL = process.env.EXPO_PUBLIC_API_URL || ""; //"http://10.0.0.100:3000";
|
|
export const BASE_WS_URL = BASE_API_URL.replace("http", "ws");
|
|
|
|
const api = ofetch.create({
|
|
baseURL: BASE_API_URL,
|
|
onRequest: (config) => {
|
|
const authToken = authStore.getState().token;
|
|
if (authToken) {
|
|
config.options.headers.set("Authorization", `Bearer ${authToken}`);
|
|
}
|
|
},
|
|
onResponseError: (error) => {
|
|
if (error.response.status === 401 && !!authStore.getState().token) {
|
|
authStore.setState({ token: null });
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
if (error.response._data) {
|
|
const message = error.response._data.message;
|
|
throw new Error(message || "Something went wrong");
|
|
}
|
|
},
|
|
});
|
|
|
|
export const queryClient = new QueryClient();
|
|
|
|
export default api;
|