Meta: Clean up the format file

This commit is contained in:
Diego Magdaleno 2021-05-19 20:44:52 -05:00
parent e3f6a29df7
commit 8872f806f7

View File

@ -1,5 +1,5 @@
import Ajv, {JSONSchemaType} from "ajv" import Ajv, { JSONSchemaType } from "ajv"
import {ValidateFunction} from 'ajv' import { ValidateFunction } from 'ajv'
import ajvFormats from 'ajv-formats'; import ajvFormats from 'ajv-formats';
import dotProp from "dot-prop"; import dotProp from "dot-prop";
import envPaths from "env-paths"; import envPaths from "env-paths";
@ -111,8 +111,8 @@ const schema: JSONSchemaType<DefaultOptions> & {
rateLimitOptions: { rateLimitOptions: {
type: "object", type: "object",
properties: { properties: {
count: {type: "number"}, count: { type: "number" },
timespan: {type: "number"}, timespan: { type: "number" },
}, },
required: ["count", "timespan"], required: ["count", "timespan"],
}, },
@ -221,7 +221,7 @@ const schema: JSONSchemaType<DefaultOptions> & {
type: "number" type: "number"
} }
}, },
required: ["maxPins", "maxTopic"], required: ["maxPins", "maxTopic"],
additionalProperties: false additionalProperties: false
}, },
rate: { rate: {
@ -230,9 +230,9 @@ const schema: JSONSchemaType<DefaultOptions> & {
ip: { ip: {
type: "object", type: "object",
properties: { properties: {
enabled: {type: "boolean"}, enabled: { type: "boolean" },
count: {type: "number"}, count: { type: "number" },
timespan: {type: "number"} timespan: { type: "number" }
}, },
required: ["enabled", "count", "timespan"], required: ["enabled", "count", "timespan"],
additionalProperties: false additionalProperties: false
@ -243,8 +243,8 @@ const schema: JSONSchemaType<DefaultOptions> & {
auth: { auth: {
type: "object", type: "object",
properties: { properties: {
login: {$ref: '#/definitions/rateLimitOptions'}, login: { $ref: '#/definitions/rateLimitOptions' },
register: {$ref: '#/definitions/rateLimitOptions'} register: { $ref: '#/definitions/rateLimitOptions' }
}, },
nullable: true, nullable: true,
required: [], required: [],
@ -278,14 +278,14 @@ const schema: JSONSchemaType<DefaultOptions> & {
captcha: { captcha: {
type: "object", type: "object",
properties: { properties: {
enabled: {type: "boolean"}, enabled: { type: "boolean" },
service: { service: {
type: "string", type: "string",
enum: ["hcaptcha", "recaptcha", null], enum: ["hcaptcha", "recaptcha", null],
nullable: true nullable: true
}, },
sitekey: { sitekey: {
type: "string", type: "string",
nullable: true nullable: true
}, },
secret: { secret: {
@ -303,9 +303,9 @@ const schema: JSONSchemaType<DefaultOptions> & {
login: { login: {
type: "object", type: "object",
properties: { properties: {
requireCaptcha: {type: "boolean"} requireCaptcha: { type: "boolean" }
}, },
required: ["requireCaptcha"], required: ["requireCaptcha"],
additionalProperties: false additionalProperties: false
}, },
register: { register: {
@ -314,9 +314,9 @@ const schema: JSONSchemaType<DefaultOptions> & {
email: { email: {
type: "object", type: "object",
properties: { properties: {
necessary: {type: "boolean"}, necessary: { type: "boolean" },
allowlist: {type: "boolean"}, allowlist: { type: "boolean" },
blocklist: {type: "boolean"}, blocklist: { type: "boolean" },
domains: { domains: {
type: "array", type: "array",
items: { items: {
@ -330,24 +330,24 @@ const schema: JSONSchemaType<DefaultOptions> & {
dateOfBirth: { dateOfBirth: {
type: "object", type: "object",
properties: { properties: {
necessary: {type: "boolean"}, necessary: { type: "boolean" },
minimum: {type: "number"} minimum: { type: "number" }
}, },
required: ["minimum", "necessary"], required: ["minimum", "necessary"],
additionalProperties: false additionalProperties: false
}, },
requireCaptcha: {type: "boolean"}, requireCaptcha: { type: "boolean" },
requireInvite: {type: "boolean"}, requireInvite: { type: "boolean" },
allowNewRegistration: {type: "boolean"}, allowNewRegistration: { type: "boolean" },
allowMultipleAccounts: {type: "boolean"}, allowMultipleAccounts: { type: "boolean" },
password: { password: {
type: "object", type: "object",
properties: { properties: {
minLength: {type: "number"}, minLength: { type: "number" },
minNumbers: {type: "number"}, minNumbers: { type: "number" },
minUpperCase: {type: "number"}, minUpperCase: { type: "number" },
minSymbols: {type: "number"}, minSymbols: { type: "number" },
blockInsecureCommonPasswords: {type: "boolean"} blockInsecureCommonPasswords: { type: "boolean" }
}, },
required: ["minLength", "minNumbers", "minUpperCase", "minSymbols", "blockInsecureCommonPasswords"], required: ["minLength", "minNumbers", "minUpperCase", "minSymbols", "blockInsecureCommonPasswords"],
additionalProperties: false additionalProperties: false
@ -371,7 +371,7 @@ type Deserialize<T> = (text: string) => T;
class Config<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]> { class Config<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]> {
readonly path: string; readonly path: string;
readonly #validator?: ValidateFunction; readonly #validator?: ValidateFunction;
readonly #defaultOptions: Partial<T> = {}; readonly #defaultOptions: Partial<T> = {};
constructor() { constructor() {
@ -386,7 +386,7 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
this.path = path.resolve(base, 'api.json'); this.path = path.resolve(base, 'api.json');
const fileStore = this.store; const fileStore = this.store;
const store = Object.assign(createPlainObject<T>(), fileStore); const store = Object.assign(createPlainObject<T>(), fileStore);
this._validate(store); this._validate(store);
@ -402,13 +402,13 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
if (!this.#validator) { if (!this.#validator) {
return; return;
} }
const valid = this.#validator(data); const valid = this.#validator(data);
if (valid || !this.#validator.errors) { if (valid || !this.#validator.errors) {
return; return;
} }
const errors = this.#validator.errors.map(({instancePath, message = ''}) => `\`${instancePath.slice(1)}\` ${message}`); const errors = this.#validator.errors.map(({ instancePath, message = '' }) => `\`${instancePath.slice(1)}\` ${message}`);
throw new Error('The config schema was violated!: ' + errors.join('; ')); throw new Error('The config schema was violated!: ' + errors.join('; '));
} }
@ -422,7 +422,7 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
if (error.code == 'ENOENT') { if (error.code == 'ENOENT') {
this._ensureDirectory(); this._ensureDirectory();
return createPlainObject(); return createPlainObject();
} }
throw error; throw error;
@ -430,7 +430,7 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
} }
private _ensureDirectory(): void { private _ensureDirectory(): void {
fs.mkdirSync(path.dirname(this.path), {recursive: true}) fs.mkdirSync(path.dirname(this.path), { recursive: true })
} }
set store(value: T) { set store(value: T) {
@ -450,12 +450,12 @@ class Config<T extends Record<string, any> = Record<string, unknown>> implements
} }
private _get<Key extends keyof T>(key: Key): T[Key] | undefined; 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, defaultValue: Default): T[Key] | Default;
private _get<Key extends keyof T, Default = unknown>(key: Key | string, defaultValue?: Default): Default | undefined { private _get<Key extends keyof T, Default = unknown>(key: Key | string, defaultValue?: Default): Default | undefined {
return dotProp.get<T[Key] | undefined>(this.store, key as string, defaultValue as T[Key]); return dotProp.get<T[Key] | undefined>(this.store, key as string, defaultValue as T[Key]);
} }
* [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]> { *[Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]> {
for (const [key, value] of Object.entries(this.store)) { for (const [key, value] of Object.entries(this.store)) {
yield [key, value]; yield [key, value];
} }