Files
flyer-crawler.projectium.com/src/utils/serverUtils.ts
Torben Sorensen 2651bd16ae
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 52s
test fixes
2026-01-03 20:01:10 -08:00

26 lines
1.0 KiB
TypeScript

// src/utils/serverUtils.ts
import type { Logger } from 'pino';
/**
* Constructs a fully qualified base URL for generating absolute URLs.
* It prioritizes `FRONTEND_URL`, then `BASE_URL`, and falls back to a localhost URL
* based on the `PORT` environment variable. It also logs a warning if the provided
* URL is invalid or missing.
*
* @param logger - The logger instance to use for warnings.
* @returns A validated, fully qualified base URL without a trailing slash.
*/
export function getBaseUrl(logger: Logger): string {
let baseUrl = (process.env.FRONTEND_URL || process.env.BASE_URL || '').trim();
if (!baseUrl || !baseUrl.startsWith('http')) {
const port = process.env.PORT || 3000;
const fallbackUrl = `http://localhost:${port}`;
if (baseUrl) {
logger.warn(
`[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('${baseUrl}'). Falling back to default local URL: ${fallbackUrl}`,
);
}
baseUrl = fallbackUrl;
}
return baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
}