UNPKG

2.95 kBTypeScriptView Raw
1import { StringMap } from '@naturalcycles/js-lib';
2import { DebugLogLevel } 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 level?: DebugLogLevel;
40 /**
41 * Keys-values will be rendered as MessageAttachment with Fields
42 */
43 kv?: StringMap<any>;
44 /**
45 * If specified - adds @name1, @name2 in the end of the message
46 */
47 mentions?: string[];
48 /**
49 * @default false
50 * By default it ignores possible errors from slack
51 */
52 throwOnError?: boolean;
53 /**
54 * @default false
55 * Skips logging message
56 */
57 noLog?: boolean;
58}
59export interface SlackAttachmentField {
60 title: string;
61 value: string;
62 short?: boolean;
63}
64export interface SlackMessageAttachment {
65 fallback?: string;
66 color?: 'good' | 'warning' | 'danger' | string;
67 pretext?: string;
68 author_name?: string;
69 author_link?: string;
70 author_icon?: string;
71 title?: string;
72 title_link?: string;
73 text?: string;
74 fields?: SlackAttachmentField[];
75 image_url?: string;
76 thumb_url?: string;
77 footer?: string;
78 footer_icon?: string;
79 ts?: number;
80 callback_id?: string;
81 mrkdwn_in?: ('pretext' | 'text' | 'fields')[];
82}
83/**
84 * Return `null` to skip (filter out) the message completely.
85 */
86export declare type SlackMessagePrefixHook<CTX = any> = (msg: SlackMessage<CTX>) => string[] | null | Promise<string[] | null>;
87export interface SlackServiceCfg<CTX = any> {
88 /**
89 * Undefined means slack is disabled.
90 */
91 webhookUrl?: string;
92 defaults?: Partial<SlackMessage>;
93 /**
94 * Override channel when msg.level is set.
95 * key: DebugLogLevel
96 * value: channel name to send message to
97 */
98 channelByLevel?: StringMap;
99 /**
100 * Function to return an array of "prefix tokens" (will be joined by ': ').
101 * Allows to skip (filter out) the message by returning `null`.
102 */
103 messagePrefixHook: SlackMessagePrefixHook<CTX>;
104}