refactor(http): 统一请求函数返回类型并增强useRequest兼容性

修改http和vue-query模块的请求函数返回类型为HttpRequestResult<T>
优化useRequest钩子以支持同步和异步请求函数
在示例组件中更新请求调用方式
This commit is contained in:
feige996
2025-09-23 16:04:49 +08:00
parent 5b78b0ef63
commit 7887b8600c
4 changed files with 32 additions and 23 deletions

View File

@@ -1,5 +1,7 @@
<script lang="ts" setup>
import type { UserItem } from '@/service'
import { ref } from 'vue'
import useRequest from '@/hooks/useRequest'
import { infoUsingGet } from '@/service/info'
const loading = ref(false)
@@ -9,7 +11,9 @@ const data = ref<UserItem>()
async function getUserInfo() {
try {
loading.value = true
const res = await (await infoUsingGet({})).promise
// 直接使用openapi生成的请求需要解构获取promise
const { promise } = await infoUsingGet({})
const res = await promise
console.log(res)
data.value = res
error.value = null
@@ -22,7 +26,9 @@ async function getUserInfo() {
loading.value = false
}
}
const { data: data2, loading: loading2, run } = useRequest(() => infoUsingGet({}), {
// 使用openapi + useRequest生成的请求
const { data: data2, loading: loading2, run } = useRequest<UserItem>(() => infoUsingGet({}), {
immediate: false,
})
</script>