Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Latest commit

 

History

History
43 lines (34 loc) · 865 Bytes

readme.md

File metadata and controls

43 lines (34 loc) · 865 Bytes

Moleculer Service

TypeScript decorators for Moleculer.

Install

$ npm install moleculer-service

Example

import { Context } from 'moleculer';
import { Service, service, action, event } from 'moleculer-service';

type WelcomeParams = {
  name: string;
};

@service('greeter')
class GreeterService extends Service {
  @action('hello')
  public async hello(ctx: Context) {
    return 'Hello, World!';
  }

  @action({
    name: 'welcome',
    params: {
      name: 'string',
    },
  })
  public async welcome(ctx: Context<WelcomeParams>) {
    return `Welcome, ${ctx.params.name}!`;
  }

  @event('some.event')
  public async onSomeEvent(payload: Record<string, any>, sender: string, eventName: string) {
    this.logger.info(`Event[${sender}][${eventName}]: ${JSON.stringify(payload)}`);
  }
}