Skip to content

Commit

Permalink
fix broker typing
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 30, 2023
1 parent f4e738e commit 73fb42e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions src/logger-factory.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -29,7 +30,7 @@ declare class LoggerFactory {

stop(): void;

getLogger(bindings: LoggerFactory.LoggerBindings): BaseLogger;
getLogger(bindings: LoggerFactory.LoggerBindings): Logger;

getBindingsKey(bindings: LoggerFactory.LoggerBindings): string;
}
Expand Down
3 changes: 2 additions & 1 deletion src/logger-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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
*/
Expand Down
5 changes: 3 additions & 2 deletions src/loggers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,5 +28,5 @@ export {

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

export declare function resolve(opt: Record<string, any> | string): Logger;
export declare function register(name: string, value: Logger): void;
export declare function resolve(opt: Record<string, any> | string): Logger<any>;
export declare function register(name: string, value: Logger<any>): void;
35 changes: 13 additions & 22 deletions src/service-broker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +39,7 @@ declare namespace ServiceBroker {
namespace?: string | null;
nodeID?: string | null;

logger?: BaseLogger | LoggerConfig | LoggerConfig[] | boolean | null;
logger?: BaseLogger<LoggerOptions> | LoggerConfig | LoggerConfig[] | boolean | null;
logLevel?: LogLevels | LogLevelConfig | null;

transporter?: BaseTransporter | string | Record<string, any> | null;
Expand Down Expand Up @@ -118,6 +119,7 @@ declare namespace ServiceBroker {
strategy?: Function | string;
strategyOptions?: Record<string, any>;
preferLocal?: boolean;
stopDelay?: number;
discoverer?: RegistryDiscovererOptions | BaseDiscoverer | string;
}

Expand Down Expand Up @@ -232,6 +234,7 @@ declare namespace ServiceBroker {
caller?: string;
headers?: Record<string, any>;
stream?: Stream;
ctx?: Context;
}

export interface MCallCallingOptions extends CallingOptions {
Expand All @@ -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;

Expand All @@ -286,7 +287,7 @@ declare class ServiceBroker {

localBus: EventEmitter2;

scope: AsyncStorage;
// scope: AsyncStorage;

metrics: MetricRegistry;

Expand Down Expand Up @@ -315,7 +316,7 @@ declare class ServiceBroker {
errorHandler(err: Error, info: Record<string, any>): void;

wrapMethod(
method: string,
name: string,
handler: ActionHandler,
bindTo?: any,
opts?: MiddlewareCallHandlerOptions
Expand Down Expand Up @@ -376,26 +377,16 @@ declare class ServiceBroker {
opts?: ServiceBroker.MCallCallingOptions
): Promise<TReturn[]>;

emit<TData>(eventName: string, data: TData, opts: Record<string, any>): Promise<void>;
emit<TData>(eventName: string, data: TData, groups: string[]): Promise<void>;
emit<TData>(eventName: string, data: TData, groups: string): Promise<void>;
emit<TData>(eventName: string, data: TData): Promise<void>;
emit<TData>(eventName: string, data?: TData, opts?: Record<string, any>): Promise<void>;
emit(eventName: string): Promise<void>;

broadcast<TData>(eventName: string, data: TData, opts: Record<string, any>): Promise<void>;
broadcast<TData>(eventName: string, data: TData, groups: string[]): Promise<void>;
broadcast<TData>(eventName: string, data: TData, groups: string): Promise<void>;
broadcast<TData>(eventName: string, data: TData): Promise<void>;
broadcast<TData>(eventName: string, data?: TData, opts?: Record<string, any>): Promise<void>;
broadcast(eventName: string): Promise<void>;

broadcastLocal<TData>(eventName: string, data: TData, opts: Record<string, any>): Promise<void>;
broadcastLocal<TData>(eventName: string, data: TData, groups: string[]): Promise<void>;
broadcastLocal<TData>(eventName: string, data: TData, groups: string): Promise<void>;
broadcastLocal<TData>(eventName: string, data: TData): Promise<void>;
broadcastLocal<TData>(eventName: string, data?: TData, opts?: Record<string, any>): Promise<void>;
broadcastLocal(eventName: string): Promise<void>;

ping(): Promise<ServiceBroker.PongResponses>;
ping(nodeID: string | string[], timeout?: number): Promise<ServiceBroker.PongResponse>;
ping(nodeID?: string | string[], timeout?: number): Promise<ServiceBroker.PongResponse>;

getHealthStatus(): ServiceBroker.NodeHealthStatus;

Expand All @@ -407,7 +398,7 @@ declare class ServiceBroker {

hasEventListener(eventName: string): boolean;

getEventListener(eventName: string): EventEndpoint[];
getEventListeners(eventName: string): EventEndpoint[];

getConstructorName(obj: any): string;

Expand Down
36 changes: 23 additions & 13 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -165,6 +175,7 @@ const INTERNAL_MIDDLEWARES = [
* Service broker class
*
* @class ServiceBroker
* @implements {ServiceBrokerClass}
*/
class ServiceBroker {
/**
Expand All @@ -186,7 +197,6 @@ class ServiceBroker {
this.Promise = Promise;
}
utils.polyfillPromise(this.Promise);
ServiceBroker.Promise = this.Promise;

// Broker started flag
this.started = false;
Expand Down Expand Up @@ -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
Expand All @@ -660,7 +670,7 @@ class ServiceBroker {
/**
* Call a handler asynchronously in all middlewares
*
* @param {String} method
* @param {String} name
* @param {Array<any>} args
* @param {Object} opts
* @returns {Promise}
Expand All @@ -674,7 +684,7 @@ class ServiceBroker {
/**
* Call a handler synchronously in all middlewares
*
* @param {String} method
* @param {String} name
* @param {Array<any>} args
* @param {Object} opts
* @returns
Expand Down Expand Up @@ -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<string, any>?} props Module properties (service name, version, ...etc
* @returns {Logger}
*
* @memberof ServiceBroker
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -892,7 +902,7 @@ class ServiceBroker {
/**
* Destroy a local service
*
* @param {Service|string|object} service
* @param {Service|string|ServiceSearchObj} service
* @returns Promise<void>
* @memberof ServiceBroker
*/
Expand Down Expand Up @@ -1333,7 +1343,7 @@ class ServiceBroker {
*
* @param {Array<Object>|Object} def Calling definitions.
* @param {Object} opts Calling options for each call.
* @returns {Promise<Array<Object>|Object>|PromiseSettledResult}
* @returns {Promise<Array<Object>|Object>|PromiseSettledResult<any>}
*
* @example
* Call `mcall` with an array:
Expand Down Expand Up @@ -1579,7 +1589,7 @@ class ServiceBroker {
*
* @param {string} eventName
* @param {any?} payload
* @param {Object?} groups
* @param {Object?} opts
* @returns
*
* @memberof ServiceBroker
Expand Down Expand Up @@ -1690,7 +1700,7 @@ class ServiceBroker {
/**
* Get local node health status
*
* @returns {Promise}
* @returns {NodeHealthStatus}
* @memberof ServiceBroker
*/
getHealthStatus() {
Expand Down
5 changes: 5 additions & 0 deletions src/service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ declare namespace Service {
this: Service<S>
) => void | Promise<void>;

export interface ServiceSearchObj {
name?: string;
version?: string | number;
}

export interface ServiceSchema<S = ServiceSettingSchema> {
name: string;
version?: string | number;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 73fb42e

Please sign in to comment.