Refactor API requests and update component imports
This commit is contained in:
@ -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 })
|
||||
}
|
||||
@ -2,7 +2,7 @@ import { useFetch } from "#app";
|
||||
import type { UseFetchOptions } from "#app";
|
||||
import { isArray } from "~/utils/utils";
|
||||
|
||||
export const useServerRequest = <T>(
|
||||
const useServerRequest = async <T>(
|
||||
url: string,
|
||||
opts?: UseFetchOptions<T, unknown>
|
||||
) => {
|
||||
@ -32,6 +32,43 @@ export const useServerRequest = <T>(
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
||||
// 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>;
|
||||
};
|
||||
|
||||
// GET请求
|
||||
export const get = <T = unknown>(
|
||||
endpoint: string,
|
||||
config?: Omit<FetchOptions, 'method'>
|
||||
): Promise<IResponse<T>> => {
|
||||
return useServerRequest<T>(endpoint, { ...config, method: 'GET' })
|
||||
}
|
||||
|
||||
// POST请求
|
||||
export const post = <T = unknown>(
|
||||
endpoint: string,
|
||||
body?: any,
|
||||
config?: Omit<FetchOptions, 'method' | 'body'>
|
||||
): Promise<IResponse<T>> => {
|
||||
return useServerRequest<T>(endpoint, { ...config, method: 'POST', body })
|
||||
}
|
||||
|
||||
|
||||
// DELETE请求
|
||||
export const del = <T = unknown>(
|
||||
endpoint: string,
|
||||
config?: Omit<FetchOptions, 'method'>
|
||||
): Promise<IResponse<T>> => {
|
||||
return useServerRequest<T>(endpoint, { ...config, method: 'DELETE' })
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
export const put = <T = unknown>(
|
||||
endpoint: string,
|
||||
body?: any,
|
||||
config?: Omit<FetchOptions, 'method' | 'body'>
|
||||
): Promise<IResponse<T>> => {
|
||||
return useServerRequest<T>(endpoint, { ...config, method: 'PUT', body })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user