/** Run all health checks and return the aggregated status. */
export async function checkHealth(version, checks) {
    const results = [];
    for (const { name, check } of checks) {
        const start = Date.now();
        try {
            await check();
            results.push({ name, status: 'pass', duration: Date.now() - start });
        }
        catch (err) {
            results.push({
                name,
                status: 'fail',
                message: err instanceof Error ? err.message : 'Unknown error',
                duration: Date.now() - start,
            });
        }
    }
    const hasFailure = results.some((c) => c.status === 'fail');
    const hasWarning = results.some((c) => c.status === 'warn');
    return {
        status: hasFailure ? 'unhealthy' : hasWarning ? 'degraded' : 'healthy',
        timestamp: new Date().toISOString(),
        version,
        checks: results,
    };
}
/** Create a standard database health check. */
export function databaseHealthCheck(db) {
    return async () => {
        await db.query('SELECT 1');
    };
}
/** Handle a health check HTTP request. */
export async function handleHealthRequest(version, checks) {
    const health = await checkHealth(version, checks);
    const status = health.status === 'healthy' ? 200 : health.status === 'degraded' ? 200 : 503;
    return new Response(JSON.stringify(health), {
        status,
        headers: { 'Content-Type': 'application/json' },
    });
}
//# sourceMappingURL=index.js.map