Created
          July 8, 2017 17:58 
        
      - 
      
- 
        Save sberryman/fd6bb4e7c2daafbdd44a70eadaa3c162 to your computer and use it in GitHub Desktop. 
    hemera pub decorator example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 'use strict' | |
| const Hemera = require('./../../packages/hemera') | |
| const nats = require('nats').connect() | |
| const hemera = new Hemera(nats, { | |
| logLevel: 'info' | |
| }) | |
| hemera.ready(() => { | |
| // assume these options come from a hemera plugin | |
| const options = { | |
| prefix: 'e', | |
| defaultType: 'order' | |
| } | |
| hemera.decorate('pub', (eventType, msg) => { | |
| if (typeof eventType === 'object') { | |
| msg = eventType; | |
| eventType = options.defaultType; | |
| } | |
| // build a topic unless provided | |
| if (!msg.topic) { | |
| msg.topic = [ | |
| options.prefix, | |
| eventType | |
| ]; | |
| // add an id and status if we have them | |
| if (msg.id) { | |
| msg.topic.push(msg.id); | |
| } | |
| if (msg.status) { | |
| msg.topic.push(msg.status); | |
| } | |
| msg.topic = msg.topic.join('.'); | |
| } | |
| // force pubsub$ | |
| msg.pubsub$ = true; | |
| // and that's a wrap! | |
| this.act(msg); | |
| }) | |
| // global namespace | |
| // topic should be e.order.abc123.created | |
| hemera.pub({ | |
| id: 'abc123', | |
| status: 'created', | |
| name: 'Test User', | |
| items: [] | |
| }); | |
| }) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment