1.0.25 • Published 5 years ago
ts-cote v1.0.25
duongvanba ts-cote
Build microservice faster with Typescript Decorators
- Define responder, requester, Subscriber, publisher with decorator
- Catch service peers online/offline event
- Get current service node infomation
Usage
See cote
docs if you want to know more about cote
Install ts-cote
yarn add ts-cote
Enable decorators in tsconfig.json
{
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
Define an event
// File : /examples/events.ts
export class SomeEvent{
message: string;
}
Define an event listener
// File : /examples/Subscriber.ts
import { CoteLoader, Subscriber } from "../src";
import { SomeEvent } from './events';
export class EventListener extends CoteLoader(__filename) {
@Subscriber(SomeEvent)
public $(data: SomeEvent){
console.log('New event SomeEvent with data : ', data);
}
}
Define a responder with publisher
// File : /examples/responder-publisher.ts
import { Cote, Publisher, Responder, Emitter} from "../src"
import { SomeEvent } from './events';
export class RandomService extends Cote(__filename) {
/**
* Notify value to all subcrible
*/
@Publisher(SomeEvent)
notify : Emitter<SomeEvent>;
@Responder()
public async plus(a: number, b: number) : Promise<number>{
this.notify({message: 'NEw plus request with params'+a+' '+b});
return a+b;
}
}
Create startpoint
// File : /examples/start-Subscriber.ts
import { EventListener } from './Subscriber';
new EventListener();
// File : /examples/start-responder-publisher.ts
import { RandomService } from './responder-publisher';
new RandomService();
// File : /examples/start-requester.ts
import { Cote, Requester, PeersOnline, Subscriber, PeersOffline} from "../src"
import { RandomService} from './responder-publisher';
import { ServiceNodeInfo } from "../src/interfaces";
export class TestRequest extends Cote(__filename) {
@Requester()
r : RandomService
@PeersOnline(RandomService)
private _(info : ServiceNodeInfo){
console.log('Random service on');
}
@PeersOffline(RandomService)
private __(info : ServiceNodeInfo){
console.log('Random service off');
}
}
(async()=>{
const x = new TestRequest();
await x.ready;
console.log(await x.r.plus(45,78));
console.log(x.getInfo(RandomService)) // Get RandomService connection info
console.log(x.getInfo()) // Get this responder connection info
})();
Run tests (require nodejs + npm)
cd examples
npm i yarn
yarn add ts-node-dev typescript --dev
ts-node-dev start-Subscriber.ts #Run in terminal window 1
ts-node-dev start-responder-publisher.ts #Run in terminal window 2
ts-node-dev start-requester.ts # Run in terminal window 3
1.0.25
5 years ago
1.0.24
5 years ago
1.0.23
5 years ago
1.0.22
6 years ago
1.0.21
6 years ago
1.0.20
6 years ago
1.0.19
6 years ago
1.0.18
6 years ago
1.0.16
6 years ago
1.0.15
6 years ago
1.0.14
6 years ago
1.0.12
6 years ago
1.0.11
6 years ago
1.0.10
6 years ago
1.0.9
6 years ago
1.0.8
6 years ago
1.0.7
6 years ago
1.0.6
6 years ago
1.0.5
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago