-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.ts
More file actions
106 lines (90 loc) · 2.96 KB
/
index.ts
File metadata and controls
106 lines (90 loc) · 2.96 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { ApiResponse } from "../../api";
import { AuthCheckInput, RevokeAccessTokenInput } from "../../interfaces";
import Knock from "../../knock";
import { TENANT_OBJECT_COLLECTION } from "../objects/constants";
import { GetSlackChannelsInput, GetSlackChannelsResponse } from "./interfaces";
class SlackClient {
private instance: Knock;
constructor(instance: Knock) {
this.instance = instance;
}
async authCheck({ tenant, knockChannelId }: AuthCheckInput) {
if (!this.instance.isAuthenticated()) {
this.instance.log("[Slack] Skipping authCheck - user not authenticated");
return { status: "not_connected" };
}
const result = await this.instance.client().makeRequest({
method: "GET",
url: `/v1/providers/slack/${knockChannelId}/auth_check`,
params: {
access_token_object: {
object_id: tenant,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
},
});
return this.handleResponse(result);
}
async getChannels(
input: GetSlackChannelsInput,
): Promise<GetSlackChannelsResponse> {
if (!this.instance.isAuthenticated()) {
this.instance.log(
"[Slack] Skipping getChannels - user not authenticated",
);
return { slack_channels: [], next_cursor: null };
}
const { knockChannelId, tenant } = input;
const queryOptions = input.queryOptions || {};
const result = await this.instance.client().makeRequest({
method: "GET",
url: `/v1/providers/slack/${knockChannelId}/channels`,
params: {
access_token_object: {
object_id: tenant,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
query_options: {
cursor: queryOptions.cursor,
limit: queryOptions.limit,
exclude_archived: queryOptions.excludeArchived,
team_id: queryOptions.teamId,
types: queryOptions.types,
},
},
});
return this.handleResponse(result);
}
async revokeAccessToken({ tenant, knockChannelId }: RevokeAccessTokenInput) {
if (!this.instance.isAuthenticated()) {
this.instance.log(
"[Slack] Skipping revokeAccessToken - user not authenticated",
);
return { status: "success" };
}
const result = await this.instance.client().makeRequest({
method: "PUT",
url: `/v1/providers/slack/${knockChannelId}/revoke_access`,
params: {
access_token_object: {
object_id: tenant,
collection: TENANT_OBJECT_COLLECTION,
},
channel_id: knockChannelId,
},
});
return this.handleResponse(result);
}
private handleResponse(response: ApiResponse) {
if (response.statusCode === "error") {
if (response.error?.response?.status < 500) {
return response.error || response.body;
}
throw new Error(response.error || response.body);
}
return response.body;
}
}
export default SlackClient;