import type { Ref, WatchSource } from 'vue'; import type { NuxtAppCompat } from '@nuxt/bridge-schema'; import type { NuxtError } from './error'; export type _Transform = (input: Input) => Output | Promise; export type PickFrom> = T extends Array ? T : T extends Record ? Pick : T; export type KeysOf = Array; export type KeyOfRes = KeysOf>; export type MultiWatchSources = (WatchSource | object)[]; export type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'; export interface AsyncDataOptions = KeysOf, DefaultT = null> { /** * Whether to fetch on the server side. * @default true */ server?: boolean; /** * Whether to resolve the async function after loading the route, instead of blocking client-side navigation * @default false */ lazy?: boolean; /** * a factory function to set the default value of the data, before the async function resolves - useful with the `lazy: true` or `immediate: false` options */ default?: () => DefaultT | Ref; /** * Provide a function which returns cached data. * A `undefined` return value will trigger a fetch. * Default is `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]` which only caches data when payloadExtraction is enabled. */ getCachedData?: (key: string, nuxtApp: NuxtAppCompat) => NoInfer | undefined; /** * A function that can be used to alter handler function result after resolving */ transform?: _Transform; /** * Only pick specified keys in this array from the handler function result */ pick?: PickKeys; /** * Watch reactive sources to auto-refresh when changed */ watch?: MultiWatchSources; /** * When set to false, will prevent the request from firing immediately * @default true */ immediate?: boolean; /** * Return data in a deep ref object (it is true by default). It can be set to false to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive. */ deep?: boolean; /** * Avoid fetching the same key more than once at a time * @default 'cancel' */ dedupe?: 'cancel' | 'defer'; } export interface AsyncDataExecuteOptions { _initial?: boolean; /** * Force a refresh, even if there is already a pending request. Previous requests will * not be cancelled, but their result will not affect the data/pending state - and any * previously awaited promises will not resolve until this new request resolves. * * Instead of using `boolean` values, use `cancel` for `true` and `defer` for `false`. * Boolean values will be removed in a future release. */ dedupe?: boolean | 'cancel' | 'defer'; } export interface _AsyncData { data: Ref; /** * @deprecated Use `status` instead. This may be removed in a future major version. */ pending: Ref; refresh: (opts?: AsyncDataExecuteOptions) => Promise; execute: (opts?: AsyncDataExecuteOptions) => Promise; clear: () => void; error: Ref; status: Ref; } export type AsyncData = _AsyncData & Promise<_AsyncData>; export declare function useAsyncData = KeysOf, DefaultT = null>(handler: (ctx?: NuxtAppCompat) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useAsyncData = KeysOf, DefaultT = DataT>(handler: (ctx?: NuxtAppCompat) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useAsyncData = KeysOf, DefaultT = null>(key: string, handler: (ctx?: NuxtAppCompat) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useAsyncData = KeysOf, DefaultT = DataT>(key: string, handler: (ctx?: NuxtAppCompat) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useLazyAsyncData = KeysOf, DefaultT = null>(handler: (ctx?: NuxtAppCompat) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useLazyAsyncData = KeysOf, DefaultT = DataT>(handler: (ctx?: NuxtAppCompat) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useLazyAsyncData = KeysOf, DefaultT = null>(key: string, handler: (ctx?: NuxtAppCompat) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function useLazyAsyncData = KeysOf, DefaultT = DataT>(key: string, handler: (ctx?: NuxtAppCompat) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | null>; export declare function refreshNuxtData(keys?: string | string[]): Promise; export declare function clearNuxtData(keys?: string | string[] | ((key: string) => boolean)): void;