-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
81 lines (66 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
// Require bluebird
const _ = require( 'lodash' );
const bluebird = require( 'bluebird' );
// sudo global
const config = require( './lib/config' );
// Lapin object placeholder
let lapin;
// Load patterns
const ReqRes = require( './lib/req-res' );
const SendRec = require( './lib/send-rec' );
const PubSub = require( './lib/pub-sub' );
function Lapin () {
// Initialize patterns
const reqRes = new ReqRes();
const sendRec = new SendRec();
const pubSub = new PubSub();
// Export interfaces
return {
'request' : reqRes.request,
'respond' : reqRes.respond,
'send' : sendRec.send,
'receive' : sendRec.receive,
'publish' : pubSub.publish,
'subscribe' : pubSub.subscribe,
// Promises
'requestPromise' : bluebird.promisify( reqRes.request ),
'sendPromise' : bluebird.promisify( sendRec.send )
};
}
// pick only the needed options
function hasMultipleOptions ( options ) {
if ( _.has( options, 'rabbit' ) ) {
return true;
}
if ( _.has( options, 'logger' ) ) {
return true;
}
if ( _.has( options, 'timeout' ) ) {
return true;
}
return false;
}
function setConfigs ( options ) {
/*
* setting config for lapin to be used across the lib
* options might not only be rabbit
*/
config.rabbit = options;
config.timeout.setOptions( options.timeout );
if ( hasMultipleOptions( options ) ) {
config.rabbit = options.rabbit;
config.logger = options.logger;
}
if ( !options || !config.rabbit ) {
// should throw error and not proceed to object creation
throw new Error( 'Rabbit required' );
}
}
module.exports = function ( options ) {
if ( !lapin ) {
setConfigs( options );
lapin = new Lapin( options );
}
return lapin;
};