mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: implement runtime configuration and API client enhancements
This commit is contained in:
parent
4663256eed
commit
78cabbc665
6 changed files with 225 additions and 4 deletions
16
apps/web/pages/_document.js
Normal file
16
apps/web/pages/_document.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { Html, Head, Main, NextScript } from 'next/document';
|
||||
|
||||
export default function Document() {
|
||||
return (
|
||||
<Html lang="en">
|
||||
<Head>
|
||||
{/* Load runtime configuration before any app code */}
|
||||
<script src="/runtime-config.js" />
|
||||
</Head>
|
||||
<body>
|
||||
<Main />
|
||||
<NextScript />
|
||||
</body>
|
||||
</Html>
|
||||
);
|
||||
}
|
||||
59
apps/web/utils/apiClient.js
Normal file
59
apps/web/utils/apiClient.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { getApiUrl } from './runtimeConfig';
|
||||
|
||||
/**
|
||||
* Creates an API client that uses runtime configuration
|
||||
* @returns {Object} API client object with fetch method
|
||||
*/
|
||||
export function createApiClient() {
|
||||
return {
|
||||
/**
|
||||
* Fetch data from the API with the correct runtime URL
|
||||
* @param {string} path - API path to fetch from
|
||||
* @param {Object} options - Fetch options
|
||||
* @returns {Promise<any>} - JSON response
|
||||
*/
|
||||
fetch: async (path, options = {}) => {
|
||||
const url = getApiUrl(path);
|
||||
console.log(`[API] Fetching from: ${url}`);
|
||||
|
||||
const response = await fetch(url, options);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* Post data to the API with the correct runtime URL
|
||||
* @param {string} path - API path to post to
|
||||
* @param {Object} data - Data to post
|
||||
* @param {Object} options - Additional fetch options
|
||||
* @returns {Promise<any>} - JSON response
|
||||
*/
|
||||
post: async (path, data, options = {}) => {
|
||||
const url = getApiUrl(path);
|
||||
console.log(`[API] Posting to: ${url}`);
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
...options,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Create a singleton instance for easier imports
|
||||
export const apiClient = createApiClient();
|
||||
52
apps/web/utils/runtimeConfig.js
Normal file
52
apps/web/utils/runtimeConfig.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* Utility functions to access runtime configuration values
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets configuration from runtime values (window.RUNTIME_CONFIG) with fallback to environment variables
|
||||
* This ensures dynamic configuration at runtime instead of build time
|
||||
*/
|
||||
export function useRuntimeConfig() {
|
||||
// For server-side rendering, use environment variables
|
||||
if (typeof window === 'undefined') {
|
||||
return {
|
||||
apiUrl: process.env.NEXT_PUBLIC_LEARNHOUSE_API_URL,
|
||||
backendUrl: process.env.NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL,
|
||||
domain: process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN,
|
||||
defaultOrg: process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG || 'default',
|
||||
multiOrg: process.env.NEXT_PUBLIC_LEARNHOUSE_MULTI_ORG === 'true',
|
||||
topDomain: process.env.NEXT_PUBLIC_LEARNHOUSE_TOP_DOMAIN
|
||||
};
|
||||
}
|
||||
|
||||
// For client-side, prioritize runtime config over baked-in environment variables
|
||||
const config = window.RUNTIME_CONFIG || {};
|
||||
return {
|
||||
apiUrl: config.LEARNHOUSE_API_URL || process.env.NEXT_PUBLIC_LEARNHOUSE_API_URL,
|
||||
backendUrl: config.LEARNHOUSE_BACKEND_URL || process.env.NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL,
|
||||
domain: config.LEARNHOUSE_DOMAIN || process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN,
|
||||
defaultOrg: config.LEARNHOUSE_DEFAULT_ORG || process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG || 'default',
|
||||
multiOrg: config.LEARNHOUSE_MULTI_ORG === 'true' || process.env.NEXT_PUBLIC_LEARNHOUSE_MULTI_ORG === 'true',
|
||||
topDomain: config.LEARNHOUSE_TOP_DOMAIN || process.env.NEXT_PUBLIC_LEARNHOUSE_TOP_DOMAIN
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the API URL with the correct domain
|
||||
* @param {string} path - The API path
|
||||
* @returns {string} - The complete API URL
|
||||
*/
|
||||
export function getApiUrl(path) {
|
||||
const config = useRuntimeConfig();
|
||||
return `${config.apiUrl || `https://${config.domain}/api/v1/`}${path}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the backend URL with the correct domain
|
||||
* @param {string} path - The backend path
|
||||
* @returns {string} - The complete backend URL
|
||||
*/
|
||||
export function getBackendUrl(path) {
|
||||
const config = useRuntimeConfig();
|
||||
return `${config.backendUrl || `https://${config.domain}/`}${path}`;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue