Refactor API requests and update component structure
This commit is contained in:
@ -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 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user