Config: Refactor config method, so we have a new get all option, fix issues in configurations
This commit is contained in:
+15
-3
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -39,8 +39,7 @@ export async function isMember(user_id: string, guild_id: string) {
|
||||
export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) {
|
||||
const user = await getPublicUser(user_id, { guilds: true });
|
||||
|
||||
const limitsUserProperties = Config.apiConfig.get('limits.user', {maxGuilds: 100, masxUsername: 32, maxFriends: 1000}) as Config.DefaultOptions;
|
||||
const { maxGuilds } = limitsUserProperties.limits.user;
|
||||
const { maxGuilds } = Config.apiConfig.getAll().limits.user;
|
||||
if (user.guilds.length >= maxGuilds) {
|
||||
throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403);
|
||||
}
|
||||
|
||||
@@ -17,14 +17,13 @@ const blocklist: string[] = []; // TODO: update ones passwordblocklist is stored
|
||||
* Returns: 0 > pw > 1
|
||||
*/
|
||||
export function check(password: string): number {
|
||||
const passwordProperties = Config.apiConfig.get('register.password', { minLength: 8, minNumbers: 2, minUpperCase: 2, minSymbols: 0, blockInsecureCommonPasswords: false }) as Config.DefaultOptions;
|
||||
const {
|
||||
minLength,
|
||||
minNumbers,
|
||||
minUpperCase,
|
||||
minSymbols,
|
||||
blockInsecureCommonPasswords,
|
||||
} = passwordProperties.register.password;
|
||||
} = Config.apiConfig.getAll().register.password;
|
||||
var strength = 0;
|
||||
|
||||
// checks for total password len
|
||||
|
||||
Reference in New Issue
Block a user