slack-logs 1.4.2 → 1.6.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/dist/index.js +1 -111
- package/package.json +7 -4
package/dist/index.js
CHANGED
|
@@ -1,111 +1 @@
|
|
|
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 ENABLE_SLACK_LOGS = (process?.env?.ENABLE_SLACK_LOGS ?? "").toString() === "true";
|
|
101
|
-
if (ENABLE_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
|
-
}
|
|
1
|
+
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});var t,o,r=e(require("axios"));exports.LogLevel=void 0,(t=exports.LogLevel||(exports.LogLevel={})).DEFAULT="DEFAULT",t.SUCCESS="SUCCESS",t.INFO="INFO",t.WARN="WARN",t.ERROR="ERROR",exports.LogColor=void 0,(o=exports.LogColor||(exports.LogColor={})).DEFAULT="#B4B4B8",o.SUCCESS="#65B741",o.INFO="#40A2D8",o.WARN="#E3651D",o.ERROR="#FF0000";const s={async log(e,t){const o=JSON.stringify(t);if(!n())return console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null;let r={text:`*${e}:* ${o}`};await l(r)},async logBlockMessage(e,t,o=exports.LogLevel.DEFAULT){if(!n())return console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null;console.log(o);const r=exports.LogColor[o],s=[],i=[],c=[];s.push({type:"divider"}),s.push({type:"header",text:{type:"plain_text",text:e,emoji:!0}}),s.push({type:"divider"}),t?.length&&t.forEach((e=>{let t=JSON.stringify(e.value);c.push({type:"section",text:{type:"mrkdwn",text:`*${e.title}:* ${t}`}})})),i.push({color:r,blocks:c});let a={text:e,blocks:s,attachments:i};await l(a)}};function n(){const e=process.env.SLACK_WEBHOOK_URL;return!(!e||null==e||void 0===e)&&e.startsWith("https://")}async function l(e){if("true"===(process?.env?.ENABLE_SLACK_LOGS??"").toString())return!1;const t=process.env.SLACK_WEBHOOK_URL;return await r.default.post(t,JSON.stringify(e),{headers:{"Content-Type":"application/json"}}).catch((e=>{console.error("🚨 Error sending log message to Slack: Webhook URL might be updated!")}))}exports.slack=s;
|
package/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "slack-logs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "slack-logs",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.ts",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build:js": "tsc",
|
|
9
|
-
"copy:ts": "
|
|
9
|
+
"copy:ts": "rollup -c",
|
|
10
10
|
"build": "npm run build:js && npm run copy:ts"
|
|
11
11
|
},
|
|
12
12
|
"author": "",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"
|
|
15
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
16
|
+
"axios": "^1.6.7",
|
|
17
|
+
"tslib": "^2.8.1"
|
|
16
18
|
},
|
|
17
19
|
"files": [
|
|
18
20
|
"./dist/ README.md"
|
|
@@ -31,6 +33,7 @@
|
|
|
31
33
|
"devDependencies": {
|
|
32
34
|
"@types/node": "^20.11.17",
|
|
33
35
|
"ncp": "^2.0.0",
|
|
36
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
34
37
|
"typescript": "^5.3.3"
|
|
35
38
|
}
|
|
36
39
|
}
|