From fc5ca96325269c7013b4d60ca3c67e7779b04095 Mon Sep 17 00:00:00 2001 From: Icebob Date: Thu, 30 Nov 2023 23:07:28 +0100 Subject: [PATCH] fix registry typing --- src/constants.js | 1 - src/internals.js | 1 - src/registry/endpoint-list.d.ts | 6 ++- src/registry/event-catalog.d.ts | 4 +- src/registry/service-catalog.d.ts | 70 +++++++++++++++++++++++++++++++ src/registry/service-catalog.js | 23 +++++----- src/registry/service-item.d.ts | 21 ++++++++++ src/registry/service-item.js | 20 ++++++--- src/service-broker.js | 1 - src/service.d.ts | 7 ++++ src/strategies/index.d.ts | 2 +- src/transit.js | 1 - src/utils.js | 1 - 13 files changed, 131 insertions(+), 27 deletions(-) create mode 100644 src/registry/service-catalog.d.ts diff --git a/src/constants.js b/src/constants.js index 397261822..5dc61c655 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,4 +1,3 @@ -// @ts-check /* * moleculer * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer) diff --git a/src/internals.js b/src/internals.js index 12850428f..636fe96c7 100644 --- a/src/internals.js +++ b/src/internals.js @@ -1,4 +1,3 @@ -// @ts-check /* * moleculer * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer) diff --git a/src/registry/endpoint-list.d.ts b/src/registry/endpoint-list.d.ts index 4eff78ba0..0528e66ab 100644 --- a/src/registry/endpoint-list.d.ts +++ b/src/registry/endpoint-list.d.ts @@ -3,8 +3,10 @@ import Node = require("./node"); import Endpoint = require("./endpoint"); import ActionEndpoint = require("./endpoint-action"); import EventEndpoint = require("./endpoint-event"); +import ServiceBroker = require("../service-broker"); +import Registry = require("./registry"); + import ServiceItem = require("./service-item"); -import { Registry, ServiceBroker } from "./event-catalog"; import Context = require("../context"); declare class EndpointList { @@ -18,7 +20,7 @@ declare class EndpointList { endpoints: (ActionEndpoint | EventEndpoint)[]; localEndpoints: (ActionEndpoint | EventEndpoint)[]; - constructor(registry: Registry, broker: ServiceBroker, name: string, group: string, EndPointFactory?: typeof Endpoint, StrategyFactory?: typeof BaseStrategy, strategyOptions?: Record); + constructor(registry: Registry, broker: ServiceBroker, name: string, group: string, EndPointFactory?: typeof ActionEndpoint | typeof EventEndpoint, StrategyFactory?: typeof BaseStrategy, strategyOptions?: Record); add(node: Node, service: ServiceItem, data: any): Endpoint; getFirst(): Endpoint | null; diff --git a/src/registry/event-catalog.d.ts b/src/registry/event-catalog.d.ts index b171dc2d2..cd4069b36 100644 --- a/src/registry/event-catalog.d.ts +++ b/src/registry/event-catalog.d.ts @@ -39,10 +39,10 @@ declare class EventCatalog { registry: Registry; broker: ServiceBroker; events: EndpointList[]; - StrategyFactory: Strategy; + StrategyFactory: typeof Strategy; EndpointFactory: typeof EventEndpoint; - constructor(registry: Registry, broker: ServiceBroker, StrategyFactory: Strategy); + constructor(registry: Registry, broker: ServiceBroker, StrategyFactory: typeof Strategy); add(node: BrokerNode, service: ServiceItem, event: ServiceEvent): EndpointList; diff --git a/src/registry/service-catalog.d.ts b/src/registry/service-catalog.d.ts new file mode 100644 index 000000000..c9c0a25db --- /dev/null +++ b/src/registry/service-catalog.d.ts @@ -0,0 +1,70 @@ +import type { ActionSchema } from "../service"; +import type { ServiceEvent, ServiceDependency } from "../service"; + +import BrokerNode = require("./node"); +import ServiceItem = require("./service-item"); + +import ServiceBroker = require("../service-broker"); +import Registry = require("./registry"); + +declare namespace ServiceCatalog { + export interface ServiceCatalogListOptions { + onlyLocal?: boolean; + onlyAvailable?: boolean; + skipInternal?: boolean; + withActions?: boolean; + withEvents?: boolean; + grouping?: boolean; + } + + export interface ServiceCatalogListResult { + name: string; + version: string | number; + fullName: string; + settings: Record; + metadata: Record; + + local: boolean; + available: boolean; + nodes?: string[]; + nodeID?: string; + + action?: Omit; + events?: Omit; + } + + export interface ServiceCatalogLocalNodeServicesResult { + name: string; + version: string | number; + fullName: string; + settings: Record; + metadata: Record; + dependencies: string | ServiceDependency | (string | ServiceDependency)[]; + + action: Record>; + events: Record>; + } +} + +declare class ServiceCatalog { + registry: Registry; + broker: ServiceBroker; + services: ServiceItem[]; + + constructor(registry: Registry, broker: ServiceBroker); + + add(node: BrokerNode, service: ServiceItem, local: boolean): ServiceItem; + + has(fullName: string, nodeID: string): boolean; + get(fullName: string, nodeID: string): ServiceItem; + + list(opts: ServiceCatalog.ServiceCatalogListOptions): ServiceCatalog.ServiceCatalogListResult[]; + + getLocalNodeServices(): ServiceCatalog.ServiceCatalogLocalNodeServicesResult[]; + + removeAllByNodeID(nodeID: string): void; + remove(fullName: string, nodeID: string): void; + +} + +export = ServiceCatalog; diff --git a/src/registry/service-catalog.js b/src/registry/service-catalog.js index 3a325a510..bf19e8f9e 100644 --- a/src/registry/service-catalog.js +++ b/src/registry/service-catalog.js @@ -1,6 +1,6 @@ /* * moleculer - * Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer) + * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer) * MIT Licensed */ @@ -13,6 +13,10 @@ const { removeFromArray } = require("../utils"); /** * Import types * + * @typedef {import("./service-catalog")} ServiceCatalogClass + * @typedef {import("./service-catalog").ServiceCatalogListOptions} ServiceCatalogListOptions + * @typedef {import("./service-catalog").ServiceCatalogListResult} ServiceCatalogListResult + * @typedef {import("./service-catalog").ServiceCatalogLocalNodeServicesResult} ServiceCatalogLocalNodeServicesResult * @typedef {import("./registry")} Registry * @typedef {import("./node")} Node * @typedef {import("../service-broker")} ServiceBroker @@ -22,6 +26,7 @@ const { removeFromArray } = require("../utils"); * Catalog for services * * @class ServiceCatalog + * @implements {ServiceCatalogClass} */ class ServiceCatalog { /** @@ -61,7 +66,7 @@ class ServiceCatalog { * * @param {String} fullName * @param {String} nodeID - * @returns + * @returns {Boolean} * @memberof ServiceCatalog */ has(fullName, nodeID) { @@ -73,7 +78,7 @@ class ServiceCatalog { * * @param {String} fullName * @param {String} nodeID - * @returns + * @returns {ServiceItem} * @memberof ServiceCatalog */ get(fullName, nodeID) { @@ -83,14 +88,8 @@ class ServiceCatalog { /** * Get a filtered list of services with actions * - * @param {Object} opts - * @param {Boolean} [opts.onlyLocal = false] - * @param {Boolean} [opts.onlyAvailable = false] - * @param {Boolean} [opts.skipInternal = false] - * @param {Boolean} [opts.withActions = false] - * @param {Boolean} [opts.withEvents = false] - * @param {Boolean} [opts.grouping = false] - * @returns {Array} + * @param {ServiceCatalogListOptions} opts + * @returns {ServiceCatalogListResult[]} * * @memberof Registry */ @@ -169,7 +168,7 @@ class ServiceCatalog { /** * Get local service list for INFO packet * - * @returns {Object} + * @returns {ServiceCatalogLocalNodeServicesResult[]} * @memberof ServiceCatalog */ getLocalNodeServices() { diff --git a/src/registry/service-item.d.ts b/src/registry/service-item.d.ts index 31786efed..2d94f8990 100644 --- a/src/registry/service-item.d.ts +++ b/src/registry/service-item.d.ts @@ -1,6 +1,27 @@ import Node = require("./node"); +import type { ActionSchema } from "../service"; +import type { ServiceEvent } from "../service"; + declare class ServiceItem { + node: Node; + name: string; + fullName: string; + version: string | number; + settings: Record; + metadata: Record; + + local: boolean; + actions: Record; + events: Record; + constructor(node: Node, service: Record, local: boolean); + + equals(fullName: string, nodeID: string): boolean; + + update(svc: Record): void; + + addAction(action: ActionSchema): void; + addEvent(event: ServiceEvent): void; } export = ServiceItem; diff --git a/src/registry/service-item.js b/src/registry/service-item.js index 5d7549702..78f910b4e 100644 --- a/src/registry/service-item.js +++ b/src/registry/service-item.js @@ -1,22 +1,32 @@ /* * moleculer - * Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer) + * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer) * MIT Licensed */ "use strict"; +/** + * Import types + * + * @typedef {import("./service-item")} ServiceItemClass + * @typedef {import("./node")} Node + * @typedef {import("../service").ActionSchema} ActionSchema + * @typedef {import("../service").ServiceEvent} ServiceEvent + */ + /** * Service class * * @class ServiceItem + * @implements {ServiceItemClass} */ class ServiceItem { /** * Creates an instance of ServiceItem. * * @param {Node} node - * @param {Object} service + * @param {object} service * @param {Boolean} local * @memberof ServiceItem */ @@ -49,7 +59,7 @@ class ServiceItem { /** * Update service properties * - * @param {any} svc + * @param {object} svc * @memberof ServiceItem */ update(svc) { @@ -62,7 +72,7 @@ class ServiceItem { /** * Add action to service * - * @param {any} action + * @param {ActionSchema} action * @memberof ServiceItem */ addAction(action) { @@ -72,7 +82,7 @@ class ServiceItem { /** * Add event to service * - * @param {any} event + * @param {ServiceEvent} event * @memberof ServiceItem */ addEvent(event) { diff --git a/src/service-broker.js b/src/service-broker.js index 1db7cd8aa..6d15ee5e6 100644 --- a/src/service-broker.js +++ b/src/service-broker.js @@ -1003,7 +1003,6 @@ class ServiceBroker { * getLocalService({ name: "posts", version: 2 }); * * @param {String|ServiceSearchObj} name - * @param {String|Number?} version * @returns {Service} * * @memberof ServiceBroker diff --git a/src/service.d.ts b/src/service.d.ts index 576ccb696..bcc568a2f 100644 --- a/src/service.d.ts +++ b/src/service.d.ts @@ -1,5 +1,6 @@ import Context = require("./context"); import ServiceBroker = require("./service-broker"); +import Strategy = require("./strategies/base"); import type { MoleculerError } from "./errors"; import type { Logger } from "./logger-factory"; import type { CacherKeygen } from "./cachers/base"; @@ -111,6 +112,8 @@ declare namespace Service { retryPolicy?: RetryPolicyOptions; fallback?: string | FallbackHandler; hooks?: ActionHooks; + strategy?: string| typeof Strategy; + strategyOptions?: Record; [key: string]: any; } @@ -172,7 +175,11 @@ declare namespace Service { context?: boolean; debounce?: number; throttle?: number; + strategy?: string| typeof Strategy; + strategyOptions?: Record; handler?: ServiceEventHandler | ServiceEventLegacyHandler; + + [key: string]: any; } export type ServiceEvents = { diff --git a/src/strategies/index.d.ts b/src/strategies/index.d.ts index a1d27dbb8..24053b0da 100644 --- a/src/strategies/index.d.ts +++ b/src/strategies/index.d.ts @@ -14,5 +14,5 @@ export { ShardStrategy as Shard, }; -export declare function resolve(opts: Record|string): BaseStrategy; +export declare function resolve(opts: Record|string): typeof BaseStrategy; export declare function register(name: string, value: BaseStrategy): void; diff --git a/src/transit.js b/src/transit.js index f83333cf2..4fe1d73dd 100644 --- a/src/transit.js +++ b/src/transit.js @@ -1,4 +1,3 @@ -// @ts-check /* * moleculer * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer) diff --git a/src/utils.js b/src/utils.js index 2dac4ecf9..f8bf04c6a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,3 @@ -// @ts-check /* * moleculer * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)