Refactor API requests and update component structure

This commit is contained in:
wangqiao
2025-08-19 22:19:08 +08:00
parent aac4dec3fd
commit 1ef219878c
12 changed files with 329 additions and 270 deletions

View File

@ -44,6 +44,7 @@ const useClientRequest = async <T = unknown>(
config?: Omit<FetchOptions, 'method'>
): Promise<T> => {
return useClientRequest<T>(endpoint, { ...config, method: 'GET' })
}
// POST请求
@ -71,4 +72,4 @@ const useClientRequest = async <T = unknown>(
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<T> => {
return useClientRequest<T>(endpoint, { ...config, method: 'PUT', body })
}
}

View File

@ -34,7 +34,7 @@ const useServerRequest = async <T>(
};
// return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
// 明确转换返回类型
const response = await useFetch<T>(url, { ...defaultOptions, ...opts } as any);
const response = await useFetch<T>(url, { ...defaultOptions, ...opts, key: 'unique-key-' + Date.now(), } as any);
return response as unknown as T;
};
@ -42,8 +42,15 @@ const useServerRequest = async <T>(
export const get = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'GET' })
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'GET' })
}
// POST请求
@ -51,8 +58,15 @@ const useServerRequest = async <T>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'POST', body })
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'POST', body })
}
@ -60,8 +74,15 @@ const useServerRequest = async <T>(
export const del = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'DELETE' })
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'DELETE' })
}
// PUT请求
@ -69,6 +90,13 @@ const useServerRequest = async <T>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'PUT', body })
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'PUT', body })
}