|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { ValidateURL } from "@src/utilities"; |
| 3 | + |
1 | 4 | export const getApiBaseUrl = (): string => {
|
2 |
| - const buildUrl = (url: string, useProxy: boolean): string => { |
3 |
| - if (url.match(/^(http|https):\/\//)) { |
4 |
| - return useProxy ? "/api" : url; // If proxying, return "/api"; otherwise, use the full URL |
5 |
| - } else { |
6 |
| - const path = url.startsWith("/") ? url.substring(1) : url; |
7 |
| - return `${window.location.origin}/${path}`; |
| 5 | + if (window.appConfig?.rerouteApi) { |
| 6 | + if (window.appConfig.rerouteApi.toString()?.toLowerCase === "true") return "/api"; |
| 7 | + else { |
| 8 | + console.error("Reroute API is set to false. Please set it to 'true' to reroute the API."); |
8 | 9 | }
|
9 |
| - }; |
| 10 | + } |
10 | 11 |
|
11 |
| - // Check runtime config first (injected via config.js) |
12 | 12 | if (window.appConfig?.apiBaseUrl) {
|
13 |
| - const useProxy = window.appConfig.proxy === "true" || window.appConfig.proxy === true; |
14 |
| - return buildUrl(window.appConfig.apiBaseUrl, useProxy); |
| 13 | + if (ValidateURL(window.appConfig.apiBaseUrl)) return window.appConfig.apiBaseUrl; |
| 14 | + else { |
| 15 | + console.error("Invalid API base URL found. Please set a valid URL in your environment variables."); |
| 16 | + } |
15 | 17 | }
|
16 | 18 |
|
17 |
| - // Fallback to Vite env var (for dev or if config.js isn’t set) |
18 | 19 | const hostUrl = import.meta.env.VITE_HOST_URL;
|
19 | 20 | if (hostUrl) {
|
20 |
| - return buildUrl(hostUrl, false); // Default to no proxy for Vite env |
| 21 | + if (ValidateURL(hostUrl)) return hostUrl; |
| 22 | + else { |
| 23 | + console.error("Invalid HOST_BASE_URL found. Please set a valid URL in your environment variables."); |
| 24 | + return ""; |
| 25 | + } |
21 | 26 | }
|
22 | 27 |
|
23 |
| - // Fallback logic based on hostname |
24 | 28 | const { hostname } = window.location;
|
25 | 29 | if (hostname === "app.autokitteh.cloud") {
|
26 | 30 | return "https://api.autokitteh.cloud";
|
|
0 commit comments