fix(api): 修改接口支持异步函数和普通函数,支持返回Promise,增强请求处理

This commit is contained in:
liaochunxin
2025-09-23 15:36:33 +08:00
parent 5b1c001a52
commit bc6dcdde93
2 changed files with 30 additions and 6 deletions

View File

@@ -21,8 +21,8 @@ export interface IFooItem {
}
/** GET 请求 */
export function getFooAPI(name: string) {
return http.get<IFooItem>('/foo', { name })
export async function getFooAPI(name: string) {
return await http.get<IFooItem>('/foo', { name })
}
/** GET 请求;支持 传递 header 的范例 */
export function getFooAPI2(name: string) {

View File

@@ -26,7 +26,7 @@ interface IUseRequestReturn<T, P = undefined> {
* @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。
*/
export default function useRequest<T, P = undefined>(
func: (args?: P) => HttpRequestResult<T>,
func: (args?: P) => Promise<T> | Promise<HttpRequestResult<T>> | HttpRequestResult<T> | T,
options: IUseRequestOptions<T> = { immediate: false },
): IUseRequestReturn<T, P> {
const loading = ref(false)
@@ -36,12 +36,36 @@ export default function useRequest<T, P = undefined>(
const run = async (args?: P) => {
loading.value = true
const { promise, requestTask: task } = func(args)
requestTask = task // Store the requestTask
error.value = false // Move error reset to the beginning
let promise: Promise<T | undefined>
const result = func(args)
if (result instanceof Promise) {
// If func returns a Promise
promise = result.then((res) => {
if (res && typeof (res as HttpRequestResult<T>).promise === 'object' && typeof (res as HttpRequestResult<T>).requestTask === 'object') {
// If the resolved value is HttpRequestResult
const { promise: p, requestTask: task } = res as HttpRequestResult<T>
requestTask = task
return p
}
return res as T | undefined
}) as Promise<T | undefined> // Cast to ensure correct type
}
else if (result && typeof (result as HttpRequestResult<T>).promise === 'object' && typeof (result as HttpRequestResult<T>).requestTask === 'object') {
// If func returns HttpRequestResult directly
const { promise: p, requestTask: task } = result as HttpRequestResult<T>
requestTask = task
promise = p
}
else {
// If func returns T directly
promise = Promise.resolve(result as T | undefined)
}
return promise
.then((res) => {
data.value = res
error.value = false
return data.value
})
.catch((err) => {