-
Notifications
You must be signed in to change notification settings - Fork 14
/
slackHook.js
69 lines (57 loc) · 2.07 KB
/
slackHook.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
"use strict";
const Transport = require("winston-transport");
const axios = require("axios").default;
module.exports = class SlackHook extends Transport {
constructor(opts) {
super(opts);
opts = opts || {};
this.name = opts.name || "slackWebhook";
this.level = opts.level || undefined;
this.webhookUrl = opts.webhookUrl;
this.formatter = opts.formatter || undefined;
this.unfurlLinks = opts.unfurlLinks || false;
this.unfurlMedia = opts.unfurlMedia || false;
this.mrkdwn = opts.mrkdwn || false;
this.channel = opts.channel || "";
this.username = opts.username || "";
this.iconEmoji = opts.iconEmoji || "";
this.iconUrl = opts.iconUrl || "";
this.emitAxiosErrors = opts.emitAxiosErrors || false;
this.axiosInstance = axios.create({
proxy: opts.proxy || undefined
});
}
async log(info, callback) {
let payload = {
unfurl_links: this.unfurlLinks,
unfurl_media: this.unfurlMedia,
mrkdwn: this.mrkdwn,
channel: this.channel,
username: this.username,
icon_emoji: this.iconEmoji,
icon_url: this.iconUrl
};
if (this.formatter && typeof this.formatter === "function") {
let layout = this.formatter(info);
if (!layout) {
callback();
return;
}
// Note: Supplying `text` when `blocks` is also supplied will cause `text`
// to be used as a fallback for clients/surfaces that don't suopport blocks
Object.keys(layout).forEach((key) => {
payload[key] = layout[key];
});
} else {
payload.text = `${info.level}: ${info.message}`;
}
try {
await this.axiosInstance.post(this.webhookUrl, payload);
this.emit("logged", info);
} catch (err) {
if (this.emitAxiosErrors) this.emit("error", err);
} finally {
callback();
}
}
};