@xpr/nestjs-slack 0.0.0 → 0.0.2
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/decorators.d.ts +14 -8
- package/decorators.js +44 -15
- package/index.d.ts +2 -2
- package/package.json +2 -3
- package/slack.d.ts +6 -6
- package/slack.js +7 -12
- package/utils.d.ts +0 -15
- package/utils.js +0 -65
package/decorators.d.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from
|
|
2
|
-
|
|
3
|
-
export
|
|
1
|
+
import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from '@slack/bolt';
|
|
2
|
+
export type Pattern = string | RegExp;
|
|
3
|
+
export type ShortCutId = Pattern | ShortcutConstraints;
|
|
4
|
+
export type ActionId = Pattern | ActionConstraints;
|
|
5
|
+
export type ViewId = Pattern | ViewConstraints;
|
|
6
|
+
export type OptionId = OptionsConstraints;
|
|
7
|
+
export type AllConstraints = Pattern | ActionConstraints | ShortcutConstraints | ViewConstraints | OptionsConstraints;
|
|
8
|
+
/**
|
|
9
|
+
* Create method decorator
|
|
10
|
+
* @param type type of listener
|
|
11
|
+
* @param event how to identify the event
|
|
12
|
+
*/
|
|
13
|
+
export declare function eventDecorator<T extends AllConstraints>(type: string, event: T): MethodDecorator;
|
|
4
14
|
/**
|
|
5
15
|
* @see https://tools.slack.dev/bolt-js/reference/
|
|
6
16
|
*/
|
|
@@ -20,7 +30,6 @@ export declare const EventTypes: {
|
|
|
20
30
|
* @constructor
|
|
21
31
|
*/
|
|
22
32
|
export declare function SlackEvent(event: string): MethodDecorator;
|
|
23
|
-
export type ShortCutId = Pattern | ShortcutConstraints;
|
|
24
33
|
/**
|
|
25
34
|
* Decorator for shortcut events
|
|
26
35
|
* @see guide https://tools.slack.dev/bolt-js/concepts/shortcuts
|
|
@@ -37,7 +46,6 @@ export declare function SlackShortcut(shortcutId: ShortCutId): MethodDecorator;
|
|
|
37
46
|
* @constructor
|
|
38
47
|
*/
|
|
39
48
|
export declare function SlackCommand(commandId: Pattern): MethodDecorator;
|
|
40
|
-
export type ActionId = Pattern | ActionConstraints;
|
|
41
49
|
/**
|
|
42
50
|
* Decorator for action events
|
|
43
51
|
* @see guide https://tools.slack.dev/bolt-js/concepts/actions
|
|
@@ -45,7 +53,6 @@ export type ActionId = Pattern | ActionConstraints;
|
|
|
45
53
|
* @constructor
|
|
46
54
|
*/
|
|
47
55
|
export declare function SlackAction(actionId: ActionId): MethodDecorator;
|
|
48
|
-
export type ViewId = Pattern | ViewConstraints;
|
|
49
56
|
/**
|
|
50
57
|
* Decorator for view events
|
|
51
58
|
* @see guide https://tools.slack.dev/bolt-js/concepts/view-submissions
|
|
@@ -53,7 +60,6 @@ export type ViewId = Pattern | ViewConstraints;
|
|
|
53
60
|
* @constructor
|
|
54
61
|
*/
|
|
55
62
|
export declare function SlackView(viewId: ViewId): MethodDecorator;
|
|
56
|
-
export type OptionId = OptionsConstraints;
|
|
57
63
|
/**
|
|
58
64
|
* Decorator for options events
|
|
59
65
|
* @see guide https://tools.slack.dev/bolt-js/concepts/options
|
|
@@ -69,4 +75,4 @@ export declare function SlackOption(optionId: OptionId): MethodDecorator;
|
|
|
69
75
|
* @param pattern
|
|
70
76
|
* @constructor
|
|
71
77
|
*/
|
|
72
|
-
export declare function SlackMessage(pattern?:
|
|
78
|
+
export declare function SlackMessage(pattern?: string | RegExp): MethodDecorator;
|
package/decorators.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventTypes = void 0;
|
|
4
|
+
exports.eventDecorator = eventDecorator;
|
|
4
5
|
exports.SlackEvent = SlackEvent;
|
|
5
6
|
exports.SlackShortcut = SlackShortcut;
|
|
6
7
|
exports.SlackCommand = SlackCommand;
|
|
@@ -8,17 +9,37 @@ exports.SlackAction = SlackAction;
|
|
|
8
9
|
exports.SlackView = SlackView;
|
|
9
10
|
exports.SlackOption = SlackOption;
|
|
10
11
|
exports.SlackMessage = SlackMessage;
|
|
11
|
-
const
|
|
12
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
13
|
+
const normalizePattern = (pattern) => {
|
|
14
|
+
if (typeof pattern === 'string') {
|
|
15
|
+
return pattern;
|
|
16
|
+
}
|
|
17
|
+
if (pattern instanceof RegExp) {
|
|
18
|
+
return pattern.toString();
|
|
19
|
+
}
|
|
20
|
+
return JSON.stringify(pattern);
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Create method decorator
|
|
24
|
+
* @param type type of listener
|
|
25
|
+
* @param event how to identify the event
|
|
26
|
+
*/
|
|
27
|
+
function eventDecorator(type, event) {
|
|
28
|
+
return (target, key, descriptor) => (0, microservices_1.EventPattern)(`${type}://${normalizePattern(event)}`, {
|
|
29
|
+
type,
|
|
30
|
+
event,
|
|
31
|
+
})(target, key, descriptor);
|
|
32
|
+
}
|
|
12
33
|
/**
|
|
13
34
|
* @see https://tools.slack.dev/bolt-js/reference/
|
|
14
35
|
*/
|
|
15
36
|
exports.EventTypes = {
|
|
16
|
-
Event:
|
|
17
|
-
Shortcut:
|
|
18
|
-
Command:
|
|
19
|
-
Action:
|
|
20
|
-
View:
|
|
21
|
-
Option:
|
|
37
|
+
Event: 'event',
|
|
38
|
+
Shortcut: 'shortcut',
|
|
39
|
+
Command: 'command',
|
|
40
|
+
Action: 'action',
|
|
41
|
+
View: 'view',
|
|
42
|
+
Option: 'option',
|
|
22
43
|
};
|
|
23
44
|
/**
|
|
24
45
|
* Decorator for event events
|
|
@@ -29,7 +50,10 @@ exports.EventTypes = {
|
|
|
29
50
|
*/
|
|
30
51
|
function SlackEvent(event) {
|
|
31
52
|
// todo input validation (types for event)
|
|
32
|
-
return (0,
|
|
53
|
+
return (target, key, descriptor) => (0, microservices_1.EventPattern)(`${exports.EventTypes.Event}://${normalizePattern(event)}`, {
|
|
54
|
+
type: exports.EventTypes.Event,
|
|
55
|
+
event,
|
|
56
|
+
})(target, key, descriptor);
|
|
33
57
|
}
|
|
34
58
|
/**
|
|
35
59
|
* Decorator for shortcut events
|
|
@@ -39,7 +63,7 @@ function SlackEvent(event) {
|
|
|
39
63
|
* @constructor
|
|
40
64
|
*/
|
|
41
65
|
function SlackShortcut(shortcutId) {
|
|
42
|
-
return
|
|
66
|
+
return eventDecorator(exports.EventTypes.Shortcut, shortcutId);
|
|
43
67
|
}
|
|
44
68
|
/**
|
|
45
69
|
* Decorator for command events
|
|
@@ -50,7 +74,7 @@ function SlackShortcut(shortcutId) {
|
|
|
50
74
|
*/
|
|
51
75
|
function SlackCommand(commandId) {
|
|
52
76
|
// todo input validation command must start with / etc.
|
|
53
|
-
return
|
|
77
|
+
return eventDecorator(exports.EventTypes.Command, commandId);
|
|
54
78
|
}
|
|
55
79
|
/**
|
|
56
80
|
* Decorator for action events
|
|
@@ -59,7 +83,7 @@ function SlackCommand(commandId) {
|
|
|
59
83
|
* @constructor
|
|
60
84
|
*/
|
|
61
85
|
function SlackAction(actionId) {
|
|
62
|
-
return
|
|
86
|
+
return eventDecorator(exports.EventTypes.Action, actionId);
|
|
63
87
|
}
|
|
64
88
|
/**
|
|
65
89
|
* Decorator for view events
|
|
@@ -68,7 +92,7 @@ function SlackAction(actionId) {
|
|
|
68
92
|
* @constructor
|
|
69
93
|
*/
|
|
70
94
|
function SlackView(viewId) {
|
|
71
|
-
return
|
|
95
|
+
return eventDecorator(exports.EventTypes.View, viewId);
|
|
72
96
|
}
|
|
73
97
|
/**
|
|
74
98
|
* Decorator for options events
|
|
@@ -77,7 +101,7 @@ function SlackView(viewId) {
|
|
|
77
101
|
* @constructor
|
|
78
102
|
*/
|
|
79
103
|
function SlackOption(optionId) {
|
|
80
|
-
return
|
|
104
|
+
return eventDecorator(exports.EventTypes.Option, optionId);
|
|
81
105
|
}
|
|
82
106
|
/**
|
|
83
107
|
* Decorator for message events
|
|
@@ -87,6 +111,11 @@ function SlackOption(optionId) {
|
|
|
87
111
|
* @param pattern
|
|
88
112
|
* @constructor
|
|
89
113
|
*/
|
|
90
|
-
function SlackMessage(pattern =
|
|
91
|
-
return (
|
|
114
|
+
function SlackMessage(pattern = '*') {
|
|
115
|
+
return (target, key, descriptor) => {
|
|
116
|
+
return (0, microservices_1.MessagePattern)(`message://${normalizePattern(pattern)}`, {
|
|
117
|
+
type: 'message',
|
|
118
|
+
pattern,
|
|
119
|
+
})(target, key, descriptor);
|
|
120
|
+
};
|
|
92
121
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import Slack from
|
|
2
|
-
export * from
|
|
1
|
+
import Slack from './slack';
|
|
2
|
+
export * from './decorators';
|
|
3
3
|
export { Slack };
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xpr/nestjs-slack",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "NestJS server implementation of the Slack Assistant",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "tsc -p tsconfig-lib.json"
|
|
8
|
-
"postbuild": "cp package.json ../../dist/nestjs-slack/. && cp readme.md ../../dist/nestjs-slack/."
|
|
7
|
+
"build": "tsc -p tsconfig-lib.json && cp package.json ../../dist/nestjs-slack/. && cp readme.md ../../dist/nestjs-slack/."
|
|
9
8
|
},
|
|
10
9
|
"author": "Ziv Perry",
|
|
11
10
|
"license": "MIT",
|
package/slack.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type CustomTransportStrategy, type MessageHandler, Server } from
|
|
2
|
-
import { App, type AppOptions } from
|
|
3
|
-
export type
|
|
1
|
+
import { type CustomTransportStrategy, type MessageHandler, Server } from '@nestjs/microservices';
|
|
2
|
+
import { App, type AppOptions } from '@slack/bolt';
|
|
3
|
+
export type SlackAssistantOptions = {
|
|
4
4
|
/**
|
|
5
5
|
* Slack application options with required types
|
|
6
6
|
* @see https://github.com/slackapi/bolt-js
|
|
@@ -9,8 +9,8 @@ export type SlackOptions = {
|
|
|
9
9
|
};
|
|
10
10
|
export default class Slack extends Server implements CustomTransportStrategy {
|
|
11
11
|
#private;
|
|
12
|
-
readonly options:
|
|
13
|
-
constructor(options:
|
|
12
|
+
readonly options: SlackAssistantOptions;
|
|
13
|
+
constructor(options: SlackAssistantOptions);
|
|
14
14
|
listen(callback: () => void): Promise<void>;
|
|
15
15
|
close(): Promise<void>;
|
|
16
16
|
on(): void;
|
|
@@ -22,7 +22,7 @@ export default class Slack extends Server implements CustomTransportStrategy {
|
|
|
22
22
|
*/
|
|
23
23
|
protected register(handler: MessageHandler): void;
|
|
24
24
|
/**
|
|
25
|
-
* Get
|
|
25
|
+
* Get the Bolt app instance
|
|
26
26
|
* @protected
|
|
27
27
|
*/
|
|
28
28
|
protected app(): App;
|
package/slack.js
CHANGED
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const microservices_1 = require("@nestjs/microservices");
|
|
4
4
|
const bolt_1 = require("@slack/bolt");
|
|
5
5
|
const decorators_1 = require("./decorators");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
6
|
class Slack extends microservices_1.Server {
|
|
8
7
|
options;
|
|
9
8
|
#app;
|
|
@@ -14,20 +13,20 @@ class Slack extends microservices_1.Server {
|
|
|
14
13
|
async listen(callback) {
|
|
15
14
|
for (const handler of this.getHandlers().values()) {
|
|
16
15
|
this.register(handler);
|
|
17
|
-
const { type, event } = handler.extras;
|
|
18
|
-
this.logger.log(`Handler [${type}] registered with (${event})`);
|
|
19
16
|
}
|
|
20
17
|
await this.app().start();
|
|
21
18
|
callback();
|
|
22
19
|
}
|
|
23
20
|
async close() {
|
|
24
|
-
this.#app && (await this.#app.stop());
|
|
21
|
+
// this.#app && (await this.#app.stop());
|
|
25
22
|
}
|
|
23
|
+
// todo web-client/WebClient events for better typing of the event argument
|
|
26
24
|
on() {
|
|
27
|
-
throw new Error(
|
|
25
|
+
throw new Error('Use SlackEvent decorator to register events');
|
|
28
26
|
}
|
|
29
27
|
unwrap() {
|
|
30
|
-
return this.#app;
|
|
28
|
+
// return this.#app as T;
|
|
29
|
+
throw new Error('Use SlackEvent decorator to register events');
|
|
31
30
|
}
|
|
32
31
|
/**
|
|
33
32
|
* Register the handler for the event
|
|
@@ -51,17 +50,13 @@ class Slack extends microservices_1.Server {
|
|
|
51
50
|
}
|
|
52
51
|
}
|
|
53
52
|
/**
|
|
54
|
-
* Get
|
|
53
|
+
* Get the Bolt app instance
|
|
55
54
|
* @protected
|
|
56
55
|
*/
|
|
57
56
|
app() {
|
|
58
57
|
if (!this.#app) {
|
|
59
|
-
// make sure to add logger if not provided
|
|
60
|
-
if (!this.options.slack.logger) {
|
|
61
|
-
this.options.slack.logger = (0, utils_1.adjustLogger)(this.logger);
|
|
62
|
-
}
|
|
63
58
|
this.#app = new bolt_1.App(this.options.slack);
|
|
64
|
-
this.logger.log(
|
|
59
|
+
this.logger.log('Bolt app created');
|
|
65
60
|
}
|
|
66
61
|
return this.#app;
|
|
67
62
|
}
|
package/utils.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { type Logger } from "@slack/bolt";
|
|
2
|
-
export type Pattern = string | RegExp;
|
|
3
|
-
/**
|
|
4
|
-
* Create method decorator
|
|
5
|
-
* @param type type of listener
|
|
6
|
-
* @param event how to identify the event
|
|
7
|
-
*/
|
|
8
|
-
export declare function eventDecorator<T>(type: string, event: T): MethodDecorator;
|
|
9
|
-
/**
|
|
10
|
-
* Create method decorator
|
|
11
|
-
* @param type type of listener
|
|
12
|
-
* @param pattern how to identify the message
|
|
13
|
-
*/
|
|
14
|
-
export declare function messageDecorator(type: string, pattern: Pattern): MethodDecorator;
|
|
15
|
-
export declare function adjustLogger(logger: any): Logger;
|
package/utils.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.eventDecorator = eventDecorator;
|
|
4
|
-
exports.messageDecorator = messageDecorator;
|
|
5
|
-
exports.adjustLogger = adjustLogger;
|
|
6
|
-
const microservices_1 = require("@nestjs/microservices");
|
|
7
|
-
const bolt_1 = require("@slack/bolt");
|
|
8
|
-
const normalizePattern = (pattern) => {
|
|
9
|
-
if (typeof pattern === "string") {
|
|
10
|
-
return pattern;
|
|
11
|
-
}
|
|
12
|
-
if (pattern instanceof RegExp) {
|
|
13
|
-
return pattern.toString();
|
|
14
|
-
}
|
|
15
|
-
return JSON.stringify(pattern);
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Create method decorator
|
|
19
|
-
* @param type type of listener
|
|
20
|
-
* @param event how to identify the event
|
|
21
|
-
*/
|
|
22
|
-
function eventDecorator(type, event) {
|
|
23
|
-
return (target, key, descriptor) => (0, microservices_1.EventPattern)(`${type}://${normalizePattern(event)}`, {
|
|
24
|
-
type,
|
|
25
|
-
event,
|
|
26
|
-
})(target, key, descriptor);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Create method decorator
|
|
30
|
-
* @param type type of listener
|
|
31
|
-
* @param pattern how to identify the message
|
|
32
|
-
*/
|
|
33
|
-
function messageDecorator(type, pattern) {
|
|
34
|
-
return (target, key, descriptor) => (0, microservices_1.MessagePattern)(`${type}://${normalizePattern(pattern)}`, {
|
|
35
|
-
type,
|
|
36
|
-
pattern,
|
|
37
|
-
})(target, key, descriptor);
|
|
38
|
-
}
|
|
39
|
-
function adjustLogger(logger) {
|
|
40
|
-
// this patch required to support NestJS logger that not use the standard "info"
|
|
41
|
-
// and use "log" instead
|
|
42
|
-
if (logger["log"] && !logger["info"]) {
|
|
43
|
-
logger["info"] = logger["log"].bind(logger);
|
|
44
|
-
}
|
|
45
|
-
// minimal logger interface required by @slack/bolt library
|
|
46
|
-
for (const method of ["debug", "info", "warn", "error"]) {
|
|
47
|
-
if (typeof logger[method] !== "function") {
|
|
48
|
-
throw new Error(`Logger must implement "${method}" method`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return Object.assign(logger, {
|
|
52
|
-
setLevel(_) {
|
|
53
|
-
// don't allow library to set the level
|
|
54
|
-
},
|
|
55
|
-
setName(name) {
|
|
56
|
-
// search for all possible setName/setContext methods, use "noop" if none
|
|
57
|
-
(logger.setName ?? logger.setContext ?? ((i) => i))(name);
|
|
58
|
-
},
|
|
59
|
-
getLevel() {
|
|
60
|
-
// always return the lowest supported level (by library, not real logger)
|
|
61
|
-
// to avoid filtering log messages by library instead of real logger
|
|
62
|
-
return bolt_1.LogLevel.DEBUG;
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
}
|