Skip to content

Commit

Permalink
service types
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 25, 2023
1 parent c2db096 commit b03f7ba
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
14 changes: 7 additions & 7 deletions src/service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ declare namespace Service {
}
}

declare class Service<S = Service.ServiceSettingSchema> implements Service.ServiceSchema<S> {
constructor(broker: ServiceBroker, schema?: Service.ServiceSchema<S>);
declare class Service<S = Service.ServiceSettingSchema> {
constructor(broker: ServiceBroker, schema?: Partial<Service.ServiceSchema<S>>);

protected parseServiceSchema(schema: Service.ServiceSchema<S>): void;
parseServiceSchema(schema: Partial<Service.ServiceSchema<S>>): void;

name: string;
fullName: string;
Expand Down Expand Up @@ -249,7 +249,7 @@ declare class Service<S = Service.ServiceSettingSchema> implements Service.Servi
*
* @param schema Schema containing the mixins to merge
*/
applyMixins(schema: Service.ServiceSchema): Service.ServiceSchema;
applyMixins(schema: Partial<Service.ServiceSchema>): Partial<Service.ServiceSchema>;

/**
* Merge two Service schema
Expand All @@ -258,9 +258,9 @@ declare class Service<S = Service.ServiceSettingSchema> implements Service.Servi
* @param svcSchema Service schema
*/
mergeSchemas(
mixinSchema: Service.ServiceSchema,
svcSchema: Service.ServiceSchema
): Service.ServiceSchema;
mixinSchema: Partial<Service.ServiceSchema>,
svcSchema: Partial<Service.ServiceSchema>
): Partial<Service.ServiceSchema>;

/**
* Merge `settings` property in schema
Expand Down
19 changes: 5 additions & 14 deletions src/service.js
Original file line number Diff line number Diff line change
@@ -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
*/

Expand Down Expand Up @@ -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<ServiceSchema>} schema schema of service
*
* @memberof Service
*/
constructor(broker, schema) {
if (!isObject(broker)) throw new ServiceSchemaError("Must set a ServiceBroker instance!");
Expand Down Expand Up @@ -249,7 +249,7 @@ class Service {
/**
* Return a service settings without protected properties.
*
* @param {Object?} settings
* @param {Record<string, any>?} settings
*/
_getPublicSettings(settings) {
if (settings && Array.isArray(settings.$secureSettings)) {
Expand All @@ -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...`);
Expand Down Expand Up @@ -326,7 +325,6 @@ class Service {
*
* @returns {Promise}
* @private
* @memberof Service
*/
_stop() {
this.logger.debug(`Service '${this.fullName}' is stopping...`);
Expand Down Expand Up @@ -361,7 +359,6 @@ class Service {
* @returns {Object}
*
* @private
* @memberof Service
*/
_createAction(actionDef, name) {
let action;
Expand Down Expand Up @@ -445,7 +442,6 @@ class Service {
* @returns {Record<string, any>}
*
* @private
* @memberof Service
*/
_createEvent(eventDef, name) {
let event;
Expand Down Expand Up @@ -539,7 +535,6 @@ class Service {
* Getter of current Context.
* @returns {Context?}
*
* @memberof Service
*
get currentContext() {
return this.broker.getCurrentContext();
Expand All @@ -548,7 +543,6 @@ class Service {
/**
* Setter of current Context
*
* @memberof Service
*
set currentContext(ctx) {
this.broker.setCurrentContext(ctx);
Expand All @@ -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);
Expand All @@ -573,7 +566,6 @@ class Service {
* @param {Partial<ServiceSchema>} schema
* @returns {Partial<ServiceSchema>}
*
* @memberof Service
*/
applyMixins(schema) {
if (schema.mixins) {
Expand Down Expand Up @@ -602,7 +594,6 @@ class Service {
* @param {Partial<ServiceSchema>} svcSchema Service schema
* @returns {Partial<ServiceSchema>} Mixed schema
*
* @memberof Service
*/
mergeSchemas(mixinSchema, svcSchema) {
const res = _.cloneDeep(mixinSchema);
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
7 changes: 5 additions & 2 deletions types/extends.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
interface Promise<T> {
delay<T>(ms: number): Promise<T>;
method<T>(fn: Function): Function;
method(fn: Function): Function;
timeout<T>(ms: number, message: String): Promise<T>;
mapSeries<T>(arr: Array<any>, fn: Function): Promise<T>;
TimeoutError: ExtendableError;
ctx: Context;
}

interface PromiseConstructor {
delay<T>(ms: number): Promise<T>;
method<T>(fn: Function): Function;
method(fn: Function): Function;
timeout<T>(ms: number, message: String): Promise<T>;
mapSeries<T>(arr: Array<any>, fn: Function): Promise<T>;
TimeoutError: ExtendableError;
}

0 comments on commit b03f7ba

Please sign in to comment.