@nu-art/slack-shared 0.400.5
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/apis.d.ts +42 -0
- package/apis.js +9 -0
- package/base-slack-builder/BaseSlackBuilder.d.ts +75 -0
- package/base-slack-builder/BaseSlackBuilder.js +140 -0
- package/base-slack-builder/index.d.ts +2 -0
- package/base-slack-builder/index.js +2 -0
- package/base-slack-builder/types.d.ts +12 -0
- package/base-slack-builder/types.js +1 -0
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +68 -0
- package/types.d.ts +3 -0
- package/types.js +1 -0
package/apis.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ApiDefResolver, BodyApi } from '@nu-art/thunderstorm-shared';
|
|
2
|
+
import { PreSendSlackStructuredMessage } from './types.js';
|
|
3
|
+
import { SlackBlock, ThreadPointer } from './base-slack-builder/index.js';
|
|
4
|
+
import { WebAPICallResult } from '@slack/web-api';
|
|
5
|
+
export type Request_PostMessage = {
|
|
6
|
+
channel: string;
|
|
7
|
+
message: any;
|
|
8
|
+
};
|
|
9
|
+
export type PostStructuredMessage = {
|
|
10
|
+
request: {
|
|
11
|
+
message: PreSendSlackStructuredMessage;
|
|
12
|
+
thread?: ThreadPointer;
|
|
13
|
+
};
|
|
14
|
+
response: {
|
|
15
|
+
threadPointer?: ThreadPointer;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export type PostFiles = {
|
|
19
|
+
request: {
|
|
20
|
+
file: any;
|
|
21
|
+
name: string;
|
|
22
|
+
thread?: ThreadPointer;
|
|
23
|
+
};
|
|
24
|
+
response: WebAPICallResult;
|
|
25
|
+
};
|
|
26
|
+
export type SendUIMessage = {
|
|
27
|
+
request: {
|
|
28
|
+
messageBlocks: SlackBlock[];
|
|
29
|
+
messageReplies: SlackBlock[][];
|
|
30
|
+
channel?: string;
|
|
31
|
+
};
|
|
32
|
+
response: void;
|
|
33
|
+
};
|
|
34
|
+
export type ApiStruct_Slack = {
|
|
35
|
+
vv1: {
|
|
36
|
+
postMessage: BodyApi<void, Request_PostMessage>;
|
|
37
|
+
postStructuredMessage: BodyApi<PostStructuredMessage['response'], PostStructuredMessage['request']>;
|
|
38
|
+
sendFEMessage: BodyApi<SendUIMessage['response'], SendUIMessage['request']>;
|
|
39
|
+
postFiles: BodyApi<PostFiles['response'], PostFiles['request']>;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export declare const ApiDef_Slack: ApiDefResolver<ApiStruct_Slack>;
|
package/apis.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { HttpMethod } from '@nu-art/thunderstorm-shared';
|
|
2
|
+
export const ApiDef_Slack = {
|
|
3
|
+
vv1: {
|
|
4
|
+
postMessage: { method: HttpMethod.POST, path: 'v1/slack/post-message' },
|
|
5
|
+
postStructuredMessage: { method: HttpMethod.POST, path: 'v1/slack/post-structured-message' },
|
|
6
|
+
sendFEMessage: { method: HttpMethod.POST, path: 'v1/slack/send-fe-message' },
|
|
7
|
+
postFiles: { method: HttpMethod.POST, path: 'v1/slack/post-files' },
|
|
8
|
+
}
|
|
9
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { CustomException, Logger, ServerErrorSeverity } from '@nu-art/ts-common';
|
|
2
|
+
import { SlackBlock, SlackFile, ThreadPointer } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Base class with logic for composing slack message, will use as base for making FE and BE versions
|
|
5
|
+
* For better slack error reporting application wide
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class BaseSlackBuilder extends Logger {
|
|
8
|
+
protected files: SlackFile[];
|
|
9
|
+
protected blocks: SlackBlock[];
|
|
10
|
+
protected replies: SlackBlock[][];
|
|
11
|
+
protected text: string;
|
|
12
|
+
protected channel: string;
|
|
13
|
+
protected constructor(channel?: string, blocks?: SlackBlock[], replies?: SlackBlock[][]);
|
|
14
|
+
/** Static template function
|
|
15
|
+
* For error reporting get slack emoji unicode for each severity level
|
|
16
|
+
* @param severity Severity level from the severity enum
|
|
17
|
+
*/
|
|
18
|
+
static SeverityEmoji: (severity: ServerErrorSeverity) => ":red_circle:" | ":large_orange_circle:" | ":large_yellow_circle:" | ":large_purple_circle:" | ":large_blue_circle:";
|
|
19
|
+
/** Static template function
|
|
20
|
+
* Generate slack message block containing a title and a text body
|
|
21
|
+
* @param title String to be printed as block title. can include any markdown accepted by slack and emoji uni-codes
|
|
22
|
+
* @param text String to be printed as block body. can include any markdown accepted by slack and emoji uni-codes
|
|
23
|
+
*/
|
|
24
|
+
static TextSectionWithTitle: (title: string, text: string) => SlackBlock[];
|
|
25
|
+
/** Static template function
|
|
26
|
+
* Return a slack message block that renders a divider (line that separates parts of the message)
|
|
27
|
+
*/
|
|
28
|
+
static Divider: () => SlackBlock;
|
|
29
|
+
/** Static template function
|
|
30
|
+
* Returns a slack message block of a paragraph, accepts all slack markdown and uni-codes
|
|
31
|
+
* @param text The block text to render
|
|
32
|
+
*/
|
|
33
|
+
static TextSection: (text: string) => SlackBlock;
|
|
34
|
+
/** Static template function
|
|
35
|
+
* Exception notification text, used to resolve header text from TS custom exceptions
|
|
36
|
+
* @param exception The custom exception to resolve title from
|
|
37
|
+
*/
|
|
38
|
+
static ExceptionNotificationText: (exception: CustomException) => string;
|
|
39
|
+
/**
|
|
40
|
+
* Add files to send in the slack message, accepts both single and multiple files at once
|
|
41
|
+
* @param files List or a single file to send
|
|
42
|
+
*/
|
|
43
|
+
addFiles: (files: SlackFile | SlackFile[]) => this;
|
|
44
|
+
/**
|
|
45
|
+
* Add new blocks to the slack message to send
|
|
46
|
+
* @param blocks List or a single block to add to the message
|
|
47
|
+
*/
|
|
48
|
+
addBlocks: (blocks: SlackBlock | SlackBlock[]) => this;
|
|
49
|
+
/**
|
|
50
|
+
* Add reply to the message composed
|
|
51
|
+
* @param replyBlocks List or single reply to add to the message
|
|
52
|
+
*/
|
|
53
|
+
addReply: (replyBlocks: SlackBlock | SlackBlock[]) => this;
|
|
54
|
+
/**
|
|
55
|
+
* Public send method, will handle the sending process to slack,
|
|
56
|
+
* First will send the message to the channel
|
|
57
|
+
* Then will send all files and replys in a thread of the original message
|
|
58
|
+
*/
|
|
59
|
+
send: () => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Abstract function, implement according to needs in each class.
|
|
62
|
+
* This function will handle sending of the main message made out of blocks
|
|
63
|
+
*/
|
|
64
|
+
protected abstract sendMessage: (() => Promise<ThreadPointer | undefined>) | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Abstract function, implement according to needs in each class.
|
|
67
|
+
* This function will handle sending files to slack in a thread of the original message
|
|
68
|
+
*/
|
|
69
|
+
protected abstract sendFiles: ((tp: ThreadPointer) => Promise<void>) | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Abstract function, implement according to needs in each class.
|
|
72
|
+
* This function will handle sending all replys in the thread of the original message
|
|
73
|
+
*/
|
|
74
|
+
protected abstract sendReplies: ((tp: ThreadPointer) => Promise<void>) | undefined;
|
|
75
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { asArray, BadImplementationException, Logger, ServerErrorSeverity } from '@nu-art/ts-common';
|
|
2
|
+
import { HttpCodes } from '@nu-art/ts-common/core/exceptions/http-codes';
|
|
3
|
+
/**
|
|
4
|
+
* Base class with logic for composing slack message, will use as base for making FE and BE versions
|
|
5
|
+
* For better slack error reporting application wide
|
|
6
|
+
*/
|
|
7
|
+
export class BaseSlackBuilder extends Logger {
|
|
8
|
+
files = [];
|
|
9
|
+
blocks = [];
|
|
10
|
+
replies = [];
|
|
11
|
+
text = 'Monitor Message'; //default value, need to double-check that;
|
|
12
|
+
channel;
|
|
13
|
+
constructor(channel, blocks, replies) {
|
|
14
|
+
super('BaseSlackBuilder');
|
|
15
|
+
// if module provided config is not of default slack channel
|
|
16
|
+
if (!channel) {
|
|
17
|
+
throw new BadImplementationException('cannot create slack builder instance without channel provided');
|
|
18
|
+
}
|
|
19
|
+
this.channel = channel;
|
|
20
|
+
if (replies)
|
|
21
|
+
this.replies = replies;
|
|
22
|
+
if (blocks)
|
|
23
|
+
this.blocks = blocks;
|
|
24
|
+
}
|
|
25
|
+
// ######################## Static Templates ########################
|
|
26
|
+
/** Static template function
|
|
27
|
+
* For error reporting get slack emoji unicode for each severity level
|
|
28
|
+
* @param severity Severity level from the severity enum
|
|
29
|
+
*/
|
|
30
|
+
static SeverityEmoji = (severity) => {
|
|
31
|
+
switch (severity) {
|
|
32
|
+
case ServerErrorSeverity.Critical:
|
|
33
|
+
return ':red_circle:';
|
|
34
|
+
case ServerErrorSeverity.Error:
|
|
35
|
+
return ':large_orange_circle:';
|
|
36
|
+
case ServerErrorSeverity.Warning:
|
|
37
|
+
return ':large_yellow_circle:';
|
|
38
|
+
case ServerErrorSeverity.Info:
|
|
39
|
+
return ':large_purple_circle:';
|
|
40
|
+
case ServerErrorSeverity.Debug:
|
|
41
|
+
return ':large_blue_circle:';
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
/** Static template function
|
|
45
|
+
* Generate slack message block containing a title and a text body
|
|
46
|
+
* @param title String to be printed as block title. can include any markdown accepted by slack and emoji uni-codes
|
|
47
|
+
* @param text String to be printed as block body. can include any markdown accepted by slack and emoji uni-codes
|
|
48
|
+
*/
|
|
49
|
+
static TextSectionWithTitle = (title, text) => {
|
|
50
|
+
return [
|
|
51
|
+
{
|
|
52
|
+
type: 'section',
|
|
53
|
+
text: {
|
|
54
|
+
type: 'mrkdwn',
|
|
55
|
+
text: title
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: 'divider'
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
type: 'section',
|
|
63
|
+
text: {
|
|
64
|
+
type: 'mrkdwn',
|
|
65
|
+
text: text
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
};
|
|
70
|
+
/** Static template function
|
|
71
|
+
* Return a slack message block that renders a divider (line that separates parts of the message)
|
|
72
|
+
*/
|
|
73
|
+
static Divider = () => {
|
|
74
|
+
return {
|
|
75
|
+
type: 'divider'
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
/** Static template function
|
|
79
|
+
* Returns a slack message block of a paragraph, accepts all slack markdown and uni-codes
|
|
80
|
+
* @param text The block text to render
|
|
81
|
+
*/
|
|
82
|
+
static TextSection = (text) => {
|
|
83
|
+
return {
|
|
84
|
+
type: 'section',
|
|
85
|
+
text: {
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
type: 'mrkdwn',
|
|
88
|
+
text: text,
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
/** Static template function
|
|
93
|
+
* Exception notification text, used to resolve header text from TS custom exceptions
|
|
94
|
+
* @param exception The custom exception to resolve title from
|
|
95
|
+
*/
|
|
96
|
+
static ExceptionNotificationText = (exception) => {
|
|
97
|
+
return `*${exception.exceptionType}* - ${exception.message}`;
|
|
98
|
+
};
|
|
99
|
+
// ######################## Message builder logic ########################
|
|
100
|
+
/**
|
|
101
|
+
* Add files to send in the slack message, accepts both single and multiple files at once
|
|
102
|
+
* @param files List or a single file to send
|
|
103
|
+
*/
|
|
104
|
+
addFiles = (files) => {
|
|
105
|
+
asArray(files).forEach(image => this.files.push(image));
|
|
106
|
+
return this;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Add new blocks to the slack message to send
|
|
110
|
+
* @param blocks List or a single block to add to the message
|
|
111
|
+
*/
|
|
112
|
+
addBlocks = (blocks) => {
|
|
113
|
+
asArray(blocks).forEach(block => this.blocks.push(block));
|
|
114
|
+
return this;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Add reply to the message composed
|
|
118
|
+
* @param replyBlocks List or single reply to add to the message
|
|
119
|
+
*/
|
|
120
|
+
addReply = (replyBlocks) => {
|
|
121
|
+
const reply = [];
|
|
122
|
+
asArray(replyBlocks).forEach(block => reply.push(block));
|
|
123
|
+
this.replies.push(reply);
|
|
124
|
+
return this;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Public send method, will handle the sending process to slack,
|
|
128
|
+
* First will send the message to the channel
|
|
129
|
+
* Then will send all files and replys in a thread of the original message
|
|
130
|
+
*/
|
|
131
|
+
send = async () => {
|
|
132
|
+
if (!this.sendMessage)
|
|
133
|
+
throw new BadImplementationException('cannot send a message without implementing this function');
|
|
134
|
+
const tp = await this.sendMessage();
|
|
135
|
+
if (!tp)
|
|
136
|
+
throw HttpCodes._5XX.INTERNAL_SERVER_ERROR('Error while sending slack message', 'Did not get thread pointer from sending slack message');
|
|
137
|
+
await this.sendReplies?.(tp);
|
|
138
|
+
await this.sendFiles?.(tp);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Stream } from 'stream';
|
|
2
|
+
import { Block, KnownBlock } from '@slack/web-api';
|
|
3
|
+
export type SlackFile = {
|
|
4
|
+
file: Buffer | Stream;
|
|
5
|
+
fileName: string;
|
|
6
|
+
title?: string;
|
|
7
|
+
};
|
|
8
|
+
export type SlackBlock = Block | KnownBlock;
|
|
9
|
+
export type ThreadPointer = {
|
|
10
|
+
ts?: string;
|
|
11
|
+
channel: string;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nu-art/slack-shared",
|
|
3
|
+
"version": "0.400.5",
|
|
4
|
+
"description": "Storm - Express & Typescript based backend framework Shared",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"TacB0sS",
|
|
7
|
+
"infra",
|
|
8
|
+
"nu-art",
|
|
9
|
+
"storm",
|
|
10
|
+
"thunderstorm",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/nu-art-js/storm",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/nu-art-js/storm/issues"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"directory": "dist",
|
|
19
|
+
"linkDirectory": true
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+ssh://git@github.com:nu-art-js/storm.git"
|
|
24
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"author": "TacB0sS",
|
|
27
|
+
"files": [
|
|
28
|
+
"**/*"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@nu-art/ts-common": "0.400.5",
|
|
35
|
+
"@nu-art/thunderstorm-shared": "0.400.5",
|
|
36
|
+
"react": "^18.0.0",
|
|
37
|
+
"react-dom": "^18.0.0",
|
|
38
|
+
"react-router-dom": "^6.9.0",
|
|
39
|
+
"@slack/web-api": "7.9.3",
|
|
40
|
+
"@slack/events-api": "^2.3.0",
|
|
41
|
+
"firebase": "^11.9.0",
|
|
42
|
+
"firebase-admin": "13.4.0",
|
|
43
|
+
"firebase-functions": "6.3.2",
|
|
44
|
+
"google-auth-library": "^10.0.0",
|
|
45
|
+
"moment": "^2.29.4",
|
|
46
|
+
"request": "^2.88.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/react": "^18.0.0",
|
|
50
|
+
"@types/react-dom": "^18.0.0",
|
|
51
|
+
"@types/react-router": "^5.1.20",
|
|
52
|
+
"@types/react-router-dom": "^5.3.3"
|
|
53
|
+
},
|
|
54
|
+
"unitConfig": {
|
|
55
|
+
"type": "typescript-lib"
|
|
56
|
+
},
|
|
57
|
+
"type": "module",
|
|
58
|
+
"exports": {
|
|
59
|
+
".": {
|
|
60
|
+
"types": "./index.d.ts",
|
|
61
|
+
"import": "./index.js"
|
|
62
|
+
},
|
|
63
|
+
"./*": {
|
|
64
|
+
"types": "./*.d.ts",
|
|
65
|
+
"import": "./*.js"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
package/types.d.ts
ADDED
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|