Skip to content

Commit

Permalink
types: loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 26, 2023
1 parent b807eaa commit 696e451
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/logger-factory.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type ServiceBroker = require("./service-broker");
import type { LEVELS, Base as BaseLogger } from "./loggers";
import type { LogLevels, Base as BaseLogger } from "./loggers";

declare namespace LoggerFactory {
export interface LoggerBindings {
Expand All @@ -16,7 +16,7 @@ declare namespace LoggerFactory {
}

export type Logger = {
[level in LEVELS]: (...args: any[]) => void;
[level in LogLevels]: (...args: any[]) => void;
};
}

Expand All @@ -25,7 +25,7 @@ declare class LoggerFactory {

constructor(broker: ServiceBroker);

init(opts: LoggerFactory.LoggerConfig | LoggerFactory.LoggerConfig[]): void;
init(opts: null | boolean | LoggerFactory.LoggerConfig | LoggerFactory.LoggerConfig[]): void;

stop(): void;

Expand Down
21 changes: 16 additions & 5 deletions src/logger-factory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand All @@ -13,14 +13,25 @@ const Loggers = require("./loggers");
const noop = () => {};
const cwd = process.cwd();

/**
* Import types
*
* @typedef {import("./service-broker")} ServiceBroker
* @typedef {import("./logger-factory")} LoggerFactoryClass
* @typedef {import("./logger-factory").LoggerBindings} LoggerBindings
* @typedef {import("./loggers/base")} BaseLogger
*/

/**
* Log factory class.
*
* @class LoggerFactory
* @implements {LoggerFactoryClass}
*/
class LoggerFactory {
/**
* Constructor of LoggerFactory
*
* @param {ServiceBroker} broker
*/
constructor(broker) {
this.broker = broker;
Expand Down Expand Up @@ -105,8 +116,8 @@ class LoggerFactory {
/**
* Get a logger for a module (service, transporter, cacher, context...etc)
*
* @param {Object} bindings
* @returns {ModuleLogger}
* @param {LoggerBindings} bindings
* @returns {BaseLogger}
*
* @memberof ServiceBroker
*/
Expand Down Expand Up @@ -154,7 +165,7 @@ class LoggerFactory {
/**
* Create a key from bindings for logger caching.
*
* @param {object} bindings
* @param {LoggerBindings} bindings
* @returns {String}
*/
getBindingsKey(bindings) {
Expand Down
8 changes: 4 additions & 4 deletions src/loggers/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import LoggerFactory = require("../logger-factory");
import ServiceBroker = require("../service-broker");

declare namespace BaseLogger {
export type LEVELS = "fatal" | "error" | "warn" | "info" | "debug" | "trace";
export type LogLevels = "fatal" | "error" | "warn" | "info" | "debug" | "trace";

export type LogHandler = (level: LEVELS, args: unknown[]) => void;
export type LogHandler = (level: LogLevels, args: unknown[]) => void;

export interface LoggerOptions {
level?: LEVELS;
level?: LogLevels;
createLogger?: Function
}
}
Expand All @@ -24,7 +24,7 @@ declare abstract class BaseLogger {

stop(): void;

getLogLevel(mod: string): BaseLogger.LEVELS | null;
getLogLevels(mod: string): BaseLogger.LogLevels | null;

getLogHandler(bindings: LoggerFactory.LoggerBindings): BaseLogger.LogHandler | null;

Expand Down
2 changes: 2 additions & 0 deletions src/loggers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export {
LEVELS
};

export type { LogLevels } from "./base";

export declare function resolve(opt: Record<string, any> | string): Logger;
export declare function register(name: string, value: Logger): void;
File renamed without changes.
20 changes: 15 additions & 5 deletions src/transit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Context = require("./context");
import BrokerNode = require("./registry/node");
import type { Packet } from "./packets";
import type { Logger } from "./logger-factory";
import ServiceBroker = require("./service-broker");
import Transporter = require("./transporters/base");

declare namespace Transit {
export interface TransitRequest {
Expand All @@ -16,18 +18,26 @@ declare namespace Transit {
}

declare class Transit {
pendingRequests: Map<string, Transit.TransitRequest>;
nodeID: string;
broker: ServiceBroker;
tx: BaseTransporter;
promise: PromiseConstructor;
logger: Logger;
nodeID: string;
instanceID: string;
opts: any;
pendingRequests: Map<string, Transit.TransitRequest>;
pendingReqStreams: Map<string, any>;
pendingResStreams: Map<string, any>;
connected: boolean;
disconnecting: boolean;
isReady: boolean;
tx: BaseTransporter;

constructor(broker: ServiceBroker, transporter: Transporter, opts: any | null);

afterConnect(wasReconnect: boolean): Promise<void>;
connect(): Promise<void>;
disconnect(): Promise<void>;
ready(): Promise<void>;
disconnect(): Promise<any>;
ready(): void;
sendDisconnectPacket(): Promise<void>;
makeSubscriptions(): Promise<void[]>;
messageHandler(cmd: string, msg: Record<string, any>): boolean | Promise<void> | undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const C = require("./constants");
* @typedef {import("./transporters/base")} Transporter
* @typedef {import("stream").Stream} Stream
* @typedef {import("./context")} Context
* @typedef {import("./transit")} TransitClass
*/

/**
* Transit class
*
* @class Transit
* @implements {TransitClass}
*/
class Transit {
/**
Expand Down
22 changes: 20 additions & 2 deletions test/typescript/tsd/Cachers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ expectType<Cachers.MemoryLRU>(memoryLRUBroker.cacher as Cachers.MemoryLRU);
// redis cacher tests
expectType<Cachers.Redis>(new Cachers.Redis());
expectAssignable<Cachers.Base>(new Cachers.Redis());
expectType<string | null>(new Cachers.Redis().prefix);
expectType<string | null>(new Cachers.Redis({ prefix: "foo" }).prefix);
expectType<string | undefined>(new Cachers.Redis().prefix);
expectType<string | undefined>(new Cachers.Redis({ prefix: "foo" }).prefix);
const redisBroker = new ServiceBroker({ cacher: new Cachers.Redis() });
expectType<Cachers.Redis>(redisBroker.cacher as Cachers.Redis);

Expand All @@ -28,6 +28,24 @@ expectType<any>(new Cachers.Redis().client);

// custom cacher tests
class CustomCacher extends Cachers.Base {
close(): Promise<unknown> {
throw new Error("Method not implemented.");
}
get(key: string): Promise<Record<string, unknown> | null> {
throw new Error("Method not implemented.");
}
getWithTTL(key: string): Promise<Record<string, unknown> | null> {
throw new Error("Method not implemented.");
}
set(key: string, data: any, ttl?: number | undefined): Promise<unknown> {
throw new Error("Method not implemented.");
}
del(key: string | string[]): Promise<unknown> {
throw new Error("Method not implemented.");
}
clean(match?: string | string[] | undefined): Promise<unknown> {
throw new Error("Method not implemented.");
}
async lock() {
return async () => {};
}
Expand Down

0 comments on commit 696e451

Please sign in to comment.