feat: Convert helpers to use JS files
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import { IHttpClientParams } from '@automatisch/types';
|
||||
import { InternalAxiosRequestConfig } from 'axios';
|
||||
import { URL } from 'node:url';
|
||||
export { AxiosInstance as IHttpClient } from 'axios';
|
||||
|
||||
import HttpError from '../../errors/http';
|
||||
import axios from '../axios-with-proxy';
|
||||
|
||||
const removeBaseUrlForAbsoluteUrls = (
|
||||
requestConfig: InternalAxiosRequestConfig
|
||||
): InternalAxiosRequestConfig => {
|
||||
const removeBaseUrlForAbsoluteUrls = (requestConfig) => {
|
||||
try {
|
||||
const url = new URL(requestConfig.url);
|
||||
requestConfig.baseURL = url.origin;
|
||||
@@ -20,33 +16,27 @@ const removeBaseUrlForAbsoluteUrls = (
|
||||
}
|
||||
};
|
||||
|
||||
export default function createHttpClient({
|
||||
$,
|
||||
baseURL,
|
||||
beforeRequest = [],
|
||||
}: IHttpClientParams) {
|
||||
export default function createHttpClient({ $, baseURL, beforeRequest = [] }) {
|
||||
const instance = axios.create({
|
||||
baseURL,
|
||||
});
|
||||
|
||||
instance.interceptors.request.use(
|
||||
(requestConfig: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
|
||||
const newRequestConfig = removeBaseUrlForAbsoluteUrls(requestConfig);
|
||||
instance.interceptors.request.use((requestConfig) => {
|
||||
const newRequestConfig = removeBaseUrlForAbsoluteUrls(requestConfig);
|
||||
|
||||
const result = beforeRequest.reduce((newConfig, beforeRequestFunc) => {
|
||||
return beforeRequestFunc($, newConfig);
|
||||
}, newRequestConfig);
|
||||
const result = beforeRequest.reduce((newConfig, beforeRequestFunc) => {
|
||||
return beforeRequestFunc($, newConfig);
|
||||
}, newRequestConfig);
|
||||
|
||||
/**
|
||||
* axios seems to want InternalAxiosRequestConfig returned not AxioRequestConfig
|
||||
* anymore even though requests do require AxiosRequestConfig.
|
||||
*
|
||||
* Since both interfaces are very similar (InternalAxiosRequestConfig
|
||||
* extends AxiosRequestConfig), we can utilize an assertion below
|
||||
**/
|
||||
return result as InternalAxiosRequestConfig;
|
||||
}
|
||||
);
|
||||
/**
|
||||
* axios seems to want InternalAxiosRequestConfig returned not AxioRequestConfig
|
||||
* anymore even though requests do require AxiosRequestConfig.
|
||||
*
|
||||
* Since both interfaces are very similar (InternalAxiosRequestConfig
|
||||
* extends AxiosRequestConfig), we can utilize an assertion below
|
||||
**/
|
||||
return result;
|
||||
});
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(response) => response,
|
Reference in New Issue
Block a user