Merge pull request #620 from automatisch/refactor/restructure-slack
refactor: Use beforeRequest hook with slack app
This commit is contained in:
@@ -8,10 +8,6 @@ type FindMessageOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
|
const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
|
||||||
const headers = {
|
|
||||||
Authorization: `Bearer ${$.auth.data.accessToken}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
query: options.query,
|
query: options.query,
|
||||||
sort: options.sortBy,
|
sort: options.sortBy,
|
||||||
@@ -20,7 +16,6 @@ const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const response = await $.http.get('/search.messages', {
|
const response = await $.http.get('/search.messages', {
|
||||||
headers,
|
|
||||||
params,
|
params,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -5,16 +5,12 @@ const postMessage = async (
|
|||||||
channelId: string,
|
channelId: string,
|
||||||
text: string
|
text: string
|
||||||
) => {
|
) => {
|
||||||
const headers = {
|
|
||||||
Authorization: `Bearer ${$.auth.data.accessToken}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
channel: channelId,
|
channel: channelId,
|
||||||
text,
|
text,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await $.http.post('/chat.postMessage', params, { headers });
|
const response = await $.http.post('/chat.postMessage', params);
|
||||||
|
|
||||||
const message: IActionOutput = {
|
const message: IActionOutput = {
|
||||||
data: {
|
data: {
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
|
import { IGlobalVariable } from '@automatisch/types';
|
||||||
import verifyCredentials from './verify-credentials';
|
import verifyCredentials from './verify-credentials';
|
||||||
|
|
||||||
const isStillVerified = async ($: any) => {
|
const isStillVerified = async ($: IGlobalVariable) => {
|
||||||
try {
|
try {
|
||||||
await verifyCredentials($);
|
await verifyCredentials($);
|
||||||
return true;
|
return true;
|
||||||
|
11
packages/backend/src/apps/slack/common/add-auth-header.ts
Normal file
11
packages/backend/src/apps/slack/common/add-auth-header.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { TBeforeRequest } from '@automatisch/types';
|
||||||
|
|
||||||
|
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
|
||||||
|
if (requestConfig.headers && $.auth.data?.accessToken) {
|
||||||
|
requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default addAuthHeader;
|
@@ -13,19 +13,15 @@ export default {
|
|||||||
error: null,
|
error: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await $.http.get('/conversations.list', {
|
const response = await $.http.get('/conversations.list');
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${$.auth.data.accessToken}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.integrationError) {
|
if (response.integrationError) {
|
||||||
channels.error = response.integrationError;
|
channels.error = response.integrationError;
|
||||||
return channels;
|
return channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.data.ok === 'false') {
|
if (response.data.ok === false) {
|
||||||
channels.error = response.data.error;
|
channels.error = response.data;
|
||||||
return channels;
|
return channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,4 +1,7 @@
|
|||||||
export default {
|
import defineApp from '../../helpers/define-app';
|
||||||
|
import addAuthHeader from './common/add-auth-header';
|
||||||
|
|
||||||
|
export default defineApp({
|
||||||
name: 'Slack',
|
name: 'Slack',
|
||||||
key: 'slack',
|
key: 'slack',
|
||||||
iconUrl: '{BASE_URL}/apps/slack/assets/favicon.svg',
|
iconUrl: '{BASE_URL}/apps/slack/assets/favicon.svg',
|
||||||
@@ -6,4 +9,6 @@ export default {
|
|||||||
supportsConnections: true,
|
supportsConnections: true,
|
||||||
baseUrl: 'https://slack.com',
|
baseUrl: 'https://slack.com',
|
||||||
apiBaseUrl: 'https://slack.com/api',
|
apiBaseUrl: 'https://slack.com/api',
|
||||||
};
|
primaryColor: '4a154b',
|
||||||
|
beforeRequest: [addAuthHeader],
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user