You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

ts-cote

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-cote

CoteJS with Typescript decorator

latest
Source
npmnpm
Version
1.0.25
Version published
Maintainers
1
Created
Source

[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

  • https://github.com/dashersw/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

FAQs

Package last updated on 12 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts