@xpr/nestjs-slack 0.0.0 → 0.0.1

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 CHANGED
@@ -1,6 +1,10 @@
1
- import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from "@slack/bolt";
2
- import { type Pattern } from "./utils";
3
- export { Pattern };
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;
4
8
  /**
5
9
  * @see https://tools.slack.dev/bolt-js/reference/
6
10
  */
@@ -20,7 +24,6 @@ export declare const EventTypes: {
20
24
  * @constructor
21
25
  */
22
26
  export declare function SlackEvent(event: string): MethodDecorator;
23
- export type ShortCutId = Pattern | ShortcutConstraints;
24
27
  /**
25
28
  * Decorator for shortcut events
26
29
  * @see guide https://tools.slack.dev/bolt-js/concepts/shortcuts
@@ -37,7 +40,6 @@ export declare function SlackShortcut(shortcutId: ShortCutId): MethodDecorator;
37
40
  * @constructor
38
41
  */
39
42
  export declare function SlackCommand(commandId: Pattern): MethodDecorator;
40
- export type ActionId = Pattern | ActionConstraints;
41
43
  /**
42
44
  * Decorator for action events
43
45
  * @see guide https://tools.slack.dev/bolt-js/concepts/actions
@@ -45,7 +47,6 @@ export type ActionId = Pattern | ActionConstraints;
45
47
  * @constructor
46
48
  */
47
49
  export declare function SlackAction(actionId: ActionId): MethodDecorator;
48
- export type ViewId = Pattern | ViewConstraints;
49
50
  /**
50
51
  * Decorator for view events
51
52
  * @see guide https://tools.slack.dev/bolt-js/concepts/view-submissions
@@ -53,7 +54,6 @@ export type ViewId = Pattern | ViewConstraints;
53
54
  * @constructor
54
55
  */
55
56
  export declare function SlackView(viewId: ViewId): MethodDecorator;
56
- export type OptionId = OptionsConstraints;
57
57
  /**
58
58
  * Decorator for options events
59
59
  * @see guide https://tools.slack.dev/bolt-js/concepts/options
@@ -69,4 +69,4 @@ export declare function SlackOption(optionId: OptionId): MethodDecorator;
69
69
  * @param pattern
70
70
  * @constructor
71
71
  */
72
- export declare function SlackMessage(pattern?: Pattern): MethodDecorator;
72
+ export declare function SlackMessage(pattern?: string | RegExp): MethodDecorator;
package/decorators.js CHANGED
@@ -8,17 +8,37 @@ exports.SlackAction = SlackAction;
8
8
  exports.SlackView = SlackView;
9
9
  exports.SlackOption = SlackOption;
10
10
  exports.SlackMessage = SlackMessage;
11
- const utils_1 = require("./utils");
11
+ const microservices_1 = require("@nestjs/microservices");
12
+ const normalizePattern = (pattern) => {
13
+ if (typeof pattern === 'string') {
14
+ return pattern;
15
+ }
16
+ if (pattern instanceof RegExp) {
17
+ return pattern.toString();
18
+ }
19
+ return JSON.stringify(pattern);
20
+ };
21
+ /**
22
+ * Create method decorator
23
+ * @param type type of listener
24
+ * @param event how to identify the event
25
+ */
26
+ function eventDecorator(type, event) {
27
+ return (target, key, descriptor) => (0, microservices_1.EventPattern)(`${type}://${normalizePattern(event)}`, {
28
+ type,
29
+ event,
30
+ })(target, key, descriptor);
31
+ }
12
32
  /**
13
33
  * @see https://tools.slack.dev/bolt-js/reference/
14
34
  */
15
35
  exports.EventTypes = {
16
- Event: "event",
17
- Shortcut: "shortcut",
18
- Command: "command",
19
- Action: "action",
20
- View: "view",
21
- Option: "option",
36
+ Event: 'event',
37
+ Shortcut: 'shortcut',
38
+ Command: 'command',
39
+ Action: 'action',
40
+ View: 'view',
41
+ Option: 'option',
22
42
  };
23
43
  /**
24
44
  * Decorator for event events
@@ -29,7 +49,10 @@ exports.EventTypes = {
29
49
  */
30
50
  function SlackEvent(event) {
31
51
  // todo input validation (types for event)
32
- return (0, utils_1.eventDecorator)(exports.EventTypes.Event, event);
52
+ return (target, key, descriptor) => (0, microservices_1.EventPattern)(`${exports.EventTypes.Event}://${normalizePattern(event)}`, {
53
+ type: exports.EventTypes.Event,
54
+ event,
55
+ })(target, key, descriptor);
33
56
  }
34
57
  /**
35
58
  * Decorator for shortcut events
@@ -39,7 +62,7 @@ function SlackEvent(event) {
39
62
  * @constructor
40
63
  */
41
64
  function SlackShortcut(shortcutId) {
42
- return (0, utils_1.eventDecorator)(exports.EventTypes.Shortcut, shortcutId);
65
+ return eventDecorator(exports.EventTypes.Shortcut, shortcutId);
43
66
  }
44
67
  /**
45
68
  * Decorator for command events
@@ -50,7 +73,7 @@ function SlackShortcut(shortcutId) {
50
73
  */
51
74
  function SlackCommand(commandId) {
52
75
  // todo input validation command must start with / etc.
53
- return (0, utils_1.eventDecorator)(exports.EventTypes.Command, commandId);
76
+ return eventDecorator(exports.EventTypes.Command, commandId);
54
77
  }
55
78
  /**
56
79
  * Decorator for action events
@@ -59,7 +82,7 @@ function SlackCommand(commandId) {
59
82
  * @constructor
60
83
  */
61
84
  function SlackAction(actionId) {
62
- return (0, utils_1.eventDecorator)(exports.EventTypes.Action, actionId);
85
+ return eventDecorator(exports.EventTypes.Action, actionId);
63
86
  }
64
87
  /**
65
88
  * Decorator for view events
@@ -68,7 +91,7 @@ function SlackAction(actionId) {
68
91
  * @constructor
69
92
  */
70
93
  function SlackView(viewId) {
71
- return (0, utils_1.eventDecorator)(exports.EventTypes.View, viewId);
94
+ return eventDecorator(exports.EventTypes.View, viewId);
72
95
  }
73
96
  /**
74
97
  * Decorator for options events
@@ -77,7 +100,7 @@ function SlackView(viewId) {
77
100
  * @constructor
78
101
  */
79
102
  function SlackOption(optionId) {
80
- return (0, utils_1.eventDecorator)(exports.EventTypes.Option, optionId);
103
+ return eventDecorator(exports.EventTypes.Option, optionId);
81
104
  }
82
105
  /**
83
106
  * Decorator for message events
@@ -87,6 +110,11 @@ function SlackOption(optionId) {
87
110
  * @param pattern
88
111
  * @constructor
89
112
  */
90
- function SlackMessage(pattern = "*") {
91
- return (0, utils_1.messageDecorator)("message", pattern);
113
+ function SlackMessage(pattern = '*') {
114
+ return (target, key, descriptor) => {
115
+ return (0, microservices_1.MessagePattern)(`message://${normalizePattern(pattern)}`, {
116
+ type: 'message',
117
+ pattern,
118
+ })(target, key, descriptor);
119
+ };
92
120
  }
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import Slack from "./slack";
2
- export * from "./decorators";
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.0",
3
+ "version": "0.0.1",
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.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 "@nestjs/microservices";
2
- import { App, type AppOptions } from "@slack/bolt";
3
- export type SlackOptions = {
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: SlackOptions;
13
- constructor(options: SlackOptions);
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/Create Bolt app instance
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("Use SlackEvent decorator to register events");
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/Create Bolt app instance
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("Bolt app created");
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
- }