refactor: rewrite header handling, lowercase apiKey, and cleanup

This commit is contained in:
Derrick Hammer 2024-03-18 09:06:52 -04:00
parent ba370250a2
commit 7bc48484a2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 49 additions and 54 deletions

View File

@ -88,7 +88,7 @@ export async function getCidUrl(
): Promise<string> { ): Promise<string> {
const opt = { ...this.customOptions, customOptions }; const opt = { ...this.customOptions, customOptions };
return addUrlQuery(path.join(this.portalUrl, cid), { return addUrlQuery(path.join(this.portalUrl, cid), {
auth_token: opt.ApiKey, auth_token: opt.apiKey,
}); });
} }

View File

@ -1,10 +1,10 @@
import { AxiosProgressEvent, AxiosRequestConfig } from "axios"; import {AxiosHeaders, AxiosProgressEvent, AxiosRequestConfig} from "axios";
import { S5Client } from "../client.js"; import {S5Client} from "../client.js";
/** /**
* Custom client options. * Custom client options.
* *
* @property [ApiKey] - Authentication password to use for a single S5 node/portal. * @property [apiKey] - Authentication password to use for a single S5 node/portal.
* @property [customUserAgent] - Custom user agent header to set. * @property [customUserAgent] - Custom user agent header to set.
* @property [customCookie] - Custom cookie header to set. WARNING: the Cookie header cannot be set in browsers. This is meant for usage in server contexts. * @property [customCookie] - Custom cookie header to set. WARNING: the Cookie header cannot be set in browsers. This is meant for usage in server contexts.
* @property [onDownloadProgress] - Optional callback to track download progress. * @property [onDownloadProgress] - Optional callback to track download progress.
@ -12,69 +12,64 @@ import { S5Client } from "../client.js";
*/ */
export type CustomClientOptions = { export type CustomClientOptions = {
ApiKey?: string; apiKey?: string;
customUserAgent?: string; customUserAgent?: string;
customCookie?: string; customCookie?: string;
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void; onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void; onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
}; };
export function optionsToConfig( export function optionsToConfig(
client: S5Client, client: S5Client,
def: CustomClientOptions, def: CustomClientOptions,
...options: CustomClientOptions[] ...options: CustomClientOptions[]
): AxiosRequestConfig { ): AxiosRequestConfig {
const config: AxiosRequestConfig = {}; const config: AxiosRequestConfig = {};
config.baseURL = client.portalUrl; config.baseURL = client.portalUrl;
const extraOptions = options.reduce((acc, val) => { const extraOptions = options.reduce((acc, val) => {
return { return {
...acc, ...acc,
...val, ...val,
}; };
}, options); }, options);
const finalOptions = { const finalOptions = {
...def, ...def,
...client.customOptions, ...client.customOptions,
...extraOptions, ...extraOptions,
}; } as CustomClientOptions;
if (finalOptions?.onDownloadProgress) { if (finalOptions?.onDownloadProgress) {
config.onDownloadProgress = finalOptions?.onDownloadProgress; config.onDownloadProgress = finalOptions?.onDownloadProgress;
} }
if (finalOptions?.onUploadProgress) { if (finalOptions?.onUploadProgress) {
config.onUploadProgress = finalOptions?.onUploadProgress; config.onUploadProgress = finalOptions?.onUploadProgress;
} }
if (finalOptions?.customCookie) { const headers = new AxiosHeaders(config.headers as AxiosHeaders)
config.headers = {
Cookie: finalOptions?.customCookie,
};
}
if (finalOptions?.customUserAgent) { if (finalOptions?.customCookie) {
config.headers = { headers.set("Cookie", finalOptions.customCookie);
...config.headers,
"User-Agent": finalOptions?.customUserAgent,
};
}
if (finalOptions?.ApiKey) { }
config.headers = { if (finalOptions?.customUserAgent) {
...config.headers, headers.set("User-Agent", finalOptions.customUserAgent);
Authorization: `Bearer ${finalOptions?.ApiKey}`, }
};
config.withCredentials = true; if (finalOptions?.apiKey) {
headers.set("Authorization", `Bearer ${finalOptions.apiKey}`);
config.withCredentials = true;
config.params = { config.params = {
...config.params, ...config.params,
auth_token: finalOptions?.ApiKey, auth_token: finalOptions?.apiKey,
}; };
} }
return config; config.headers = headers;
return config;
} }