slack-logs 1.1.3 → 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 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
- SLACK_WEBHOOK_URL="https://hooks.slack.com/services/******/******/********************"
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slack-logs",
3
- "version": "1.1.3",
3
+ "version": "1.3.0",
4
4
  "description": "slack-logs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.ts",
package/src/index.ts CHANGED
@@ -1,9 +1,12 @@
1
- import { SLACK_DEBUGGER, SLACK_WEBHOOK_URL } from "./constants/common-variables";
2
1
  import axios from "axios";
3
2
 
4
3
  interface Slack {
5
4
  log(label: string, data: any): Promise<null | undefined>;
6
- logBlockMessage(label: string, objectData: BlocksInterface[], error_type?: LogLevel): Promise<null | undefined>;
5
+ logBlockMessage(
6
+ label: string,
7
+ objectData: BlocksInterface[],
8
+ error_type?: LogLevel
9
+ ): Promise<null | undefined>;
7
10
  }
8
11
  interface BlocksInterface {
9
12
  title: string;
@@ -29,7 +32,9 @@ export const slack: Slack = {
29
32
  const messageData = JSON.stringify(data);
30
33
  if (!isValidSlackWebhookUrl()) {
31
34
  // SLACK_WEBHOOK_URL is valid
32
- console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
35
+ console.error(
36
+ "🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
37
+ );
33
38
  return null;
34
39
  }
35
40
 
@@ -39,10 +44,16 @@ export const slack: Slack = {
39
44
  };
40
45
  await axiosCall(payload);
41
46
  },
42
- async logBlockMessage(label: string, objectData: BlocksInterface[], error_type: LogLevel = LogLevel.DEFAULT) {
47
+ async logBlockMessage(
48
+ label: string,
49
+ objectData: BlocksInterface[],
50
+ error_type: LogLevel = LogLevel.DEFAULT
51
+ ) {
43
52
  if (!isValidSlackWebhookUrl()) {
44
53
  // SLACK_WEBHOOK_URL is valid
45
- console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
54
+ console.error(
55
+ "🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨"
56
+ );
46
57
  return null;
47
58
  }
48
59
  console.log(error_type);
@@ -68,7 +79,7 @@ export const slack: Slack = {
68
79
  });
69
80
 
70
81
  if (objectData?.length) {
71
- objectData.forEach(element => {
82
+ objectData.forEach((element) => {
72
83
  let value = JSON.stringify(element.value);
73
84
  attachmentsBlocks.push({
74
85
  type: "section",
@@ -85,7 +96,7 @@ export const slack: Slack = {
85
96
  });
86
97
 
87
98
  let payload = {
88
- text: "Application Error!",
99
+ text: label,
89
100
  blocks: blocks,
90
101
  attachments: attachments,
91
102
  };
@@ -94,8 +105,13 @@ export const slack: Slack = {
94
105
  };
95
106
 
96
107
  function isValidSlackWebhookUrl(): boolean {
108
+ const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
97
109
  // Check if SLACK_WEBHOOK_URL is not null or undefined
98
- if (!SLACK_WEBHOOK_URL || SLACK_WEBHOOK_URL == null || typeof SLACK_WEBHOOK_URL == "undefined") {
110
+ if (
111
+ !SLACK_WEBHOOK_URL ||
112
+ SLACK_WEBHOOK_URL == null ||
113
+ typeof SLACK_WEBHOOK_URL == "undefined"
114
+ ) {
99
115
  return false;
100
116
  }
101
117
 
@@ -104,16 +120,19 @@ function isValidSlackWebhookUrl(): boolean {
104
120
  }
105
121
 
106
122
  async function axiosCall(payload: any) {
123
+ const ENABLE_SLACK_LOGS: boolean =
124
+ (process?.env?.ENABLE_SLACK_LOGS ?? "").toString() === "true";
125
+
126
+ if (ENABLE_SLACK_LOGS) return false;
127
+
128
+ const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
107
129
  return await axios
108
130
  .post(SLACK_WEBHOOK_URL as string, JSON.stringify(payload), {
109
131
  headers: { "Content-Type": "application/json" },
110
132
  })
111
- .then(response => {
112
- if (SLACK_DEBUGGER) {
113
- console.log(" Log message sent to Slack successfully");
114
- }
115
- })
116
- .catch(error => {
117
- console.error("🚨 Error sending log message to Slack: Webhook URL might be updated!");
133
+ .catch((error) => {
134
+ console.error(
135
+ "🚨 Error sending log message to Slack: Webhook URL might be updated!"
136
+ );
118
137
  });
119
138
  }
package/tsconfig.json CHANGED
@@ -1,13 +1,13 @@
1
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
- }
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
+ }
@@ -1,7 +0,0 @@
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;
@@ -1,4 +0,0 @@
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 DELETED
@@ -1,110 +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 common_variables_1 = require("./constants/common-variables");
8
- const axios_1 = __importDefault(require("axios"));
9
- var LogLevel;
10
- (function (LogLevel) {
11
- LogLevel["DEFAULT"] = "DEFAULT";
12
- LogLevel["SUCCESS"] = "SUCCESS";
13
- LogLevel["INFO"] = "INFO";
14
- LogLevel["WARN"] = "WARN";
15
- LogLevel["ERROR"] = "ERROR";
16
- })(LogLevel || (exports.LogLevel = LogLevel = {}));
17
- var LogColor;
18
- (function (LogColor) {
19
- LogColor["DEFAULT"] = "#B4B4B8";
20
- LogColor["SUCCESS"] = "#65B741";
21
- LogColor["INFO"] = "#40A2D8";
22
- LogColor["WARN"] = "#E3651D";
23
- LogColor["ERROR"] = "#FF0000";
24
- })(LogColor || (exports.LogColor = LogColor = {}));
25
- exports.slack = {
26
- async log(label, data) {
27
- const messageData = JSON.stringify(data);
28
- if (!isValidSlackWebhookUrl()) {
29
- // SLACK_WEBHOOK_URL is valid
30
- console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
31
- return null;
32
- }
33
- const message = `*${label}:* ${messageData}`;
34
- let payload = {
35
- text: message,
36
- };
37
- await axiosCall(payload);
38
- },
39
- async logBlockMessage(label, objectData, error_type = LogLevel.DEFAULT) {
40
- if (!isValidSlackWebhookUrl()) {
41
- // SLACK_WEBHOOK_URL is valid
42
- console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
43
- return null;
44
- }
45
- console.log(error_type);
46
- const messageBodyColor = LogColor[error_type];
47
- const blocks = [];
48
- const attachments = [];
49
- const attachmentsBlocks = [];
50
- blocks.push({
51
- type: "divider",
52
- });
53
- // Label of slack message
54
- blocks.push({
55
- type: "header",
56
- text: {
57
- type: "plain_text",
58
- text: label,
59
- emoji: true,
60
- },
61
- });
62
- blocks.push({
63
- type: "divider",
64
- });
65
- if (objectData?.length) {
66
- objectData.forEach(element => {
67
- let value = JSON.stringify(element.value);
68
- attachmentsBlocks.push({
69
- type: "section",
70
- text: {
71
- type: "mrkdwn",
72
- text: `*${element.title}:* ${value}`,
73
- },
74
- });
75
- });
76
- }
77
- attachments.push({
78
- color: messageBodyColor,
79
- blocks: attachmentsBlocks,
80
- });
81
- let payload = {
82
- text: "Application Error!",
83
- blocks: blocks,
84
- attachments: attachments,
85
- };
86
- await axiosCall(payload);
87
- },
88
- };
89
- function isValidSlackWebhookUrl() {
90
- // Check if SLACK_WEBHOOK_URL is not null or undefined
91
- if (!common_variables_1.SLACK_WEBHOOK_URL || common_variables_1.SLACK_WEBHOOK_URL == null || typeof common_variables_1.SLACK_WEBHOOK_URL == "undefined") {
92
- return false;
93
- }
94
- // Check if SLACK_WEBHOOK_URL starts with "https://"
95
- return common_variables_1.SLACK_WEBHOOK_URL.startsWith("https://");
96
- }
97
- async function axiosCall(payload) {
98
- return await axios_1.default
99
- .post(common_variables_1.SLACK_WEBHOOK_URL, JSON.stringify(payload), {
100
- headers: { "Content-Type": "application/json" },
101
- })
102
- .then(response => {
103
- if (common_variables_1.SLACK_DEBUGGER) {
104
- console.log("✅ Log message sent to Slack successfully");
105
- }
106
- })
107
- .catch(error => {
108
- console.error("🚨 Error sending log message to Slack: Webhook URL might be updated!");
109
- });
110
- }
package/dist/index.ts DELETED
@@ -1,119 +0,0 @@
1
- import { SLACK_DEBUGGER, SLACK_WEBHOOK_URL } from "./constants/common-variables";
2
- import axios from "axios";
3
-
4
- interface Slack {
5
- log(label: string, data: any): Promise<null | undefined>;
6
- logBlockMessage(label: string, objectData: BlocksInterface[], error_type?: LogLevel): Promise<null | undefined>;
7
- }
8
- interface BlocksInterface {
9
- title: string;
10
- value: any;
11
- }
12
-
13
- export enum LogLevel {
14
- DEFAULT = "DEFAULT",
15
- SUCCESS = "SUCCESS",
16
- INFO = "INFO",
17
- WARN = "WARN",
18
- ERROR = "ERROR",
19
- }
20
- export enum LogColor {
21
- DEFAULT = "#B4B4B8",
22
- SUCCESS = "#65B741",
23
- INFO = "#40A2D8",
24
- WARN = "#E3651D",
25
- ERROR = "#FF0000",
26
- }
27
- export const slack: Slack = {
28
- async log(label: string, data: any) {
29
- const messageData = JSON.stringify(data);
30
- if (!isValidSlackWebhookUrl()) {
31
- // SLACK_WEBHOOK_URL is valid
32
- console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
33
- return null;
34
- }
35
-
36
- const message = `*${label}:* ${messageData}`;
37
- let payload = {
38
- text: message,
39
- };
40
- await axiosCall(payload);
41
- },
42
- async logBlockMessage(label: string, objectData: BlocksInterface[], error_type: LogLevel = LogLevel.DEFAULT) {
43
- if (!isValidSlackWebhookUrl()) {
44
- // SLACK_WEBHOOK_URL is valid
45
- console.error("🚨 Invalid Slack webhook URL. Kindly check 'SLACK_WEBHOOK_URL' in your .env file! 🚨");
46
- return null;
47
- }
48
- console.log(error_type);
49
- const messageBodyColor = LogColor[error_type];
50
-
51
- const blocks: object[] = [];
52
- const attachments: object[] = [];
53
- const attachmentsBlocks: object[] = [];
54
- blocks.push({
55
- type: "divider",
56
- });
57
- // Label of slack message
58
- blocks.push({
59
- type: "header",
60
- text: {
61
- type: "plain_text",
62
- text: label,
63
- emoji: true,
64
- },
65
- });
66
- blocks.push({
67
- type: "divider",
68
- });
69
-
70
- if (objectData?.length) {
71
- objectData.forEach(element => {
72
- let value = JSON.stringify(element.value);
73
- attachmentsBlocks.push({
74
- type: "section",
75
- text: {
76
- type: "mrkdwn",
77
- text: `*${element.title}:* ${value}`,
78
- },
79
- });
80
- });
81
- }
82
- attachments.push({
83
- color: messageBodyColor,
84
- blocks: attachmentsBlocks,
85
- });
86
-
87
- let payload = {
88
- text: "Application Error!",
89
- blocks: blocks,
90
- attachments: attachments,
91
- };
92
- await axiosCall(payload);
93
- },
94
- };
95
-
96
- function isValidSlackWebhookUrl(): boolean {
97
- // Check if SLACK_WEBHOOK_URL is not null or undefined
98
- if (!SLACK_WEBHOOK_URL || SLACK_WEBHOOK_URL == null || typeof SLACK_WEBHOOK_URL == "undefined") {
99
- return false;
100
- }
101
-
102
- // Check if SLACK_WEBHOOK_URL starts with "https://"
103
- return SLACK_WEBHOOK_URL.startsWith("https://");
104
- }
105
-
106
- async function axiosCall(payload: any) {
107
- return await axios
108
- .post(SLACK_WEBHOOK_URL as string, JSON.stringify(payload), {
109
- headers: { "Content-Type": "application/json" },
110
- })
111
- .then(response => {
112
- if (SLACK_DEBUGGER) {
113
- console.log("✅ Log message sent to Slack successfully");
114
- }
115
- })
116
- .catch(error => {
117
- console.error("🚨 Error sending log message to Slack: Webhook URL might be updated!");
118
- });
119
- }
@@ -1,4 +0,0 @@
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 };