UNPKG

3.1 kBTypeScriptView Raw
1import { StringMap } from '@naturalcycles/js-lib';
2import { DebugLogLevel, 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 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 /**
59 * Defaults to:
60 * includeErrorData: true
61 * includeErrorStack: true
62 */
63 inspectOptions?: InspectAnyOptions;
64}
65export interface SlackAttachmentField {
66 title: string;
67 value: string;
68 short?: boolean;
69}
70export interface SlackMessageAttachment {
71 fallback?: string;
72 color?: 'good' | 'warning' | 'danger' | string;
73 pretext?: string;
74 author_name?: string;
75 author_link?: string;
76 author_icon?: string;
77 title?: string;
78 title_link?: string;
79 text?: string;
80 fields?: SlackAttachmentField[];
81 image_url?: string;
82 thumb_url?: string;
83 footer?: string;
84 footer_icon?: string;
85 ts?: number;
86 callback_id?: string;
87 mrkdwn_in?: ('pretext' | 'text' | 'fields')[];
88}
89/**
90 * Return `null` to skip (filter out) the message completely.
91 */
92export declare type SlackMessagePrefixHook<CTX = any> = (msg: SlackMessage<CTX>) => string[] | null | Promise<string[] | null>;
93export interface SlackServiceCfg<CTX = any> {
94 /**
95 * Undefined means slack is disabled.
96 */
97 webhookUrl?: string;
98 defaults?: Partial<SlackMessage>;
99 /**
100 * Override channel when msg.level is set.
101 * key: DebugLogLevel
102 * value: channel name to send message to
103 */
104 channelByLevel?: StringMap;
105 /**
106 * Function to return an array of "prefix tokens" (will be joined by ': ').
107 * Allows to skip (filter out) the message by returning `null`.
108 */
109 messagePrefixHook: SlackMessagePrefixHook<CTX>;
110}