refactor: rewrite http-client as functional
This commit is contained in:
@@ -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 {
|
||||
|
@@ -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);
|
||||
|
@@ -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,
|
||||
|
@@ -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() {
|
||||
|
@@ -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;
|
||||
}
|
||||
|
7
packages/backend/src/types/axios.d.ts
vendored
Normal file
7
packages/backend/src/types/axios.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
declare module 'axios' {
|
||||
interface AxiosResponse {
|
||||
integrationError?: IJSONObject;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user