slack-logs 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -1
- package/package.json +1 -1
- package/src/index.ts +4 -4
- package/dist/index.js +0 -111
- package/dist/index.ts +0 -138
package/README.md
CHANGED
|
@@ -38,7 +38,16 @@ Here is a basic example of how to use the Slack Log package to send a message to
|
|
|
38
38
|
|
|
39
39
|
```
|
|
40
40
|
...
|
|
41
|
-
|
|
41
|
+
# Slack Webhook URL for sending logs and notifications.
|
|
42
|
+
# Replace with your actual webhook URL.
|
|
43
|
+
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/******/******/********************
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# Optional field, defaults to "true".
|
|
47
|
+
# Set to "false" to disable Slack logs globally without changing the code.
|
|
48
|
+
ENABLE_SLACK_LOGS=true
|
|
49
|
+
# Note: No need to define this variable unless you want to disable Slack logs.
|
|
50
|
+
# If variable is not defined, it defaults to "true".
|
|
42
51
|
...
|
|
43
52
|
```
|
|
44
53
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -52,7 +52,7 @@ export const slack: Slack = {
|
|
|
52
52
|
if (!isValidSlackWebhookUrl()) {
|
|
53
53
|
// SLACK_WEBHOOK_URL is valid
|
|
54
54
|
console.error(
|
|
55
|
-
"🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
|
|
55
|
+
"🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
|
|
56
56
|
);
|
|
57
57
|
return null;
|
|
58
58
|
}
|
|
@@ -120,10 +120,10 @@ function isValidSlackWebhookUrl(): boolean {
|
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
async function axiosCall(payload: any) {
|
|
123
|
-
const
|
|
124
|
-
process?.env?.
|
|
123
|
+
const ENABLE_SLACK_LOGS: boolean =
|
|
124
|
+
(process?.env?.ENABLE_SLACK_LOGS ?? "").toString() === "true";
|
|
125
125
|
|
|
126
|
-
if (
|
|
126
|
+
if (ENABLE_SLACK_LOGS) return false;
|
|
127
127
|
|
|
128
128
|
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
|
|
129
129
|
return await axios
|
package/dist/index.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.slack = exports.LogColor = exports.LogLevel = void 0;
|
|
7
|
-
const axios_1 = __importDefault(require("axios"));
|
|
8
|
-
var LogLevel;
|
|
9
|
-
(function (LogLevel) {
|
|
10
|
-
LogLevel["DEFAULT"] = "DEFAULT";
|
|
11
|
-
LogLevel["SUCCESS"] = "SUCCESS";
|
|
12
|
-
LogLevel["INFO"] = "INFO";
|
|
13
|
-
LogLevel["WARN"] = "WARN";
|
|
14
|
-
LogLevel["ERROR"] = "ERROR";
|
|
15
|
-
})(LogLevel || (exports.LogLevel = LogLevel = {}));
|
|
16
|
-
var LogColor;
|
|
17
|
-
(function (LogColor) {
|
|
18
|
-
LogColor["DEFAULT"] = "#B4B4B8";
|
|
19
|
-
LogColor["SUCCESS"] = "#65B741";
|
|
20
|
-
LogColor["INFO"] = "#40A2D8";
|
|
21
|
-
LogColor["WARN"] = "#E3651D";
|
|
22
|
-
LogColor["ERROR"] = "#FF0000";
|
|
23
|
-
})(LogColor || (exports.LogColor = LogColor = {}));
|
|
24
|
-
exports.slack = {
|
|
25
|
-
async log(label, data) {
|
|
26
|
-
const messageData = JSON.stringify(data);
|
|
27
|
-
if (!isValidSlackWebhookUrl()) {
|
|
28
|
-
// SLACK_WEBHOOK_URL is valid
|
|
29
|
-
console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
const message = `*${label}:* ${messageData}`;
|
|
33
|
-
let payload = {
|
|
34
|
-
text: message,
|
|
35
|
-
};
|
|
36
|
-
await axiosCall(payload);
|
|
37
|
-
},
|
|
38
|
-
async logBlockMessage(label, objectData, error_type = LogLevel.DEFAULT) {
|
|
39
|
-
if (!isValidSlackWebhookUrl()) {
|
|
40
|
-
// SLACK_WEBHOOK_URL is valid
|
|
41
|
-
console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
console.log(error_type);
|
|
45
|
-
const messageBodyColor = LogColor[error_type];
|
|
46
|
-
const blocks = [];
|
|
47
|
-
const attachments = [];
|
|
48
|
-
const attachmentsBlocks = [];
|
|
49
|
-
blocks.push({
|
|
50
|
-
type: "divider",
|
|
51
|
-
});
|
|
52
|
-
// Label of slack message
|
|
53
|
-
blocks.push({
|
|
54
|
-
type: "header",
|
|
55
|
-
text: {
|
|
56
|
-
type: "plain_text",
|
|
57
|
-
text: label,
|
|
58
|
-
emoji: true,
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
blocks.push({
|
|
62
|
-
type: "divider",
|
|
63
|
-
});
|
|
64
|
-
if (objectData?.length) {
|
|
65
|
-
objectData.forEach((element) => {
|
|
66
|
-
let value = JSON.stringify(element.value);
|
|
67
|
-
attachmentsBlocks.push({
|
|
68
|
-
type: "section",
|
|
69
|
-
text: {
|
|
70
|
-
type: "mrkdwn",
|
|
71
|
-
text: `*${element.title}:* ${value}`,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
attachments.push({
|
|
77
|
-
color: messageBodyColor,
|
|
78
|
-
blocks: attachmentsBlocks,
|
|
79
|
-
});
|
|
80
|
-
let payload = {
|
|
81
|
-
text: label,
|
|
82
|
-
blocks: blocks,
|
|
83
|
-
attachments: attachments,
|
|
84
|
-
};
|
|
85
|
-
await axiosCall(payload);
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
function isValidSlackWebhookUrl() {
|
|
89
|
-
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
|
|
90
|
-
// Check if SLACK_WEBHOOK_URL is not null or undefined
|
|
91
|
-
if (!SLACK_WEBHOOK_URL ||
|
|
92
|
-
SLACK_WEBHOOK_URL == null ||
|
|
93
|
-
typeof SLACK_WEBHOOK_URL == "undefined") {
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
// Check if SLACK_WEBHOOK_URL starts with "https://"
|
|
97
|
-
return SLACK_WEBHOOK_URL.startsWith("https://");
|
|
98
|
-
}
|
|
99
|
-
async function axiosCall(payload) {
|
|
100
|
-
const DISABLE_SLACK_LOGS = process?.env?.DISABLE_SLACK_LOGS === "false";
|
|
101
|
-
if (DISABLE_SLACK_LOGS)
|
|
102
|
-
return false;
|
|
103
|
-
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
|
|
104
|
-
return await axios_1.default
|
|
105
|
-
.post(SLACK_WEBHOOK_URL, JSON.stringify(payload), {
|
|
106
|
-
headers: { "Content-Type": "application/json" },
|
|
107
|
-
})
|
|
108
|
-
.catch((error) => {
|
|
109
|
-
console.error("🚨 Error sending log message to Slack: Webhook URL might be updated!");
|
|
110
|
-
});
|
|
111
|
-
}
|
package/dist/index.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
|
|
3
|
-
interface Slack {
|
|
4
|
-
log(label: string, data: any): Promise<null | undefined>;
|
|
5
|
-
logBlockMessage(
|
|
6
|
-
label: string,
|
|
7
|
-
objectData: BlocksInterface[],
|
|
8
|
-
error_type?: LogLevel
|
|
9
|
-
): Promise<null | undefined>;
|
|
10
|
-
}
|
|
11
|
-
interface BlocksInterface {
|
|
12
|
-
title: string;
|
|
13
|
-
value: any;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export enum LogLevel {
|
|
17
|
-
DEFAULT = "DEFAULT",
|
|
18
|
-
SUCCESS = "SUCCESS",
|
|
19
|
-
INFO = "INFO",
|
|
20
|
-
WARN = "WARN",
|
|
21
|
-
ERROR = "ERROR",
|
|
22
|
-
}
|
|
23
|
-
export enum LogColor {
|
|
24
|
-
DEFAULT = "#B4B4B8",
|
|
25
|
-
SUCCESS = "#65B741",
|
|
26
|
-
INFO = "#40A2D8",
|
|
27
|
-
WARN = "#E3651D",
|
|
28
|
-
ERROR = "#FF0000",
|
|
29
|
-
}
|
|
30
|
-
export const slack: Slack = {
|
|
31
|
-
async log(label: string, data: any) {
|
|
32
|
-
const messageData = JSON.stringify(data);
|
|
33
|
-
if (!isValidSlackWebhookUrl()) {
|
|
34
|
-
// SLACK_WEBHOOK_URL is valid
|
|
35
|
-
console.error(
|
|
36
|
-
"🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
|
|
37
|
-
);
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const message = `*${label}:* ${messageData}`;
|
|
42
|
-
let payload = {
|
|
43
|
-
text: message,
|
|
44
|
-
};
|
|
45
|
-
await axiosCall(payload);
|
|
46
|
-
},
|
|
47
|
-
async logBlockMessage(
|
|
48
|
-
label: string,
|
|
49
|
-
objectData: BlocksInterface[],
|
|
50
|
-
error_type: LogLevel = LogLevel.DEFAULT
|
|
51
|
-
) {
|
|
52
|
-
if (!isValidSlackWebhookUrl()) {
|
|
53
|
-
// SLACK_WEBHOOK_URL is valid
|
|
54
|
-
console.error(
|
|
55
|
-
"🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨",
|
|
56
|
-
);
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
console.log(error_type);
|
|
60
|
-
const messageBodyColor = LogColor[error_type];
|
|
61
|
-
|
|
62
|
-
const blocks: object[] = [];
|
|
63
|
-
const attachments: object[] = [];
|
|
64
|
-
const attachmentsBlocks: object[] = [];
|
|
65
|
-
blocks.push({
|
|
66
|
-
type: "divider",
|
|
67
|
-
});
|
|
68
|
-
// Label of slack message
|
|
69
|
-
blocks.push({
|
|
70
|
-
type: "header",
|
|
71
|
-
text: {
|
|
72
|
-
type: "plain_text",
|
|
73
|
-
text: label,
|
|
74
|
-
emoji: true,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
blocks.push({
|
|
78
|
-
type: "divider",
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (objectData?.length) {
|
|
82
|
-
objectData.forEach((element) => {
|
|
83
|
-
let value = JSON.stringify(element.value);
|
|
84
|
-
attachmentsBlocks.push({
|
|
85
|
-
type: "section",
|
|
86
|
-
text: {
|
|
87
|
-
type: "mrkdwn",
|
|
88
|
-
text: `*${element.title}:* ${value}`,
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
attachments.push({
|
|
94
|
-
color: messageBodyColor,
|
|
95
|
-
blocks: attachmentsBlocks,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
let payload = {
|
|
99
|
-
text: label,
|
|
100
|
-
blocks: blocks,
|
|
101
|
-
attachments: attachments,
|
|
102
|
-
};
|
|
103
|
-
await axiosCall(payload);
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
function isValidSlackWebhookUrl(): boolean {
|
|
108
|
-
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
|
|
109
|
-
// Check if SLACK_WEBHOOK_URL is not null or undefined
|
|
110
|
-
if (
|
|
111
|
-
!SLACK_WEBHOOK_URL ||
|
|
112
|
-
SLACK_WEBHOOK_URL == null ||
|
|
113
|
-
typeof SLACK_WEBHOOK_URL == "undefined"
|
|
114
|
-
) {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Check if SLACK_WEBHOOK_URL starts with "https://"
|
|
119
|
-
return SLACK_WEBHOOK_URL.startsWith("https://");
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function axiosCall(payload: any) {
|
|
123
|
-
const DISABLE_SLACK_LOGS: boolean =
|
|
124
|
-
process?.env?.DISABLE_SLACK_LOGS === "false";
|
|
125
|
-
|
|
126
|
-
if (DISABLE_SLACK_LOGS) return false;
|
|
127
|
-
|
|
128
|
-
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
|
|
129
|
-
return await axios
|
|
130
|
-
.post(SLACK_WEBHOOK_URL as string, JSON.stringify(payload), {
|
|
131
|
-
headers: { "Content-Type": "application/json" },
|
|
132
|
-
})
|
|
133
|
-
.catch((error) => {
|
|
134
|
-
console.error(
|
|
135
|
-
"🚨 Error sending log message to Slack: Webhook URL might be updated!"
|
|
136
|
-
);
|
|
137
|
-
});
|
|
138
|
-
}
|