summary refs log tree commit diff
path: root/src/util/Config.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/Config.ts')
-rw-r--r--src/util/Config.ts18
1 files changed, 15 insertions, 3 deletions
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<DefaultOptions> & { definitions: { rateLimitOptions: JSONSchemaType<RateLimitOptions> @@ -398,6 +398,10 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements } } + private _has<Key extends keyof T>(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<T extends Record<string, any> = Record<string, unknown>> 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<T extends Record<string, any> = Record<string, unknown>> implements return this._get(key, defaultValue); } + public getAll(): DefaultOptions { + return this.store as unknown as DefaultOptions + } + private _get<Key extends keyof T>(key: Key): T[Key] | undefined; private _get<Key extends keyof T, Default = unknown>(key: Key, defaultValue: Default): T[Key] | Default; private _get<Key extends keyof T, Default = unknown>(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<T[Key] | undefined>(this.store, key as string, defaultValue as T[Key]); }