2022-04-29 09:38:22 +00:00
|
|
|
import { onBeforeMount, onServerPrefetch, onUnmounted, ref, getCurrentInstance, watch, unref } from 'vue'
|
2022-03-16 22:49:30 +00:00
|
|
|
import type { Ref, WatchSource } from 'vue'
|
2022-09-14 09:22:03 +00:00
|
|
|
import { NuxtApp, useNuxtApp } from '../nuxt'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-11-15 12:09:07 +00:00
|
|
|
export type _Transform<Input = any, Output = any> = (input: Input) => Output
|
2021-10-11 22:36:50 +00:00
|
|
|
|
2022-06-08 19:45:12 +00:00
|
|
|
export type PickFrom<T, K extends Array<string>> = T extends Array<any>
|
|
|
|
? T
|
|
|
|
: T extends Record<string, any>
|
|
|
|
? keyof T extends K[number]
|
|
|
|
? T // Exact same keys as the target, skip Pick
|
|
|
|
: Pick<T, K[number]>
|
|
|
|
: T
|
|
|
|
|
2021-10-12 08:52:09 +00:00
|
|
|
export type KeysOf<T> = Array<keyof T extends string ? keyof T : string>
|
2021-10-11 22:36:50 +00:00
|
|
|
export type KeyOfRes<Transform extends _Transform> = KeysOf<ReturnType<Transform>>
|
|
|
|
|
2022-08-15 13:55:46 +00:00
|
|
|
type MultiWatchSources = (WatchSource<unknown> | object)[]
|
2022-03-16 22:49:30 +00:00
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
export interface AsyncDataOptions<
|
|
|
|
DataT,
|
|
|
|
Transform extends _Transform<DataT, any> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<_Transform> = KeyOfRes<Transform>
|
2022-08-15 13:55:46 +00:00
|
|
|
> {
|
2021-01-18 12:46:19 +00:00
|
|
|
server?: boolean
|
2021-11-15 12:09:07 +00:00
|
|
|
lazy?: boolean
|
2022-08-12 17:47:58 +00:00
|
|
|
default?: () => DataT | Ref<DataT> | null
|
2021-10-11 22:36:50 +00:00
|
|
|
transform?: Transform
|
|
|
|
pick?: PickKeys
|
2022-03-16 22:49:30 +00:00
|
|
|
watch?: MultiWatchSources
|
2022-03-31 10:24:28 +00:00
|
|
|
initialCache?: boolean
|
2022-09-07 09:47:40 +00:00
|
|
|
immediate?: boolean
|
2022-03-31 10:24:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 09:47:40 +00:00
|
|
|
export interface AsyncDataExecuteOptions {
|
2022-03-31 10:24:28 +00:00
|
|
|
_initial?: boolean
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 16:02:45 +00:00
|
|
|
export interface _AsyncData<DataT, ErrorT> {
|
2022-08-12 17:47:58 +00:00
|
|
|
data: Ref<DataT | null>
|
2021-02-03 18:14:30 +00:00
|
|
|
pending: Ref<boolean>
|
2022-09-07 09:47:40 +00:00
|
|
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
|
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
2022-08-12 17:47:58 +00:00
|
|
|
error: Ref<ErrorT | null>
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 16:02:45 +00:00
|
|
|
export type AsyncData<Data, Error> = _AsyncData<Data, Error> & Promise<_AsyncData<Data, Error>>
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
const getDefault = () => null
|
2022-07-07 16:26:04 +00:00
|
|
|
export function useAsyncData<
|
|
|
|
DataT,
|
|
|
|
DataE = Error,
|
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
|
|
|
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
|
|
|
options?: AsyncDataOptions<DataT, Transform, PickKeys>
|
|
|
|
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true>
|
2021-10-11 22:36:50 +00:00
|
|
|
export function useAsyncData<
|
|
|
|
DataT,
|
2022-04-29 18:42:22 +00:00
|
|
|
DataE = Error,
|
2021-10-11 22:36:50 +00:00
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
|
|
|
key: string,
|
|
|
|
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
2022-07-07 16:26:04 +00:00
|
|
|
options?: AsyncDataOptions<DataT, Transform, PickKeys>
|
|
|
|
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true>
|
|
|
|
export function useAsyncData<
|
|
|
|
DataT,
|
|
|
|
DataE = Error,
|
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
2022-08-12 17:47:58 +00:00
|
|
|
> (...args: any[]): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true> {
|
2022-07-07 16:26:04 +00:00
|
|
|
const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
|
|
|
|
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
|
|
|
|
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
let [key, handler, options = {}] = args as [string, (ctx?: NuxtApp) => Promise<DataT>, AsyncDataOptions<DataT, Transform, PickKeys>]
|
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Validate arguments
|
|
|
|
if (typeof key !== 'string') {
|
2022-07-07 16:26:04 +00:00
|
|
|
throw new TypeError('[nuxt] [asyncData] key must be a string.')
|
2021-10-08 14:21:55 +00:00
|
|
|
}
|
|
|
|
if (typeof handler !== 'function') {
|
2022-07-07 16:26:04 +00:00
|
|
|
throw new TypeError('[nuxt] [asyncData] handler must be a function.')
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Apply defaults
|
2022-07-06 22:12:31 +00:00
|
|
|
options.server = options.server ?? true
|
|
|
|
options.default = options.default ?? getDefault
|
|
|
|
|
2021-11-15 12:09:07 +00:00
|
|
|
// TODO: remove support for `defer` in Nuxt 3 RC
|
|
|
|
if ((options as any).defer) {
|
|
|
|
console.warn('[useAsyncData] `defer` has been renamed to `lazy`. Support for `defer` will be removed in RC.')
|
|
|
|
}
|
|
|
|
options.lazy = options.lazy ?? (options as any).defer ?? false
|
2022-03-31 10:24:28 +00:00
|
|
|
options.initialCache = options.initialCache ?? true
|
2022-09-07 09:47:40 +00:00
|
|
|
options.immediate = options.immediate ?? true
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Setup nuxt instance payload
|
|
|
|
const nuxt = useNuxtApp()
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2022-08-15 13:55:46 +00:00
|
|
|
const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined
|
2022-04-05 11:51:07 +00:00
|
|
|
|
2022-08-30 10:34:09 +00:00
|
|
|
// Create or use a shared asyncData entity
|
|
|
|
if (!nuxt._asyncData[key]) {
|
|
|
|
nuxt._asyncData[key] = {
|
|
|
|
data: ref(useInitialCache() ? nuxt.payload.data[key] : options.default?.() ?? null),
|
|
|
|
pending: ref(!useInitialCache()),
|
|
|
|
error: ref(nuxt.payload._errors[key] ?? null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Else, Soemhow check for confliciting keys with different defaults or fetcher
|
|
|
|
const asyncData = { ...nuxt._asyncData[key] } as AsyncData<DataT, DataE>
|
2021-03-17 09:17:18 +00:00
|
|
|
|
2022-09-07 09:47:40 +00:00
|
|
|
asyncData.refresh = asyncData.execute = (opts = {}) => {
|
2021-10-08 14:21:55 +00:00
|
|
|
// Avoid fetching same key more than once at a time
|
2022-03-31 10:24:28 +00:00
|
|
|
if (nuxt._asyncDataPromises[key]) {
|
2021-04-03 10:03:20 +00:00
|
|
|
return nuxt._asyncDataPromises[key]
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
2022-03-31 10:24:28 +00:00
|
|
|
// Avoid fetching same key that is already fetched
|
2022-04-05 11:51:07 +00:00
|
|
|
if (opts._initial && useInitialCache()) {
|
2022-03-31 10:24:28 +00:00
|
|
|
return nuxt.payload.data[key]
|
|
|
|
}
|
2021-10-08 14:21:55 +00:00
|
|
|
asyncData.pending.value = true
|
2021-11-24 19:59:04 +00:00
|
|
|
// TODO: Cancel previous promise
|
2022-08-09 21:48:48 +00:00
|
|
|
nuxt._asyncDataPromises[key] = new Promise<DataT>(
|
|
|
|
(resolve, reject) => {
|
|
|
|
try {
|
|
|
|
resolve(handler(nuxt))
|
|
|
|
} catch (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
2021-10-08 14:21:55 +00:00
|
|
|
.then((result) => {
|
2021-10-11 22:36:50 +00:00
|
|
|
if (options.transform) {
|
|
|
|
result = options.transform(result)
|
|
|
|
}
|
|
|
|
if (options.pick) {
|
2022-08-26 15:47:29 +00:00
|
|
|
result = pick(result as any, options.pick) as DataT
|
2021-10-11 22:36:50 +00:00
|
|
|
}
|
2021-10-08 14:21:55 +00:00
|
|
|
asyncData.data.value = result
|
|
|
|
asyncData.error.value = null
|
|
|
|
})
|
|
|
|
.catch((error: any) => {
|
|
|
|
asyncData.error.value = error
|
2022-08-12 17:47:58 +00:00
|
|
|
asyncData.data.value = unref(options.default?.() ?? null)
|
2021-10-08 14:21:55 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
asyncData.pending.value = false
|
|
|
|
nuxt.payload.data[key] = asyncData.data.value
|
2021-11-29 11:22:38 +00:00
|
|
|
if (asyncData.error.value) {
|
|
|
|
nuxt.payload._errors[key] = true
|
|
|
|
}
|
2021-10-08 14:21:55 +00:00
|
|
|
delete nuxt._asyncDataPromises[key]
|
|
|
|
})
|
|
|
|
return nuxt._asyncDataPromises[key]
|
|
|
|
}
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2022-03-31 10:24:28 +00:00
|
|
|
const initialFetch = () => asyncData.refresh({ _initial: true })
|
|
|
|
|
2021-10-25 10:36:38 +00:00
|
|
|
const fetchOnServer = options.server !== false && nuxt.payload.serverRendered
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Server side
|
2022-09-07 09:47:40 +00:00
|
|
|
if (process.server && fetchOnServer && options.immediate) {
|
2022-03-31 10:24:28 +00:00
|
|
|
const promise = initialFetch()
|
2021-11-15 12:09:07 +00:00
|
|
|
onServerPrefetch(() => promise)
|
2021-10-08 14:21:55 +00:00
|
|
|
}
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Client side
|
|
|
|
if (process.client) {
|
2022-08-30 10:48:26 +00:00
|
|
|
// Setup hook callbacks once per instance
|
|
|
|
const instance = getCurrentInstance()
|
|
|
|
if (instance && !instance._nuxtOnBeforeMountCbs) {
|
|
|
|
instance._nuxtOnBeforeMountCbs = []
|
|
|
|
const cbs = instance._nuxtOnBeforeMountCbs
|
|
|
|
if (instance) {
|
|
|
|
onBeforeMount(() => {
|
|
|
|
cbs.forEach((cb) => { cb() })
|
|
|
|
cbs.splice(0, cbs.length)
|
|
|
|
})
|
|
|
|
onUnmounted(() => cbs.splice(0, cbs.length))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 16:19:10 +00:00
|
|
|
if (fetchOnServer && nuxt.isHydrating && key in nuxt.payload.data) {
|
2021-11-24 19:59:04 +00:00
|
|
|
// 1. Hydration (server: true): no fetch
|
2021-10-08 14:21:55 +00:00
|
|
|
asyncData.pending.value = false
|
2022-09-07 09:47:40 +00:00
|
|
|
} else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.lazy) && options.immediate) {
|
2021-11-24 19:59:04 +00:00
|
|
|
// 2. Initial load (server: false): fetch on mounted
|
2022-08-24 19:05:28 +00:00
|
|
|
// 3. Initial load or navigation (lazy: true): fetch on mounted
|
2022-03-31 10:24:28 +00:00
|
|
|
instance._nuxtOnBeforeMountCbs.push(initialFetch)
|
2022-09-07 09:47:40 +00:00
|
|
|
} else if (options.immediate) {
|
2021-11-24 19:59:04 +00:00
|
|
|
// 4. Navigation (lazy: false) - or plugin usage: await fetch
|
2022-03-31 10:24:28 +00:00
|
|
|
initialFetch()
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
2022-03-16 22:49:30 +00:00
|
|
|
if (options.watch) {
|
2022-03-28 17:12:41 +00:00
|
|
|
watch(options.watch, () => asyncData.refresh())
|
|
|
|
}
|
|
|
|
const off = nuxt.hook('app:data:refresh', (keys) => {
|
|
|
|
if (!keys || keys.includes(key)) {
|
|
|
|
return asyncData.refresh()
|
2022-03-16 22:49:30 +00:00
|
|
|
}
|
2022-03-28 17:12:41 +00:00
|
|
|
})
|
|
|
|
if (instance) {
|
|
|
|
onUnmounted(off)
|
2022-03-16 22:49:30 +00:00
|
|
|
}
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
2021-03-01 09:45:37 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Allow directly awaiting on asyncData
|
2022-04-06 16:02:45 +00:00
|
|
|
const asyncDataPromise = Promise.resolve(nuxt._asyncDataPromises[key]).then(() => asyncData) as AsyncData<DataT, DataE>
|
2021-10-08 14:21:55 +00:00
|
|
|
Object.assign(asyncDataPromise, asyncData)
|
|
|
|
|
2022-04-06 16:02:45 +00:00
|
|
|
return asyncDataPromise as AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE>
|
2021-10-11 22:36:50 +00:00
|
|
|
}
|
2022-07-07 16:26:04 +00:00
|
|
|
export function useLazyAsyncData<
|
|
|
|
DataT,
|
|
|
|
DataE = Error,
|
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
|
|
|
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
|
|
|
options?: Omit<AsyncDataOptions<DataT, Transform, PickKeys>, 'lazy'>
|
|
|
|
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true>
|
2021-11-15 12:09:07 +00:00
|
|
|
export function useLazyAsyncData<
|
|
|
|
DataT,
|
2022-04-29 18:42:22 +00:00
|
|
|
DataE = Error,
|
2021-11-15 12:09:07 +00:00
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
|
|
|
key: string,
|
|
|
|
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
2022-07-07 16:26:04 +00:00
|
|
|
options?: Omit<AsyncDataOptions<DataT, Transform, PickKeys>, 'lazy'>
|
|
|
|
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true>
|
|
|
|
export function useLazyAsyncData<
|
|
|
|
DataT,
|
|
|
|
DataE = Error,
|
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
2022-08-12 17:47:58 +00:00
|
|
|
> (...args: any[]): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true> {
|
2022-07-07 16:26:04 +00:00
|
|
|
const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
|
|
|
|
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
|
|
|
|
const [key, handler, options] = args as [string, (ctx?: NuxtApp) => Promise<DataT>, AsyncDataOptions<DataT, Transform, PickKeys>]
|
|
|
|
// @ts-ignore
|
|
|
|
return useAsyncData(key, handler, { ...options, lazy: true }, null)
|
2021-11-15 12:09:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 17:12:41 +00:00
|
|
|
export function refreshNuxtData (keys?: string | string[]): Promise<void> {
|
|
|
|
if (process.server) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
const _keys = keys ? Array.isArray(keys) ? keys : [keys] : undefined
|
|
|
|
return useNuxtApp().callHook('app:data:refresh', _keys)
|
|
|
|
}
|
|
|
|
|
2022-09-07 13:25:37 +00:00
|
|
|
export function clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void {
|
2022-09-07 11:20:09 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
2022-09-07 13:25:37 +00:00
|
|
|
const _allKeys = Object.keys(nuxtApp.payload.data)
|
|
|
|
const _keys: string[] = !keys
|
|
|
|
? _allKeys
|
|
|
|
: typeof keys === 'function'
|
|
|
|
? _allKeys.filter(keys)
|
|
|
|
: Array.isArray(keys) ? keys : [keys]
|
2022-09-07 11:20:09 +00:00
|
|
|
|
|
|
|
for (const key of _keys) {
|
|
|
|
if (key in nuxtApp.payload.data) {
|
|
|
|
nuxtApp.payload.data[key] = undefined
|
|
|
|
}
|
|
|
|
if (key in nuxtApp.payload._errors) {
|
|
|
|
nuxtApp.payload._errors[key] = undefined
|
|
|
|
}
|
|
|
|
if (nuxtApp._asyncData[key]) {
|
|
|
|
nuxtApp._asyncData[key]!.data.value = undefined
|
|
|
|
nuxtApp._asyncData[key]!.error.value = undefined
|
|
|
|
nuxtApp._asyncData[key]!.pending.value = false
|
|
|
|
}
|
|
|
|
if (key in nuxtApp._asyncDataPromises) {
|
|
|
|
nuxtApp._asyncDataPromises[key] = undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
function pick (obj: Record<string, any>, keys: string[]) {
|
|
|
|
const newObj = {}
|
|
|
|
for (const key of keys) {
|
2022-08-12 17:47:58 +00:00
|
|
|
(newObj as any)[key] = obj[key]
|
2021-10-11 22:36:50 +00:00
|
|
|
}
|
|
|
|
return newObj
|
2021-03-01 09:45:37 +00:00
|
|
|
}
|