Compare commits
No commits in common. "b6f335bc09edb0a665537fd605f594f365c2950f" and "c1b55c5d98b5e0b22b1416018db828ba59166522" have entirely different histories.
b6f335bc09
...
c1b55c5d98
61
src/axios.ts
61
src/axios.ts
|
@ -1,46 +1,41 @@
|
||||||
import Axios, { AxiosError, AxiosRequestConfig } from "axios";
|
import Axios, {AxiosError, AxiosRequestConfig} from "axios";
|
||||||
import { S5Error } from "./client.js";
|
import {S5Error} from "./client.js";
|
||||||
|
|
||||||
export interface CancelablePromise<T> extends Promise<T> {
|
export interface CancelablePromise<T> extends Promise<T> {
|
||||||
cancel: () => void;
|
cancel: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const customInstance = <T>(
|
export const customInstance = <T>(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
options?: AxiosRequestConfig,
|
options?: AxiosRequestConfig,
|
||||||
): CancelablePromise<T> => {
|
): CancelablePromise<T> => {
|
||||||
const abort = new AbortController();
|
const source = Axios.CancelToken.source();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Hack to ensure that the data is passed to the request as an option.
|
Hack to ensure that the data is passed to the request as an option.
|
||||||
*/
|
*/
|
||||||
if (options?.data) {
|
if (options?.data) {
|
||||||
config = config || {};
|
config = config || {};
|
||||||
config.data = options.data;
|
config.data = options.data;
|
||||||
delete config.data;
|
delete config.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = Axios.create({ baseURL: options?.baseURL });
|
const instance = Axios.create({baseURL: options?.baseURL});
|
||||||
const promise = instance({
|
const promise = instance({
|
||||||
signal: abort.signal,
|
...config,
|
||||||
...config,
|
...options,
|
||||||
...options,
|
cancelToken: source.token,
|
||||||
})
|
}).then(({data}) => data).catch((error) => {
|
||||||
.then(({ data }) => data)
|
if (Axios.isCancel(error)) {
|
||||||
.catch((error) => {
|
return;
|
||||||
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
|
// @ts-ignore
|
||||||
promise.cancel = () => {
|
promise.cancel = () => {
|
||||||
abort.abort("Query was cancelled");
|
source.cancel("Query was cancelled");
|
||||||
};
|
};
|
||||||
|
|
||||||
return promise as CancelablePromise<T>;
|
return promise as CancelablePromise<T>;
|
||||||
};
|
};
|
||||||
|
|
|
@ -71,13 +71,13 @@ import { blake3 } from "@noble/hashes/blake3";
|
||||||
import { base64urlDecode, base64urlEncode } from "./utils/encoding.js";
|
import { base64urlDecode, base64urlEncode } from "./utils/encoding.js";
|
||||||
|
|
||||||
export class S5Error extends Error {
|
export class S5Error extends Error {
|
||||||
public statusCode: number;
|
public statusCode: number;
|
||||||
|
|
||||||
constructor(message: string, statusCode: number) {
|
constructor(message: string, statusCode: number) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = "S5Error";
|
this.name = "S5Error";
|
||||||
this.statusCode = statusCode;
|
this.statusCode = statusCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,7 +18,6 @@ export type CustomClientOptions = {
|
||||||
customCookie?: string;
|
customCookie?: string;
|
||||||
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
||||||
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
||||||
httpConfig?: AxiosRequestConfig;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export function optionsToConfig(
|
export function optionsToConfig(
|
||||||
|
@ -30,7 +29,7 @@ export function optionsToConfig(
|
||||||
| CustomRegistryOptions
|
| CustomRegistryOptions
|
||||||
)[]
|
)[]
|
||||||
): AxiosRequestConfig {
|
): AxiosRequestConfig {
|
||||||
let config: AxiosRequestConfig = {};
|
const config: AxiosRequestConfig = {};
|
||||||
|
|
||||||
config.baseURL = client.portalUrl;
|
config.baseURL = client.portalUrl;
|
||||||
|
|
||||||
|
@ -76,12 +75,5 @@ export function optionsToConfig(
|
||||||
|
|
||||||
config.headers = headers;
|
config.headers = headers;
|
||||||
|
|
||||||
if (finalOptions?.httpConfig) {
|
|
||||||
config = {
|
|
||||||
...config,
|
|
||||||
...finalOptions.httpConfig,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue