-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
187 lines (156 loc) · 4.43 KB
/
index.js
File metadata and controls
187 lines (156 loc) · 4.43 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Module dependencies.
*/
var object = require('object-component')
var request = require('superagent')
var url = require('url')
var log = require('debug')('notifier-client')
/**
* Expose `NotifierClient` constructor.
*/
module.exports = NotifierClient
var defaults = {
url: 'http://localhost:9001/api/events',
token: null
}
/**
* Creates a Client instance. Takes an object with options.
* If required options aren't specified, notifier-client considers
* no server is available and will not send any request.
*
* @param {Object} options for configuring the client instance.
* - url {String} full URL to the notifier events endpoint. Defaults to `http://localhost:9001/api/events`
* - token {String} `notifier` api token - REQUIRED.
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
function NotifierClient (options) {
if (!(this instanceof NotifierClient)) {
return new NotifierClient(options)
}
var opts = object.merge({}, defaults)
opts = object.merge(opts, options || {})
this.config = { url: url.parse(opts.url), token: opts.token }
if (!this.enabled()) {
log('Notifications disabled - Error with notifier-client options')
} else {
log('Notifications configured with options: %j and URL: %s', this.config, this._buildUrl())
}
}
/**
* Checks if the `notifier-client` is all set-up and enabled for use.
* @return {Boolean} whether the `notifier` should be considered enabled
*/
NotifierClient.prototype.enabled = function() {
return this._isValidConfig()
}
/**
* Sends notification
*
* @param {Object} or {String} either event with data or just event name
* @param {Function} optional callback
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
NotifierClient.prototype.notify = function(event, callback) {
this.event = {}
if ('object' === typeof event) {
this.event = event
this.send(callback)
} else {
this.event.event = event
}
return this
}
/**
* Initialize user field of event
*
* @param {String} recepient/user id
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
NotifierClient.prototype.to = function(recipient) {
this.event.user = recipient
return this
}
/**
* Initialize data field of event
*
* @param {Object} event data
* @return {NotifierClient} `NotifierClient` instance
* @api public
*/
NotifierClient.prototype.withData = function(data) {
if (typeof data === 'object') {
this.event = object.merge(this.event, data)
} else {
event[data] = data
}
return this
}
/**
* Sends notification request
*
* @param {Function} optional callback
* @api public
*/
NotifierClient.prototype.send = function(callback) {
if (this.enabled()) {
callback = callback || function () {}
request
.post(this._buildUrl(this.config))
.set('Accept', 'application/json')
.send(this.event)
.end(function (err, res) {
if (err) {
if ('ECONNREFUSED' === err.code) {
log('Unable connect to the notifier server - Error: %j', err)
} else {
log('Unexpected error when sending event %j', this.event)
}
return callback(err)
}
if (res.body.error || res.statusCode > 201) {
log('Error for event %j: %s', this.event, res.body.error)
return callback(res.body)
}
callback(null, res.body)
})
} else {
log('Unable to send notification request - notifier disabled')
}
}
/**
* Builds request URL
*
* @api private
*/
NotifierClient.prototype._buildUrl = function () {
return this._pruneDefaultPorts(url.format({
protocol: this.config.url.protocol,
hostname: this.config.url.hostname,
port: this.config.url.port,
pathname: this.config.url.path,
search: '?access_token=' + this.config.token
}))
}
/**
* Asserts the current configuration
* @return {Boolean} whether the configuration is valid.
*/
NotifierClient.prototype._isValidConfig = function() {
var o = this.config
return !!(o.url.protocol && o.url.host && o.url.path && o.token)
}
/**
* Omit default ports for http and https from urls
* @param {String} an url
* @return {String} url without port number if port matches default for protocol.
*/
NotifierClient.prototype._pruneDefaultPorts = function(urlToPrune) {
var urlObj = url.parse(urlToPrune)
if((urlObj.protocol == 'http' && urlObj.port == 80) || (urlObj.protocol == 'https' && urlObj.port == 443)){
delete urlObj.port
}
return url.format(urlObj)
}