19 lines
732 B
TypeScript
19 lines
732 B
TypeScript
import type { HttpCheckResult } from "@/lib/types/http-result";
|
|||
|
|
|
||
|
|
export async function collectHttp(
|
||
|
|
url: string,
|
||
|
|
method: string,
|
||
|
|
timeoutMs: number,
|
||
|
|
expectedStatus: number
|
||
|
|
): Promise<Omit<HttpCheckResult, "consecutiveFailures">> {
|
||
|
|
const start = performance.now();
|
||
|
|
try {
|
||
|
|
const response = await fetch(url, { method, signal: AbortSignal.timeout(timeoutMs), redirect: "follow" });
|
||
|
|
const latencyMs = Math.round(performance.now() - start);
|
||
|
|
return { up: response.status === expectedStatus, statusCode: response.status, latencyMs, error: null };
|
||
|
|
} catch (err) {
|
||
|
|
const latencyMs = Math.round(performance.now() - start);
|
||
|
|
return { up: false, statusCode: null, latencyMs, error: (err as Error).message };
|
||
|
|
}
|
||
|
|
}
|