diff --git a/packages/backend/src/apps/github/authentication.ts b/packages/backend/src/apps/github/authentication.ts index e186c78c..64d2f0af 100644 --- a/packages/backend/src/apps/github/authentication.ts +++ b/packages/backend/src/apps/github/authentication.ts @@ -4,19 +4,19 @@ import type { IField, IJSONObject, } from '@automatisch/types'; -import HttpClient from '../../helpers/http-client'; +import createHttpClient, { IHttpClient } from '../../helpers/http-client'; import { URLSearchParams } from 'url'; export default class Authentication implements IAuthentication { appData: IApp; connectionData: IJSONObject; scopes: string[] = ['read:org', 'repo', 'user']; - client: HttpClient; + client: IHttpClient; constructor(appData: IApp, connectionData: IJSONObject) { this.connectionData = connectionData; this.appData = appData; - this.client = new HttpClient({ baseURL: 'https://github.com' }); + this.client = createHttpClient({ baseURL: 'https://github.com' }); } get oauthRedirectUrl(): string { diff --git a/packages/backend/src/apps/slack/client/index.ts b/packages/backend/src/apps/slack/client/index.ts index ba47be53..5e5d79e3 100644 --- a/packages/backend/src/apps/slack/client/index.ts +++ b/packages/backend/src/apps/slack/client/index.ts @@ -1,5 +1,5 @@ import { IFlow, IStep, IConnection } from '@automatisch/types'; -import HttpClient from '../../../helpers/http-client'; +import createHttpClient, { IHttpClient } from '../../../helpers/http-client'; import VerifyAccessToken from './endpoints/verify-access-token'; import PostMessageToChannel from './endpoints/post-message-to-channel'; import FindMessages from './endpoints/find-messages'; @@ -8,7 +8,7 @@ export default class SlackClient { flow: IFlow; step: IStep; connection: IConnection; - httpClient: HttpClient; + httpClient: IHttpClient; verifyAccessToken: VerifyAccessToken; postMessageToChannel: PostMessageToChannel; @@ -21,7 +21,7 @@ export default class SlackClient { this.flow = flow; this.step = step; - this.httpClient = new HttpClient({ baseURL: SlackClient.baseUrl }); + this.httpClient = createHttpClient({ baseURL: SlackClient.baseUrl }); this.verifyAccessToken = new VerifyAccessToken(this); this.postMessageToChannel = new PostMessageToChannel(this); this.findMessages = new FindMessages(this); diff --git a/packages/backend/src/apps/twitter/client/index.ts b/packages/backend/src/apps/twitter/client/index.ts index 3cc3a72e..4cb899b5 100644 --- a/packages/backend/src/apps/twitter/client/index.ts +++ b/packages/backend/src/apps/twitter/client/index.ts @@ -1,7 +1,7 @@ import { IFlow, IStep, IConnection } from '@automatisch/types'; import OAuth from 'oauth-1.0a'; import crypto from 'crypto'; -import HttpClient from '../../../helpers/http-client'; +import createHttpClient, { IHttpClient } from '../../../helpers/http-client'; import OAuthRequestToken from './endpoints/oauth-request-token'; import VerifyAccessToken from './endpoints/verify-access-token'; import GetCurrentUser from './endpoints/get-current-user'; @@ -16,7 +16,7 @@ export default class TwitterClient { step: IStep; connection: IConnection; oauthClient: OAuth; - httpClient: HttpClient; + httpClient: IHttpClient; oauthRequestToken: OAuthRequestToken; verifyAccessToken: VerifyAccessToken; @@ -34,7 +34,7 @@ export default class TwitterClient { this.flow = flow; this.step = step; - this.httpClient = new HttpClient({ baseURL: TwitterClient.baseUrl }); + this.httpClient = createHttpClient({ baseURL: TwitterClient.baseUrl }); const consumerData = { key: this.connection.formattedData.consumerKey as string, diff --git a/packages/backend/src/apps/typeform/authentication.ts b/packages/backend/src/apps/typeform/authentication.ts index 7eedc0d1..0229b42c 100644 --- a/packages/backend/src/apps/typeform/authentication.ts +++ b/packages/backend/src/apps/typeform/authentication.ts @@ -5,12 +5,12 @@ import type { IJSONObject, } from '@automatisch/types'; import { URLSearchParams } from 'url'; -import HttpClient from '../../helpers/http-client'; +import createHttpClient, { IHttpClient } from '../../helpers/http-client'; export default class Authentication implements IAuthentication { appData: IApp; connectionData: IJSONObject; - client: HttpClient; + client: IHttpClient; scope: string[] = [ 'forms:read', @@ -25,7 +25,7 @@ export default class Authentication implements IAuthentication { constructor(appData: IApp, connectionData: IJSONObject) { this.connectionData = connectionData; this.appData = appData; - this.client = new HttpClient({ baseURL: 'https://api.typeform.com' }); + this.client = createHttpClient({ baseURL: 'https://api.typeform.com' }); } get oauthRedirectUrl() { diff --git a/packages/backend/src/helpers/http-client/index.ts b/packages/backend/src/helpers/http-client/index.ts index 8c0861c7..dd134e5d 100644 --- a/packages/backend/src/helpers/http-client/index.ts +++ b/packages/backend/src/helpers/http-client/index.ts @@ -1,34 +1,19 @@ -import axios, { AxiosInstance, AxiosResponse } from 'axios'; -import { IJSONObject, IHttpClientParams } from '@automatisch/types'; +import axios from 'axios'; +export { AxiosInstance as IHttpClient } from 'axios'; +import { IHttpClientParams } from '@automatisch/types'; -type ExtendedAxiosResponse = AxiosResponse & { integrationError: IJSONObject }; +export default function createcreateHttpClient({ baseURL, }: IHttpClientParams) { + const instance = axios.create({ + baseURL, + }); -export default class HttpClient { - instance: AxiosInstance; + instance.interceptors.response.use( + (response) => response, + (error) => { + error.response.integrationError = error.response.data; + return error.response; + } + ); - constructor(params: IHttpClientParams) { - this.instance = axios.create({ - baseURL: params.baseURL, - }); - - this.instance.interceptors.response.use( - (response) => response, - (error) => { - error.response.integrationError = error.response.data; - return error.response; - } - ); - } - - async get(path: string, options?: IJSONObject) { - return (await this.instance.get(path, options)) as ExtendedAxiosResponse; - } - - async post(path: string, body: IJSONObject | string, options?: IJSONObject) { - return (await this.instance.post( - path, - body, - options - )) as ExtendedAxiosResponse; - } + return instance; } diff --git a/packages/backend/src/types/axios.d.ts b/packages/backend/src/types/axios.d.ts new file mode 100644 index 00000000..948f46b4 --- /dev/null +++ b/packages/backend/src/types/axios.d.ts @@ -0,0 +1,7 @@ +import { IJSONObject } from '@automatisch/types'; + +declare module 'axios' { + interface AxiosResponse { + integrationError?: IJSONObject; + } +}