import type { MaybeRefOrGetter, MultiWatchSources, Ref } from 'vue'; import type { DedupeOption, DefaultAsyncDataErrorValue, DefaultAsyncDataValue } from 'nuxt/app/defaults'; import type { NuxtApp } from '../nuxt.js'; import type { NuxtError } from './error.js'; export type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'; export type _Transform = (input: Input) => Output | Promise; export type PickFrom> = T extends Array ? T : T extends Record ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick : T; export type KeysOf = Array; export type KeyOfRes = KeysOf>; export type { MultiWatchSources }; export type NoInfer = [T][T extends any ? 0 : never]; export type AsyncDataRefreshCause = 'initial' | 'refresh:hook' | 'refresh:manual' | 'watch'; export interface AsyncDataOptions = KeysOf, DefaultT = DefaultAsyncDataValue> { /** * 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. * An `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: NuxtApp, context: { cause: AsyncDataRefreshCause; }) => NoInfer | undefined; /** * A function that can be used to alter handler function result after resolving. * Do not use it along with the `pick` option. */ transform?: _Transform; /** * Only pick specified keys in this array from the handler function result. * Do not use it along with the `transform` option. */ 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 { /** * 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?: DedupeOption; cause?: AsyncDataRefreshCause; /** @internal */ cachedData?: any; } export interface _AsyncData { data: Ref; pending: Ref; refresh: (opts?: AsyncDataExecuteOptions) => Promise; execute: (opts?: AsyncDataExecuteOptions) => Promise; clear: () => void; error: Ref; status: Ref; } export type AsyncData = _AsyncData & Promise<_AsyncData>; /** * Provides access to data that resolves asynchronously in an SSR-friendly composable. * See {@link https://nuxt.com/docs/api/composables/use-async-data} * @since 3.0.0 * @param handler An asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side. * @param options customize the behavior of useAsyncData */ export declare function useAsyncData = KeysOf, DefaultT = DefaultAsyncDataValue>(handler: (ctx?: NuxtApp) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; export declare function useAsyncData = KeysOf, DefaultT = DataT>(handler: (ctx?: NuxtApp) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; /** * Provides access to data that resolves asynchronously in an SSR-friendly composable. * See {@link https://nuxt.com/docs/api/composables/use-async-data} * @param key A unique key to ensure that data fetching can be properly de-duplicated across requests. * @param handler An asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side. * @param options customize the behavior of useAsyncData */ export declare function useAsyncData = KeysOf, DefaultT = DefaultAsyncDataValue>(key: MaybeRefOrGetter, handler: (ctx?: NuxtApp) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; export declare function useAsyncData = KeysOf, DefaultT = DataT>(key: MaybeRefOrGetter, handler: (ctx?: NuxtApp) => Promise, options?: AsyncDataOptions): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; /** * Provides access to data that resolves asynchronously in an SSR-friendly composable. * See {@link https://nuxt.com/docs/api/composables/use-lazy-async-data} * @since 3.0.0 * @param handler An asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side. * @param options customize the behavior of useLazyAsyncData */ export declare function useLazyAsyncData = KeysOf, DefaultT = DefaultAsyncDataValue>(handler: (ctx?: NuxtApp) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; export declare function useLazyAsyncData = KeysOf, DefaultT = DataT>(handler: (ctx?: NuxtApp) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; /** * Provides access to data that resolves asynchronously in an SSR-friendly composable. * See {@link https://nuxt.com/docs/api/composables/use-lazy-async-data} * @param key A unique key to ensure that data fetching can be properly de-duplicated across requests. * @param handler An asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side. * @param options customize the behavior of useLazyAsyncData */ export declare function useLazyAsyncData = KeysOf, DefaultT = DefaultAsyncDataValue>(key: MaybeRefOrGetter, handler: (ctx?: NuxtApp) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; export declare function useLazyAsyncData = KeysOf, DefaultT = DataT>(key: MaybeRefOrGetter, handler: (ctx?: NuxtApp) => Promise, options?: Omit, 'lazy'>): AsyncData | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError) | DefaultAsyncDataErrorValue>; /** @since 3.1.0 */ export declare function useNuxtData(key: string): { data: Ref; }; /** @since 3.0.0 */ export declare function refreshNuxtData(keys?: string | string[]): Promise; /** @since 3.0.0 */ export declare function clearNuxtData(keys?: string | string[] | ((key: string) => boolean)): void; export type CreatedAsyncData = Omit<_AsyncData)>, 'clear' | 'refresh'> & { _off: () => void; _hash?: Record; _default: () => unknown; _init: boolean; _deps: number; _execute: (opts?: AsyncDataExecuteOptions) => Promise; };