@xpr/nestjs-slack 1.0.0 → 1.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 +42 -3
- package/decorators.js +9 -8
- package/license +21 -0
- package/package.json +3 -2
- package/readme.md +8 -12
- package/slack.d.ts +2 -2
- package/slack.js +5 -3
- package/utils.d.ts +1 -1
- package/utils.js +8 -8
package/decorators.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from
|
|
2
|
-
import { type Pattern } from
|
|
3
|
-
|
|
1
|
+
import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from "@slack/bolt";
|
|
2
|
+
import { type Pattern } from "./utils";
|
|
3
|
+
import type { AllMiddlewareArgs, AnyMiddlewareArgs, SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs, SlackEventMiddlewareArgs, SlackOptionsMiddlewareArgs, SlackShortcutMiddlewareArgs, SlackViewMiddlewareArgs } from "@slack/bolt/dist/types";
|
|
4
4
|
export { Pattern };
|
|
5
|
+
export type EndpointArgs<T extends AnyMiddlewareArgs> = T & AllMiddlewareArgs;
|
|
5
6
|
/**
|
|
6
7
|
* @see https://tools.slack.dev/bolt-js/reference/
|
|
7
8
|
*/
|
|
@@ -12,11 +13,16 @@ export declare const EventTypes: {
|
|
|
12
13
|
Action: string;
|
|
13
14
|
View: string;
|
|
14
15
|
Option: string;
|
|
16
|
+
Message: string;
|
|
15
17
|
};
|
|
16
18
|
/**
|
|
17
19
|
* Decorator for Slack controller
|
|
18
20
|
*/
|
|
19
21
|
export declare function SlackController(): ClassDecorator;
|
|
22
|
+
/**
|
|
23
|
+
* Arguments for the slack event handler
|
|
24
|
+
*/
|
|
25
|
+
export type SlackEventArgs = EndpointArgs<SlackEventMiddlewareArgs>;
|
|
20
26
|
/**
|
|
21
27
|
* Decorator for event events
|
|
22
28
|
* @see guide https://tools.slack.dev/bolt-js/concepts/event-listening
|
|
@@ -24,6 +30,13 @@ export declare function SlackController(): ClassDecorator;
|
|
|
24
30
|
* @param event
|
|
25
31
|
*/
|
|
26
32
|
export declare function SlackEvent(event: string): MethodDecorator;
|
|
33
|
+
/**
|
|
34
|
+
* Arguments for the slack shortcut handler
|
|
35
|
+
*/
|
|
36
|
+
export type SlackShortcutArgs = EndpointArgs<SlackShortcutMiddlewareArgs>;
|
|
37
|
+
/**
|
|
38
|
+
* Shortcut id
|
|
39
|
+
*/
|
|
27
40
|
export type ShortCutId = Pattern | ShortcutConstraints;
|
|
28
41
|
/**
|
|
29
42
|
* Decorator for shortcut events
|
|
@@ -32,6 +45,10 @@ export type ShortCutId = Pattern | ShortcutConstraints;
|
|
|
32
45
|
* @param shortcutId
|
|
33
46
|
*/
|
|
34
47
|
export declare function SlackShortcut(shortcutId: ShortCutId): MethodDecorator;
|
|
48
|
+
/**
|
|
49
|
+
* Arguments for the slack command handler
|
|
50
|
+
*/
|
|
51
|
+
export type SlackCommandArgs = EndpointArgs<SlackCommandMiddlewareArgs>;
|
|
35
52
|
/**
|
|
36
53
|
* Decorator for command events
|
|
37
54
|
* @see guide https://tools.slack.dev/bolt-js/concepts/commands
|
|
@@ -39,6 +56,13 @@ export declare function SlackShortcut(shortcutId: ShortCutId): MethodDecorator;
|
|
|
39
56
|
* @param commandId
|
|
40
57
|
*/
|
|
41
58
|
export declare function SlackCommand(commandId: Pattern): MethodDecorator;
|
|
59
|
+
/**
|
|
60
|
+
* Arguments for the slack action handler
|
|
61
|
+
*/
|
|
62
|
+
export type SlackActionArgs = EndpointArgs<SlackActionMiddlewareArgs>;
|
|
63
|
+
/**
|
|
64
|
+
* Action id
|
|
65
|
+
*/
|
|
42
66
|
export type ActionId = Pattern | ActionConstraints;
|
|
43
67
|
/**
|
|
44
68
|
* Decorator for action events
|
|
@@ -46,6 +70,13 @@ export type ActionId = Pattern | ActionConstraints;
|
|
|
46
70
|
* @param actionId
|
|
47
71
|
*/
|
|
48
72
|
export declare function SlackAction(actionId: ActionId): MethodDecorator;
|
|
73
|
+
/**
|
|
74
|
+
* Arguments for the slack view handler
|
|
75
|
+
*/
|
|
76
|
+
export type SlackViewArgs = EndpointArgs<SlackViewMiddlewareArgs>;
|
|
77
|
+
/**
|
|
78
|
+
* View id
|
|
79
|
+
*/
|
|
49
80
|
export type ViewId = Pattern | ViewConstraints;
|
|
50
81
|
/**
|
|
51
82
|
* Decorator for view events
|
|
@@ -53,6 +84,13 @@ export type ViewId = Pattern | ViewConstraints;
|
|
|
53
84
|
* @param viewId
|
|
54
85
|
*/
|
|
55
86
|
export declare function SlackView(viewId: ViewId): MethodDecorator;
|
|
87
|
+
/**
|
|
88
|
+
* Arguments for the slack option handler
|
|
89
|
+
*/
|
|
90
|
+
export type SlackOptionArgs = EndpointArgs<SlackOptionsMiddlewareArgs>;
|
|
91
|
+
/**
|
|
92
|
+
* Options id
|
|
93
|
+
*/
|
|
56
94
|
export type OptionId = OptionsConstraints;
|
|
57
95
|
/**
|
|
58
96
|
* Decorator for options events
|
|
@@ -60,6 +98,7 @@ export type OptionId = OptionsConstraints;
|
|
|
60
98
|
* @param optionId
|
|
61
99
|
*/
|
|
62
100
|
export declare function SlackOption(optionId: OptionId): MethodDecorator;
|
|
101
|
+
export type SlackMessageArgs = EndpointArgs<SlackEventMiddlewareArgs<"message">>;
|
|
63
102
|
/**
|
|
64
103
|
* Decorator for message events
|
|
65
104
|
*
|
package/decorators.js
CHANGED
|
@@ -15,12 +15,13 @@ const utils_1 = require("./utils");
|
|
|
15
15
|
* @see https://tools.slack.dev/bolt-js/reference/
|
|
16
16
|
*/
|
|
17
17
|
exports.EventTypes = {
|
|
18
|
-
Event:
|
|
19
|
-
Shortcut:
|
|
20
|
-
Command:
|
|
21
|
-
Action:
|
|
22
|
-
View:
|
|
23
|
-
Option:
|
|
18
|
+
Event: "event",
|
|
19
|
+
Shortcut: "shortcut",
|
|
20
|
+
Command: "command",
|
|
21
|
+
Action: "action",
|
|
22
|
+
View: "view",
|
|
23
|
+
Option: "option",
|
|
24
|
+
Message: "message",
|
|
24
25
|
};
|
|
25
26
|
/**
|
|
26
27
|
* Decorator for Slack controller
|
|
@@ -88,6 +89,6 @@ function SlackOption(optionId) {
|
|
|
88
89
|
* @see reference https://api.slack.com/events/message
|
|
89
90
|
* @param pattern
|
|
90
91
|
*/
|
|
91
|
-
function SlackMessage(pattern =
|
|
92
|
-
return (0, utils_1.
|
|
92
|
+
function SlackMessage(pattern = "*") {
|
|
93
|
+
return (0, utils_1.eventDecorator)(exports.EventTypes.Message, pattern);
|
|
93
94
|
}
|
package/license
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2025 to Ziv Perry.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to delal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xpr/nestjs-slack",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "NestJS server implementation of the Slack Assistant",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -42,7 +42,8 @@
|
|
|
42
42
|
"files": [
|
|
43
43
|
"*.js",
|
|
44
44
|
"*.d.ts",
|
|
45
|
-
"readme.md"
|
|
45
|
+
"readme.md",
|
|
46
|
+
"license"
|
|
46
47
|
],
|
|
47
48
|
"release": {
|
|
48
49
|
"extends": "semantic-release-monorepo",
|
package/readme.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# @xpr/nestjs-slack
|
|
2
2
|
|
|
3
|
-
A NestJS microservice transport and decorators for
|
|
3
|
+
A NestJS microservice transport and decorators for
|
|
4
|
+
[Slack Bolt Apps](https://github.com/slackapi/bolt-js).
|
|
4
5
|
|
|
5
6
|
### Usage
|
|
6
7
|
|
|
@@ -31,27 +32,22 @@ await app.listen();
|
|
|
31
32
|
Example of a Slack controller with listener for action and a command.
|
|
32
33
|
|
|
33
34
|
```ts
|
|
34
|
-
import {
|
|
35
|
-
SlackAction,
|
|
36
|
-
SlackCommand,
|
|
37
|
-
SlackController,
|
|
38
|
-
} from '@xpr/nestjs-slack';
|
|
35
|
+
import { SlackAction, SlackCommand, SlackController } from "@xpr/nestjs-slack";
|
|
39
36
|
|
|
40
37
|
@SlackController()
|
|
41
38
|
export class MyController {
|
|
42
|
-
|
|
43
|
-
@SlackAction('button-action')
|
|
39
|
+
@SlackAction("button-action")
|
|
44
40
|
async onAction({ ack, respond, payload }: SlackActionMiddlewareArgs) {
|
|
45
41
|
await ack();
|
|
46
42
|
await respond(`Button clicked! with payload ${JSON.stringify(payload)}`);
|
|
47
43
|
}
|
|
48
44
|
|
|
49
|
-
@SlackCommand(
|
|
45
|
+
@SlackCommand("/ping")
|
|
50
46
|
async onPing({ ack, respond }: SlackCommandMiddlewareArgs) {
|
|
51
47
|
await ack();
|
|
52
48
|
await respond({
|
|
53
|
-
text:
|
|
54
|
-
response_type:
|
|
49
|
+
text: "pong!",
|
|
50
|
+
response_type: "in_channel",
|
|
55
51
|
});
|
|
56
52
|
}
|
|
57
53
|
}
|
|
@@ -63,4 +59,4 @@ export class MyController {
|
|
|
63
59
|
|
|
64
60
|
## Decorators
|
|
65
61
|
|
|
66
|
-
See [source file](./decorators.ts).
|
|
62
|
+
See [source file](./decorators.ts).
|
package/slack.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type CustomTransportStrategy, type MessageHandler, Server } from
|
|
2
|
-
import { App, type AppOptions } from
|
|
1
|
+
import { type CustomTransportStrategy, type MessageHandler, Server } from "@nestjs/microservices";
|
|
2
|
+
import { App, type AppOptions } from "@slack/bolt";
|
|
3
3
|
export type SlackOptions = {
|
|
4
4
|
/**
|
|
5
5
|
* Slack application options with required types
|
package/slack.js
CHANGED
|
@@ -22,7 +22,7 @@ class Slack extends microservices_1.Server {
|
|
|
22
22
|
this.#app && (await this.#app.stop());
|
|
23
23
|
}
|
|
24
24
|
on() {
|
|
25
|
-
throw new Error(
|
|
25
|
+
throw new Error("Use SlackEvent decorator to register events");
|
|
26
26
|
}
|
|
27
27
|
unwrap() {
|
|
28
28
|
return this.#app;
|
|
@@ -35,6 +35,9 @@ class Slack extends microservices_1.Server {
|
|
|
35
35
|
register(handler) {
|
|
36
36
|
const { type, event } = handler.extras;
|
|
37
37
|
switch (type) {
|
|
38
|
+
case decorators_1.EventTypes.Message:
|
|
39
|
+
this.app().message(event, handler);
|
|
40
|
+
break;
|
|
38
41
|
case decorators_1.EventTypes.Shortcut:
|
|
39
42
|
this.app().shortcut(event, handler);
|
|
40
43
|
break;
|
|
@@ -42,7 +45,6 @@ class Slack extends microservices_1.Server {
|
|
|
42
45
|
this.app().action(event, handler);
|
|
43
46
|
break;
|
|
44
47
|
case decorators_1.EventTypes.Event:
|
|
45
|
-
// todo improve by providing types for events names
|
|
46
48
|
this.app().event(event, handler);
|
|
47
49
|
break;
|
|
48
50
|
case decorators_1.EventTypes.Command:
|
|
@@ -67,7 +69,7 @@ class Slack extends microservices_1.Server {
|
|
|
67
69
|
this.options.slack.logger = (0, utils_1.adjustLogger)(this.logger);
|
|
68
70
|
}
|
|
69
71
|
this.#app = new bolt_1.App(this.options.slack);
|
|
70
|
-
this.logger.log(
|
|
72
|
+
this.logger.log("Bolt app created");
|
|
71
73
|
}
|
|
72
74
|
return this.#app;
|
|
73
75
|
}
|
package/utils.d.ts
CHANGED
package/utils.js
CHANGED
|
@@ -6,7 +6,7 @@ exports.adjustLogger = adjustLogger;
|
|
|
6
6
|
const microservices_1 = require("@nestjs/microservices");
|
|
7
7
|
const bolt_1 = require("@slack/bolt");
|
|
8
8
|
const normalizePattern = (pattern) => {
|
|
9
|
-
if (typeof pattern ===
|
|
9
|
+
if (typeof pattern === "string") {
|
|
10
10
|
return pattern;
|
|
11
11
|
}
|
|
12
12
|
if (pattern instanceof RegExp) {
|
|
@@ -39,29 +39,29 @@ function messageDecorator(type, pattern) {
|
|
|
39
39
|
function adjustLogger(logger) {
|
|
40
40
|
// this patch required to support NestJS logger that not use the standard "info"
|
|
41
41
|
// and use "log" instead
|
|
42
|
-
if (logger[
|
|
43
|
-
logger[
|
|
42
|
+
if (logger["log"] && !logger["info"]) {
|
|
43
|
+
logger["info"] = logger["log"].bind(logger);
|
|
44
44
|
}
|
|
45
45
|
// minimal logger interface required by @slack/bolt library
|
|
46
|
-
for (const method of [
|
|
47
|
-
if (typeof logger[method] !==
|
|
46
|
+
for (const method of ["debug", "info", "warn", "error"]) {
|
|
47
|
+
if (typeof logger[method] !== "function") {
|
|
48
48
|
throw new Error(`Logger must implement "${method}" method`);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
// add compatibility with @slack/bolt library
|
|
52
|
-
if (!logger[
|
|
52
|
+
if (!logger["setLevel"]) {
|
|
53
53
|
logger.setLevel = () => {
|
|
54
54
|
// don't allow library to set the level
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
if (!logger[
|
|
57
|
+
if (!logger["getLevel"]) {
|
|
58
58
|
logger.getLevel = () => {
|
|
59
59
|
// always return the lowest supported level (by library, not real logger)
|
|
60
60
|
// to avoid filtering log messages by library instead of real logger
|
|
61
61
|
return bolt_1.LogLevel.DEBUG;
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
|
-
if (!logger[
|
|
64
|
+
if (!logger["setName"]) {
|
|
65
65
|
logger.setName = (name) => {
|
|
66
66
|
// search for all possible setName/setContext methods, use "noop" if none
|
|
67
67
|
(logger.setName ?? logger.setContext ?? ((i) => i))(name);
|