Preview
useFetch
Type
typescriptdeclare function useFetch<T>( url: MaybeRefOrGetter<string>, fetchOptions?: UseFetchOptions, ): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>;
Details
useFetch
requester
UseFetchOptions
interface UseFetchOptions extends RequestInit {
/**
* Initial data before the request finished
*
* @default null
*/
initialData?: any;
/**
* Will automatically run fetch when `useFetch` is used
*
* @default true
*/
immediate?: boolean;
/**
* execute fetch only when condition is true
* @default true
*/
condition?: MaybeRefOrGetter<boolean>;
/**
* Will automatically refetch when:
* - the URL is changed if the URL is a ref
* - the payload is changed if the payload is a ref
*
* @default false
*/
refetch?: boolean;
/**
* Auto refetch interval in millisecond
* @default undefined
*/
refresh?: number;
/**
* Allow cache the request result and reuse it if cacheResolve result is same
*
* @default undefined
*/
cacheSetting?: {
expiration?: number;
cacheResolve?: (config: InternalConfig & { url: string }) => string;
};
/**
* Debounce interval in millisecond
*
* @default undefined
*/
debounce?:
| number
| {
wait: number;
options?: {
leading?: boolean;
maxWait?: number;
trailing?: boolean;
};
};
/**
* Throttle interval in millisecond
*
* @default undefined
*/
throttle?:
| number
| {
wait: number;
options?: {
leading?: boolean;
trailing?: boolean;
};
};
/**
* Indicates if the fetch request has finished
*/
isFinished?: boolean;
/**
* Timeout for abort request after number of millisecond
* `0` means use browser default
*
* @default undefined
*/
timeout?: number;
/**
* Fetch function
*/
fetch?: typeof window.fetch;
/**
* Allow update the `data` ref when fetch error whenever provided, or mutated in the `onFetchError` callback
*
* @default false
*/
updateDataOnError?: boolean;
/**
* Will run immediately before the fetch request is dispatched
*/
beforeFetch?: (
ctx: BeforeFetchContext,
) =>
| Promise<Partial<BeforeFetchContext> | void>
| Partial<BeforeFetchContext>
| void;
/**
* Will run immediately after the fetch request is returned.
* Runs after any 2xx response
*/
afterFetch?: (
ctx: AfterFetchContext,
) => Promise<Partial<AfterFetchContext>> | Partial<AfterFetchContext>;
/**
* Will run immediately after the fetch request is returned.
* Runs after any 4xx and 5xx response
*/
onFetchError?: (ctx: {
data: any;
response: Response | null;
error: any;
}) => Promise<Partial<OnFetchErrorContext>> | Partial<OnFetchErrorContext>;
}
initialData
Type
typescriptany;
Details
Initial data before the request is completed
Default:
null
immediate
Type
typescriptboolean;
Details
Whether to automatically execute the request when
useFetch
is usedDefault:
true
condition
Type
typescriptMaybeRefOrGetter<boolean>;
Details
Execute request only when condition is true
Default:
true
refetch
Type
typescriptboolean;
Details
Whether to automatically refetch when:
- URL is a ref and changes
- payload is a ref and changes
Default:
false
refresh
Type
typescriptnumber;
Details
Auto refresh interval in milliseconds
retry
Type
typescriptnumber;
Details
Number of automatic retry attempts on error
cacheSetting
Type
typescriptinterface InternalConfig { method: HttpMethod; type: DataType; payload: unknown; payloadType?: string; } interface CacheSetting { expiration?: number; cacheResolve?: (config: InternalConfig & { url: string }) => string; }
Details
Request result cache settings
Default:
{}
debounce
Type
typescripttype Debounce = | number | { wait: number; options?: { leading?: boolean; maxWait?: number; trailing?: boolean; }; };
Details
Request debounce interval in milliseconds. When an object is used, the usage and effect are the same as lodash.debounce
throttle
Type
typescripttype Throttle = | number | { wait: number; options?: { leading?: boolean; trailing?: boolean; }; };
Details
Request throttle interval in milliseconds. When an object is used, the usage and effect are the same as lodash.throttle
isFinished
Type
typescriptReadonly<Ref<boolean>>;
Details
Indicates if the fetch request has finished
timeout
Type
typescriptnumber;
Details
Request timeout in milliseconds,
0
means use browser default
fetch
Type
typescripttypeof window.fetch;
Details
Custom fetch function implementation
updateDataOnError
Type
typescriptboolean;
Details
Whether to allow updating the
data
ref when a fetch error occursDefault:
false
beforeFetch
Type
typescriptinterface BeforeFetchContext { url: string; options: UseFetchOptions; cancel: Fn; } type beforeFetch = ( ctx: BeforeFetchContext, ) => | Promise<Partial<BeforeFetchContext> | void> | Partial<BeforeFetchContext> | void;
Details
Hook function before the request is sent
afterFetch
Type
typescript(ctx: AfterFetchContext) => Promise<Partial<AfterFetchContext>> | Partial<AfterFetchContext>;
Details
Hook function after successful request (2xx response)
onFetchError
Type
typescript(ctx: { data: any; response: Response | null; error: any }) => Promise<Partial<OnFetchErrorContext>> | Partial<OnFetchErrorContext>;
Details
Hook function after failed request (4xx, 5xx response)
requestInit
Type
typescriptinterface RequestInit { /** A BodyInit object or null to set request's body. */ body?: BodyInit | null; /** A string indicating how the request will interact with the browser's cache to set request's cache. */ cache?: RequestCache; /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */ credentials?: RequestCredentials; /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ headers?: HeadersInit; /** A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ integrity?: string; /** A boolean to set request's keepalive. */ keepalive?: boolean; /** A string to set request's method. */ method?: string; /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */ mode?: RequestMode; priority?: RequestPriority; /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ redirect?: RequestRedirect; /** A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. */ referrer?: string; /** A referrer policy to set request's referrerPolicy. */ referrerPolicy?: ReferrerPolicy; /** An AbortSignal to set request's signal. */ signal?: AbortSignal | null; /** Can only be null. Used to disassociate request from any Window. */ window?: null; }
Details
UseFetchOptions
inherits all properties fromRequestInit
, allowing you to set anyRequestInit
property
UseFetchReturn
- Type
interface UseFetchReturn<T> {
/**
* Indicates if the fetch request has finished
*/
isFinished: Readonly<Ref<boolean>>;
/**
* The statusCode of the HTTP fetch response
*/
statusCode: Ref<number | null>;
/**
* The raw response of the fetch response
*/
response: Ref<Response | null>;
/**
* 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.
*/
loading: Readonly<Ref<boolean>>;
/**
* Indicates if the fetch request is able to be aborted
*/
canAbort: ComputedRef<boolean>;
/**
* Indicates if the fetch request was aborted
*/
aborted: Ref<boolean>;
/**
* promise stream
*/
promise$: Readonly<Subjection>;
/**
* Abort the fetch request
*/
abort: Fn;
/**
* Cancel refresh request
*/
cancelRefresh: Fn;
/**
* clear current fetch cache
*/
clearCache: Fn;
/**
* Manually call the fetch
* (default not throwing error)
*/
execute: (throwOnFailed?: boolean) => Promise<any> | void;
/**
* Fires after the fetch request has finished
*/
onFetchResponse: EventHookOn<Response>;
/**
* Fires after a fetch request error
*/
onFetchError: EventHookOn;
/**
* Fires after a fetch has completed
*/
onFetchFinally: EventHookOn;
get: (payload?: MaybeRefOrGetter<unknown>) => UseFetchResult<T>;
post: (
payload?: MaybeRefOrGetter<unknown>,
type?: string,
) => UseFetchResult<T>;
put: (
payload?: MaybeRefOrGetter<unknown>,
type?: string,
) => UseFetchResult<T>;
delete: (
payload?: MaybeRefOrGetter<unknown>,
type?: string,
) => UseFetchResult<T>;
patch: (
payload?: MaybeRefOrGetter<unknown>,
type?: string,
) => UseFetchResult<T>;
head: (
payload?: MaybeRefOrGetter<unknown>,
type?: string,
) => UseFetchResult<T>;
options: (
payload?: MaybeRefOrGetter<unknown>,
type?: string,
) => UseFetchResult<T>;
json: <JSON = any>() => UseFetchReturn<JSON> &
PromiseLike<UseFetchReturn<JSON>>;
text: () => UseFetchReturn<string> & PromiseLike<UseFetchReturn<string>>;
blob: () => UseFetchReturn<Blob> & PromiseLike<UseFetchReturn<Blob>>;
arrayBuffer: () => UseFetchReturn<ArrayBuffer> &
PromiseLike<UseFetchReturn<ArrayBuffer>>;
formData: () => UseFetchReturn<FormData> &
PromiseLike<UseFetchReturn<FormData>>;
}
Details
The return type of
useFetch
, containing the following properties and methods:
data
Type
typescriptRef<T | null>;
Details The fetch response body on success, may either be JSON or text
loading
Type
typescriptReadonly<Ref<boolean>>;
Details Indicates if the request is currently being fetched
error
Type
typescriptRef<any>;
Details
Any fetch errors that may have occurred
isFinished
Type
typescriptReadonly<Ref<boolean>>;
Details
Indicates if the fetch request has finished
statusCode
Type
typescriptRef<number | null>;
Details
The statusCode of the HTTP fetch response
response
Type
typescriptRef<Response | null>;
Details
The raw fetch response object
canAbort
Type
typescriptComputedRef<boolean>;
Details
Indicates if the current request can be aborted
aborted
Type
typescriptRef<boolean>;
Details
Indicates if the request was aborted
promise$
Type
typescriptReadonly<Subjection>;
Details
fluth stream that pushes latest data to subscribers after each request
execute
Type
typescript(throwOnFailed?: boolean) => Promise<any> | void;
Details
- Manually trigger fetch request (default not throwing error)
- Returns void when
debounce
orthrottle
is set
abort
Type
typescript() => void;
Details
Abort the current fetch request
cancelRefresh
Type
typescript() => void;
Details
Cancel auto refresh requests
clearCache
Type
typescript() => void;
Details
Clear the cache for the current request
onFetchResponse
Type
typescriptEventHookOn<Response>;
Details
Callback after fetch request completes
onFetchError
Type
typescriptEventHookOn;
Details
Callback when fetch request encounters an error
onFetchFinally
Type
typescriptEventHookOn;
Details
Callback when fetch request completes (regardless of success or failure)
get
Type
typescripttype get: (payload?: MaybeRefOrGetter<unknown>) => UseFetchResult<T>;
Details
Sets fetch request method to
GET
and provides payload. The payload will be parsed into query parameters and appended to the URL. Payload can be reactive data.
post
Type
typescripttype post: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchResult<T>;
Details
- Sets fetch request method to
POST
and provides payload. The payload will be included in the request body. Payload can be reactive data. - Type can specify the
Content-Type
header, commonly set to json or text
- Sets fetch request method to
put
Type
typescripttype put: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchResult<T>;
Details
- Sets fetch request method to
PUT
and provides payload. The payload will be included in the request body. Payload can be reactive data. - Type can specify the
Content-Type
header, commonly set to json or text
- Sets fetch request method to
delete
Type
typescripttype delete: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchResult<T>;
Details
- Sets fetch request method to
DELETE
and provides payload. The payload will be included in the request body. Payload can be reactive data. - Type can specify the
Content-Type
header, commonly set to json or text
- Sets fetch request method to
patch
Type
typescripttype patch: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchResult<T>;
Details
- Sets fetch request method to
PATCH
and provides payload. The payload will be included in the request body. Payload can be reactive data. - Type can specify the
Content-Type
header, commonly set to json or text
- Sets fetch request method to
head
Type
typescripttype head: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchResult<T>;
Details
- Sets fetch request method to
HEAD
and provides payload. The payload will be included in the request body. Payload can be reactive data. - Type can specify the
Content-Type
header, commonly set to json or text
- Sets fetch request method to
options
Type
typescripttype options: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchResult<T>;
Details
- Sets fetch request method to
OPTIONS
and provides payload. The payload will be included in the request body. Payload can be reactive data. - Type can specify the
Content-Type
header, commonly set to json or text
- Sets fetch request method to
json
Type
typescripttype json: <JSON = any>() => UseFetchReturn<JSON> & PromiseLike<UseFetchReturn<JSON>>;
Details
Parse response body as JSON format
text
Type
typescripttype text: () => UseFetchReturn<string> & PromiseLike<UseFetchReturn<string>>;
Details
Parse response body as text format
blob
Type
typescripttype blob: () => UseFetchReturn<Blob> & PromiseLike<UseFetchReturn<Blob>>;
Details
Parse response body as Blob format
arrayBuffer
Type
typescripttype arrayBuffer: () => UseFetchReturn<ArrayBuffer> & PromiseLike<UseFetchReturn<ArrayBuffer>>;
Details
Parse response body as ArrayBuffer format
formData
Type
typescripttype formData: () => UseFetchReturn<FormData> & PromiseLike<UseFetchReturn<FormData>>;
Details
Parse response body as FormData format
createFetch
Type
typescriptinterface CreateFetchOptions { baseUrl?: MaybeRefOrGetter<string>; combination?: "overwrite" | "chain"; options?: UseFetchOptions; } declare function createFetch(config?: CreateFetchOptions): typeof useFetch;
Details
Create a
useFetch
with preset configuration
clearFetchCache
Type
typescriptdeclare function clearFetchCache: () => void
Details
Clear all
useFetch
caches