Merge pull request #345 from automatisch/refactor/use-http-client-for-slack-auth

refactor: Use http client for slack authentication
This commit is contained in:
Ömer Faruk Aydın
2022-07-17 00:46:53 +03:00
committed by GitHub

View File

@@ -1,22 +1,38 @@
import type { IAuthentication, IApp, IJSONObject } from '@automatisch/types';
import { WebClient } from '@slack/web-api';
import HttpClient from '../../helpers/http-client';
import qs from 'qs';
export default class Authentication implements IAuthentication {
appData: IApp;
connectionData: IJSONObject;
client: WebClient;
client: HttpClient;
static requestOptions: IJSONObject = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
constructor(appData: IApp, connectionData: IJSONObject) {
this.client = new WebClient();
this.client = new HttpClient({ baseURL: 'https://slack.com/api' });
this.connectionData = connectionData;
this.appData = appData;
}
async verifyCredentials() {
const { bot_id: botId, user: screenName } = await this.client.auth.test({
token: this.connectionData.accessToken as string,
});
const response = await this.client.post(
'/auth.test',
qs.stringify({ token: this.connectionData.accessToken }),
Authentication.requestOptions
);
if (response.data.ok === false) {
throw new Error(
`Error occured while verifying credentials: ${response.data.error}.(More info: https://api.slack.com/methods/auth.test#errors)`
);
}
const { bot_id: botId, user: screenName } = response.data;
return {
botId,
@@ -27,9 +43,11 @@ export default class Authentication implements IAuthentication {
async isStillVerified() {
try {
await this.client.auth.test({
token: this.connectionData.accessToken as string,
});
await this.client.post(
'/auth.test',
qs.stringify({ token: this.connectionData.accessToken }),
Authentication.requestOptions
);
return true;
} catch (error) {