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