From b03f7ba7ca8d7bbdc547629b507c6429c562ed33 Mon Sep 17 00:00:00 2001 From: Icebob Date: Sat, 25 Nov 2023 16:34:15 +0100 Subject: [PATCH] service types --- src/service.d.ts | 14 +++++++------- src/service.js | 19 +++++-------------- tsconfig.json | 4 +++- types/extends.d.ts | 7 +++++-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/service.d.ts b/src/service.d.ts index 63b2a212b..54f61c783 100644 --- a/src/service.d.ts +++ b/src/service.d.ts @@ -197,10 +197,10 @@ declare namespace Service { } } -declare class Service implements Service.ServiceSchema { - constructor(broker: ServiceBroker, schema?: Service.ServiceSchema); +declare class Service { + constructor(broker: ServiceBroker, schema?: Partial>); - protected parseServiceSchema(schema: Service.ServiceSchema): void; + parseServiceSchema(schema: Partial>): void; name: string; fullName: string; @@ -249,7 +249,7 @@ declare class Service implements Service.Servi * * @param schema Schema containing the mixins to merge */ - applyMixins(schema: Service.ServiceSchema): Service.ServiceSchema; + applyMixins(schema: Partial): Partial; /** * Merge two Service schema @@ -258,9 +258,9 @@ declare class Service implements Service.Servi * @param svcSchema Service schema */ mergeSchemas( - mixinSchema: Service.ServiceSchema, - svcSchema: Service.ServiceSchema - ): Service.ServiceSchema; + mixinSchema: Partial, + svcSchema: Partial + ): Partial; /** * Merge `settings` property in schema diff --git a/src/service.js b/src/service.js index 1751f7a41..605c95858 100644 --- a/src/service.js +++ b/src/service.js @@ -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 */ @@ -45,16 +45,16 @@ function isNewSignature(args) { /** * Service class * - * @class Service + * @typedef {import("./service")} ServiceClass + * @implements {ServiceClass} */ class Service { /** * Creates an instance of Service by schema. * * @param {ServiceBroker} broker broker of service - * @param {ServiceSchema} schema schema of service + * @param {Partial} schema schema of service * - * @memberof Service */ constructor(broker, schema) { if (!isObject(broker)) throw new ServiceSchemaError("Must set a ServiceBroker instance!"); @@ -249,7 +249,7 @@ class Service { /** * Return a service settings without protected properties. * - * @param {Object?} settings + * @param {Record?} settings */ _getPublicSettings(settings) { if (settings && Array.isArray(settings.$secureSettings)) { @@ -263,7 +263,6 @@ class Service { * Initialize service. It called `created` handler in schema * * @private - * @memberof Service */ _init() { this.logger.debug(`Service '${this.fullName}' is creating...`); @@ -326,7 +325,6 @@ class Service { * * @returns {Promise} * @private - * @memberof Service */ _stop() { this.logger.debug(`Service '${this.fullName}' is stopping...`); @@ -361,7 +359,6 @@ class Service { * @returns {Object} * * @private - * @memberof Service */ _createAction(actionDef, name) { let action; @@ -445,7 +442,6 @@ class Service { * @returns {Record} * * @private - * @memberof Service */ _createEvent(eventDef, name) { let event; @@ -539,7 +535,6 @@ class Service { * Getter of current Context. * @returns {Context?} * - * @memberof Service * get currentContext() { return this.broker.getCurrentContext(); @@ -548,7 +543,6 @@ class Service { /** * Setter of current Context * - * @memberof Service * set currentContext(ctx) { this.broker.setCurrentContext(ctx); @@ -561,7 +555,6 @@ class Service { * @param {number?} timeout Timeout in milliseconds * @param {number?} interval Check interval in milliseconds * @returns {Promise} - * @memberof Service */ waitForServices(serviceNames, timeout, interval) { return this.broker.waitForServices(serviceNames, timeout, interval, this.logger); @@ -573,7 +566,6 @@ class Service { * @param {Partial} schema * @returns {Partial} * - * @memberof Service */ applyMixins(schema) { if (schema.mixins) { @@ -602,7 +594,6 @@ class Service { * @param {Partial} svcSchema Service schema * @returns {Partial} Mixed schema * - * @memberof Service */ mergeSchemas(mixinSchema, svcSchema) { const res = _.cloneDeep(mixinSchema); diff --git a/tsconfig.json b/tsconfig.json index e836e977d..b397be456 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,9 @@ "moduleResolution": "node", "noEmit": true, "lib": ["ES2022"], + "typeRoots": ["types/**/*.d.ts"], + "types": ["node"] }, - "include": ["**.js", "types/**/*.ts", "src/service.js"], + "include": ["**.js"], "exclude": ["node_modules", ".eslintrc.js", "prettier.config.js"] } diff --git a/types/extends.d.ts b/types/extends.d.ts index b1b25b4ab..a1d382ddf 100644 --- a/types/extends.d.ts +++ b/types/extends.d.ts @@ -1,13 +1,16 @@ interface Promise { delay(ms: number): Promise; - method(fn: Function): Function; + method(fn: Function): Function; timeout(ms: number, message: String): Promise; mapSeries(arr: Array, fn: Function): Promise; + TimeoutError: ExtendableError; + ctx: Context; } interface PromiseConstructor { delay(ms: number): Promise; - method(fn: Function): Function; + method(fn: Function): Function; timeout(ms: number, message: String): Promise; mapSeries(arr: Array, fn: Function): Promise; + TimeoutError: ExtendableError; }