xiaozhi-esp32-server/main/manager-mobile/src/hooks/useRequest.ts
lapy 2be70e8e6c feat(manager-mobile): westernize H5 app, copy, and shared crypto
- English locale defaults; remove duplicate zh-only locale files

- SM2 utility sharing with the web client where applicable
2026-04-25 17:05:15 +01:00

51 lines
1.6 KiB
TypeScript

import type { Ref } from 'vue'
interface IUseRequestOptions<T> {
/** Whether to execute immediately */
immediate?: boolean
/** Initial data */
initialData?: T
}
interface IUseRequestReturn<T> {
loading: Ref<boolean>
error: Ref<boolean | Error>
data: Ref<T | undefined>
run: () => Promise<T | undefined>
}
/**
* useRequest is a customized request hook for handling async requests and responses.
* @param func A function that executes async requests, returns a Promise containing response data.
* @param options Object containing request options {immediate, initialData}.
* @param options.immediate Whether to execute request immediately, defaults to false.
* @param options.initialData Initial data, defaults to undefined.
* @returns Returns an object {loading, error, data, run}, containing request loading status, error info, response data and manual trigger function.
*/
export default function useRequest<T>(
func: () => Promise<IResData<T>>,
options: IUseRequestOptions<T> = { immediate: false },
): IUseRequestReturn<T> {
const loading = ref(false)
const error = ref(false)
const data = ref<T | undefined>(options.initialData) as Ref<T | undefined>
const run = async () => {
loading.value = true
return func()
.then((res) => {
data.value = res.data
error.value = false
return data.value
})
.catch((err) => {
error.value = err
throw err
})
.finally(() => {
loading.value = false
})
}
options.immediate && run()
return { loading, error, data, run }
}