@xpr/nestjs-slack 1.0.1 → 2.0.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/decorators.d.ts +35 -5
- package/decorators.js +37 -9
- package/index.d.ts +1 -0
- package/license +21 -0
- package/package.json +3 -2
- package/readme.md +8 -12
- package/slack.d.ts +10 -3
- package/slack.js +3 -4
- package/utils.d.ts +2 -1
- package/utils.js +8 -8
package/decorators.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { AllMiddlewareArgs, AnyMiddlewareArgs, SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs, SlackEventMiddlewareArgs, SlackOptionsMiddlewareArgs, SlackShortcutMiddlewareArgs, SlackViewMiddlewareArgs } from '@slack/bolt/dist/types';
|
|
1
|
+
import { type Pattern } from "./utils";
|
|
2
|
+
import type { ActionConstraints, AllMiddlewareArgs, AnyMiddlewareArgs, OptionsConstraints, ShortcutConstraints, SlackActionMiddlewareArgs, SlackCommandMiddlewareArgs, SlackEventMiddlewareArgs, SlackOptionsMiddlewareArgs, SlackShortcutMiddlewareArgs, SlackViewMiddlewareArgs, ViewConstraints } from "@slack/bolt/dist/types";
|
|
4
3
|
export { Pattern };
|
|
5
4
|
export type EndpointArgs<T extends AnyMiddlewareArgs> = T & AllMiddlewareArgs;
|
|
6
5
|
/**
|
|
@@ -16,15 +15,45 @@ export declare const EventTypes: {
|
|
|
16
15
|
Message: string;
|
|
17
16
|
};
|
|
18
17
|
/**
|
|
19
|
-
* Decorator for Slack controller
|
|
18
|
+
* Decorator for Slack/Bolt application controller
|
|
19
|
+
*
|
|
20
|
+
* @example slack controller:
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { SlackController } from '@xpr/nestjs-slack';
|
|
23
|
+
*
|
|
24
|
+
* @SlackController()
|
|
25
|
+
* class MySlackController {
|
|
26
|
+
* // add decorated methods
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
20
29
|
*/
|
|
21
30
|
export declare function SlackController(): ClassDecorator;
|
|
22
31
|
/**
|
|
23
32
|
* Arguments for the slack event handler
|
|
33
|
+
*
|
|
34
|
+
* See {@link SlackEvent}
|
|
24
35
|
*/
|
|
25
36
|
export type SlackEventArgs = EndpointArgs<SlackEventMiddlewareArgs>;
|
|
26
37
|
/**
|
|
27
38
|
* Decorator for event events
|
|
39
|
+
*
|
|
40
|
+
* Usage:
|
|
41
|
+
* ```ts
|
|
42
|
+
* import {
|
|
43
|
+
* SlackController,
|
|
44
|
+
* SlackEvent,
|
|
45
|
+
* SlackEventArgs,
|
|
46
|
+
* } from '@xpr/nestjs-slack';
|
|
47
|
+
*
|
|
48
|
+
* @SlackController()
|
|
49
|
+
* class MySlackController {
|
|
50
|
+
*
|
|
51
|
+
* @SlackEvent('event_name')
|
|
52
|
+
* foo(args: SlackEventArgs) {
|
|
53
|
+
* }
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
28
57
|
* @see guide https://tools.slack.dev/bolt-js/concepts/event-listening
|
|
29
58
|
* @see api https://api.slack.com/apis/events-api
|
|
30
59
|
* @param event
|
|
@@ -98,7 +127,7 @@ export type OptionId = OptionsConstraints;
|
|
|
98
127
|
* @param optionId
|
|
99
128
|
*/
|
|
100
129
|
export declare function SlackOption(optionId: OptionId): MethodDecorator;
|
|
101
|
-
export type SlackMessageArgs = EndpointArgs<SlackEventMiddlewareArgs<
|
|
130
|
+
export type SlackMessageArgs = EndpointArgs<SlackEventMiddlewareArgs<"message">>;
|
|
102
131
|
/**
|
|
103
132
|
* Decorator for message events
|
|
104
133
|
*
|
|
@@ -107,3 +136,4 @@ export type SlackMessageArgs = EndpointArgs<SlackEventMiddlewareArgs<'message'>>
|
|
|
107
136
|
* @param pattern
|
|
108
137
|
*/
|
|
109
138
|
export declare function SlackMessage(pattern?: Pattern): MethodDecorator;
|
|
139
|
+
//# sourceMappingURL=decorators.d.ts.map
|
package/decorators.js
CHANGED
|
@@ -15,22 +15,50 @@ 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:
|
|
24
|
-
Message:
|
|
18
|
+
Event: "event",
|
|
19
|
+
Shortcut: "shortcut",
|
|
20
|
+
Command: "command",
|
|
21
|
+
Action: "action",
|
|
22
|
+
View: "view",
|
|
23
|
+
Option: "option",
|
|
24
|
+
Message: "message",
|
|
25
25
|
};
|
|
26
26
|
/**
|
|
27
|
-
* Decorator for Slack controller
|
|
27
|
+
* Decorator for Slack/Bolt application controller
|
|
28
|
+
*
|
|
29
|
+
* @example slack controller:
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { SlackController } from '@xpr/nestjs-slack';
|
|
32
|
+
*
|
|
33
|
+
* @SlackController()
|
|
34
|
+
* class MySlackController {
|
|
35
|
+
* // add decorated methods
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
28
38
|
*/
|
|
29
39
|
function SlackController() {
|
|
30
40
|
return (target) => (0, common_1.Controller)()(target);
|
|
31
41
|
}
|
|
32
42
|
/**
|
|
33
43
|
* Decorator for event events
|
|
44
|
+
*
|
|
45
|
+
* Usage:
|
|
46
|
+
* ```ts
|
|
47
|
+
* import {
|
|
48
|
+
* SlackController,
|
|
49
|
+
* SlackEvent,
|
|
50
|
+
* SlackEventArgs,
|
|
51
|
+
* } from '@xpr/nestjs-slack';
|
|
52
|
+
*
|
|
53
|
+
* @SlackController()
|
|
54
|
+
* class MySlackController {
|
|
55
|
+
*
|
|
56
|
+
* @SlackEvent('event_name')
|
|
57
|
+
* foo(args: SlackEventArgs) {
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
34
62
|
* @see guide https://tools.slack.dev/bolt-js/concepts/event-listening
|
|
35
63
|
* @see api https://api.slack.com/apis/events-api
|
|
36
64
|
* @param event
|
|
@@ -89,6 +117,6 @@ function SlackOption(optionId) {
|
|
|
89
117
|
* @see reference https://api.slack.com/events/message
|
|
90
118
|
* @param pattern
|
|
91
119
|
*/
|
|
92
|
-
function SlackMessage(pattern =
|
|
120
|
+
function SlackMessage(pattern = "*") {
|
|
93
121
|
return (0, utils_1.eventDecorator)(exports.EventTypes.Message, pattern);
|
|
94
122
|
}
|
package/index.d.ts
CHANGED
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": "
|
|
3
|
+
"version": "2.0.0",
|
|
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,11 +1,17 @@
|
|
|
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
|
-
* Slack application options with required types
|
|
5
|
+
* Slack application options with required types.
|
|
6
6
|
* @see https://github.com/slackapi/bolt-js
|
|
7
7
|
*/
|
|
8
8
|
slack: AppOptions;
|
|
9
|
+
/**
|
|
10
|
+
* Whether to inject NestJS logger into the Slack app.
|
|
11
|
+
* The injected logger override any provided in the `slack` options.
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
injectNestjsLogger?: boolean;
|
|
9
15
|
};
|
|
10
16
|
export default class Slack extends Server implements CustomTransportStrategy {
|
|
11
17
|
#private;
|
|
@@ -27,3 +33,4 @@ export default class Slack extends Server implements CustomTransportStrategy {
|
|
|
27
33
|
*/
|
|
28
34
|
protected app(): App;
|
|
29
35
|
}
|
|
36
|
+
//# sourceMappingURL=slack.d.ts.map
|
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;
|
|
@@ -64,12 +64,11 @@ class Slack extends microservices_1.Server {
|
|
|
64
64
|
*/
|
|
65
65
|
app() {
|
|
66
66
|
if (!this.#app) {
|
|
67
|
-
|
|
68
|
-
if (!this.options.slack.logger) {
|
|
67
|
+
if (this.options.injectNestjsLogger && !this.options.slack.logger) {
|
|
69
68
|
this.options.slack.logger = (0, utils_1.adjustLogger)(this.logger);
|
|
70
69
|
}
|
|
71
70
|
this.#app = new bolt_1.App(this.options.slack);
|
|
72
|
-
this.logger.log(
|
|
71
|
+
this.logger.log("Bolt app created");
|
|
73
72
|
}
|
|
74
73
|
return this.#app;
|
|
75
74
|
}
|
package/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Logger } from
|
|
1
|
+
import { type Logger } from "@slack/bolt";
|
|
2
2
|
export type Pattern = string | RegExp;
|
|
3
3
|
/**
|
|
4
4
|
* Create method decorator
|
|
@@ -13,3 +13,4 @@ export declare function eventDecorator<T>(type: string, event: T): MethodDecorat
|
|
|
13
13
|
*/
|
|
14
14
|
export declare function messageDecorator(type: string, pattern: Pattern): MethodDecorator;
|
|
15
15
|
export declare function adjustLogger(logger: any): Logger;
|
|
16
|
+
//# sourceMappingURL=utils.d.ts.map
|
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);
|