diff --git a/src/vertex/lib/envConstants.ts b/src/vertex/lib/envConstants.ts index 5ff4c51e8a..a0da34b29f 100644 --- a/src/vertex/lib/envConstants.ts +++ b/src/vertex/lib/envConstants.ts @@ -4,18 +4,23 @@ */ import { stripTrailingSlash } from './utils'; +/** + * Gets the default API URL fallback based on the current environment + * @returns {string} The default API URL + */ +export const getDefaultApiUrl = (): string => { + const env = getEnvironment().toLowerCase(); + if (env === 'production') return 'https://vertex.airqo.net/api/v2'; + if (env === 'staging') return 'https://staging-vertex.airqo.net/api/v2'; + return 'http://localhost:3000'; +}; + /** * Gets the API base URL from environment variables * @returns {string} The API base URL - * @throws {Error} If API_URL is not defined */ export const getApiBaseUrl = (): string => { - const apiUrl = process.env.NEXT_PUBLIC_API_URL; - - if (!apiUrl) { - throw new Error('NEXT_PUBLIC_API_URL environment variable is not defined'); - } - + const apiUrl = process.env.NEXT_PUBLIC_API_URL || getDefaultApiUrl(); return stripTrailingSlash(apiUrl); }; @@ -153,7 +158,6 @@ export const getEnvConfig = () => { */ export const validateEnvironment = (): boolean => { const required = [ - 'NEXT_PUBLIC_API_URL', 'NEXT_PUBLIC_API_TOKEN', 'NEXT_PUBLIC_CLOUDINARY_NAME', 'NEXT_PUBLIC_CLOUDINARY_PRESET', diff --git a/src/vertex/vertex.config.ts b/src/vertex/vertex.config.ts index b427a63c67..012b37720e 100644 --- a/src/vertex/vertex.config.ts +++ b/src/vertex/vertex.config.ts @@ -3,6 +3,7 @@ import { validateVertexConfig, type VertexConfigInput, } from "./core/config/vertex-config"; +import { getDefaultApiUrl } from "./lib/envConstants"; const config: VertexConfigInput = { ...defaultVertexConfig, @@ -17,9 +18,9 @@ const config: VertexConfigInput = { }, api: { adapter: "airqo", - baseUrl: process.env.NEXT_PUBLIC_API_URL, + baseUrl: process.env.NEXT_PUBLIC_API_URL || getDefaultApiUrl(), publicMeasurementsBaseUrl: - process.env.NEXT_PUBLIC_API_BASE_URL || "https://api.airqo.net", + process.env.NEXT_PUBLIC_API_BASE_URL || getDefaultApiUrl(), }, auth: { provider: "airqo",