Skip to content

Decorators and typed calls of moleculer services without any extra code

License

Notifications You must be signed in to change notification settings

anthrax63/moleculer-typed-proxy

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Moleculer Typed Proxy

Decorators and typed calls of moleculer services without any extra code.

Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

Product Name Screen Shot

The Moleculer is a great framework. However, from the box it is not very typescript friendly. The description of actions and then their subsequent call through a broker are not strongly typed, which can complicate maintenance and refactoring. This library combines the ability to declaratively describe services using decorators, as well as call methods or events using strong typing.

(back to top)

Built With

(back to top)

Getting Started

This is an example of how to use this library. Library provides decorators for simple service definition and ServiceProxyFactory that is used for creating proxies for typed accessing services. Decorators are based on moleculer-service-decorators, so you can find decorator's documentation there.

ServiceProxyFactory has simple interface with ony one method create. The method takes one argument - class definition. See example:

import moleculer, { ServiceBroker } from 'moleculer';
import {
    ServiceProxyFactory,
    service,
    action,
    string,
} from 'moleculer-typed-proxy';

@service({
    name: 'example-service',
})
export class ExampleService extends moleculer.Service {
    @action({
        name: 'say.hello',
    })
    async sayHello(@string() name: string) {
        return `Hello, ${name}!`;
    }
}

const broker = new ServiceBroker();
const factory = new ServiceProxyFactory(broker);

const serviceProxy = factory.create(ExampleService);

serviceProxy.sayHello('Johny').then((result) => {
    // Result has returned via broker, not direct call
    console.log(result);
});

Method create does not create the instance of derived service! It's just using class interface for know hot to call methods or fire events via broker.

Installation

  • npm
    npm install moleculer-typed-proxy
  • yarn
    yarn add moleculer-typed-proxy

Usage

Emitting events:

import moleculer, { ServiceBroker } from 'moleculer';
import {
    ServiceProxyFactory,
    service,
    event,
    string,
    number,
} from 'moleculer-typed-proxy';

@service({
    name: 'logging-service',
})
export class LoggingService extends moleculer.Service {
    @event({
        name: 'user-registered',
    })
    userRegistered(
        @string() login: string,
        @string() name: string,
        @number() age: number,
    ) {
        console.log('User registered', login, name, age);
    }
}

const broker = new ServiceBroker();
const factory = new ServiceProxyFactory(broker);

const serviceProxy = factory.create(LoggingService);

serviceProxy.userRegistered('login', 'name', 25);

Using with inversify:

import moleculer, { ServiceBroker } from 'moleculer';
import {
    ServiceProxyFactory,
    service,
    event,
    string,
    number,
    action,
} from 'moleculer-typed-proxy';
import { Container, inject } from 'inversify';

export class BaseService extends moleculer.Service {
    constructor(
        @inject('ServiceBroker')
            broker: ServiceBroker,
        @inject('ServiceProxyFactory')
        protected readonly proxyFactory: ServiceProxyFactory,
    ) {
        super(broker);
    }
}

@service({
    name: 'user-service',
})
export class UserService extends BaseService {
    @action({
        name: 'register-user',
    })
    registerUser(
        @string() login: string,
        @string() name: string,
        @number() age: number,
    ) {
        // Add user to db
        // ...
        // Emit event of another service
        this.proxyFactory
            .create(LoggingService)
            .userRegistered(login, name, age);
    }
}

@service({
    name: 'logging-service',
})
export class LoggingService extends BaseService {
    @event({
        name: 'user-registered',
    })
    async userRegistered(
        @string() login: string,
        @string() name: string,
        @number() age: number,
    ) {
        console.log('User registered', login, name, age);
    }
}

const container = new Container();
container.bind('ServiceBroker').to(moleculer.ServiceBroker).inSingletonScope();
container
    .bind('ServiceProxyFactory')
    .to(ServiceProxyFactory)
    .inSingletonScope();

// Get proxy
const userServiceProxy = container
    .get<ServiceProxyFactory>(`ServiceProxyFactory`)
    .create(UserService);

// Call action
userServiceProxy.registerUser('login', 'name', 25).then(() => {
    console.log('User created successfully');
});

See examples directory for more examples.

Roadmap

  • Write e2e tests
  • Write more complex tests

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Denis Bezrukov - @DenisBezrukov

Project Link: https://github.com/anthrax63/moleculer-typed-proxy

(back to top)

About

Decorators and typed calls of moleculer services without any extra code

Resources

License

Stars

Watchers

Forks

Packages

No packages published