All files / health-indicator/http http.health.ts

94.54% Statements 52/55
92.3% Branches 12/13
100% Functions 6/6
94.33% Lines 50/53

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180    2x 2x 2x         2x   2x 2x 2x                               2x       10x   10x   10x 10x     10x             10x             9x 9x                                         4x 1x     3x       3x 1x 1x     3x                                           4x 4x     4x         4x   4x 4x 2x   2x     3x               6x 6x     6x     6x   6x 6x       4x 1x   3x 1x     2x 2x     4x   4x 2x 1x     1x           2x      
import { type URL } from 'url';
import type * as NestJSAxios from '@nestjs/axios';
import { ConsoleLogger, Inject, Injectable, Scope } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { lastValueFrom, type Observable } from 'rxjs';
import {
  type AxiosRequestConfig,
  type AxiosResponse,
} from './axios.interfaces';
import { HealthIndicator, type HealthIndicatorResult } from '..';
import { type AxiosError } from '../../errors/axios.error';
import { HealthCheckError } from '../../health-check/health-check.error';
import { TERMINUS_LOGGER } from '../../health-check/logger/logger.provider';
import { checkPackages, isAxiosError } from '../../utils';
 
interface HttpClientLike {
  request<T = any>(config: any): Observable<AxiosResponse<T>>;
}
 
/**
 * The HTTPHealthIndicator contains health indicators
 * which are used for health checks related to HTTP requests
 *
 * @publicApi
 * @module TerminusModule
 */
@Injectable({
  scope: Scope.TRANSIENT,
})
export class HttpHealthIndicator extends HealthIndicator {
  private nestJsAxios!: typeof NestJSAxios;
 
  constructor(
    private readonly moduleRef: ModuleRef,
    @Inject(TERMINUS_LOGGER)
    private readonly logger: ConsoleLogger,
  ) {
    super();
    Iif (this.logger instanceof ConsoleLogger) {
      this.logger.setContext(HttpHealthIndicator.name);
    }
    this.checkDependantPackages();
  }
 
  /**
   * Checks if the dependant packages are present
   */
  private checkDependantPackages() {
    this.nestJsAxios = checkPackages(
      ['@nestjs/axios'],
      this.constructor.name,
    )[0];
  }
 
  private getHttpService() {
    try {
      return this.moduleRef.get(this.nestJsAxios.HttpService, {
        strict: false,
      });
    } catch (err) {
      this.logger.error(
        'It seems like "HttpService" is not available in the current context. Are you sure you imported the HttpModule from the @nestjs/axios package?',
      );
      throw new Error(
        'It seems like "HttpService" is not available in the current context. Are you sure you imported the HttpModule from the @nestjs/axios package?',
      );
    }
  }
 
  /**
   * Prepares and throw a HealthCheckError
   * @param key The key which will be used for the result object
   * @param error The thrown error
   *
   * @throws {HealthCheckError}
   */
  private generateHttpError(key: string, error: AxiosError | any) {
    if (!isAxiosError(error)) {
      return;
    }
 
    const response: { [key: string]: any } = {
      message: error.message,
    };
 
    if (error.response) {
      response.statusCode = error.response.status;
      response.statusText = error.response.statusText;
    }
 
    throw new HealthCheckError(
      error.message,
      this.getStatus(key, false, response),
    );
  }
 
  /**
   * Checks if the given url response in the given timeout
   * and returns a result object corresponding to the result
   * @param key The key which will be used for the result object
   * @param url The url which should be request
   * @param options Optional axios options
   *
   * @throws {HealthCheckError} In case the health indicator failed
   *
   * @example
   * httpHealthIndicator.pingCheck('google', 'https://google.com', { timeout: 800 })
   */
  async pingCheck(
    key: string,
    url: string,
    {
      httpClient,
      ...options
    }: AxiosRequestConfig & { httpClient?: HttpClientLike } = {},
  ): Promise<HealthIndicatorResult> {
    let isHealthy = false;
    // In case the user has a preconfigured HttpService (see `HttpModule.register`)
    // we just let him/her pass in this HttpService so that he/she does not need to
    // reconfigure it.
    // https://github.com/nestjs/terminus/issues/1151
    const httpService = httpClient || this.getHttpService();
 
    try {
      await lastValueFrom(httpService.request({ url, ...options }));
      isHealthy = true;
    } catch (err) {
      this.generateHttpError(key, err);
    }
 
    return this.getStatus(key, isHealthy);
  }
 
  async responseCheck<T>(
    key: string,
    url: URL | string,
    callback: (response: AxiosResponse<T>) => boolean | Promise<boolean>,
    {
      httpClient,
      ...options
    }: AxiosRequestConfig & { httpClient?: HttpClientLike } = {},
  ): Promise<HealthIndicatorResult> {
    const httpService = httpClient || this.getHttpService();
 
    let response: AxiosResponse;
    let axiosError: AxiosError | null = null;
 
    try {
      response = await lastValueFrom(
        httpService.request({ url: url.toString(), ...options }),
      );
    } catch (error) {
      if (!isAxiosError(error)) {
        throw error;
      }
      if (!error.response) {
        throw this.generateHttpError(key, error);
      }
 
      response = error.response;
      axiosError = error;
    }
 
    const isHealthy = await callback(response);
 
    if (!isHealthy) {
      if (axiosError) {
        throw this.generateHttpError(key, axiosError);
      }
 
      throw new HealthCheckError(
        `${key} is not available`,
        this.getStatus(key, false),
      );
    }
 
    return this.getStatus(key, true);
  }
}