From 73fb42efa5c2708b82141380a8388b202a3bef8e Mon Sep 17 00:00:00 2001 From: Icebob Date: Thu, 30 Nov 2023 20:41:36 +0100 Subject: [PATCH] fix broker typing --- CHANGELOG.md | 3 +++ src/logger-factory.d.ts | 5 +++-- src/logger-factory.js | 3 ++- src/loggers/index.d.ts | 5 +++-- src/service-broker.d.ts | 35 +++++++++++++---------------------- src/service-broker.js | 36 +++++++++++++++++++++++------------- src/service.d.ts | 5 +++++ tsconfig.json | 3 ++- 8 files changed, 54 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4089575ce..97502ef2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -204,6 +204,9 @@ You can see example about migration here: https://github.com/icebob/fastest-vali The previously used huge one-file `index.d.ts` file has been rewritten and separated to multiple `d.ts` files, all are placed besides the source file. It may causes breaking changes in Typescript projects. +## Other breaking changes + +- `ServiceBroker.Promise` is removed. Use `broker.Promise` or `this.Promise` inside a `Service`. # New features diff --git a/src/logger-factory.d.ts b/src/logger-factory.d.ts index ec10d564f..fafde59a1 100644 --- a/src/logger-factory.d.ts +++ b/src/logger-factory.d.ts @@ -1,12 +1,13 @@ import type ServiceBroker = require("./service-broker"); import type { LogLevels, Base as BaseLogger } from "./loggers"; +import type { Logger } from "./logger-factory"; declare namespace LoggerFactory { export interface LoggerBindings { nodeID: string; ns: string; mod: string; - svc: string; + svc?: string; ver?: string; } @@ -29,7 +30,7 @@ declare class LoggerFactory { stop(): void; - getLogger(bindings: LoggerFactory.LoggerBindings): BaseLogger; + getLogger(bindings: LoggerFactory.LoggerBindings): Logger; getBindingsKey(bindings: LoggerFactory.LoggerBindings): string; } diff --git a/src/logger-factory.js b/src/logger-factory.js index f75e442ab..d6da33dfb 100644 --- a/src/logger-factory.js +++ b/src/logger-factory.js @@ -19,6 +19,7 @@ const cwd = process.cwd(); * @typedef {import("./service-broker")} ServiceBroker * @typedef {import("./logger-factory")} LoggerFactoryClass * @typedef {import("./logger-factory").LoggerBindings} LoggerBindings + * @typedef {import("./logger-factory").Logger} Logger * @typedef {import("./loggers/base")} BaseLogger */ @@ -117,7 +118,7 @@ class LoggerFactory { * Get a logger for a module (service, transporter, cacher, context...etc) * * @param {LoggerBindings} bindings - * @returns {BaseLogger} + * @returns {Logger} * * @memberof ServiceBroker */ diff --git a/src/loggers/index.d.ts b/src/loggers/index.d.ts index 25474dfad..7f1b40d15 100644 --- a/src/loggers/index.d.ts +++ b/src/loggers/index.d.ts @@ -10,6 +10,7 @@ import File = require("./file"); import Log4js = require("./log4js"); import Pino = require("./pino"); import Winston = require("./winston"); +import { LoggerOptions } from "./base"; export { Logger as Base, @@ -27,5 +28,5 @@ export { export type { LogLevels } from "./base"; -export declare function resolve(opt: Record | string): Logger; -export declare function register(name: string, value: Logger): void; +export declare function resolve(opt: Record | string): Logger; +export declare function register(name: string, value: Logger): void; diff --git a/src/service-broker.d.ts b/src/service-broker.d.ts index 3b17f4c1f..8d4740eb3 100644 --- a/src/service-broker.d.ts +++ b/src/service-broker.d.ts @@ -29,6 +29,7 @@ import type Service = require("./service"); import type { ServiceDependency } from "./service"; import type { Stream } from "stream"; import { ReplOptions } from "repl"; +import { LoggerOptions } from "./loggers/base"; declare namespace ServiceBroker { type BrokerSyncLifecycleHandler = (broker: ServiceBroker) => void; @@ -38,7 +39,7 @@ declare namespace ServiceBroker { namespace?: string | null; nodeID?: string | null; - logger?: BaseLogger | LoggerConfig | LoggerConfig[] | boolean | null; + logger?: BaseLogger | LoggerConfig | LoggerConfig[] | boolean | null; logLevel?: LogLevels | LogLevelConfig | null; transporter?: BaseTransporter | string | Record | null; @@ -118,6 +119,7 @@ declare namespace ServiceBroker { strategy?: Function | string; strategyOptions?: Record; preferLocal?: boolean; + stopDelay?: number; discoverer?: RegistryDiscovererOptions | BaseDiscoverer | string; } @@ -232,6 +234,7 @@ declare namespace ServiceBroker { caller?: string; headers?: Record; stream?: Stream; + ctx?: Context; } export interface MCallCallingOptions extends CallingOptions { @@ -252,17 +255,15 @@ declare namespace ServiceBroker { declare class ServiceBroker { static MOLECULER_VERSION: string; - static PROTOCOL_VERSION: "5"; + static PROTOCOL_VERSION: string; static INTERNAL_MIDDLEWARES: string[]; static defaultOptions: ServiceBroker.ServiceBrokerOptions; - static Promise: PromiseConstructor; - MOLECULER_VERSION: string; - PROTOCOL_VERSION: "5"; + PROTOCOL_VERSION: string; options: ServiceBroker.ServiceBrokerOptions; @@ -286,7 +287,7 @@ declare class ServiceBroker { localBus: EventEmitter2; - scope: AsyncStorage; + // scope: AsyncStorage; metrics: MetricRegistry; @@ -315,7 +316,7 @@ declare class ServiceBroker { errorHandler(err: Error, info: Record): void; wrapMethod( - method: string, + name: string, handler: ActionHandler, bindTo?: any, opts?: MiddlewareCallHandlerOptions @@ -376,26 +377,16 @@ declare class ServiceBroker { opts?: ServiceBroker.MCallCallingOptions ): Promise; - emit(eventName: string, data: TData, opts: Record): Promise; - emit(eventName: string, data: TData, groups: string[]): Promise; - emit(eventName: string, data: TData, groups: string): Promise; - emit(eventName: string, data: TData): Promise; + emit(eventName: string, data?: TData, opts?: Record): Promise; emit(eventName: string): Promise; - broadcast(eventName: string, data: TData, opts: Record): Promise; - broadcast(eventName: string, data: TData, groups: string[]): Promise; - broadcast(eventName: string, data: TData, groups: string): Promise; - broadcast(eventName: string, data: TData): Promise; + broadcast(eventName: string, data?: TData, opts?: Record): Promise; broadcast(eventName: string): Promise; - broadcastLocal(eventName: string, data: TData, opts: Record): Promise; - broadcastLocal(eventName: string, data: TData, groups: string[]): Promise; - broadcastLocal(eventName: string, data: TData, groups: string): Promise; - broadcastLocal(eventName: string, data: TData): Promise; + broadcastLocal(eventName: string, data?: TData, opts?: Record): Promise; broadcastLocal(eventName: string): Promise; - ping(): Promise; - ping(nodeID: string | string[], timeout?: number): Promise; + ping(nodeID?: string | string[], timeout?: number): Promise; getHealthStatus(): ServiceBroker.NodeHealthStatus; @@ -407,7 +398,7 @@ declare class ServiceBroker { hasEventListener(eventName: string): boolean; - getEventListener(eventName: string): EventEndpoint[]; + getEventListeners(eventName: string): EventEndpoint[]; getConstructorName(obj: any): string; diff --git a/src/service-broker.js b/src/service-broker.js index 2e0bc55be..1db7cd8aa 100644 --- a/src/service-broker.js +++ b/src/service-broker.js @@ -35,11 +35,21 @@ const C = require("./constants"); /** * Import types * - * @typedef {import("./service-broker").CallingOptions} CallingOptions Calling options + * @typedef {import("./context")} Context + * @typedef {import("./registry/endpoint")} Endpoint + * @typedef {import("./service")} Service + * @typedef {import("./service").ServiceSearchObj} ServiceSearchObj + * @typedef {import("./service-broker")} ServiceBrokerClass + * @typedef {import("./service-broker").ServiceBrokerOptions} ServiceBrokerOptions + * @typedef {import("./service-broker").CallingOptions} CallingOptions + * @typedef {import("./service-broker").NodeHealthStatus} NodeHealthStatus + * @typedef {import("./logger-factory").Logger} Logger */ /** * Default broker options + * + * @type {ServiceBrokerOptions} */ const defaultOptions = { namespace: "", @@ -165,6 +175,7 @@ const INTERNAL_MIDDLEWARES = [ * Service broker class * * @class ServiceBroker + * @implements {ServiceBrokerClass} */ class ServiceBroker { /** @@ -186,7 +197,6 @@ class ServiceBroker { this.Promise = Promise; } utils.polyfillPromise(this.Promise); - ServiceBroker.Promise = this.Promise; // Broker started flag this.started = false; @@ -645,7 +655,7 @@ class ServiceBroker { /** * Wrap a method with middlewares * - * @param {string} method + * @param {string} name * @param {Function} handler * @param {any} bindTo * @param {Object} opts @@ -660,7 +670,7 @@ class ServiceBroker { /** * Call a handler asynchronously in all middlewares * - * @param {String} method + * @param {String} name * @param {Array} args * @param {Object} opts * @returns {Promise} @@ -674,7 +684,7 @@ class ServiceBroker { /** * Call a handler synchronously in all middlewares * - * @param {String} method + * @param {String} name * @param {Array} args * @param {Object} opts * @returns @@ -709,8 +719,8 @@ class ServiceBroker { * Get a custom logger for sub-modules (service, transporter, cacher, context...etc) * * @param {String} mod Name of module - * @param {Object} props Module properties (service name, version, ...etc - * @returns {ModuleLogger} + * @param {Record?} props Module properties (service name, version, ...etc + * @returns {Logger} * * @memberof ServiceBroker */ @@ -768,8 +778,8 @@ class ServiceBroker { /** * Load a service from file * - * @param {string} Path of service - * @returns {Service} Loaded service + * @param {string} filePath + * @returns {Service} * * @memberof ServiceBroker */ @@ -892,7 +902,7 @@ class ServiceBroker { /** * Destroy a local service * - * @param {Service|string|object} service + * @param {Service|string|ServiceSearchObj} service * @returns Promise * @memberof ServiceBroker */ @@ -1333,7 +1343,7 @@ class ServiceBroker { * * @param {Array|Object} def Calling definitions. * @param {Object} opts Calling options for each call. - * @returns {Promise|Object>|PromiseSettledResult} + * @returns {Promise|Object>|PromiseSettledResult} * * @example * Call `mcall` with an array: @@ -1579,7 +1589,7 @@ class ServiceBroker { * * @param {string} eventName * @param {any?} payload - * @param {Object?} groups + * @param {Object?} opts * @returns * * @memberof ServiceBroker @@ -1690,7 +1700,7 @@ class ServiceBroker { /** * Get local node health status * - * @returns {Promise} + * @returns {NodeHealthStatus} * @memberof ServiceBroker */ getHealthStatus() { diff --git a/src/service.d.ts b/src/service.d.ts index 54f61c783..576ccb696 100644 --- a/src/service.d.ts +++ b/src/service.d.ts @@ -18,6 +18,11 @@ declare namespace Service { this: Service ) => void | Promise; + export interface ServiceSearchObj { + name?: string; + version?: string | number; + } + export interface ServiceSchema { name: string; version?: string | number; diff --git a/tsconfig.json b/tsconfig.json index 8db7458e2..d37139f54 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,8 @@ "target": "ES2022", "moduleResolution": "node", "noEmit": true, - "lib": ["ES2022"] + "lib": ["ES2022"], + "resolveJsonModule": true }, "include": ["types/extends.d.ts", "src/**/*.js"], "exclude": ["node_modules", ".eslintrc.js", "prettier.config.js"]