moleculer-typed-proxy
v1.0.4
Published
Moleculer proxy to use typed calls
Downloads
670
Maintainers
Readme
About The Project
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.
Built With
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).
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt
for more information.
Contact
Denis Bezrukov - @DenisBezrukov
Project Link: https://github.com/anthrax63/moleculer-typed-proxy