UNPKG

3.02 kBTypeScriptView Raw
1import { AnyObject, CommonLogger } from '@naturalcycles/js-lib';
2import { InspectAnyOptions } from '..';
3/**
4 * Properties that exists both in SlackApiBody (as per Slack API) and SlackMessage (our abstraction).
5 */
6export interface SlackMessageProps {
7 /**
8 * @default bot
9 */
10 username?: string;
11 channel?: string;
12 icon_url?: string;
13 /**
14 * @default :spider_web:
15 */
16 icon_emoji?: string;
17 attachments?: SlackMessageAttachment[];
18}
19export interface SlackApiBody extends SlackMessageProps {
20 text: string;
21}
22export interface SlackMessage<CTX = any> extends SlackMessageProps {
23 /**
24 * The only *required* field.
25 *
26 * You can throw anything at it, it'll handle it appropriately:
27 * String - as is
28 * Object - pass via util.inspect()
29 * Array - will pass each item via util.inspect() and join with \n
30 * Error - print the stack nicely
31 *
32 * If you don't want the default Array behavior - you can pre-util.inspect() it yourself to your liking.
33 */
34 items: any;
35 /**
36 * Optional "context object", to be used by `messagePrefixHook`.
37 */
38 ctx?: CTX;
39 /**
40 * Keys-values will be rendered as MessageAttachment with Fields
41 */
42 kv?: AnyObject;
43 /**
44 * If specified - adds @name1, @name2 in the end of the message
45 */
46 mentions?: string[];
47 /**
48 * By default it ignores possible errors from slack
49 *
50 * @default false
51 */
52 throwOnError?: boolean;
53}
54export interface SlackAttachmentField {
55 title: string;
56 value: string;
57 short?: boolean;
58}
59export interface SlackMessageAttachment {
60 fallback?: string;
61 color?: 'good' | 'warning' | 'danger' | string;
62 pretext?: string;
63 author_name?: string;
64 author_link?: string;
65 author_icon?: string;
66 title?: string;
67 title_link?: string;
68 text?: string;
69 fields?: SlackAttachmentField[];
70 image_url?: string;
71 thumb_url?: string;
72 footer?: string;
73 footer_icon?: string;
74 ts?: number;
75 callback_id?: string;
76 mrkdwn_in?: ('pretext' | 'text' | 'fields')[];
77}
78/**
79 * Return `null` to skip (filter out) the message completely.
80 */
81export declare type SlackMessagePrefixHook<CTX = any> = (msg: SlackMessage<CTX>) => string[] | null | Promise<string[] | null>;
82export interface SlackServiceCfg<CTX = any> {
83 /**
84 * Undefined means slack is disabled.
85 */
86 webhookUrl?: string;
87 defaults?: Partial<SlackMessage>;
88 /**
89 * Function to return an array of "prefix tokens" (will be joined by ': ').
90 * Allows to skip (filter out) the message by returning `null`.
91 */
92 messagePrefixHook: SlackMessagePrefixHook<CTX>;
93 /**
94 * By default SlackService logs every message to console.log
95 * Pass another logger if needed.
96 * Pass `noopLogger` to suppress logging completely.
97 */
98 logger: CommonLogger;
99 /**
100 * Defaults to:
101 * includeErrorData: true
102 * includeErrorStack: true
103 */
104 inspectOptions: InspectAnyOptions;
105}