| 1234567891011121314151617181920212223242526272829303132333435363738 |
- import axios from "axios";
- import { useAuthStore } from "@/stores/auth";
- import { mockAxiosResponse, mockMode } from "./mock";
- export const apiClient = axios.create({
- baseURL: "/gateway",
- timeout: 20000,
- });
- apiClient.interceptors.request.use((config) => {
- if (mockMode) {
- config.adapter = async (adapterConfig) => mockAxiosResponse(adapterConfig);
- }
- const { apiKey, userId } = useAuthStore.getState();
- if (apiKey) config.headers.set("x-api-key", apiKey);
- if (userId) config.headers.set("x-user-id", userId);
- return config;
- });
- apiClient.interceptors.response.use(
- (response) => response,
- (error) => {
- if (error.config && (error.response?.status === 404 || !error.response)) {
- return Promise.resolve(mockAxiosResponse(error.config));
- }
- if (error.response?.status === 401) {
- useAuthStore.getState().logout();
- if (window.location.pathname !== "/login") {
- window.location.assign("/login");
- }
- }
- return Promise.reject(error);
- },
- );
- export function tenantParams(extra?: Record<string, string | number | boolean | undefined | null>) {
- return { ...extra };
- }
|