scaffold react dashboard

This commit is contained in:
2025-04-12 11:16:46 +00:00
parent c8b31bedda
commit a6ced69393
14 changed files with 4635 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
const API_BASE_URL = import.meta.env.VITE_API_URL || '';
export class ApiError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.name = 'ApiError';
this.status = status;
}
}
export async function apiRequest<T>(
endpoint: string,
options: RequestInit = {},
): Promise<T> {
const url = `${API_BASE_URL}${endpoint}`;
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
if (!response.ok) {
const message = await response.text().catch(() => 'Request failed');
throw new ApiError(response.status, message);
}
return response.json();
}
export function buildQueryString(
params: Record<string, string | number | boolean | undefined>,
): string {
const filtered = Object.entries(params).filter(
([, value]) => value !== undefined,
);
if (filtered.length === 0) return '';
return '?' + new URLSearchParams(
filtered.map(([key, value]) => [key, String(value)]),
).toString();
}