UNPKG

@uni-use/request

Version:

An easy to use axios based http client combinatorial api with support for return sequentially

105 lines (96 loc) 3.03 kB
import type { AxiosInstance } from 'axios'; import type { AxiosResponse } from 'axios'; import type { CancelToken } from 'axios'; import type { ComputedRef } from 'vue-demi'; import type { InternalAxiosRequestConfig } from 'axios'; import type { Ref } from 'vue-demi'; /** * Void function */ export declare type Fn = () => void; /** * Maybe it's a ref, or a plain value * * ```ts * type MaybeRef<T> = T | Ref<T> * ``` */ export declare type MaybeRef<T> = T | Ref<T>; /** * Maybe it's a ref, or a plain value, or a getter function * * ```ts * type MaybeRefOrGetter<T> = (() => T) | T | Ref<T> | ComputedRef<T> * ``` */ export declare type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T); declare function useRequest<T = any>(url: string, config: InternalAxiosRequestConfig<T> & UseRequestConfig, options?: UseRequestConfig): UseRequestReturn<T> & PromiseLike<UseRequestReturn<T>>; export default useRequest; export declare interface UseRequestConfig { unique?: boolean; orderly?: boolean; cancelToken?: CancelToken; signal?: AbortSignal; type?: string; error?: boolean; refetch?: boolean; immediate?: boolean; setHeaders?(instance: AxiosInstance): void; beforeRequest?(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>; afterRequest?(res: AxiosResponse<any>): AxiosResponse<any> | Promise<AxiosResponse<any>>; updateDataOnError?: boolean; onRequestError?(error: any): void; onResponseError?(error: any): void; onError?(error: any): void; onCancel?(error: any): void; } export declare interface UseRequestRequestOptions<D = any> extends InternalAxiosRequestConfig<D> { unique?: boolean; orderly?: boolean; cancelToken?: CancelToken; signal?: AbortSignal; type?: string; error?: boolean; } export declare interface UseRequestReturn<T> { /** * Indicates if the fetch request has finished */ isFinished: Ref<boolean>; /** * The statusCode of the HTTP fetch response */ statusCode: Ref<number | null>; statusText: Ref<string>; /** * The raw response of the fetch response */ response: Ref<AxiosResponse<any> | undefined>; /** * Any fetch errors that may have occurred */ error: Ref<any>; /** * The fetch response body on success, may either be JSON or text */ data: Ref<T | null>; /** * Indicates if the request is currently being fetched. */ isFetching: Ref<boolean>; /** * Indicates if the fetch request is able to be aborted */ canAbort?: ComputedRef<boolean>; /** * Indicates if the fetch request was aborted */ /** * Abort the fetch request */ /** * request main function */ request: <T = any, R = AxiosResponse<T>, D = any>(config: UseRequestRequestOptions<D>) => Promise<R>; } export { }