28 lines
853 B
TypeScript
28 lines
853 B
TypeScript
import { BackendSettings, Config } from "../types/config";
|
|
import { Notification } from "../types/notification";
|
|
|
|
export abstract class Backend<C extends BackendSettings> {
|
|
public abstract readonly name: string;
|
|
protected abstract requiredFields: readonly (keyof C)[];
|
|
protected abstract notify(config: C, notification: Notification): void;
|
|
|
|
#validate(config: Partial<C>): C {
|
|
for (const field of this.requiredFields) {
|
|
if (config[field] === undefined) {
|
|
throw new Error(`The '${String(field)}' configuration field of'${this.name}' consumer is required`)
|
|
}
|
|
}
|
|
|
|
return config as C;
|
|
}
|
|
|
|
public remind(config: Config, notification: Notification) {
|
|
const cfg = config.backend[this.name] as Partial<C>;
|
|
|
|
if (cfg.enable !== true) {
|
|
return
|
|
}
|
|
|
|
this.notify(this.#validate(cfg), notification);
|
|
}
|
|
} |