refactor: rewrite http-client as functional
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user