62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
function trimTrailingSlash(value) {
|
|
return value ? value.replace(/\/$/, '') : value;
|
|
}
|
|
|
|
function getWindowOrigin() {
|
|
if (typeof window === 'undefined') {
|
|
return '';
|
|
}
|
|
|
|
return window.location.origin;
|
|
}
|
|
|
|
function toWsOrigin(value) {
|
|
if (!value) {
|
|
return value;
|
|
}
|
|
|
|
return value
|
|
.replace(/^http:\/\//i, 'ws://')
|
|
.replace(/^https:\/\//i, 'wss://');
|
|
}
|
|
|
|
export function getApiBaseUrl() {
|
|
return trimTrailingSlash(import.meta.env.VITE_API_BASE_URL || getWindowOrigin() || '');
|
|
}
|
|
|
|
export function getSocketIoUrl() {
|
|
return trimTrailingSlash(import.meta.env.VITE_SOCKET_IO_URL || getApiBaseUrl() || getWindowOrigin() || '');
|
|
}
|
|
|
|
export function getDaemonSocketUrl() {
|
|
const configured = import.meta.env.VITE_DAEMON_SOCKET;
|
|
if (configured) {
|
|
return configured;
|
|
}
|
|
|
|
return toWsOrigin(getWindowOrigin());
|
|
}
|
|
|
|
export function getPublicBaseUrl() {
|
|
return trimTrailingSlash(import.meta.env.VITE_PUBLIC_BASE_URL || getWindowOrigin() || 'https://www.your-part.de');
|
|
}
|
|
|
|
export function getChatWsUrlFromEnv() {
|
|
const directUrl = import.meta.env.VITE_CHAT_WS_URL;
|
|
if (directUrl) {
|
|
return directUrl.trim();
|
|
}
|
|
|
|
const host = import.meta.env.VITE_CHAT_WS_HOST;
|
|
const port = import.meta.env.VITE_CHAT_WS_PORT;
|
|
const protocol = import.meta.env.VITE_CHAT_WS_PROTOCOL || (typeof window !== 'undefined' && window.location.protocol === 'https:' ? 'wss' : 'ws');
|
|
|
|
if (host || port) {
|
|
const resolvedHost = host || (typeof window !== 'undefined' ? window.location.hostname : 'localhost');
|
|
const resolvedPort = port ? `:${port}` : '';
|
|
return `${protocol}://${resolvedHost}${resolvedPort}`;
|
|
}
|
|
|
|
return toWsOrigin(getWindowOrigin());
|
|
}
|