Skip to content

Commit c461609

Browse files
committed
Refactor API endpoint handling in admin and main JavaScript files to support explicit endpoint configuration. Update config.js to define a new API endpoint variable, enhancing flexibility in API integration.
1 parent ef90f13 commit c461609

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

assets/js/admin.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@ document.addEventListener('DOMContentLoaded', () => {
1111
const resetAuthButton = document.getElementById('reset-auth-button');
1212
const ADMIN_PASS_STORAGE_KEY = 'mb3r-admin-pass';
1313
const storage = window.MB3RStorage;
14-
const apiBaseUrl = (window.__MB3R_API_BASE__ || '').replace(/\/$/, '');
15-
const isApiConfigured = Boolean(apiBaseUrl);
14+
const explicitEndpoint =
15+
typeof window.__MB3R_API_ENDPOINT__ === 'string'
16+
? window.__MB3R_API_ENDPOINT__.trim().replace(/\/$/, '')
17+
: '';
18+
const apiBaseUrl =
19+
typeof window.__MB3R_API_BASE__ === 'string'
20+
? window.__MB3R_API_BASE__.trim().replace(/\/$/, '')
21+
: '';
22+
const isApiConfigured = Boolean(explicitEndpoint || apiBaseUrl);
1623
const shouldFallbackToLocal = (status) =>
1724
!status || status >= 500 || status === 404 || status === 405 || !isApiConfigured;
18-
const buildApiUrl = (path) => (isApiConfigured ? `${apiBaseUrl}${path}` : null);
25+
const resolveEndpoint = (path) => {
26+
if (explicitEndpoint) {
27+
return explicitEndpoint;
28+
}
29+
if (apiBaseUrl) {
30+
return `${apiBaseUrl}${path}`;
31+
}
32+
return null;
33+
};
1934
let currentPassword = sessionStorage.getItem(ADMIN_PASS_STORAGE_KEY) || '';
2035
const userLocale = navigator.language || 'en-US';
2136

@@ -121,7 +136,7 @@ document.addEventListener('DOMContentLoaded', () => {
121136
throw error;
122137
}
123138

124-
const endpoint = buildApiUrl('/applications');
139+
const endpoint = resolveEndpoint('/applications');
125140

126141
if (!endpoint) {
127142
const configError = new Error('API endpoint is not configured.');

assets/js/config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
window.__MB3R_API_BASE__ =
2-
window.__MB3R_API_BASE__ || 'https://nkphoglftmnfikxzuyqr.supabase.co/functions/v1';
1+
window.__MB3R_API_ENDPOINT__ =
2+
window.__MB3R_API_ENDPOINT__ ||
3+
'https://nkphoglftmnfikxzuyqr.supabase.co/functions/v1/database-access';
4+
window.__MB3R_API_BASE__ = window.__MB3R_API_BASE__ || '';

assets/js/main.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ document.addEventListener('DOMContentLoaded', () => {
44
const themeToggleButton = document.getElementById('theme-toggle');
55
const currentTheme = localStorage.getItem('theme');
66
const storage = window.MB3RStorage;
7-
const apiBaseUrl = (window.__MB3R_API_BASE__ || '').replace(/\/$/, '');
8-
const isApiConfigured = Boolean(apiBaseUrl);
7+
const explicitEndpoint =
8+
typeof window.__MB3R_API_ENDPOINT__ === 'string'
9+
? window.__MB3R_API_ENDPOINT__.trim().replace(/\/$/, '')
10+
: '';
11+
const apiBaseUrl =
12+
typeof window.__MB3R_API_BASE__ === 'string'
13+
? window.__MB3R_API_BASE__.trim().replace(/\/$/, '')
14+
: '';
15+
const isApiConfigured = Boolean(explicitEndpoint || apiBaseUrl);
916

1017
const createLocalRecord = (payload, source = 'local') => ({
1118
id: `${source}-${Date.now()}`,
@@ -19,11 +26,19 @@ document.addEventListener('DOMContentLoaded', () => {
1926
const shouldFallbackToLocal = (status) =>
2027
!status || status >= 500 || status === 404 || status === 405 || !isApiConfigured;
2128

22-
const buildApiUrl = (path) => (isApiConfigured ? `${apiBaseUrl}${path}` : null);
29+
const resolveEndpoint = (path) => {
30+
if (explicitEndpoint) {
31+
return explicitEndpoint;
32+
}
33+
if (apiBaseUrl) {
34+
return `${apiBaseUrl}${path}`;
35+
}
36+
return null;
37+
};
2338

2439
if (!isApiConfigured) {
2540
console.warn(
26-
'[mb3r] API base URL is not configured. Set window.__MB3R_API_BASE__ in assets/js/config.js.'
41+
'[mb3r] API endpoint is not configured. Set window.__MB3R_API_ENDPOINT__ (or __MB3R_API_BASE__).'
2742
);
2843
}
2944

@@ -150,7 +165,7 @@ document.addEventListener('DOMContentLoaded', () => {
150165
};
151166

152167
const submitRequest = async (payload) => {
153-
const endpoint = buildApiUrl('/applications');
168+
const endpoint = resolveEndpoint('/applications');
154169

155170
if (!endpoint) {
156171
const error = new Error('API endpoint is not configured.');

0 commit comments

Comments
 (0)