-
Notifications
You must be signed in to change notification settings - Fork 955
/
Copy pathslack-api-client.ts
51 lines (45 loc) · 1.35 KB
/
slack-api-client.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
import axios, { AxiosResponse } from 'axios';
/**
* Centralized Slack API client for all requests
*/
export class SlackApiClient {
readonly token: string;
readonly cookie: string;
constructor(token: string, cookie: string) {
this.token = token;
this.cookie = cookie;
}
private getBaseHeaders(): Record<string, string> {
return {
'Cookie': `d=${this.cookie}`,
};
}
/**
* POST to a Slack API endpoint
*/
async post(endpoint: string, data: any, formData = false): Promise<AxiosResponse> {
const url = `https://slack.com/api/${endpoint}`;
let headers = this.getBaseHeaders();
let payload = data;
if (formData) {
headers = { ...headers, ...data.getHeaders() };
} else {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
payload = new URLSearchParams(data).toString();
}
return axios.post(url, payload, { headers, maxBodyLength: Infinity, validateStatus: () => true });
}
/**
* GET from a Slack API endpoint
*/
async get(endpoint: string, params: Record<string, any> = {}): Promise<AxiosResponse> {
const url = `https://slack.com/api/${endpoint}`;
const headers = this.getBaseHeaders();
return axios.get(url, { headers, params, validateStatus: () => true });
}
}
export interface SlackApiResponse {
ok: boolean;
error?: string;
[key: string]: any;
}