/lib/health-indicator/dns/dns.health.ts

https://github.com/nestjs/terminus · TypeScript · 89 lines · 45 code · 6 blank · 38 comment · 3 complexity · 867aea43f5a40ac3f4d704332d6992d5 MD5 · raw file

  1. import { Injectable, HttpService } from '@nestjs/common';
  2. import { AxiosResponse, AxiosRequestConfig, AxiosError } from 'axios';
  3. import { HealthIndicator, HealthIndicatorResult } from '../';
  4. import { HealthCheckError } from '../../health-check/health-check.error';
  5. /**
  6. * The DNSHealthIndicator contains health indicators
  7. * which are used for health checks related to HTTP requests
  8. * and DNS
  9. *
  10. * @publicApi
  11. * @module TerminusModule
  12. */
  13. @Injectable()
  14. export class DNSHealthIndicator extends HealthIndicator {
  15. /**
  16. * Initializes the health indicator
  17. * @param httpService The HttpService provided by Nest
  18. */
  19. constructor(private readonly httpService: HttpService) {
  20. super();
  21. }
  22. /**
  23. * Executes a request with the given parameters
  24. * @param url The url of the health check
  25. * @param options The optional axios options of the request
  26. */
  27. private async pingDNS(
  28. url: string,
  29. options: AxiosRequestConfig,
  30. ): Promise<AxiosResponse<any> | any> {
  31. return await this.httpService.request({ url, ...options }).toPromise();
  32. }
  33. /**
  34. * Prepares and throw a HealthCheckError
  35. * @param key The key which will be used for the result object
  36. * @param error The thrown error
  37. *
  38. * @throws {HealthCheckError}
  39. */
  40. private generateHttpError(key: string, error: AxiosError) {
  41. // TODO: Check for `error.isAxiosError`
  42. // Upgrade axios for that as soon ^0.19.0 is released
  43. if (error) {
  44. const response: { [key: string]: any } = {
  45. message: error.message,
  46. };
  47. if (error.response) {
  48. response.statusCode = error.response.status;
  49. response.statusText = error.response.statusText;
  50. }
  51. throw new HealthCheckError(
  52. error.message,
  53. this.getStatus(key, false, response),
  54. );
  55. }
  56. }
  57. /**
  58. * Checks if the given url respons in the given timeout
  59. * and returns a result object corresponding to the result
  60. * @param key The key which will be used for the result object
  61. * @param url The url which should be request
  62. * @param options Optional axios options
  63. *
  64. * @throws {HealthCheckError} In case the health indicator failed
  65. *
  66. * @example
  67. * dnsHealthIndicator.pingCheck('google', 'https://google.com', { timeout: 800 })
  68. */
  69. async pingCheck(
  70. key: string,
  71. url: string,
  72. options: AxiosRequestConfig = {},
  73. ): Promise<HealthIndicatorResult> {
  74. let isHealthy = false;
  75. try {
  76. await this.pingDNS(url, options);
  77. isHealthy = true;
  78. } catch (err) {
  79. this.generateHttpError(key, err);
  80. }
  81. return this.getStatus(key, isHealthy);
  82. }
  83. }