(
baseUrl: string,
options?: { timeoutMs?: number }
)
| 60 | } |
| 61 | |
| 62 | export async function checkServerReachable( |
| 63 | baseUrl: string, |
| 64 | options?: { timeoutMs?: number } |
| 65 | ): Promise<ServerReachabilityResult> { |
| 66 | const timeoutMs = options?.timeoutMs ?? 1_000; |
| 67 | const normalizedBaseUrl = normalizeBaseUrl(baseUrl); |
| 68 | |
| 69 | const controller = new AbortController(); |
| 70 | const timeout = setTimeout(() => controller.abort(), timeoutMs); |
| 71 | |
| 72 | try { |
| 73 | const resp = await fetch(`${normalizedBaseUrl}/health`, { |
| 74 | method: "GET", |
| 75 | signal: controller.signal, |
| 76 | }); |
| 77 | |
| 78 | if (!resp.ok) { |
| 79 | return { status: "unreachable", error: `HTTP ${resp.status} from /health` }; |
| 80 | } |
| 81 | |
| 82 | const data = (await resp.json()) as { status?: unknown }; |
| 83 | if (data.status !== "ok") { |
| 84 | return { |
| 85 | status: "unreachable", |
| 86 | error: "Unexpected /health response", |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | return { status: "ok" }; |
| 91 | } catch (error) { |
| 92 | return { |
| 93 | status: "unreachable", |
| 94 | error: formatError(error), |
| 95 | }; |
| 96 | } finally { |
| 97 | clearTimeout(timeout); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | export async function checkAuth( |
| 102 | client: ApiClient, |
no test coverage detected