refactor: rewrite http-client as functional

This commit is contained in:
Ali BARIN
2022-10-03 17:05:02 +02:00
parent 3b8a12810c
commit 52e4e09e85
6 changed files with 34 additions and 42 deletions

View File

@@ -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;
}