@xpr/nestjs-slack 0.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.
@@ -0,0 +1,72 @@
1
+ import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from "@slack/bolt";
2
+ import { type Pattern } from "./utils";
3
+ export { Pattern };
4
+ /**
5
+ * @see https://tools.slack.dev/bolt-js/reference/
6
+ */
7
+ export declare const EventTypes: {
8
+ Event: string;
9
+ Shortcut: string;
10
+ Command: string;
11
+ Action: string;
12
+ View: string;
13
+ Option: string;
14
+ };
15
+ /**
16
+ * Decorator for event events
17
+ * @see guide https://tools.slack.dev/bolt-js/concepts/event-listening
18
+ * @see api https://api.slack.com/apis/events-api
19
+ * @param event
20
+ * @constructor
21
+ */
22
+ export declare function SlackEvent(event: string): MethodDecorator;
23
+ export type ShortCutId = Pattern | ShortcutConstraints;
24
+ /**
25
+ * Decorator for shortcut events
26
+ * @see guide https://tools.slack.dev/bolt-js/concepts/shortcuts
27
+ * @see api https://api.slack.com/interactivity/shortcuts
28
+ * @param shortcutId
29
+ * @constructor
30
+ */
31
+ export declare function SlackShortcut(shortcutId: ShortCutId): MethodDecorator;
32
+ /**
33
+ * Decorator for command events
34
+ * @see guide https://tools.slack.dev/bolt-js/concepts/commands
35
+ * @see api https://api.slack.com/interactivity/slash-commands
36
+ * @param commandId
37
+ * @constructor
38
+ */
39
+ export declare function SlackCommand(commandId: Pattern): MethodDecorator;
40
+ export type ActionId = Pattern | ActionConstraints;
41
+ /**
42
+ * Decorator for action events
43
+ * @see guide https://tools.slack.dev/bolt-js/concepts/actions
44
+ * @param actionId
45
+ * @constructor
46
+ */
47
+ export declare function SlackAction(actionId: ActionId): MethodDecorator;
48
+ export type ViewId = Pattern | ViewConstraints;
49
+ /**
50
+ * Decorator for view events
51
+ * @see guide https://tools.slack.dev/bolt-js/concepts/view-submissions
52
+ * @param viewId
53
+ * @constructor
54
+ */
55
+ export declare function SlackView(viewId: ViewId): MethodDecorator;
56
+ export type OptionId = OptionsConstraints;
57
+ /**
58
+ * Decorator for options events
59
+ * @see guide https://tools.slack.dev/bolt-js/concepts/options
60
+ * @param optionId
61
+ * @constructor
62
+ */
63
+ export declare function SlackOption(optionId: OptionId): MethodDecorator;
64
+ /**
65
+ * Decorator for message events
66
+ *
67
+ * @see guide https://tools.slack.dev/bolt-js/concepts/message-listening
68
+ * @see reference https://api.slack.com/events/message
69
+ * @param pattern
70
+ * @constructor
71
+ */
72
+ export declare function SlackMessage(pattern?: Pattern): MethodDecorator;
package/decorators.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventTypes = void 0;
4
+ exports.SlackEvent = SlackEvent;
5
+ exports.SlackShortcut = SlackShortcut;
6
+ exports.SlackCommand = SlackCommand;
7
+ exports.SlackAction = SlackAction;
8
+ exports.SlackView = SlackView;
9
+ exports.SlackOption = SlackOption;
10
+ exports.SlackMessage = SlackMessage;
11
+ const utils_1 = require("./utils");
12
+ /**
13
+ * @see https://tools.slack.dev/bolt-js/reference/
14
+ */
15
+ exports.EventTypes = {
16
+ Event: "event",
17
+ Shortcut: "shortcut",
18
+ Command: "command",
19
+ Action: "action",
20
+ View: "view",
21
+ Option: "option",
22
+ };
23
+ /**
24
+ * Decorator for event events
25
+ * @see guide https://tools.slack.dev/bolt-js/concepts/event-listening
26
+ * @see api https://api.slack.com/apis/events-api
27
+ * @param event
28
+ * @constructor
29
+ */
30
+ function SlackEvent(event) {
31
+ // todo input validation (types for event)
32
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Event, event);
33
+ }
34
+ /**
35
+ * Decorator for shortcut events
36
+ * @see guide https://tools.slack.dev/bolt-js/concepts/shortcuts
37
+ * @see api https://api.slack.com/interactivity/shortcuts
38
+ * @param shortcutId
39
+ * @constructor
40
+ */
41
+ function SlackShortcut(shortcutId) {
42
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Shortcut, shortcutId);
43
+ }
44
+ /**
45
+ * Decorator for command events
46
+ * @see guide https://tools.slack.dev/bolt-js/concepts/commands
47
+ * @see api https://api.slack.com/interactivity/slash-commands
48
+ * @param commandId
49
+ * @constructor
50
+ */
51
+ function SlackCommand(commandId) {
52
+ // todo input validation command must start with / etc.
53
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Command, commandId);
54
+ }
55
+ /**
56
+ * Decorator for action events
57
+ * @see guide https://tools.slack.dev/bolt-js/concepts/actions
58
+ * @param actionId
59
+ * @constructor
60
+ */
61
+ function SlackAction(actionId) {
62
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Action, actionId);
63
+ }
64
+ /**
65
+ * Decorator for view events
66
+ * @see guide https://tools.slack.dev/bolt-js/concepts/view-submissions
67
+ * @param viewId
68
+ * @constructor
69
+ */
70
+ function SlackView(viewId) {
71
+ return (0, utils_1.eventDecorator)(exports.EventTypes.View, viewId);
72
+ }
73
+ /**
74
+ * Decorator for options events
75
+ * @see guide https://tools.slack.dev/bolt-js/concepts/options
76
+ * @param optionId
77
+ * @constructor
78
+ */
79
+ function SlackOption(optionId) {
80
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Option, optionId);
81
+ }
82
+ /**
83
+ * Decorator for message events
84
+ *
85
+ * @see guide https://tools.slack.dev/bolt-js/concepts/message-listening
86
+ * @see reference https://api.slack.com/events/message
87
+ * @param pattern
88
+ * @constructor
89
+ */
90
+ function SlackMessage(pattern = "*") {
91
+ return (0, utils_1.messageDecorator)("message", pattern);
92
+ }
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import Slack from "./slack";
2
+ export * from "./decorators";
3
+ export { Slack };
package/index.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.Slack = void 0;
21
+ const slack_1 = __importDefault(require("./slack"));
22
+ exports.Slack = slack_1.default;
23
+ __exportStar(require("./decorators"), exports);
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@xpr/nestjs-slack",
3
+ "version": "0.0.0",
4
+ "description": "NestJS server implementation of the Slack Assistant",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build": "tsc -p tsconfig-lib.json",
8
+ "postbuild": "cp package.json ../../dist/nestjs-slack/. && cp readme.md ../../dist/nestjs-slack/."
9
+ },
10
+ "author": "Ziv Perry",
11
+ "license": "MIT",
12
+ "keywords": [],
13
+ "engines": {
14
+ "node": ">=20.0.0",
15
+ "npm": ">=10.0.0"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/ziv/nestjs-slack-assistant.git",
20
+ "directory": "libs/slack-assistant"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/ziv/nestjs-slack-assistant/issues"
24
+ },
25
+ "homepage": "https://github.com/ziv/nestjs-slack-assistant#readme",
26
+ "devDependencies": {
27
+ "@tsconfig/node20": "^20.1.4",
28
+ "@types/node": "^22.10.6"
29
+ },
30
+ "peerDependencies": {
31
+ "@nestjs/microservices": ">=11.0.13",
32
+ "@slack/bolt": ">=4.2.1",
33
+ "reflect-metadata": ">=0.2.2"
34
+ }
35
+ }
package/readme.md ADDED
@@ -0,0 +1 @@
1
+ # @xpr/nestjs-slack
package/slack.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { type CustomTransportStrategy, type MessageHandler, Server } from "@nestjs/microservices";
2
+ import { App, type AppOptions } from "@slack/bolt";
3
+ export type SlackOptions = {
4
+ /**
5
+ * Slack application options with required types
6
+ * @see https://github.com/slackapi/bolt-js
7
+ */
8
+ slack: AppOptions;
9
+ };
10
+ export default class Slack extends Server implements CustomTransportStrategy {
11
+ #private;
12
+ readonly options: SlackOptions;
13
+ constructor(options: SlackOptions);
14
+ listen(callback: () => void): Promise<void>;
15
+ close(): Promise<void>;
16
+ on(): void;
17
+ unwrap<T>(): T;
18
+ /**
19
+ * Register the handler for the event
20
+ * @param handler
21
+ * @protected
22
+ */
23
+ protected register(handler: MessageHandler): void;
24
+ /**
25
+ * Get/Create Bolt app instance
26
+ * @protected
27
+ */
28
+ protected app(): App;
29
+ }
package/slack.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const microservices_1 = require("@nestjs/microservices");
4
+ const bolt_1 = require("@slack/bolt");
5
+ const decorators_1 = require("./decorators");
6
+ const utils_1 = require("./utils");
7
+ class Slack extends microservices_1.Server {
8
+ options;
9
+ #app;
10
+ constructor(options) {
11
+ super();
12
+ this.options = options;
13
+ }
14
+ async listen(callback) {
15
+ for (const handler of this.getHandlers().values()) {
16
+ this.register(handler);
17
+ const { type, event } = handler.extras;
18
+ this.logger.log(`Handler [${type}] registered with (${event})`);
19
+ }
20
+ await this.app().start();
21
+ callback();
22
+ }
23
+ async close() {
24
+ this.#app && (await this.#app.stop());
25
+ }
26
+ on() {
27
+ throw new Error("Use SlackEvent decorator to register events");
28
+ }
29
+ unwrap() {
30
+ return this.#app;
31
+ }
32
+ /**
33
+ * Register the handler for the event
34
+ * @param handler
35
+ * @protected
36
+ */
37
+ register(handler) {
38
+ const { type, event } = handler.extras;
39
+ switch (type) {
40
+ case decorators_1.EventTypes.Shortcut:
41
+ return this.app().shortcut(event, handler);
42
+ case decorators_1.EventTypes.Action:
43
+ return this.app().action(event, handler);
44
+ case decorators_1.EventTypes.Event:
45
+ return this.app().event(event, // todo improve by providing types
46
+ handler);
47
+ case decorators_1.EventTypes.Command:
48
+ return this.app().command(event, handler);
49
+ case decorators_1.EventTypes.Option:
50
+ return this.app().options(event, handler);
51
+ }
52
+ }
53
+ /**
54
+ * Get/Create Bolt app instance
55
+ * @protected
56
+ */
57
+ app() {
58
+ 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
+ this.#app = new bolt_1.App(this.options.slack);
64
+ this.logger.log("Bolt app created");
65
+ }
66
+ return this.#app;
67
+ }
68
+ }
69
+ exports.default = Slack;
package/utils.d.ts ADDED
@@ -0,0 +1,15 @@
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 ADDED
@@ -0,0 +1,65 @@
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
+ }