Files
kandev/apps/web/lib/ws/utils.ts
T
2026-04-23 18:09:53 +01:00

27 lines
687 B
TypeScript

/**
* Convert HTTP URL to WebSocket URL.
* http://localhost:38429 → ws://localhost:38429/ws
* https://api.example.com → wss://api.example.com/ws
*/
export function httpToWebSocketUrl(baseUrl: string): string {
try {
const url = new URL(baseUrl);
const protocol = url.protocol === "https:" ? "wss:" : "ws:";
return `${protocol}//${url.host}/ws`;
} catch {
throw new Error(`Invalid URL format: ${baseUrl}`);
}
}
/**
* Validate backend URL format
*/
export function isValidBackendUrl(url: string): boolean {
try {
const parsed = new URL(url);
return parsed.protocol === "http:" || parsed.protocol === "https:";
} catch {
return false;
}
}