-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
62 lines (58 loc) · 2.98 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
export * from "./block-action.ts"
export * from "./dialog-action.ts"
export * from "./interactive-message.ts"
export * from "./workflow-step-edit.ts"
import { AckFn, RespondFn, SayArguments, SayFn } from "../utilities.ts"
import { BlockAction } from "./block-action.ts"
import { DialogSubmitAction, DialogValidation } from "./dialog-action.ts"
import { InteractiveMessage } from "./interactive-message.ts"
import { WorkflowStepEdit } from "./workflow-step-edit.ts"
/**
* All known actions from Slack's Block Kit interactive components, message actions, dialogs, and legacy interactive
* messages.
*
* TODO: BlockAction's default generic parameter (ElementAction) might be too specific to allow for this type to be used
* as a constraint on SlackActionMiddlewareArgs' Action generic parameter.
*
* If someone were to instantiate SlackActionMiddlewareArgs<BlockAction<SomeNewAction>>, would it work? We need it to
* work as long as SomeNewAction implements BasicElementAction.
*
* We don't want to substitute BlockAction with BlockAction<BasicElementAction> here because that means the completions
* offered when no generic parameter is bound would be limited to BasicElementAction rather than the union of known
* actions - ElementAction.
*/
export type SlackAction =
| BlockAction
| InteractiveMessage
| DialogSubmitAction
| WorkflowStepEdit
/**
* Arguments which listeners and middleware receive to process an action from Slack's Block Kit interactive components,
* message actions, dialogs, or legacy interactive messages.
*
* The type parameter `Action` represents the entire JSON-encoded request body from Slack. The generic type
* `BlockAction<ElementAction>` can be used to create a type for this parameter based on an element's action type. In
* this case `ElementAction` must extend `BasicElementAction`.
*/
export interface SlackActionMiddlewareArgs<
Action extends SlackAction = SlackAction,
> {
payload: Action extends BlockAction<infer ElementAction> ? ElementAction
: Action extends InteractiveMessage<infer InteractiveAction> ? InteractiveAction
: Action
action: Action extends BlockAction<infer ElementAction> ? ElementAction
: Action extends InteractiveMessage<infer InteractiveAction> ? InteractiveAction
: Action
body: Action
// all action types except dialog submission have a channel context
say: Action extends Exclude<SlackAction, DialogSubmitAction | WorkflowStepEdit> ? SayFn : never
respond: RespondFn
ack: ActionAckFn<Action>
}
/**
* Type function which given an action `A` returns a corresponding type for the `ack()` function. The function is used
* to acknowledge the receipt (and possibly signal failure) of an action from a listener or middleware.
*/
type ActionAckFn<A extends SlackAction> = A extends InteractiveMessage ? AckFn<string | SayArguments>
: A extends DialogSubmitAction ? AckFn<DialogValidation> // message action and block actions don't accept any value in the ack response
: AckFn<void>