@xpr/nestjs-slack 0.0.1 → 0.2.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,10 +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;
1
+ import { ActionConstraints, OptionsConstraints, ShortcutConstraints, ViewConstraints } from "@slack/bolt";
2
+ import { type Pattern } from "./utils";
3
+ export { Pattern };
8
4
  /**
9
5
  * @see https://tools.slack.dev/bolt-js/reference/
10
6
  */
@@ -24,6 +20,7 @@ export declare const EventTypes: {
24
20
  * @constructor
25
21
  */
26
22
  export declare function SlackEvent(event: string): MethodDecorator;
23
+ export type ShortCutId = Pattern | ShortcutConstraints;
27
24
  /**
28
25
  * Decorator for shortcut events
29
26
  * @see guide https://tools.slack.dev/bolt-js/concepts/shortcuts
@@ -40,6 +37,7 @@ export declare function SlackShortcut(shortcutId: ShortCutId): MethodDecorator;
40
37
  * @constructor
41
38
  */
42
39
  export declare function SlackCommand(commandId: Pattern): MethodDecorator;
40
+ export type ActionId = Pattern | ActionConstraints;
43
41
  /**
44
42
  * Decorator for action events
45
43
  * @see guide https://tools.slack.dev/bolt-js/concepts/actions
@@ -47,6 +45,7 @@ export declare function SlackCommand(commandId: Pattern): MethodDecorator;
47
45
  * @constructor
48
46
  */
49
47
  export declare function SlackAction(actionId: ActionId): MethodDecorator;
48
+ export type ViewId = Pattern | ViewConstraints;
50
49
  /**
51
50
  * Decorator for view events
52
51
  * @see guide https://tools.slack.dev/bolt-js/concepts/view-submissions
@@ -54,6 +53,7 @@ export declare function SlackAction(actionId: ActionId): MethodDecorator;
54
53
  * @constructor
55
54
  */
56
55
  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?: string | RegExp): MethodDecorator;
72
+ export declare function SlackMessage(pattern?: Pattern): MethodDecorator;
package/decorators.js CHANGED
@@ -8,37 +8,17 @@ exports.SlackAction = SlackAction;
8
8
  exports.SlackView = SlackView;
9
9
  exports.SlackOption = SlackOption;
10
10
  exports.SlackMessage = SlackMessage;
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
- }
11
+ const utils_1 = require("./utils");
32
12
  /**
33
13
  * @see https://tools.slack.dev/bolt-js/reference/
34
14
  */
35
15
  exports.EventTypes = {
36
- Event: 'event',
37
- Shortcut: 'shortcut',
38
- Command: 'command',
39
- Action: 'action',
40
- View: 'view',
41
- Option: 'option',
16
+ Event: "event",
17
+ Shortcut: "shortcut",
18
+ Command: "command",
19
+ Action: "action",
20
+ View: "view",
21
+ Option: "option",
42
22
  };
43
23
  /**
44
24
  * Decorator for event events
@@ -49,10 +29,7 @@ exports.EventTypes = {
49
29
  */
50
30
  function SlackEvent(event) {
51
31
  // todo input validation (types for 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);
32
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Event, event);
56
33
  }
57
34
  /**
58
35
  * Decorator for shortcut events
@@ -62,7 +39,7 @@ function SlackEvent(event) {
62
39
  * @constructor
63
40
  */
64
41
  function SlackShortcut(shortcutId) {
65
- return eventDecorator(exports.EventTypes.Shortcut, shortcutId);
42
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Shortcut, shortcutId);
66
43
  }
67
44
  /**
68
45
  * Decorator for command events
@@ -73,7 +50,7 @@ function SlackShortcut(shortcutId) {
73
50
  */
74
51
  function SlackCommand(commandId) {
75
52
  // todo input validation command must start with / etc.
76
- return eventDecorator(exports.EventTypes.Command, commandId);
53
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Command, commandId);
77
54
  }
78
55
  /**
79
56
  * Decorator for action events
@@ -82,7 +59,7 @@ function SlackCommand(commandId) {
82
59
  * @constructor
83
60
  */
84
61
  function SlackAction(actionId) {
85
- return eventDecorator(exports.EventTypes.Action, actionId);
62
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Action, actionId);
86
63
  }
87
64
  /**
88
65
  * Decorator for view events
@@ -91,7 +68,7 @@ function SlackAction(actionId) {
91
68
  * @constructor
92
69
  */
93
70
  function SlackView(viewId) {
94
- return eventDecorator(exports.EventTypes.View, viewId);
71
+ return (0, utils_1.eventDecorator)(exports.EventTypes.View, viewId);
95
72
  }
