diff --git a/src/axios.ts b/src/axios.ts index a873002..da357ea 100644 --- a/src/axios.ts +++ b/src/axios.ts @@ -1,41 +1,46 @@ -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 extends Promise { - cancel: () => void; + cancel: () => void; } export const customInstance = ( - config: AxiosRequestConfig, - options?: AxiosRequestConfig, + config: AxiosRequestConfig, + options?: AxiosRequestConfig, ): CancelablePromise => { - const source = Axios.CancelToken.source(); + const source = Axios.CancelToken.source(); - /* + /* Hack to ensure that the data is passed to the request as an option. */ - if (options?.data) { - config = config || {}; - config.data = options.data; - delete config.data; - } + if (options?.data) { + config = config || {}; + config.data = options.data; + delete config.data; + } - const instance = Axios.create({baseURL: options?.baseURL}); - const promise = instance({ - ...config, - ...options, - cancelToken: source.token, - }).then(({data}) => data).catch((error) => { - if (Axios.isCancel(error)) { - return; - } - throw new S5Error((error as AxiosError).message, (error as AxiosError).response?.status as number); - }) + const instance = Axios.create({ baseURL: options?.baseURL }); + const promise = instance({ + ...config, + ...options, + cancelToken: source.token, + }) + .then(({ data }) => data) + .catch((error) => { + if (Axios.isCancel(error)) { + return; + } + throw new S5Error( + (error as AxiosError).message, + (error as AxiosError).response?.status as number, + ); + }); - // @ts-ignore - promise.cancel = () => { - source.cancel("Query was cancelled"); - }; + // @ts-ignore + promise.cancel = () => { + source.cancel("Query was cancelled"); + }; - return promise as CancelablePromise; + return promise as CancelablePromise; }; diff --git a/src/client.ts b/src/client.ts index de40043..ce9b8db 100644 --- a/src/client.ts +++ b/src/client.ts @@ -71,13 +71,13 @@ import { blake3 } from "@noble/hashes/blake3"; import { base64urlDecode, base64urlEncode } from "./utils/encoding.js"; export class S5Error extends Error { - public statusCode: number; + public statusCode: number; - constructor(message: string, statusCode: number) { - super(message); - this.name = "S5Error"; - this.statusCode = statusCode; - } + constructor(message: string, statusCode: number) { + super(message); + this.name = "S5Error"; + this.statusCode = statusCode; + } } /** diff --git a/src/utils/options.ts b/src/utils/options.ts index cf90fb8..469058f 100644 --- a/src/utils/options.ts +++ b/src/utils/options.ts @@ -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; }