Refactor API requests and update component structure

This commit is contained in:
wangqiao
2025-08-20 22:58:08 +08:00
parent 1ef219878c
commit 2fe36051f8
8 changed files with 123 additions and 109 deletions

View File

@ -35,22 +35,15 @@ const useServerRequest = async <T>(
// return 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;
return response.data.value as unknown as T;
};
// GET请求
export const get = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'GET' })
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'GET' })
}
// POST请求
@ -58,15 +51,8 @@ const useServerRequest = async <T>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'POST', body })
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'POST', body })
}
@ -74,15 +60,8 @@ const useServerRequest = async <T>(
export const del = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'DELETE' })
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'DELETE' })
}
// PUT请求
@ -90,13 +69,6 @@ const useServerRequest = async <T>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<{
data: T;
pending: boolean;
error: any;
refresh: () => Promise<void>;
execute: () => Promise<void>;
status: number;
}> => {
return useServerRequest(endpoint, { ...config, method: 'PUT', body })
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'PUT', body })
}