refactor: revamp for older report logic with benefits of newer one
This commit is contained in:
@@ -3,6 +3,24 @@ import axios, { AxiosInstance } from "axios";
|
||||
let apiClient: AxiosInstance | null = null;
|
||||
let _onUnauthorized: (() => void) | undefined;
|
||||
|
||||
function serializeParams(params: Record<string, any>): string {
|
||||
const searchParams = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(params ?? {})) {
|
||||
if (value === undefined || value === null) continue;
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) searchParams.append(key, String(item));
|
||||
} else if (typeof value === "object") {
|
||||
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
||||
if (nestedValue === undefined || nestedValue === null) continue;
|
||||
searchParams.append(`${key}[${nestedKey}]`, String(nestedValue));
|
||||
}
|
||||
} else {
|
||||
searchParams.append(key, String(value));
|
||||
}
|
||||
}
|
||||
return searchParams.toString();
|
||||
}
|
||||
|
||||
export function initApi(baseUrl: string, getToken?: () => string | null, onUnauthorized?: () => void): AxiosInstance {
|
||||
if (apiClient && apiClient.defaults.baseURL === baseUrl) {
|
||||
_onUnauthorized = onUnauthorized;
|
||||
@@ -14,6 +32,7 @@ export function initApi(baseUrl: string, getToken?: () => string | null, onUnaut
|
||||
apiClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
paramsSerializer: serializeParams,
|
||||
});
|
||||
|
||||
apiClient.interceptors.request.use((config) => {
|
||||
|
||||
Reference in New Issue
Block a user