From cfdd4cda447cc8cbdf90583ef5edcb17b80a02b7 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 14 Apr 2023 21:56:37 +0300 Subject: [PATCH] fixed typings --- src/LoggerClient.ts | 16 +++++++--------- src/LoggerInterface.ts | 10 +++++----- src/MasterLogger.ts | 27 ++++++++++++--------------- src/Types.ts | 13 ++++++++++++- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/LoggerClient.ts b/src/LoggerClient.ts index 96632d7..860d05f 100644 --- a/src/LoggerClient.ts +++ b/src/LoggerClient.ts @@ -80,10 +80,8 @@ class LoggerClient implements Logger { const spacer = ' '.repeat(LoggerClient.MaxChars - this.#_name.length); const header = `${`[${this.#_name.substring(0, LoggerClient.MaxChars)}]${spacer}`} `; - // eslint-disable-next-line no-console - if (!process.send || !process.connected) - // eslint-disable-next-line no-console - console.log(`${header} ${message}`); + if (!process.send || !process.connected) + throw new Error(`Missing connection to master proces`); else process.send({ [this.#_guard]: true, header, message, ...opts }); @@ -91,23 +89,23 @@ class LoggerClient implements Logger { // These methods are dynamically implemented by the constructor // eslint-disable-next-line @typescript-eslint/no-unused-vars - error (_str: string, _opts: WriteOptions): void { + error (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - warn (_str: string, _opts: WriteOptions): void { + warn (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - status (_str: string, _opts: WriteOptions): void { + status (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - info (_str: string, _opts: WriteOptions): void { + info (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - debug (_str: string, _opts: WriteOptions): void { + debug (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } diff --git a/src/LoggerInterface.ts b/src/LoggerInterface.ts index 5a70c29..c0734f4 100644 --- a/src/LoggerInterface.ts +++ b/src/LoggerInterface.ts @@ -1,9 +1,9 @@ import { WriteOptions } from "./Types"; export interface Logger { - error(str: string, opts: WriteOptions): void - warn(str: string, opts: WriteOptions): void - status(str: string, opts: WriteOptions): void - info(str: string, opts: WriteOptions): void - debug(str: string, opts: WriteOptions): void + error(str: string, opts?: WriteOptions): void + warn(str: string, opts?: WriteOptions): void + status(str: string, opts?: WriteOptions): void + info(str: string, opts?: WriteOptions): void + debug(str: string, opts?: WriteOptions): void } \ No newline at end of file diff --git a/src/MasterLogger.ts b/src/MasterLogger.ts index ce1c8f7..0f69aa8 100644 --- a/src/MasterLogger.ts +++ b/src/MasterLogger.ts @@ -10,18 +10,12 @@ import { inspect } from 'node:util'; // Own import DiscordWebhook from '@navy.gif/discord-webhook'; import Defaults, { LogLevel } from './Defaults.js'; -import { Shard, WriteOptions } from './Types.js'; +import { IPCMessage, LogFunction, Shard, WriteOptions } from './Types.js'; import { addLogLevel } from '../index.js'; import { Logger } from './LoggerInterface.js'; const DAY = 1000 * 60 * 60 * 24; -type IPCMessage = { - [key: string]: string | object - _guard: string - type: string -} - type FuncsType = { [key: string]: { func: (...strings: string[]) => string, @@ -35,6 +29,8 @@ type WriteStreams = { class MasterLogger implements Logger { + [key: string]: LogFunction | unknown; + #_guard: string; #_logLevel: LogLevel; #_logLevelMapping: {[key: string]: number}; @@ -146,9 +142,10 @@ class MasterLogger implements Logger { if (!msg[this.#_guard]) return; const { message, type, header, broadcast } = msg; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - this[type](message, { subheader: header, shard, broadcast }); + const func = this[type] as LogFunction; + if (!func) + throw new Error(`Attempted use of invalid logging function of type: ${type}, ensure client and master have the same type definitions.`); + func(message, { subheader: header, shard, broadcast }); }); } @@ -228,23 +225,23 @@ class MasterLogger implements Logger { // These methods are dynamically implemented by the constructor // eslint-disable-next-line @typescript-eslint/no-unused-vars - error (_str: string, _opts: WriteOptions): void { + error (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - warn (_str: string, _opts: WriteOptions): void { + warn (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - status (_str: string, _opts: WriteOptions): void { + status (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - info (_str: string, _opts: WriteOptions): void { + info (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - debug (_str: string, _opts: WriteOptions): void { + debug (_str: string, _opts?: WriteOptions): void { throw new Error('Method not implemented.'); } diff --git a/src/Types.ts b/src/Types.ts index 255c877..9c11acf 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -10,4 +10,15 @@ type WriteOptions = { broadcast?: boolean } -export { WriteOptions, Shard }; \ No newline at end of file +type IPCMessage = { + [key: string]: string | boolean + _guard: string + type: string, + message: string, + header: string, + broadcast: boolean +} + +type LogFunction = (str: string, opts: WriteOptions) => void + +export { WriteOptions, Shard, IPCMessage, LogFunction }; \ No newline at end of file