完成管理员user-list

This commit is contained in:
2025-05-12 10:27:50 +08:00
parent ecc6307266
commit 53a0f4456b
16 changed files with 648 additions and 41 deletions

View File

@@ -4,7 +4,18 @@ export interface StanderResponse<T> {
data?: T;
}
const fetcher = async<T>(url: string, options?: RequestInit): Promise<StanderResponse<T>> => {
export class ApiError extends Error {
constructor(
public statusCode: number,
message: string,
public data?: unknown,
) {
super(message);
this.name = 'ApiError';
}
}
const fetcher = async<T>(url: string, options?: RequestInit): Promise<T> => {
const res = await fetch(url, {
method: 'GET',
headers: {
@@ -17,11 +28,12 @@ const fetcher = async<T>(url: string, options?: RequestInit): Promise<StanderRes
...options,
});
if (!res.ok) {
return await res.json();
const result = await res.json();
if (result.statusCode !== 200) {
throw new ApiError(result.statusCode, result.message, result.data);
}
return await res.json();
return result.data as T;
}
export default fetcher