1.2.7 • Published 2 years ago

@pestras/micro-nats v1.2.7

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Pestras Micro Nats

Pestras microservice plugin for nats messaging server support.

install

npm i @pestras/micro @pestras/micro-nats

Plug In

import { SERVICE, Micro } from '@pestras/micro';
import { MicroNats } from '@pestras/micro-nats';

Micro.plugins(new MicroNats());

@SERVICE()
class test {}

Micro.start(Test);

MicroNats class accepts a single optional argument connection.

NameTypeDefaultDescription
connectionstring | number | NatsConnectionOptions'localhost:4222'see Nats Docs

SUBJECT DECORATOR

Used to subscribe to nats server pulished subjects, and also accepts a subject string as a first argument and an optional config object.

NameTypeDefaultDescription
hooksstring[][]hooks methods that should be called before the route handler
dataQuotanumber1024 * 100Subject msg data size limit
payloadNats.PayloadPayload.JSONsee Nats Docs
optionsNats.SubscriptionOptionsnullsee Nats Docs
metaanyextra details that will be passed to the handler, useful for multiple subjects
import { SERVICE, Micro } from '@pestras/micro';
import { SUBJECT, NatsMsg } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

@SERVICE({ workers: 3 })
class Email {
  
  async auth(nats: Client, msg: NatsMsg, handlerName: string) {
    // if hook failed its purpose should check for msg reply if exists and return false
    if (msg.reply) {
      nats.publish(msg.replay, { error: 'some error' })
      return false
    }

    // otherwise
    return true;
  }

  @SUBJECT('user.insert', {
    hooks: ['auth'],
    options: { queue: 'emailServiceWorker' }
  })
  sendActivationEmail(nats: Client, msg: NatsMsg) {
    let auth = msg.data.auth;
  }

Hooks must return or resolve (async) to true on success or false on failure.

Multible Subjects

Multible subjects can be used on the same handler.

import { SERVICE, Micro } from '@pestras/micro';
import { SUBJECT, NatsMsg } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

interface MsgInput { id: string; email: string }

@SERVICE()
class Email {

  @SUBJECT('emails.new', { meta: { template: "newEmail" } })
  @SUBJECT('emails.reactivate', { meta: { template: "reactivateEmail" } })
  sendActivataionEmail(client: Client, msg: NatsMsg<MsgInput>, meta: any) {
    // send email
    let emailTemplate = meta.template;
  }
}

Sub Services

// comments.service.ts
import { SUBJECT, NatsEvents } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

export class Comments implements NatsEvents {

  onNatsConnected(client: Client) {
    // ...
  }
  
  validate(client: Client, msg: NatsMsg, handlerName: string) { return true }
  
  @SUBJECT('newComment', {
    // auth hook from the main service
    // validate hook from the local service (sub service)
    hooks: ['auth', 'validate']
  })
  create(client: Client, msg: NatsMsg) {
    
  }
}
// main.ts
import { Micro, SERVICE } from '@pestras/micro';
import { SUBJECT, NATS_HOOK } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

@SERVICE()
class Articles {

  onInit() {    
    Micro.store.someSharedValue = "shared value";
  }
  
  async auth(client: Client, msg: NatsMsg, handlerName: string) {
    return true;
  }
  
  validate(client: Client, msg: NatsMsg, handlerName: string) {
    return true;
  }

  @SUBJECT('newArticle', {
    // both hooks from the main service
    hooks: ['auth', 'validate']
  })
  create(client: Client, msg: NatsMsg) {
    
  }
}

// pass sub services as an array to the second argument of Micro.start method
Micro.start(Articles, [Comments]);
  • Local hooks has the priority over main service hooks.
  • Subservices have their own lifecycle events.

lifecycle Events

onNatsConnected

Called whenever nats driver has a successfull connection

import { SERVICE, Micro } from '@pestras/micro';
import { SUBJECT, NatsMsg, NatsEvents } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

interface MsgInput { id: string; email: string }

@SERVICE()
class Email implements NatsEvents {

  onNatsConnected(client: Client) {
    // ...
  }

  @SUBJECT('emails.new')
  sendActivataionEmail(client: Client, msg: NatsMsg<MsgInput>) {
    // send email
  }
}

Thank you

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago