Refactor API requests and update component imports

This commit is contained in:
wangqiao
2025-08-18 14:28:10 +08:00
parent 07b4d3de99
commit 9ae3abeded
91 changed files with 669 additions and 884 deletions

View File

@ -3,7 +3,7 @@ import { isArray } from "~/utils/utils";
type FetchType = typeof $fetch;
export type FetchOptions = Parameters<FetchType>[1];
export const useClientRequest = <T = unknown>(
const useClientRequest = async <T = unknown>(
url: string,
opts?: FetchOptions
) => {
@ -33,5 +33,42 @@ export const useClientRequest = <T = unknown>(
},
};
return $fetch<T>(url, { ...defaultOptions, ...opts });
// 明确转换返回类型
const response = await $fetch(url, { ...defaultOptions, ...opts });
return response as unknown as IResponse<T>;
};
// GET请求
export const get = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<IResponse<T>> => {
return useClientRequest<T>(endpoint, { ...config, method: 'GET' })
}
// POST请求
export const post = <T = unknown>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<IResponse<T>> => {
return useClientRequest<T>(endpoint, { ...config, method: 'POST', body })
}
// DELETE请求
export const del = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<IResponse<T>> => {
return useClientRequest<T>(endpoint, { ...config, method: 'DELETE' })
}
// PUT请求
export const put = <T = unknown>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<IResponse<T>> => {
return useClientRequest<T>(endpoint, { ...config, method: 'PUT', body })
}