-
Notifications
You must be signed in to change notification settings - Fork 399
/
index.ts
68 lines (62 loc) · 2.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { SlackEvent } from '@slack/types';
import type { SayFn, StringIndexed } from '../utilities';
/**
* Arguments which listeners and middleware receive to process an event from Slack's Events API.
*/
export type SlackEventMiddlewareArgs<EventType extends string = string> = {
payload: EventFromType<EventType>;
event: EventFromType<EventType>;
body: EnvelopedEvent<EventFromType<EventType>>;
// Add `ack` as undefined for global middleware in TypeScript TODO: but why? spend some time digging into this
ack?: undefined;
} & (EventType extends 'message'
? // If this is a message event, add a `message` property
{ message: EventFromType<EventType> }
: unknown) &
(EventFromType<EventType> extends { channel: string } | { item: { channel: string } }
? // If this event contains a channel, add a `say` utility function
{ say: SayFn }
: unknown);
export interface BaseSlackEvent<T extends string = string> {
type: T;
}
export type EventTypePattern = string | RegExp;
export type FunctionInputs = Record<string, unknown>;
/**
* A Slack Events API event wrapped in the standard envelope.
*
* This describes the entire JSON-encoded body of a request from Slack's Events API.
*/
export interface EnvelopedEvent<Event = BaseSlackEvent> extends StringIndexed {
token: string;
team_id: string;
enterprise_id?: string;
api_app_id: string;
event: Event;
type: 'event_callback';
event_id: string;
event_time: number;
is_ext_shared_channel?: boolean;
authorizations?: Authorization[];
}
interface Authorization {
enterprise_id: string | null;
team_id: string | null;
user_id: string;
is_bot: boolean;
is_enterprise_install?: boolean;
}
/**
* Type function which given a string `T` returns a type for the matching Slack event(s).
*
* When the string matches known event(s) from the `SlackEvent` union, only those types are returned (also as a union).
* Otherwise, the `BasicSlackEvent<T>` type is returned.
*/
export type EventFromType<T extends string> = KnownEventFromType<T> extends never
? BaseSlackEvent<T>
: KnownEventFromType<T>;
export type KnownEventFromType<T extends string> = Extract<SlackEvent, { type: T }>;
/**
* Type function which tests whether or not the given `Event` contains a channel ID context for where the event
* occurred, and returns `Type` when the test passes. Otherwise this returns `undefined`.
*/