96
73
  /**
97
74
  * Decorator for options events
@@ -100,7 +77,7 @@ function SlackView(viewId) {
100
77
  * @constructor
101
78
  */
102
79
  function SlackOption(optionId) {
103
- return eventDecorator(exports.EventTypes.Option, optionId);
80
+ return (0, utils_1.eventDecorator)(exports.EventTypes.Option, optionId);
104
81
  }
105
82
  /**
106
83
  * Decorator for message events
@@ -110,11 +87,6 @@ function SlackOption(optionId) {
110
87
  * @param pattern
111
88
  * @constructor
112
89
  */
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
- };
90
+ function SlackMessage(pattern = "*") {
91
+ return (0, utils_1.messageDecorator)("message", pattern);
120
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.1",
3
+ "version": "0.2.0",
4
4
  "description": "NestJS server implementation of the Slack Assistant",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "build": "tsc -p tsconfig.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
- import { type CustomTransportStrategy, type MessageHandler, Server } from '@nestjs/microservices';
2
- import { App, type AppOptions } from '@slack/bolt';
3
- export type SlackAssistantOptions = {
1
+ import { type CustomTransportStrategy, type MessageHandler, Server } from "@nestjs/microservices";
2
+ import { App, type AppOptions } from "@slack/bolt";
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
@@ -13,20 +13,20 @@ class Slack extends microservices_1.Server {
13
13
  async listen(callback) {
14
14
  for (const handler of this.getHandlers().values()) {
15
15
  this.register(handler);
16
+ const { type, event } = handler.extras;
17
+ this.logger.log(`Handler [${type}] registered with (${event})`);
16
18
  }
17
19
  await this.app().start();
18
20
  callback();
19
21
  }
20
22
  async close() {
21
- // this.#app && (await this.#app.stop());
23
+ this.#app && (await this.#app.stop());
22
24
  }
23
- // todo web-client/WebClient events for better typing of the event argument
24
25
  on() {
25
- throw new Error('Use SlackEvent decorator to register events');
26
+ throw new Error("Use SlackEvent decorator to register events");
26
27
  }
27
28
  unwrap() {
28
- // return this.#app as T;
29
- throw new Error('Use SlackEvent decorator to register events');
29
+ return this.#app;
30
30
  }
31
31
  /**
32
32
  * Register the handler for the event
@@ -50,13 +50,13 @@ class Slack extends microservices_1.Server {
50
50
  }
51
51
  }
52
52
  /**
53
- * Get the Bolt app instance
53
+ * Get/Create Bolt app instance
54
54
  * @protected
55
55
  */
56
56
  app() {
57
57
  if (!this.#app) {
58
58
  this.#app = new bolt_1.App(this.options.slack);
59
- this.logger.log('Bolt app created');
59
+ this.logger.log("Bolt app created");
60
60
  }
61
61
  return this.#app;
62
62
  }
package/utils.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export type Pattern = string | RegExp;
2
+ /**
3
+ * Create method decorator
4
+ * @param type type of listener
5
+ * @param event how to identify the event
6
+ */
7
+ export declare function eventDecorator<T>(type: string, event: T): MethodDecorator;
8
+ /**
9
+ * Create method decorator
10
+ * @param type type of listener
11
+ * @param pattern how to identify the message
12
+ */
13
+ export declare function messageDecorator(type: string, pattern: Pattern): MethodDecorator;
package/utils.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.eventDecorator = eventDecorator;
4
+ exports.messageDecorator = messageDecorator;
5
+ const microservices_1 = require("@nestjs/microservices");
6
+ const normalizePattern = (pattern) => {
7
+ if (typeof pattern === "string") {
8
+ return pattern;
9
+ }
10
+ if (pattern instanceof RegExp) {
11
+ return pattern.toString();
12
+ }
13
+ return JSON.stringify(pattern);
14
+ };
15
+ /**
16
+ * Create method decorator
17
+ * @param type type of listener
18
+ * @param event how to identify the event
19
+ */
20
+ function eventDecorator(type, event) {
21
+ return (target, key, descriptor) => (0, microservices_1.EventPattern)(`${type}://${normalizePattern(event)}`, {
22
+ type,
23
+ event,
24
+ })(target, key, descriptor);
25
+ }
26
+ /**
27
+ * Create method decorator
28
+ * @param type type of listener
29
+ * @param pattern how to identify the message
30
+ */
31
+ function messageDecorator(type, pattern) {
32
+ return (target, key, descriptor) => (0, microservices_1.MessagePattern)(`${type}://${normalizePattern(pattern)}`, {
33
+ type,
34
+ pattern,
35
+ })(target, key, descriptor);
36
+ }