decompose structure

This commit is contained in:
Первов Артем
2026-06-20 00:51:18 +03:00
parent dbda8ee613
commit 31add10e56
77 changed files with 4112 additions and 3974 deletions

30
src/shared/api/http.ts Normal file
View File

@@ -0,0 +1,30 @@
export type QueryParamValue = string | number | string[] | undefined;
/** Выполняет GET-запрос и добавляет query-параметры в едином формате для всех API. */
export async function getJson<T>(
baseUrl: string,
path: string,
params?: Record<string, QueryParamValue>,
init?: RequestInit,
): Promise<T> {
const url = new URL(path, baseUrl);
Object.entries(params ?? {}).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((item) => url.searchParams.append(key, item));
return;
}
if (value !== undefined && value !== '') {
url.searchParams.set(key, String(value));
}
});
const response = await fetch(url, init);
if (!response.ok) {
const message = await response.text();
throw new Error(message || `${response.status} ${response.statusText}`);
}
return response.json() as Promise<T>;
}