From 05057b922a84c6b0331357867dfa50166320b318 Mon Sep 17 00:00:00 2001 From: Diego Magdaleno Date: Fri, 21 May 2021 18:18:58 -0500 Subject: Config: Refactor config method, so we have a new get all option, fix issues in configurations --- src/util/Config.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/util/Config.ts') diff --git a/src/util/Config.ts b/src/util/Config.ts index 1dcd61c0..ee7a2f96 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -1,5 +1,4 @@ -import Ajv, { JSONSchemaType } from "ajv" -import { ValidateFunction } from 'ajv' +import Ajv, { JSONSchemaType, ValidateFunction } from "ajv" import ajvFormats from 'ajv-formats'; import dotProp from "dot-prop"; import envPaths from "env-paths"; @@ -101,6 +100,7 @@ export interface DefaultOptions { }; } + const schema: JSONSchemaType & { definitions: { rateLimitOptions: JSONSchemaType @@ -398,6 +398,10 @@ class Config = Record> implements } } + private _has(key: Key | string): boolean { + return dotProp.has(this.store, key as string); + } + private _validate(data: T | unknown): void { if (!this.#validator) { return; @@ -432,7 +436,7 @@ class Config = Record> implements private _ensureDirectory(): void { fs.mkdirSync(path.dirname(this.path), { recursive: true }) } - + set store(value: T) { this._validate(value); @@ -449,9 +453,17 @@ class Config = Record> implements return this._get(key, defaultValue); } + public getAll(): DefaultOptions { + return this.store as unknown as DefaultOptions + } + private _get(key: Key): T[Key] | undefined; private _get(key: Key, defaultValue: Default): T[Key] | Default; private _get(key: Key | string, defaultValue?: Default): Default | undefined { + if (!this._has(key)) { + throw new Error("Tried to acess a non existant property in the config"); + } + return dotProp.get(this.store, key as string, defaultValue as T[Key]); } -- cgit 1.5.1