Skip to content

Commit

Permalink
fix registry typing
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 30, 2023
1 parent 1e473a1 commit fc5ca96
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-check
/*
* moleculer
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
Expand Down
1 change: 0 additions & 1 deletion src/internals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-check
/*
* moleculer
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
Expand Down
6 changes: 4 additions & 2 deletions src/registry/endpoint-list.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<string, any>);
constructor(registry: Registry, broker: ServiceBroker, name: string, group: string, EndPointFactory?: typeof ActionEndpoint | typeof EventEndpoint, StrategyFactory?: typeof BaseStrategy, strategyOptions?: Record<string, any>);

add(node: Node, service: ServiceItem, data: any): Endpoint;
getFirst(): Endpoint | null;
Expand Down
4 changes: 2 additions & 2 deletions src/registry/event-catalog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
70 changes: 70 additions & 0 deletions src/registry/service-catalog.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;
metadata: Record<string, any>;

local: boolean;
available: boolean;
nodes?: string[];
nodeID?: string;

action?: Omit<ActionSchema, "handler" | "remoteHandler" | "service">;
events?: Omit<ServiceEvent, "handler" | "remoteHandler" | "service">;
}

export interface ServiceCatalogLocalNodeServicesResult {
name: string;
version: string | number;
fullName: string;
settings: Record<string, any>;
metadata: Record<string, any>;
dependencies: string | ServiceDependency | (string | ServiceDependency)[];

action: Record<string, Omit<ActionSchema, "handler" | "remoteHandler" | "service">>;
events: Record<string, Omit<ServiceEvent, "handler" | "remoteHandler" | "service">>;
}
}

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;
23 changes: 11 additions & 12 deletions src/registry/service-catalog.js
Original file line number Diff line number Diff line change
@@ -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
*/

Expand All @@ -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
Expand All @@ -22,6 +26,7 @@ const { removeFromArray } = require("../utils");
* Catalog for services
*
* @class ServiceCatalog
* @implements {ServiceCatalogClass}
*/
class ServiceCatalog {
/**
Expand Down Expand Up @@ -61,7 +66,7 @@ class ServiceCatalog {
*
* @param {String} fullName
* @param {String} nodeID
* @returns
* @returns {Boolean}
* @memberof ServiceCatalog
*/
has(fullName, nodeID) {
Expand All @@ -73,7 +78,7 @@ class ServiceCatalog {
*
* @param {String} fullName
* @param {String} nodeID
* @returns
* @returns {ServiceItem}
* @memberof ServiceCatalog
*/
get(fullName, nodeID) {
Expand All @@ -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
*/
Expand Down Expand Up @@ -169,7 +168,7 @@ class ServiceCatalog {
/**
* Get local service list for INFO packet
*
* @returns {Object}
* @returns {ServiceCatalogLocalNodeServicesResult[]}
* @memberof ServiceCatalog
*/
getLocalNodeServices() {
Expand Down
21 changes: 21 additions & 0 deletions src/registry/service-item.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;
metadata: Record<string, any>;

local: boolean;
actions: Record<string, ActionSchema>;
events: Record<string, ServiceEvent>;

constructor(node: Node, service: Record<string, any>, local: boolean);

equals(fullName: string, nodeID: string): boolean;

update(svc: Record<string, any>): void;

addAction(action: ActionSchema): void;
addEvent(event: ServiceEvent): void;
}
export = ServiceItem;
20 changes: 15 additions & 5 deletions src/registry/service-item.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -49,7 +59,7 @@ class ServiceItem {
/**
* Update service properties
*
* @param {any} svc
* @param {object} svc
* @memberof ServiceItem
*/
update(svc) {
Expand All @@ -62,7 +72,7 @@ class ServiceItem {
/**
* Add action to service
*
* @param {any} action
* @param {ActionSchema} action
* @memberof ServiceItem
*/
addAction(action) {
Expand All @@ -72,7 +82,7 @@ class ServiceItem {
/**
* Add event to service
*
* @param {any} event
* @param {ServiceEvent} event
* @memberof ServiceItem
*/
addEvent(event) {
Expand Down
1 change: 0 additions & 1 deletion src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,6 @@ class ServiceBroker {
* getLocalService({ name: "posts", version: 2 });
*
* @param {String|ServiceSearchObj} name
* @param {String|Number?} version
* @returns {Service}
*
* @memberof ServiceBroker
Expand Down
7 changes: 7 additions & 0 deletions src/service.d.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -111,6 +112,8 @@ declare namespace Service {
retryPolicy?: RetryPolicyOptions;
fallback?: string | FallbackHandler;
hooks?: ActionHooks;
strategy?: string| typeof Strategy;
strategyOptions?: Record<string, any>;

[key: string]: any;
}
Expand Down Expand Up @@ -172,7 +175,11 @@ declare namespace Service {
context?: boolean;
debounce?: number;
throttle?: number;
strategy?: string| typeof Strategy;
strategyOptions?: Record<string, any>;
handler?: ServiceEventHandler | ServiceEventLegacyHandler;

[key: string]: any;
}

export type ServiceEvents<S = ServiceSettingSchema> = {
Expand Down
2 changes: 1 addition & 1 deletion src/strategies/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export {
ShardStrategy as Shard,
};

export declare function resolve(opts: Record<string, any>|string): BaseStrategy;
export declare function resolve(opts: Record<string, any>|string): typeof BaseStrategy;
export declare function register(name: string, value: BaseStrategy): void;
1 change: 0 additions & 1 deletion src/transit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-check
/*
* moleculer
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
Expand Down
1 change: 0 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-check
/*
* moleculer
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
Expand Down

0 comments on commit fc5ca96

Please sign in to comment.