diff --git a/index.d.ts b/index.d.ts index c6dbcf354..d95ddafbe 100644 --- a/index.d.ts +++ b/index.d.ts @@ -720,12 +720,14 @@ declare namespace Moleculer { version?: string | number; } - type ServiceSyncLifecycleHandler = (this: Service) => void; - type ServiceAsyncLifecycleHandler = ( - this: Service + type ServiceSyncLifecycleHandler> = ( + this: T + ) => void; + type ServiceAsyncLifecycleHandler> = ( + this: T ) => void | Promise; - interface ServiceSchema { + interface ServiceSchema { name: string; version?: string | number; settings?: S; @@ -737,9 +739,9 @@ declare namespace Moleculer { hooks?: ServiceHooks; events?: ServiceEvents; - created?: ServiceSyncLifecycleHandler | ServiceSyncLifecycleHandler[]; - started?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[]; - stopped?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[]; + created?: ServiceSyncLifecycleHandler | ServiceSyncLifecycleHandler[]; + started?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[]; + stopped?: ServiceAsyncLifecycleHandler | ServiceAsyncLifecycleHandler[]; [name: string]: any; } diff --git a/test/typescript/hello-world/greeter.service.ts b/test/typescript/hello-world/greeter.service.ts index decf7c2d9..93debc26e 100644 --- a/test/typescript/hello-world/greeter.service.ts +++ b/test/typescript/hello-world/greeter.service.ts @@ -2,88 +2,149 @@ import { Service } from "../../../"; import { Context } from "../../../"; -import { ActionHooks, ServiceHooks, ServiceHooksAfter, ServiceSchema } from "../../../"; +import { ServiceSettingSchema, ServiceSchema } from "../../../"; -type GreeterWelcomeParams = { - name: string -}; +export interface ActionWelcomeParams { + name: string; +} -export default class GreeterService extends Service { - constructor(broker) { - super(broker); +interface GreeterSettings extends ServiceSettingSchema { + defaultName: string; +} - this.parseServiceSchema({ - name: "greeter", - hooks: { - before: { - welcome(ctx: Context): void { - // console.log(`Service hook "before".`); - ctx.params.name = ctx.params.name.toUpperCase(); - } +interface GreeterMethods { + uppercase(str: string): string; +} + +interface GreeterLocalVars { + myVar: string; +} + +type GreeterThis = Service & GreeterMethods & GreeterLocalVars; + +const GreeterService: ServiceSchema = { + name: "greeter", + + /** + * Hooks + */ + hooks: { + before: { + welcome(this: GreeterThis, ctx: Context): void { + // console.log(`Service hook "before".`); + ctx.params.name = this.uppercase(ctx.params.name); + } + }, + after: { + hello: "anotherHookAfter", + welcome: [ + function (this: GreeterThis, ctx: Context, res: any): any { + // console.log(`Service sync hook "after".`); + return res; }, - after: { - hello: "anotherHookAfter", - welcome: [ - function (ctx: Context, res: any): any { - // console.log(`Service sync hook "after".`); - return res; - }, - async (ctx: Context, res: any): Promise => { - // console.log(`Service async hook "after".`); - return await Promise.resolve(res); - }, - "anotherHookAfter" - ], - } as ServiceHooksAfter, - error: { - welcome(ctx: Context, err: Error): void { - // console.log(`Service hook "error".`); - throw err; - } - } - } as ServiceHooks, - actions: { - hello: { - handler: this.hello, - hooks: { - after: [ - function (ctx: Context, res: any): any { - // console.log(`Action sync hook "after".`); - return res; - }, - async (ctx: Context, res: any): Promise => { - // console.log(`Action async hook "after".`); - return await Promise.resolve(res); - }, - "anotherHookAfter" - ] - } as ActionHooks + async function (this: GreeterThis, ctx: Context, res: any): Promise { + // console.log(`Service async hook "after".`); + return await Promise.resolve(res); }, - welcome: this.welcome + "anotherHookAfter" + ], + }, + error: { + welcome(this: GreeterThis, ctx: Context, err: Error): void { + // console.log(`Service hook "error".`); + throw err; + } + } + }, + + /** + * Settings + */ + settings: { + defaultName: "Moleculer", + }, + + /** + * Dependencies + */ + dependencies: [], + + /** + * Actions + */ + actions: { + hello: { + rest: { + method: "GET", + path: "/hello", + }, + handler(this: GreeterThis, ctx: Context): string { + return `Hello ${this.settings.defaultName}`; + }, + hooks: { + after: [ + function (this: GreeterThis, ctx: Context, res: any): any { + // console.log(`Action sync hook "after".`); + return res; + }, + async function (ctx: Context, res: any): Promise { + // console.log(`Action async hook "after".`); + return await Promise.resolve(res); + }, + "anotherHookAfter" + ] } - } as ServiceSchema); - } + }, + + welcome: { + rest: "GET /welcome/:name", + params: { + name: "string", + }, + handler(this: GreeterThis, ctx: Context): string { + return `Welcome, ${ctx.params.name}`; + }, + }, + }, + + /** + * Events + */ + events: {}, /** - * Say a 'Hello' - * - * @returns + * Methods */ - hello() { - return "Hello Moleculer TS"; - } + methods: { + uppercase(this: GreeterThis, str: string): string { + return str.toUpperCase(); + }, + + async anotherHookAfter(this: GreeterThis, ctx: Context, res: any): Promise { + return await Promise.resolve(res); + } + }, + + /** + * Service created lifecycle event handler + */ + created(this: GreeterThis) { + this.logger.info(`${this.name} service - lifecycle method "created" called.`); + }, /** - * Welcome a username - * - * @param {String} name - User name + * Service started lifecycle event handler */ - welcome(ctx: Context) { - return `Welcome, ${ctx.params.name}!`; - } - - async anotherHookAfter(ctx: Context, res: any): Promise { - // console.log(`Another async hook "after".`); - return await Promise.resolve(res); - } + async started(this: GreeterThis) { + this.logger.info(`${this.name} service - lifecycle method "started" called.`); + }, + + /** + * Service stopped lifecycle event handler + */ + async stopped(this: GreeterThis) { + this.logger.info(`${this.name} service - lifecycle method "stopped" called.`); + }, }; + +export default GreeterService; diff --git a/test/typescript/hello-world/index.ts b/test/typescript/hello-world/index.ts index ce7488e86..d1313431d 100644 --- a/test/typescript/hello-world/index.ts +++ b/test/typescript/hello-world/index.ts @@ -29,10 +29,8 @@ broker.loadService(path.join(__dirname, "greeter.service.ts")); await broker.call("greeter.hello"); const res = await broker.call("greeter.welcome", { name: "Typescript" }); - broker.logger.info(""); - broker.logger.info("Result: ", res); - broker.logger.info(""); - if (res != "Welcome, TYPESCRIPT!") + broker.logger.info(`Result: ${res}`); + if (res != "Welcome, TYPESCRIPT") throw new Error("Result is mismatch!"); else await broker.stop();