import { NuxtError } from "./error.js"; import { NuxtApp } from "../nuxt.js"; import { MaybeRefOrGetter, MultiWatchSources, Ref } from "vue"; //#region src/app/composables/asyncData.d.ts type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'; type _Transform = (input: Input) => Output | Promise; type AsyncDataHandler = (nuxtApp: NuxtApp, options: { signal: AbortSignal; }) => Promise; type PickFrom> = T extends Array ? T : T extends Record ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick : T; type KeysOf = Array; type KeyOfRes = KeysOf>; type NoInfer = [T][T extends any ? 0 : never]; type AsyncDataRefreshCause = 'initial' | 'refresh:hook' | 'refresh:manual' | 'watch'; interface BaseAsyncDataOptions = KeysOf, DefaultT = undefined> { /** * 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; /** * 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 false 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'; /** * A timeout in milliseconds after which the request will be aborted if it has not resolved yet. */ timeout?: number; /** * Controls whether to run the async function * @default true */ enabled?: MaybeRefOrGetter; /** * Whether to store resolved data in the Nuxt payload (and therefore serialize it into `__NUXT_DATA__` when server-rendered). * When `false`, data fetched on the server is kept out of the payload entirely and the client will refetch * after hydration if a component renders it. Pair with lazy hydration (e.g. `hydrate-never`) to avoid * hydration mismatches and unnecessary client fetches. * @default true */ serialize?: boolean; } interface AsyncDataOptions = KeysOf, DefaultT = undefined> extends BaseAsyncDataOptions { /** * 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; } interface AsyncDataOptionsWithTransform = KeysOf, DefaultT = undefined> extends BaseAsyncDataOptions { /** * 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; /** * A function that can be used to alter handler function result after resolving. * Do not use it along with the `pick` option. */ transform: _Transform; } 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. */ dedupe?: 'cancel' | 'defer'; cause?: AsyncDataRefreshCause; /** @internal */ cachedData?: any; signal?: AbortSignal; timeout?: number; } interface _AsyncData { data: Ref; pending: Ref; refresh: (opts?: AsyncDataExecuteOptions) => Promise; execute: (opts?: AsyncDataExecuteOptions) => Promise; clear: () => void; error: Ref; status: Ref; } type AsyncData = _AsyncData & Promise<_AsyncData>; type NuxtErrorFor = NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError; type FactoryDataT = [unknown] extends [FDataT] ? ResT : FDataT; type FactoryDefaultT = [undefined] extends [FDefaultT] ? Fallback : FDefaultT; type FactoryPickKeys = [Array] extends [FPickKeys] ? PickKeys : FPickKeys & KeysOf; interface UseAsyncData = never[], FDefaultT = undefined> { = KeysOf, DefaultT = FactoryDefaultT>(handler: AsyncDataHandler, opts: AsyncDataOptionsWithTransform): AsyncData | DefaultT, NuxtErrorFor | undefined>; = KeysOf, DefaultT = FactoryDefaultT>(handler: AsyncDataHandler, opts: AsyncDataOptionsWithTransform): AsyncData | DefaultT, NuxtErrorFor | undefined>; , PickKeys extends KeysOf = KeysOf, DefaultT = FactoryDefaultT>(handler: AsyncDataHandler, opts?: AsyncDataOptions): AsyncData> | DefaultT, NuxtErrorFor | undefined>; , PickKeys extends KeysOf = KeysOf, DefaultT = FactoryDefaultT>(handler: AsyncDataHandler, opts?: AsyncDataOptions): AsyncData> | DefaultT, NuxtErrorFor | undefined>; = KeysOf, DefaultT = FactoryDefaultT>(key: MaybeRefOrGetter, handler: AsyncDataHandler, opts: AsyncDataOptionsWithTransform): AsyncData | DefaultT, NuxtErrorFor | undefined>; = KeysOf, DefaultT = FactoryDefaultT>(key: MaybeRefOrGetter, handler: AsyncDataHandler, opts: AsyncDataOptionsWithTransform): AsyncData | DefaultT, NuxtErrorFor | undefined>; , PickKeys extends KeysOf = KeysOf, DefaultT = FactoryDefaultT>(key: MaybeRefOrGetter, handler: AsyncDataHandler, opts?: AsyncDataOptions): AsyncData> | DefaultT, NuxtErrorFor | undefined>; , PickKeys extends KeysOf = KeysOf, DefaultT = FactoryDefaultT>(key: MaybeRefOrGetter, handler: AsyncDataHandler, opts?: AsyncDataOptions): AsyncData> | DefaultT, NuxtErrorFor | undefined>; } interface CreateUseAsyncData { = KeysOf, FDefaultT = undefined>(options?: Partial> | ((callerOptions: AsyncDataOptions) => Partial>)): UseAsyncData; } declare const createUseAsyncData: CreateUseAsyncData; declare const useAsyncData: UseAsyncData; declare const useLazyAsyncData: UseAsyncData; /** @since 3.1.0 */ declare function useNuxtData(key: string): { data: Ref; }; /** @since 3.0.0 */ declare function refreshNuxtData(keys?: string | string[]): Promise; /** @since 3.0.0 */ declare function clearNuxtData(keys?: string | string[] | ((key: string) => boolean)): void; type CreatedAsyncData = Omit<_AsyncData)>, 'clear' | 'refresh'> & { _off: () => void; _hash?: Record; _default: () => unknown; _init: boolean; _deps: number; _execute: (opts?: AsyncDataExecuteOptions) => Promise; _abortController?: AbortController; }; //#endregion export { AsyncData, AsyncDataExecuteOptions, AsyncDataHandler, AsyncDataOptions, AsyncDataOptionsWithTransform, AsyncDataRefreshCause, AsyncDataRequestStatus, CreateUseAsyncData, CreatedAsyncData, KeyOfRes, KeysOf, type MultiWatchSources, NoInfer, PickFrom, UseAsyncData, _AsyncData, _Transform, clearNuxtData, createUseAsyncData, refreshNuxtData, useAsyncData, useLazyAsyncData, useNuxtData };