Merge pull request #555 from automatisch/refactor-http-client

refactor: rewrite http-client as functional
This commit is contained in:
Ömer Faruk Aydın
2022-10-04 01:43:22 +03:00
committed by GitHub
6 changed files with 34 additions and 42 deletions

View File

@@ -4,19 +4,19 @@ import type {
IField, IField,
IJSONObject, IJSONObject,
} from '@automatisch/types'; } from '@automatisch/types';
import HttpClient from '../../helpers/http-client'; import createHttpClient, { IHttpClient } from '../../helpers/http-client';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
export default class Authentication implements IAuthentication { export default class Authentication implements IAuthentication {
appData: IApp; appData: IApp;
connectionData: IJSONObject; connectionData: IJSONObject;
scopes: string[] = ['read:org', 'repo', 'user']; scopes: string[] = ['read:org', 'repo', 'user'];
client: HttpClient; client: IHttpClient;
constructor(appData: IApp, connectionData: IJSONObject) { constructor(appData: IApp, connectionData: IJSONObject) {
this.connectionData = connectionData; this.connectionData = connectionData;
this.appData = appData; this.appData = appData;
this.client = new HttpClient({ baseURL: 'https://github.com' }); this.client = createHttpClient({ baseURL: 'https://github.com' });
} }
get oauthRedirectUrl(): string { get oauthRedirectUrl(): string {

View File

@@ -1,5 +1,5 @@
import { IFlow, IStep, IConnection } from '@automatisch/types'; 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 VerifyAccessToken from './endpoints/verify-access-token';
import PostMessageToChannel from './endpoints/post-message-to-channel'; import PostMessageToChannel from './endpoints/post-message-to-channel';
import FindMessages from './endpoints/find-messages'; import FindMessages from './endpoints/find-messages';
@@ -8,7 +8,7 @@ export default class SlackClient {
flow: IFlow; flow: IFlow;
step: IStep; step: IStep;
connection: IConnection; connection: IConnection;
httpClient: HttpClient; httpClient: IHttpClient;
verifyAccessToken: VerifyAccessToken; verifyAccessToken: VerifyAccessToken;
postMessageToChannel: PostMessageToChannel; postMessageToChannel: PostMessageToChannel;
@@ -21,7 +21,7 @@ export default class SlackClient {
this.flow = flow; this.flow = flow;
this.step = step; this.step = step;
this.httpClient = new HttpClient({ baseURL: SlackClient.baseUrl }); this.httpClient = createHttpClient({ baseURL: SlackClient.baseUrl });
this.verifyAccessToken = new VerifyAccessToken(this); this.verifyAccessToken = new VerifyAccessToken(this);
this.postMessageToChannel = new PostMessageToChannel(this); this.postMessageToChannel = new PostMessageToChannel(this);
this.findMessages = new FindMessages(this); this.findMessages = new FindMessages(this);

View File

@@ -1,7 +1,7 @@
import { IFlow, IStep, IConnection } from '@automatisch/types'; import { IFlow, IStep, IConnection } from '@automatisch/types';
import OAuth from 'oauth-1.0a'; import OAuth from 'oauth-1.0a';
import crypto from 'crypto'; 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 OAuthRequestToken from './endpoints/oauth-request-token';
import VerifyAccessToken from './endpoints/verify-access-token'; import VerifyAccessToken from './endpoints/verify-access-token';
import GetCurrentUser from './endpoints/get-current-user'; import GetCurrentUser from './endpoints/get-current-user';
@@ -16,7 +16,7 @@ export default class TwitterClient {
step: IStep; step: IStep;
connection: IConnection; connection: IConnection;
oauthClient: OAuth; oauthClient: OAuth;
httpClient: HttpClient; httpClient: IHttpClient;
oauthRequestToken: OAuthRequestToken; oauthRequestToken: OAuthRequestToken;
verifyAccessToken: VerifyAccessToken; verifyAccessToken: VerifyAccessToken;
@@ -34,7 +34,7 @@ export default class TwitterClient {
this.flow = flow; this.flow = flow;
this.step = step; this.step = step;
this.httpClient = new HttpClient({ baseURL: TwitterClient.baseUrl }); this.httpClient = createHttpClient({ baseURL: TwitterClient.baseUrl });
const consumerData = { const consumerData = {
key: this.connection.formattedData.consumerKey as string, key: this.connection.formattedData.consumerKey as string,

View File

@@ -5,12 +5,12 @@ import type {
IJSONObject, IJSONObject,
} from '@automatisch/types'; } from '@automatisch/types';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
import HttpClient from '../../helpers/http-client'; import createHttpClient, { IHttpClient } from '../../helpers/http-client';
export default class Authentication implements IAuthentication { export default class Authentication implements IAuthentication {
appData: IApp; appData: IApp;
connectionData: IJSONObject; connectionData: IJSONObject;
client: HttpClient; client: IHttpClient;
scope: string[] = [ scope: string[] = [
'forms:read', 'forms:read',
@@ -25,7 +25,7 @@ export default class Authentication implements IAuthentication {
constructor(appData: IApp, connectionData: IJSONObject) { constructor(appData: IApp, connectionData: IJSONObject) {
this.connectionData = connectionData; this.connectionData = connectionData;
this.appData = appData; this.appData = appData;
this.client = new HttpClient({ baseURL: 'https://api.typeform.com' }); this.client = createHttpClient({ baseURL: 'https://api.typeform.com' });
} }
get oauthRedirectUrl() { get oauthRedirectUrl() {

View File

@@ -1,34 +1,19 @@
import axios, { AxiosInstance, AxiosResponse } from 'axios'; import axios from 'axios';
import { IJSONObject, IHttpClientParams } from '@automatisch/types'; 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({
export default class HttpClient { baseURL,
instance: AxiosInstance;
constructor(params: IHttpClientParams) {
this.instance = axios.create({
baseURL: params.baseURL,
}); });
this.instance.interceptors.response.use( instance.interceptors.response.use(
(response) => response, (response) => response,
(error) => { (error) => {
error.response.integrationError = error.response.data; error.response.integrationError = error.response.data;
return error.response; return error.response;
} }
); );
}
async get(path: string, options?: IJSONObject) { return instance;
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;
}
} }

7
packages/backend/src/types/axios.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
import { IJSONObject } from '@automatisch/types';
declare module 'axios' {
interface AxiosResponse {
integrationError?: IJSONObject;
}
}