All files / apps/api-gateway/src/health downstream.health.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 272x 2x   2x     2x   4x 4x     2x 1x   1x   3x                  
import { Injectable } from '@nestjs/common';
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus';
 
const PROBE_TIMEOUT_MS = 1_000;
 
@Injectable()
export class DownstreamHealthIndicator {
  async check(key: string, baseUrl: string): Promise<HealthIndicatorResult> {
    try {
      const response = await fetch(`${baseUrl}/health`, {
        signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
      });
      if (!response.ok) {
        throw new Error(`responded ${response.status}`);
      }
      return { [key]: { status: 'up' } };
    } catch (error) {
      throw new HealthCheckError(`${key} is unreachable`, {
        [key]: {
          status: 'down',
          message: error instanceof Error ? error.message : String(error),
        },
      });
    }
  }
}