(...args: any[])
| 273 | opts?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, |
| 274 | ): AsyncData<PickFrom<DataT, [Array<never>] extends [FPickKeys] ? PickKeys : FPickKeys & KeysOf<DataT>> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined> |
| 275 | function useAsyncData< |
| 276 | ResT, |
| 277 | NuxtErrorDataT = unknown, |
| 278 | DataT = ResT, |
| 279 | PickKeys extends KeysOf<DataT> = KeysOf<DataT>, |
| 280 | DefaultT = undefined, |
| 281 | > (...args: any[]): AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined> { |
| 282 | const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined |
| 283 | if (_isAutoKeyNeeded(args[0], args[1])) { args.unshift(autoKey) } |
| 284 | |
| 285 | // eslint-disable-next-line prefer-const |
| 286 | let [_key, _handler, opts = {}] = args as [MaybeRefOrGetter<string>, AsyncDataHandler<ResT>, AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>] |
| 287 | let keyChanging = false |
| 288 | /** True if key is a Ref or getter; false for static string. When false, key watcher is skipped. */ |
| 289 | const isKeyReactive = isRef(_key) || typeof _key === 'function' |
| 290 | |
| 291 | // Validate arguments |
| 292 | const key = (isKeyReactive ? computed(() => toValue(_key)!) : { value: _key as string }) as { readonly value: string } |
| 293 | if (!key.value || typeof key.value !== 'string') { |
| 294 | throw new TypeError('[nuxt] [useAsyncData] key must be a non-empty string.') |
| 295 | } |
| 296 | if (typeof _handler !== 'function') { |
| 297 | throw new TypeError('[nuxt] [useAsyncData] handler must be a function.') |
| 298 | } |
| 299 | |
| 300 | const shouldFactoryOptionsOverride = typeof options === 'function' |
| 301 | |
| 302 | // Setup nuxt instance payload |
| 303 | const nuxtApp = useNuxtApp() |
| 304 | |
| 305 | const factoryOptions = shouldFactoryOptionsOverride ? options(opts as any) : options |
| 306 | // assign factory defaults |
| 307 | if (!shouldFactoryOptionsOverride) { |
| 308 | for (const key in factoryOptions) { |
| 309 | // factory doesn't have a value set for the key |
| 310 | if (factoryOptions[key as keyof typeof factoryOptions] === undefined) { continue } |
| 311 | // opts already has a value set for the key |
| 312 | if (opts[key as keyof typeof opts] !== undefined) { continue } |
| 313 | opts[key as keyof typeof opts] = factoryOptions[key as keyof typeof factoryOptions] as any |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | opts.server ??= true |
| 318 | opts.default ??= getDefault as () => DefaultT |
| 319 | opts.getCachedData ??= getDefaultCachedData |
| 320 | |
| 321 | opts.lazy ??= false |
| 322 | opts.immediate ??= true |
| 323 | opts.deep ??= asyncDataDefaults.deep |
| 324 | opts.dedupe ??= 'cancel' |
| 325 | |
| 326 | // assign overrides from factory |
| 327 | if (shouldFactoryOptionsOverride) { |
| 328 | for (const key in factoryOptions) { |
| 329 | if (factoryOptions[key as keyof typeof factoryOptions] === undefined) { continue } |
| 330 | opts[key as keyof typeof opts] = factoryOptions[key as keyof typeof factoryOptions] as any |
| 331 | } |
| 332 | } |
searching dependent graphs…