Refactor API requests and update imports

This commit is contained in:
wangqiao
2025-08-18 22:15:55 +08:00
parent ba5f777ed0
commit aac4dec3fd
17 changed files with 113 additions and 193 deletions

View File

@ -35,14 +35,14 @@ const useClientRequest = async <T = unknown>(
// 明确转换返回类型
const response = await $fetch(url, { ...defaultOptions, ...opts });
return response as unknown as IResponse<T>;
return response as unknown as T;
};
// GET请求
export const get = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useClientRequest<T>(endpoint, { ...config, method: 'GET' })
}
@ -51,7 +51,7 @@ const useClientRequest = async <T = unknown>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useClientRequest<T>(endpoint, { ...config, method: 'POST', body })
}
@ -60,7 +60,7 @@ const useClientRequest = async <T = unknown>(
export const del = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useClientRequest<T>(endpoint, { ...config, method: 'DELETE' })
}
@ -69,6 +69,6 @@ const useClientRequest = async <T = unknown>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useClientRequest<T>(endpoint, { ...config, method: 'PUT', body })
}

View File

@ -35,14 +35,14 @@ const useServerRequest = async <T>(
// return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
// 明确转换返回类型
const response = await useFetch<T>(url, { ...defaultOptions, ...opts } as any);
return response as unknown as IResponse<T>;
return response as unknown as T;
};
// GET请求
export const get = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'GET' })
}
@ -51,7 +51,7 @@ const useServerRequest = async <T>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'POST', body })
}
@ -60,7 +60,7 @@ const useServerRequest = async <T>(
export const del = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'DELETE' })
}
@ -69,6 +69,6 @@ const useServerRequest = async <T>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<IResponse<T>> => {
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'PUT', body })
}