-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
74 lines (68 loc) · 1.73 KB
/
index.ts
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
import build from 'pino-abstract-transport';
import axios from 'axios';
export async function sendNotiToSlack({
url,
channel,
username,
emoji,
text,
}: {
url: string;
channel: string;
username?: string;
emoji?: string;
text: string;
}) {
const configs = {
method: 'POST',
url,
headers: { 'content-type': 'application/json' },
data: {
channel,
username,
icon_emoji: emoji,
text,
},
};
try {
const response = await axios(configs);
if (response.status !== 200) {
console.error({ ...response });
}
} catch (e) {
throw new Error(`${e}`);
}
}
export default async function (opts: {
webhookUrl: string;
channel: string;
username?: string;
emoji?: string;
verbose?: boolean;
}) {
const {
webhookUrl,
channel,
username = 'webhookbot',
emoji = ':ghost:',
verbose = false,
} = opts;
if (!webhookUrl || !channel) {
throw new Error('The required options(webhookUrl, channel) are missing');
}
return build(async (source) => {
for await (const obj of source) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { time, level, msg, err, error, stack, ...props } = obj;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { message: errMessage, stack: errStack, ...errorProps} = err || error || {};
await sendNotiToSlack({
url: webhookUrl,
channel,
username,
emoji,
text: verbose ? JSON.stringify(obj) : msg,
});
}
});
}