diff --git a/.env.development b/.env.development new file mode 100644 index 00000000..dc27dbcb --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +VITE_API_URL=http://localhost:8090 diff --git a/.env.production b/.env.production new file mode 100644 index 00000000..7bcd4274 --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +VITE_API_URL=/ diff --git a/packages/shared/src/utils/api-client.ts b/packages/shared/src/utils/api-client.ts index d8419b2e..5d87f360 100644 --- a/packages/shared/src/utils/api-client.ts +++ b/packages/shared/src/utils/api-client.ts @@ -12,14 +12,15 @@ import { CoreHaloRunV1alpha1LinkGroupApi, } from "@halo-dev/api-client"; import axios from "axios"; +import type { AxiosInstance } from "axios"; -const baseUrl = "http://localhost:8090"; - +let apiUrl: string | undefined; const axiosInstance = axios.create({ - baseURL: "http://localhost:8090", withCredentials: true, }); +let apiClient = setupApiClient(axiosInstance); + axiosInstance.interceptors.response.use( (response) => { return response; @@ -33,38 +34,38 @@ axiosInstance.interceptors.response.use( } ); -const apiClient = { - extension: { - configMap: new V1alpha1ConfigMapApi(undefined, baseUrl, axiosInstance), - personalAccessToken: new V1alpha1PersonalAccessTokenApi( - undefined, - baseUrl, - axiosInstance - ), - roleBinding: new V1alpha1RoleBindingApi(undefined, baseUrl, axiosInstance), - role: new V1alpha1RoleApi(undefined, baseUrl, axiosInstance), - setting: new V1alpha1SettingApi(undefined, baseUrl, axiosInstance), - reverseProxy: new PluginHaloRunV1alpha1ReverseProxyApi( - undefined, - baseUrl, - axiosInstance - ), - plugin: new PluginHaloRunV1alpha1PluginApi( - undefined, - baseUrl, - axiosInstance - ), - user: new V1alpha1UserApi(undefined, baseUrl, axiosInstance), - - // TODO optional - link: new CoreHaloRunV1alpha1LinkApi(undefined, baseUrl, axiosInstance), - linkGroup: new CoreHaloRunV1alpha1LinkGroupApi( - undefined, - baseUrl, - axiosInstance - ), - }, - user: new ApiHaloRunV1alpha1UserApi(undefined, baseUrl, axiosInstance), +const setApiUrl = (url: string) => { + axiosInstance.defaults.baseURL = url; + apiUrl = url; + apiClient = setupApiClient(axiosInstance); }; -export { apiClient }; +function setupApiClient(axios: AxiosInstance) { + return { + extension: { + configMap: new V1alpha1ConfigMapApi(undefined, apiUrl, axios), + personalAccessToken: new V1alpha1PersonalAccessTokenApi( + undefined, + apiUrl, + axios + ), + roleBinding: new V1alpha1RoleBindingApi(undefined, apiUrl, axios), + role: new V1alpha1RoleApi(undefined, apiUrl, axios), + setting: new V1alpha1SettingApi(undefined, apiUrl, axios), + reverseProxy: new PluginHaloRunV1alpha1ReverseProxyApi( + undefined, + apiUrl, + axios + ), + plugin: new PluginHaloRunV1alpha1PluginApi(undefined, apiUrl, axios), + user: new V1alpha1UserApi(undefined, apiUrl, axios), + + // TODO optional + link: new CoreHaloRunV1alpha1LinkApi(undefined, apiUrl, axios), + linkGroup: new CoreHaloRunV1alpha1LinkGroupApi(undefined, apiUrl, axios), + }, + user: new ApiHaloRunV1alpha1UserApi(undefined, apiUrl, axios), + }; +} + +export { apiClient, setApiUrl }; diff --git a/src/main.ts b/src/main.ts index ec6a896b..26d3a436 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,7 +8,7 @@ import type { MenuItemType, Plugin, } from "@halo-dev/admin-shared"; -import { apiClient } from "@halo-dev/admin-shared"; +import { apiClient, setApiUrl } from "@halo-dev/admin-shared"; import { menus, minimenus, registerMenu } from "./router/menus.config"; // setup import "./setup/setupStyles"; @@ -25,6 +25,7 @@ import { useRoleStore } from "@/stores/role"; const app = createApp(App); setupComponents(app); +setApiUrl(import.meta.env.VITE_API_URL); app.use(createPinia());