slack-logs 1.0.10

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.
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SLACK_DEBUGGER = exports.SLACK_WEBHOOK_URL = void 0;
4
+ const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
5
+ exports.SLACK_WEBHOOK_URL = SLACK_WEBHOOK_URL;
6
+ const SLACK_DEBUGGER = process?.env?.SLACK_DEBUGGER === "true";
7
+ exports.SLACK_DEBUGGER = SLACK_DEBUGGER;
@@ -0,0 +1,4 @@
1
+ const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
2
+ const SLACK_DEBUGGER: boolean = process?.env?.SLACK_DEBUGGER === "true";
3
+
4
+ export { SLACK_WEBHOOK_URL, SLACK_DEBUGGER };
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
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 = void 0;
7
+ const common_variables_1 = require("./constants/common-variables");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ exports.slack = {
10
+ async log(label, data) {
11
+ const messageData = JSON.stringify(data);
12
+ if (!isValidSlackWebhookUrl()) {
13
+ // SLACK_WEBHOOK_URL is valid
14
+ console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
15
+ return null;
16
+ }
17
+ const message = `*${label}:* ${messageData}`;
18
+ let payload = {
19
+ text: message,
20
+ };
21
+ await axiosCall(payload);
22
+ },
23
+ async logBlockMessage(label, objectData) {
24
+ if (!isValidSlackWebhookUrl()) {
25
+ // SLACK_WEBHOOK_URL is valid
26
+ console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
27
+ return null;
28
+ }
29
+ const blocks = [];
30
+ // Label of slack message
31
+ blocks.push({
32
+ type: "header",
33
+ text: {
34
+ type: "plain_text",
35
+ text: label,
36
+ emoji: true,
37
+ },
38
+ });
39
+ if (objectData?.length) {
40
+ objectData.forEach((element) => {
41
+ let value = JSON.stringify(element.value);
42
+ blocks.push({
43
+ type: "section",
44
+ text: {
45
+ type: "mrkdwn",
46
+ text: `*${element.title}:* ${value}`,
47
+ },
48
+ });
49
+ });
50
+ }
51
+ let payload = {
52
+ text: "Application Error!",
53
+ blocks: blocks,
54
+ };
55
+ await axiosCall(payload);
56
+ },
57
+ };
58
+ function isValidSlackWebhookUrl() {
59
+ // Check if SLACK_WEBHOOK_URL is not null or undefined
60
+ if (!common_variables_1.SLACK_WEBHOOK_URL ||
61
+ common_variables_1.SLACK_WEBHOOK_URL == null ||
62
+ typeof common_variables_1.SLACK_WEBHOOK_URL == "undefined") {
63
+ return false;
64
+ }
65
+ // Check if SLACK_WEBHOOK_URL starts with "https://"
66
+ return common_variables_1.SLACK_WEBHOOK_URL.startsWith("https://");
67
+ }
68
+ async function axiosCall(payload) {
69
+ return await axios_1.default
70
+ .post(common_variables_1.SLACK_WEBHOOK_URL, JSON.stringify(payload), {
71
+ headers: { "Content-Type": "application/json" },
72
+ })
73
+ .then((response) => {
74
+ if (common_variables_1.SLACK_DEBUGGER) {
75
+ console.log("✅ Log message sent to Slack successfully");
76
+ }
77
+ })
78
+ .catch((error) => {
79
+ console.error("🚨 Error sending log message to Slack:", error);
80
+ });
81
+ }
package/dist/index.ts ADDED
@@ -0,0 +1,104 @@
1
+ import {
2
+ SLACK_DEBUGGER,
3
+ SLACK_WEBHOOK_URL,
4
+ } from "./constants/common-variables";
5
+ import axios from "axios";
6
+
7
+ interface Slack {
8
+ log(label: string, data: any): Promise<null | undefined>;
9
+ logBlockMessage(
10
+ label: string,
11
+ objectData: BlocksInterface[]
12
+ ): Promise<null | undefined>;
13
+ }
14
+ interface BlocksInterface {
15
+ title: string;
16
+ value: any;
17
+ }
18
+ export const slack: Slack = {
19
+ async log(label: string, data: any) {
20
+ const messageData = JSON.stringify(data);
21
+ if (!isValidSlackWebhookUrl()) {
22
+ // SLACK_WEBHOOK_URL is valid
23
+ console.error(
24
+ "🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
25
+ );
26
+ return null;
27
+ }
28
+
29
+ const message = `*${label}:* ${messageData}`;
30
+ let payload = {
31
+ text: message,
32
+ };
33
+ await axiosCall(payload);
34
+ },
35
+ async logBlockMessage(label: string, objectData: BlocksInterface[]) {
36
+ if (!isValidSlackWebhookUrl()) {
37
+ // SLACK_WEBHOOK_URL is valid
38
+ console.error(
39
+ "🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
40
+ );
41
+ return null;
42
+ }
43
+
44
+ const blocks: object[] = [];
45
+
46
+ // Label of slack message
47
+ blocks.push({
48
+ type: "header",
49
+ text: {
50
+ type: "plain_text",
51
+ text: label,
52
+ emoji: true,
53
+ },
54
+ });
55
+
56
+ if (objectData?.length) {
57
+ objectData.forEach((element) => {
58
+ let value = JSON.stringify(element.value);
59
+ blocks.push({
60
+ type: "section",
61
+ text: {
62
+ type: "mrkdwn",
63
+ text: `*${element.title}:* ${value}`,
64
+ },
65
+ });
66
+ });
67
+ }
68
+
69
+ let payload = {
70
+ text: "Application Error!",
71
+ blocks: blocks,
72
+ };
73
+ await axiosCall(payload);
74
+ },
75
+ };
76
+
77
+ function isValidSlackWebhookUrl(): boolean {
78
+ // Check if SLACK_WEBHOOK_URL is not null or undefined
79
+ if (
80
+ !SLACK_WEBHOOK_URL ||
81
+ SLACK_WEBHOOK_URL == null ||
82
+ typeof SLACK_WEBHOOK_URL == "undefined"
83
+ ) {
84
+ return false;
85
+ }
86
+
87
+ // Check if SLACK_WEBHOOK_URL starts with "https://"
88
+ return SLACK_WEBHOOK_URL.startsWith("https://");
89
+ }
90
+
91
+ async function axiosCall(payload: any) {
92
+ return await axios
93
+ .post(SLACK_WEBHOOK_URL as string, JSON.stringify(payload), {
94
+ headers: { "Content-Type": "application/json" },
95
+ })
96
+ .then((response) => {
97
+ if (SLACK_DEBUGGER) {
98
+ console.log("✅ Log message sent to Slack successfully");
99
+ }
100
+ })
101
+ .catch((error) => {
102
+ console.error("🚨 Error sending log message to Slack:", error);
103
+ });
104
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "slack-logs",
3
+ "version": "1.0.10",
4
+ "description": "slack-logs",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.ts",
7
+ "scripts": {
8
+ "build:js": "tsc",
9
+ "copy:ts": "ncp src dist",
10
+ "build": "npm run build:js && npm run copy:ts"
11
+ },
12
+ "author": "",
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "axios": "^1.6.7"
16
+ },
17
+ "keywords": [
18
+ "slack",
19
+ "logs",
20
+ "slack-logs",
21
+ "slack-logger",
22
+ "logger",
23
+ "console",
24
+ "console logs",
25
+ "jkg",
26
+ "jaykumar-gohil"
27
+ ],
28
+ "devDependencies": {
29
+ "@types/node": "^20.11.17",
30
+ "ncp": "^2.0.0",
31
+ "typescript": "^5.3.3"
32
+ }
33
+ }
@@ -0,0 +1,4 @@
1
+ const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
2
+ const SLACK_DEBUGGER: boolean = process?.env?.SLACK_DEBUGGER === "true";
3
+
4
+ export { SLACK_WEBHOOK_URL, SLACK_DEBUGGER };
package/src/index.ts ADDED
@@ -0,0 +1,104 @@
1
+ import {
2
+ SLACK_DEBUGGER,
3
+ SLACK_WEBHOOK_URL,
4
+ } from "./constants/common-variables";
5
+ import axios from "axios";
6
+
7
+ interface Slack {
8
+ log(label: string, data: any): Promise<null | undefined>;
9
+ logBlockMessage(
10
+ label: string,
11
+ objectData: BlocksInterface[]
12
+ ): Promise<null | undefined>;
13
+ }
14
+ interface BlocksInterface {
15
+ title: string;
16
+ value: any;
17
+ }
18
+ export const slack: Slack = {
19
+ async log(label: string, data: any) {
20
+ const messageData = JSON.stringify(data);
21
+ if (!isValidSlackWebhookUrl()) {
22
+ // SLACK_WEBHOOK_URL is valid
23
+ console.error(
24
+ "🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
25
+ );
26
+ return null;
27
+ }
28
+
29
+ const message = `*${label}:* ${messageData}`;
30
+ let payload = {
31
+ text: message,
32
+ };
33
+ await axiosCall(payload);
34
+ },
35
+ async logBlockMessage(label: string, objectData: BlocksInterface[]) {
36
+ if (!isValidSlackWebhookUrl()) {
37
+ // SLACK_WEBHOOK_URL is valid
38
+ console.error(
39
+ "🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
40
+ );
41
+ return null;
42
+ }
43
+
44
+ const blocks: object[] = [];
45
+
46
+ // Label of slack message
47
+ blocks.push({
48
+ type: "header",
49
+ text: {
50
+ type: "plain_text",
51
+ text: label,
52
+ emoji: true,
53
+ },
54
+ });
55
+
56
+ if (objectData?.length) {
57
+ objectData.forEach((element) => {
58
+ let value = JSON.stringify(element.value);
59
+ blocks.push({
60
+ type: "section",
61
+ text: {
62
+ type: "mrkdwn",
63
+ text: `*${element.title}:* ${value}`,
64
+ },
65
+ });
66
+ });
67
+ }
68
+
69
+ let payload = {
70
+ text: "Application Error!",
71
+ blocks: blocks,
72
+ };
73
+ await axiosCall(payload);
74
+ },
75
+ };
76
+
77
+ function isValidSlackWebhookUrl(): boolean {
78
+ // Check if SLACK_WEBHOOK_URL is not null or undefined
79
+ if (
80
+ !SLACK_WEBHOOK_URL ||
81
+ SLACK_WEBHOOK_URL == null ||
82
+ typeof SLACK_WEBHOOK_URL == "undefined"
83
+ ) {
84
+ return false;
85
+ }
86
+
87
+ // Check if SLACK_WEBHOOK_URL starts with "https://"
88
+ return SLACK_WEBHOOK_URL.startsWith("https://");
89
+ }
90
+
91
+ async function axiosCall(payload: any) {
92
+ return await axios
93
+ .post(SLACK_WEBHOOK_URL as string, JSON.stringify(payload), {
94
+ headers: { "Content-Type": "application/json" },
95
+ })
96
+ .then((response) => {
97
+ if (SLACK_DEBUGGER) {
98
+ console.log("✅ Log message sent to Slack successfully");
99
+ }
100
+ })
101
+ .catch((error) => {
102
+ console.error("🚨 Error sending log message to Slack:", error);
103
+ });
104
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist",
4
+ "target": "ESNext",
5
+ "module": "CommonJS",
6
+ "lib": ["ESNext"],
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "forceConsistentCasingInFileNames": true
11
+ },
12
+ "exclude": ["node_modules", "dist"]
13
+ }