import { Injectable } from '@nestjs/common'; import type { CheckDetails } from '../health/helpers'; import { CheckResult } from '../health/helpers'; import type { HealthIndicator } from '../health/types'; import type { Config } from './config.abstract'; import { ReloadEvents } from './reload'; import type { ConfigSource } from './sources'; @Injectable() export class ConfigReloadIndicator implements HealthIndicator { private errors = new Map(); constructor(private events: ReloadEvents) { this.events .on('reload_failure', (source, err) => this.errors.set(source, err)) .on('update_failure', (config, err) => this.errors.set(config, err)) .on('reload', (source) => this.errors.delete(source)) .on('update', (config) => this.errors.delete(config)); } async check(): Promise { if (this.errors.size === 0) { return CheckResult.up({}); } const details: CheckDetails = {}; for (const [key, err] of this.errors.entries()) { details[key.constructor.name] = { error: err }; } return CheckResult.down(details); } }