Skip to content

Commit 692a426

Browse files
committed
Config from file
1 parent 688f0cb commit 692a426

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

ultroid/public/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"apiUrl": ""
3+
}

ultroid/utils/api.ts

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
1-
const API_URL = process.env.NEXT_PUBLIC_ULTROID_API_URL;
2-
3-
if (!API_URL) {
4-
throw new Error('NEXT_PUBLIC_ULTROID_API_URL is not defined in environment variables');
1+
// Declare global window interface
2+
declare global {
3+
interface Window {
4+
__ULTROID_CONFIG__: {
5+
apiUrl: string;
6+
};
7+
}
58
}
69

10+
// Helper function to get API URL - only fetches once and caches in window object
11+
const getApiUrl = async (): Promise<string> => {
12+
// Return from window if already loaded
13+
if (typeof window !== 'undefined' && window.__ULTROID_CONFIG__?.apiUrl) {
14+
return window.__ULTROID_CONFIG__.apiUrl;
15+
}
16+
17+
try {
18+
const response = await fetch('/config.json');
19+
const config = await response.json();
20+
21+
// Store in window object
22+
if (typeof window !== 'undefined') {
23+
24+
25+
window.__ULTROID_CONFIG__ = {
26+
apiUrl: config.apiUrl
27+
};
28+
}
29+
30+
return config.apiUrl || process.env.NEXT_PUBLIC_ULTROID_API_URL;
31+
} catch (error) {
32+
console.error('Failed to load config:', error);
33+
// Fallback to env variable if config fails
34+
const fallbackUrl = process.env.NEXT_PUBLIC_ULTROID_API_URL;
35+
36+
// Store fallback in window object
37+
if (typeof window !== 'undefined') {
38+
window.__ULTROID_CONFIG__ = {
39+
apiUrl: fallbackUrl
40+
};
41+
}
42+
43+
return fallbackUrl;
44+
}
45+
};
46+
747
// For Telegram Web App, we need to use HTTPS in production
848
// but for local development, we'll use HTTP
9-
const BASE_URL = API_URL;
49+
const BASE_URL = await getApiUrl();
1050

1151
export type UserData = {
1252
name: string;
@@ -39,6 +79,11 @@ const getInitData = (): string | null => {
3979

4080
// Helper function to make authenticated API calls
4181
const makeAuthenticatedRequest = async (endpoint: string, method: string = 'GET') => {
82+
const apiUrl = await getApiUrl();
83+
if (!apiUrl) {
84+
throw new Error('API URL is not configured');
85+
}
86+
4287
const initData = getInitData();
4388
const headers: HeadersInit = {
4489
'Content-Type': 'application/json'
@@ -50,7 +95,7 @@ const makeAuthenticatedRequest = async (endpoint: string, method: string = 'GET'
5095
console.warn('No init data available - request will not be authenticated');
5196
}
5297

53-
const response = await fetch(`${BASE_URL}${endpoint}`, {
98+
const response = await fetch(`${apiUrl}${endpoint}`, {
5499
method,
55100
headers
56101
});
@@ -69,6 +114,15 @@ const makeAuthenticatedRequest = async (endpoint: string, method: string = 'GET'
69114
};
70115

71116
export const api = {
117+
// Set API URL programmatically
118+
setApiUrl(url: string) {
119+
if (typeof window !== 'undefined') {
120+
window.__ULTROID_CONFIG__ = {
121+
apiUrl: url
122+
};
123+
}
124+
},
125+
72126
async getUserData(): Promise<UserData> {
73127
try {
74128
return await makeAuthenticatedRequest('/api/user');

0 commit comments

Comments
 (0)