0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/lib/notify.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

var Handlebars = require('handlebars')
var request = require('request')
var Logger = require('./logger')
var handleNotify = function(metadata, notifyEntry) {
var regex
2017-04-26 07:20:24 -05:00
if (metadata.name && notifyEntry.packagePattern) {
regex = new RegExp(notifyEntry.packagePattern, notifyEntry.packagePatternFlags || '')
2017-04-26 07:20:24 -05:00
if (!regex.test(metadata.name)) {
return
}
}
var template = Handlebars.compile(notifyEntry.content)
var content = template( metadata )
var options = {
body: content
}
// provides fallback support, it's accept an Object {} and Array of {}
2017-04-26 07:20:24 -05:00
if (notifyEntry.headers && Array.isArray(notifyEntry.headers)) {
var header = {}
notifyEntry.headers.map(function(item) {
if (Object.is(item, item)) {
for (var key in item) {
2017-04-26 07:20:24 -05:00
header[key] = item[key]
}
}
2017-04-26 07:20:24 -05:00
})
options.headers = header;
} else if (Object.is(notifyEntry.headers, notifyEntry.headers)) {
2017-04-26 07:20:24 -05:00
options.headers = notifyEntry.headers
}
options.method = notifyEntry.method
2017-04-26 07:20:24 -05:00
if ( notifyEntry.endpoint ) {
options.url = notifyEntry.endpoint
}
request(options, function(err, response, body) {
if (err) {
2017-04-26 07:20:24 -05:00
Logger.logger.error( { err: err }, ' notify error: @{err.message}' )
} else {
Logger.logger.info({ content: content}, 'A notification has been shipped: @{content}')
if (body) {
2017-04-26 07:20:24 -05:00
Logger.logger.debug( { body: body }, ' body: @{body}' )
}
}
2017-04-26 07:20:24 -05:00
})
}
module.exports.notify = function(metadata, config) {
if (config.notify) {
2017-04-26 07:20:24 -05:00
if (config.notify.content) {
handleNotify(metadata, config.notify)
}
else {
2017-04-26 07:20:24 -05:00
for (var key in config.notify) {
if (config.notify.hasOwnProperty(key)) {
handleNotify(metadata, config.notify[key])
}
}
}
}
}