Skip to content

Create Adapter

Daniel Vasylenko edited this page Jun 26, 2017 · 4 revisions

The aim of an adapter is to ease the syntax or expose some of the features, that a user may be willing to use.

A basic adapter should be a class, accepting the following constructor arguments:

  • stream {Object} - instance of a stream
  • settings {Object} - stream settings

Let's take a look at a Winston adapter, that is a default one, used inside a logger.

Without it, a user would have to use the following api to use a logger:

const { stream } = require('logtify')(...);

stream.log('info', 'My message', { metadata: { a: 'b'} });

Thanks to a winston adapter, we can do the following:

const { stream, logger } = require('logtify')(...);

logger.info('My message', { metadata: { a: 'b' } });

Easy, right?

To create your own adapter, you should do the following:

/**
 if you want to create an adapter for your subscriber, do the following:
**/
const { CustomSubscriber } = require ('CustomSubscriber');
class CustomAdapter {
   constructor(stream, settings){
      this.stream = stream;
      this.settings = settings || {};
      this.subscriber = new CustomSubscriber(this.settings);
   }
   // your logic
}

module.exports = CustomAdapter;

References:

Clone this wiki locally