slack-logs 1.8.0 → 2.9.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 +105 -18
- package/dist/index.d.ts +30 -0
- package/dist/index.js +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -34,27 +34,17 @@ Before you start, ensure you have created an Incoming Webhook in Slack and have
|
|
|
34
34
|
|
|
35
35
|
Here is a basic example of how to use the Slack Log package to send a message to your Slack channel:
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
#### Option 1: Use `slackLogConfig(...)`
|
|
38
38
|
|
|
39
39
|
```
|
|
40
|
-
|
|
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".
|
|
51
|
-
...
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
```
|
|
55
|
-
import { slack } from "slack-logs";
|
|
40
|
+
import { slack, slackLogConfig } from "slack-logs";
|
|
56
41
|
or
|
|
57
|
-
const { slack } = require('slack-logs');
|
|
42
|
+
const { slack, slackLogConfig } = require('slack-logs');
|
|
43
|
+
|
|
44
|
+
slackLogConfig({
|
|
45
|
+
webhookUrl: "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
|
|
46
|
+
enableAlerts: true,
|
|
47
|
+
});
|
|
58
48
|
|
|
59
49
|
/*Slack Notification with default format*/
|
|
60
50
|
slack.log("Data", [{ title: "1yes!" }]);
|
|
@@ -72,6 +62,49 @@ slack.logBlockMessage("Validation Message!", payload);
|
|
|
72
62
|
|
|
73
63
|
```
|
|
74
64
|
|
|
65
|
+
`slackLogConfig(...)` rules:
|
|
66
|
+
|
|
67
|
+
- `webhookUrl` and `enableAlerts` are both required
|
|
68
|
+
- if `slackLogConfig(...)` is used, env values are ignored for both fields
|
|
69
|
+
- `slackLogConfig(...)` does not ask for values interactively
|
|
70
|
+
|
|
71
|
+
#### Option 2: Use environment variables
|
|
72
|
+
|
|
73
|
+
Use env values only when `slackLogConfig(...)` is not called.
|
|
74
|
+
|
|
75
|
+
`.env` 🚨
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
...
|
|
79
|
+
# Slack Webhook URL for sending logs and notifications.
|
|
80
|
+
# Replace with your actual webhook URL.
|
|
81
|
+
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
|
|
82
|
+
|
|
83
|
+
# Optional field.
|
|
84
|
+
# If not defined, logs are sent by default.
|
|
85
|
+
# true / True / TRUE sends logs.
|
|
86
|
+
# false / False / FALSE skips logs.
|
|
87
|
+
ENABLE_SLACK_LOGS=true
|
|
88
|
+
...
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`ENABLE_SLACK_LOGS` behavior:
|
|
92
|
+
|
|
93
|
+
- if not defined, default is `true`
|
|
94
|
+
- `true`, `"true"`, `"True"` and any case variation send logs
|
|
95
|
+
- `false`, `"false"`, `"False"` and any case variation do not send logs
|
|
96
|
+
- any other value is treated as `false`
|
|
97
|
+
|
|
98
|
+
#### Webhook validation
|
|
99
|
+
|
|
100
|
+
Expected format:
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
If webhook URL format is invalid, package logs a console error and skips sending.
|
|
107
|
+
|
|
75
108
|
```
|
|
76
109
|
/*Slack Notification with colored block format*/
|
|
77
110
|
|
|
@@ -174,3 +207,57 @@ Contributions are welcome! If you have a feature request, bug report, or a pull
|
|
|
174
207
|
### License:
|
|
175
208
|
|
|
176
209
|
This package is licensed under the MIT License - see the LICENSE file for details.
|
|
210
|
+
|
|
211
|
+
### Raw body
|
|
212
|
+
|
|
213
|
+
Use `slack.rawBody(...)` when you want to send a custom Slack payload as-is.
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
import { slack } from "slack-logs";
|
|
217
|
+
|
|
218
|
+
const payload = {
|
|
219
|
+
blocks: [
|
|
220
|
+
{
|
|
221
|
+
type: "section",
|
|
222
|
+
text: {
|
|
223
|
+
type: "mrkdwn",
|
|
224
|
+
text: "Hello from custom payload",
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
type: "divider",
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
type: "actions",
|
|
232
|
+
elements: [
|
|
233
|
+
{
|
|
234
|
+
type: "button",
|
|
235
|
+
text: {
|
|
236
|
+
type: "plain_text",
|
|
237
|
+
text: "Open",
|
|
238
|
+
emoji: true,
|
|
239
|
+
},
|
|
240
|
+
value: "click_me_123",
|
|
241
|
+
url: "https://google.com",
|
|
242
|
+
},
|
|
243
|
+
],
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
slack.rawBody(payload);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
`slack.rawBody(...)` expects a plain object payload like:
|
|
252
|
+
|
|
253
|
+
```
|
|
254
|
+
{
|
|
255
|
+
"blocks": [...]
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
If payload is invalid or sending fails, it logs a console error.
|
|
260
|
+
|
|
261
|
+
Try payload experiments here:
|
|
262
|
+
|
|
263
|
+
[Slack Block Kit Builder](https://app.slack.com/block-kit-builder/T9D4GM7L0)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
interface Slack {
|
|
2
|
+
log(label: string, data: any): Promise<null | undefined>;
|
|
3
|
+
logBlockMessage(label: string, objectData: BlocksInterface[], error_type?: LogLevel): Promise<null | undefined>;
|
|
4
|
+
rawBody(payload: Record<string, any>): Promise<null | undefined>;
|
|
5
|
+
}
|
|
6
|
+
export interface SlackLogConfigOptions {
|
|
7
|
+
webhookUrl: string;
|
|
8
|
+
enableAlerts: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface BlocksInterface {
|
|
11
|
+
title: string;
|
|
12
|
+
value: any;
|
|
13
|
+
}
|
|
14
|
+
export declare enum LogLevel {
|
|
15
|
+
DEFAULT = "DEFAULT",
|
|
16
|
+
SUCCESS = "SUCCESS",
|
|
17
|
+
INFO = "INFO",
|
|
18
|
+
WARN = "WARN",
|
|
19
|
+
ERROR = "ERROR"
|
|
20
|
+
}
|
|
21
|
+
export declare enum LogColor {
|
|
22
|
+
DEFAULT = "#B4B4B8",
|
|
23
|
+
SUCCESS = "#65B741",
|
|
24
|
+
INFO = "#40A2D8",
|
|
25
|
+
WARN = "#E3651D",
|
|
26
|
+
ERROR = "#FF0000"
|
|
27
|
+
}
|
|
28
|
+
export declare function slackLogConfig(config: SlackLogConfigOptions): void;
|
|
29
|
+
export declare const slack: Slack;
|
|
30
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
1
|
+
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});var o,t,r=e(require("axios"));exports.LogLevel=void 0,(o=exports.LogLevel||(exports.LogLevel={})).DEFAULT="DEFAULT",o.SUCCESS="SUCCESS",o.INFO="INFO",o.WARN="WARN",o.ERROR="ERROR",exports.LogColor=void 0,(t=exports.LogColor||(exports.LogColor={})).DEFAULT="#B4B4B8",t.SUCCESS="#65B741",t.INFO="#40A2D8",t.WARN="#E3651D",t.ERROR="#FF0000";const n={};const l={async log(e,o){const t=JSON.stringify(o);if(!s())return console.error("🚨 Invalid Slack webhook URL. Kindly check slackLogConfig(...) or 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null;let r={text:`*${e}:* ${t}`};await a(r)},async logBlockMessage(e,o,t=exports.LogLevel.DEFAULT){if(!s())return console.error("🚨 Invalid Slack webhook URL. Kindly check slackLogConfig(...) or 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null;const r=exports.LogColor[t],n=[],l=[],c=[];n.push({type:"divider"}),n.push({type:"header",text:{type:"plain_text",text:e,emoji:!0}}),n.push({type:"divider"}),o?.length&&o.forEach((e=>{let o=JSON.stringify(e.value);c.push({type:"section",text:{type:"mrkdwn",text:`*${e.title}:* ${o}`}})})),l.push({color:r,blocks:c});let i={text:e,blocks:n,attachments:l};await a(i)},rawBody:async e=>s()?function(e){return!!e&&"object"==typeof e&&!Array.isArray(e)}(e)?void await a(e):(console.error('🚨 Invalid Slack payload for rawBody(...). Expected an object like { "blocks": [...] } 🚨'),null):(console.error("🚨 Invalid Slack webhook URL. Kindly check slackLogConfig(...) or 'SLACK_WEBHOOK_URL' in your .env file! 🚨"),null)};function s(){const e=c();if(!e)return!1;return!!/^https:\/\/hooks\.slack\.com\/services\/[A-Za-z0-9]+\/[A-Za-z0-9]+\/[A-Za-z0-9]+$/.test(e)||(console.error(`🚨 Slack webhook URL does not look in correct format. Current value is "${e}", expected format is "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX". 🚨`),!1)}function c(){return n.webhookUrl??process.env.SLACK_WEBHOOK_URL}async function a(e){if(!function(){if("boolean"==typeof n.enableAlerts)return n.enableAlerts;const e=(process?.env?.ENABLE_SLACK_LOGS??"").toString().trim();return!e||"true"===e.toLowerCase()||(e.toLowerCase(),!1)}())return null;const o=c();return await r.default.post(o,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=l,exports.slackLogConfig=function(e){n.webhookUrl=e.webhookUrl,n.enableAlerts=e.enableAlerts};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "slack-logs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.9.0",
|
|
4
4
|
"description": "slack-logs",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"copy:ts": "rollup -c",
|
|
10
10
|
"remove-dist": "rm -rf dist",
|
|
11
11
|
"build": "npm run remove-dist && npm run build:js && npm run copy:ts",
|
|
12
|
-
"package:deploy": "npm run build && npm pub"
|
|
12
|
+
"package:deploy": "npm run build && npm pub",
|
|
13
|
+
"postpublish": "node scripts/release-git.js"
|
|
13
14
|
},
|
|
14
15
|
"author": "",
|
|
15
16
|
"license": "MIT",
|
|
@@ -19,7 +20,8 @@
|
|
|
19
20
|
"tslib": "^2.8.1"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
22
|
-
"
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
23
25
|
],
|
|
24
26
|
"keywords": [
|
|
25
27
|
"slack-logs",
|