diff --git a/nodejs/sec-config.yaml b/nodejs/sec-config.yaml index c3975f4..d9dd53e 100644 --- a/nodejs/sec-config.yaml +++ b/nodejs/sec-config.yaml @@ -9,7 +9,7 @@ default: service-default0X: plugin: service-default0 enabled: true - config: + config: testa: 5 testb: 6 service-default1X: @@ -21,3 +21,6 @@ default: service-default3X: plugin: service-default3 enabled: true + service-default4X: + plugin: service-default4 + enabled: true diff --git a/nodejs/src/base/base.ts b/nodejs/src/base/base.ts index 684ba7c..7bfc419 100644 --- a/nodejs/src/base/base.ts +++ b/nodejs/src/base/base.ts @@ -43,7 +43,7 @@ export abstract class MainBase { * The name of the plugin * This is also the mapped name, or the name defined in the config rather than it's original defined name */ - public readonly pluginName!: string; + public declare readonly pluginName: string; constructor(config: MainBaseConfig) { this.appId = config.appId; diff --git a/nodejs/src/base/service.ts b/nodejs/src/base/service.ts index 93c5d45..6967608 100644 --- a/nodejs/src/base/service.ts +++ b/nodejs/src/base/service.ts @@ -30,6 +30,7 @@ export abstract class BSBService< ? null : BSBReferencePluginConfigDefinition > { + public static PLUGIN_NAME: string; public abstract readonly initBeforePlugins?: Array; public abstract readonly initAfterPlugins?: Array; public abstract readonly runBeforePlugins?: Array; @@ -63,6 +64,7 @@ export abstract class BSBService< * DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS */ export class BSBServiceRef extends BSBService { + public static PLUGIN_NAME = "BSBServiceRef"; public methods = {}; public initBeforePlugins?: string[] | undefined; public initAfterPlugins?: string[] | undefined; diff --git a/nodejs/src/base/serviceClient.ts b/nodejs/src/base/serviceClient.ts index 749fdab..057fed7 100644 --- a/nodejs/src/base/serviceClient.ts +++ b/nodejs/src/base/serviceClient.ts @@ -2,12 +2,16 @@ import { IPluginLogger, DynamicallyReferencedMethodCallable, } from "../interfaces"; -import { BSBService, BSBError, PluginEvents } from "./index"; +import { BSBService, BSBError, PluginEvents, BSBServiceRef } from "./index"; import { DynamicallyReferencedMethodType } from "@bettercorp/tools/lib/Interfaces"; +/** + * @deprecated Use ServiceClient instead + * @description [NOT REALLY DEPRECATED] - ONLY USE THIS IF YOU NEED SPECIFIC CLIENT LOGIC, OTHERWISE USE ServiceClient + */ export abstract class BSBServiceClient { - protected readonly log!: IPluginLogger; - protected readonly events!: PluginEvents< + protected declare readonly log: IPluginLogger; + protected declare readonly events: PluginEvents< Service["_virtual_internal_events"]["emitEvents"], Service["_virtual_internal_events"]["onEvents"], Service["_virtual_internal_events"]["emitReturnableEvents"], @@ -15,7 +19,7 @@ export abstract class BSBServiceClient { Service["_virtual_internal_events"]["emitBroadcast"], Service["_virtual_internal_events"]["onBroadcast"] >; - public callMethod( + protected callMethod( // eslint-disable-next-line @typescript-eslint/no-unused-vars ...args: DynamicallyReferencedMethodCallable< DynamicallyReferencedMethodType, @@ -47,6 +51,48 @@ export abstract class BSBServiceClient { public abstract run?(): Promise; } +export class ServiceClient< + Service extends BSBService, + ServiceT extends typeof BSBServiceRef = any +> extends BSBServiceClient { + public readonly pluginName: string = "{UNSET SERVICE CLIENT PLUGIN NAME}"; + public readonly initBeforePlugins?: Array; + public readonly initAfterPlugins?: Array; + public readonly runBeforePlugins?: Array; + public readonly runAfterPlugins?: Array; + public dispose?(): void; + public init?(): Promise; + public run?(): Promise; + + public override callMethod( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ...args: DynamicallyReferencedMethodCallable< + DynamicallyReferencedMethodType, + TA + > + ): DynamicallyReferencedMethodCallable< + DynamicallyReferencedMethodType, + TA, + false + > { + return this.callMethod(...args); + } + + public declare events: PluginEvents< + Service["_virtual_internal_events"]["emitEvents"], + Service["_virtual_internal_events"]["onEvents"], + Service["_virtual_internal_events"]["emitReturnableEvents"], + Service["_virtual_internal_events"]["onReturnableEvents"], + Service["_virtual_internal_events"]["emitBroadcast"], + Service["_virtual_internal_events"]["onBroadcast"] + >; + + constructor(service: ServiceT, context: BSBService) { + super(context); + this.pluginName = service.PLUGIN_NAME; + } +} + /** * DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS */ diff --git a/nodejs/src/plugins/service-default1/plugin.ts b/nodejs/src/plugins/service-default1/plugin.ts index aff186c..03b561b 100644 --- a/nodejs/src/plugins/service-default1/plugin.ts +++ b/nodejs/src/plugins/service-default1/plugin.ts @@ -36,7 +36,7 @@ export class Plugin extends BSBService { run?(): void | Promise; private count = 0; - public override async init() { + public async init() { this.log.info("INIT SERVICE"); this.events.onEvent("onReceivable", async (a: number, b: number) => { this.count++; diff --git a/nodejs/src/plugins/service-default2/plugin.ts b/nodejs/src/plugins/service-default2/plugin.ts index eeb15f5..b9a7650 100644 --- a/nodejs/src/plugins/service-default2/plugin.ts +++ b/nodejs/src/plugins/service-default2/plugin.ts @@ -8,13 +8,13 @@ export class Plugin extends BSBService { public methods = {}; dispose?(): void; init?(): void | Promise; - public override initAfterPlugins: string[] = ["service-default1"]; + public initAfterPlugins: string[] = ["service-default1"]; private testClient: testClient; constructor(config: BSBServiceConstructor) { super(config); this.testClient = new testClient(this); } - public override async run() { + public async run() { await this.testClient.abc(10, 12, 11, 13); } } diff --git a/nodejs/src/plugins/service-default3/plugin.ts b/nodejs/src/plugins/service-default3/plugin.ts index 37f6aeb..6f02aee 100644 --- a/nodejs/src/plugins/service-default3/plugin.ts +++ b/nodejs/src/plugins/service-default3/plugin.ts @@ -2,19 +2,30 @@ import { BSBService, BSBServiceConstructor } from "../../"; import { testClient } from "../service-default1"; export class Plugin extends BSBService { + public static PLUGIN_NAME = "service-default3"; public initBeforePlugins?: string[] | undefined; public runBeforePlugins?: string[] | undefined; public runAfterPlugins?: string[] | undefined; - public methods = {}; + public methods = { + testMethod: () => { + this.log.info("TEST CALLABLE OK"); + return "test"; + }, + }; dispose?(): void; - init?(): void | Promise; - public override initAfterPlugins: string[] = ["service-default2"]; + public initAfterPlugins: string[] = ["service-default2"]; private testClient: testClient; constructor(config: BSBServiceConstructor) { super(config); this.testClient = new testClient(this); } - public override async run() { + public async init() { + await this.events.onReturnableEvent("onReverseReturnable", async (tex: string) => { + this.log.warn("onReverseReturnable ({tex})", { tex }); + return tex.split("").reverse().join(""); + }); + } + public async run() { await this.testClient.abc(18, 19, 20, 21); this.log.error("Error {a}", new Error("err"), { a: "b" }); } diff --git a/nodejs/src/plugins/service-default4/plugin.ts b/nodejs/src/plugins/service-default4/plugin.ts new file mode 100644 index 0000000..d2aab1c --- /dev/null +++ b/nodejs/src/plugins/service-default4/plugin.ts @@ -0,0 +1,29 @@ +import { BSBService, BSBServiceConstructor, ServiceClient } from "../../"; +import { Plugin as S3Plugin } from "../service-default3/plugin"; + +export class Plugin extends BSBService { + public initBeforePlugins?: string[] | undefined; + public runBeforePlugins?: string[] | undefined; + public runAfterPlugins?: string[] | undefined; + public methods = {}; + dispose?(): void; + init?(): void | Promise; + public override initAfterPlugins: string[] = []; + private service3: ServiceClient; + constructor(config: BSBServiceConstructor) { + super(config); + this.service3 = new ServiceClient(S3Plugin, this); + } + public override async run() { + this.log.info("TEST CALLABLE OK ? [{result}]", { + result: this.service3.callMethod("testMethod"), + }); + this.log.info("TEST RETURNABLE OK ? [{result}]", { + result: await this.service3.events.emitEventAndReturn( + "onReverseReturnable", + 5, + "teXt" + ), + }); + } +}