Compare commits
2 Commits
c1b55c5d98
...
b6f335bc09
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | b6f335bc09 | |
Derrick Hammer | 6bdc43dd85 |
23
src/axios.ts
23
src/axios.ts
|
@ -1,5 +1,5 @@
|
|||
import Axios, {AxiosError, AxiosRequestConfig} from "axios";
|
||||
import {S5Error} from "./client.js";
|
||||
import Axios, { AxiosError, AxiosRequestConfig } from "axios";
|
||||
import { S5Error } from "./client.js";
|
||||
|
||||
export interface CancelablePromise<T> extends Promise<T> {
|
||||
cancel: () => void;
|
||||
|
@ -9,7 +9,7 @@ export const customInstance = <T>(
|
|||
config: AxiosRequestConfig,
|
||||
options?: AxiosRequestConfig,
|
||||
): CancelablePromise<T> => {
|
||||
const source = Axios.CancelToken.source();
|
||||
const abort = new AbortController();
|
||||
|
||||
/*
|
||||
Hack to ensure that the data is passed to the request as an option.
|
||||
|
@ -20,21 +20,26 @@ export const customInstance = <T>(
|
|||
delete config.data;
|
||||
}
|
||||
|
||||
const instance = Axios.create({baseURL: options?.baseURL});
|
||||
const instance = Axios.create({ baseURL: options?.baseURL });
|
||||
const promise = instance({
|
||||
signal: abort.signal,
|
||||
...config,
|
||||
...options,
|
||||
cancelToken: source.token,
|
||||
}).then(({data}) => data).catch((error) => {
|
||||
})
|
||||
.then(({ data }) => data)
|
||||
.catch((error) => {
|
||||
if (Axios.isCancel(error)) {
|
||||
return;
|
||||
}
|
||||
throw new S5Error((error as AxiosError).message, (error as AxiosError).response?.status as number);
|
||||
})
|
||||
throw new S5Error(
|
||||
(error as AxiosError).message,
|
||||
(error as AxiosError).response?.status as number,
|
||||
);
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
promise.cancel = () => {
|
||||
source.cancel("Query was cancelled");
|
||||
abort.abort("Query was cancelled");
|
||||
};
|
||||
|
||||
return promise as CancelablePromise<T>;
|
||||
|
|
|
@ -18,6 +18,7 @@ export type CustomClientOptions = {
|
|||
customCookie?: string;
|
||||
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
||||
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
||||
httpConfig?: AxiosRequestConfig;
|
||||
};
|
||||
|
||||
export function optionsToConfig(
|
||||
|
@ -29,7 +30,7 @@ export function optionsToConfig(
|
|||
| CustomRegistryOptions
|
||||
)[]
|
||||
): AxiosRequestConfig {
|
||||
const config: AxiosRequestConfig = {};
|
||||
let config: AxiosRequestConfig = {};
|
||||
|
||||
config.baseURL = client.portalUrl;
|
||||
|
||||
|
@ -75,5 +76,12 @@ export function optionsToConfig(
|
|||
|
||||
config.headers = headers;
|
||||
|
||||
if (finalOptions?.httpConfig) {
|
||||
config = {
|
||||
...config,
|
||||
...finalOptions.httpConfig,
|
||||
};
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue