2022-12-11 21:44:52 +00:00
|
|
|
import { getCurrentInstance, reactive, toRefs } from 'vue'
|
|
|
|
import type { DefineComponent, defineComponent } from 'vue'
|
2023-03-10 08:01:21 +00:00
|
|
|
import { useHead } from '@unhead/vue'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { NuxtApp } from '../nuxt'
|
2023-05-03 10:02:07 +00:00
|
|
|
import { useNuxtApp } from '../nuxt'
|
2021-10-08 14:21:55 +00:00
|
|
|
import { useAsyncData } from './asyncData'
|
2022-10-14 08:36:03 +00:00
|
|
|
import { useRoute } from './router'
|
2023-04-27 10:51:33 +00:00
|
|
|
import { createError } from './error'
|
2022-11-15 14:47:17 +00:00
|
|
|
|
2021-04-03 10:03:20 +00:00
|
|
|
export const NuxtComponentIndicator = '__nuxt_component'
|
|
|
|
|
2022-07-06 19:15:00 +00:00
|
|
|
async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<string, any>>, fn: (nuxtApp: NuxtApp) => Promise<Record<string, any>>) {
|
2023-05-03 10:02:07 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
2021-06-30 10:53:10 +00:00
|
|
|
const route = useRoute()
|
2022-08-12 17:47:58 +00:00
|
|
|
const vm = getCurrentInstance()!
|
2023-06-05 18:36:26 +00:00
|
|
|
const { fetchKey, _fetchKeyBase } = vm.proxy!.$options
|
|
|
|
const key = (typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey) ||
|
|
|
|
([_fetchKeyBase, route.fullPath, route.matched.findIndex(r => Object.values(r.components || {}).includes(vm.type))].join(':'))
|
2023-05-03 10:02:07 +00:00
|
|
|
const { data, error } = await useAsyncData(`options:asyncdata:${key}`, () => nuxtApp.runWithContext(() => fn(nuxtApp)))
|
2023-04-27 10:51:33 +00:00
|
|
|
if (error.value) {
|
|
|
|
throw createError(error.value)
|
|
|
|
}
|
2022-01-11 17:41:55 +00:00
|
|
|
if (data.value && typeof data.value === 'object') {
|
|
|
|
Object.assign(await res, toRefs(reactive(data.value)))
|
|
|
|
} else if (process.dev) {
|
|
|
|
console.warn('[nuxt] asyncData should return an object', data)
|
|
|
|
}
|
2021-06-30 10:53:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 10:11:24 +00:00
|
|
|
/*! @__NO_SIDE_EFFECTS__ */
|
2021-04-03 10:03:20 +00:00
|
|
|
export const defineNuxtComponent: typeof defineComponent =
|
2023-06-05 18:36:26 +00:00
|
|
|
function defineNuxtComponent (...args: any[]): any {
|
|
|
|
const [options, key] = args
|
2021-04-03 10:03:20 +00:00
|
|
|
const { setup } = options
|
|
|
|
|
2021-09-30 18:19:55 +00:00
|
|
|
// Avoid wrapping if no options api is used
|
2022-11-15 14:47:17 +00:00
|
|
|
if (!setup && !options.asyncData && !options.head) {
|
2021-04-03 10:03:20 +00:00
|
|
|
return {
|
|
|
|
[NuxtComponentIndicator]: true,
|
|
|
|
...options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
[NuxtComponentIndicator]: true,
|
2023-06-05 18:36:26 +00:00
|
|
|
_fetchKeyBase: key,
|
2021-04-03 10:03:20 +00:00
|
|
|
...options,
|
|
|
|
setup (props, ctx) {
|
2023-04-10 21:57:13 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
2023-05-03 10:02:07 +00:00
|
|
|
const res = setup ? Promise.resolve(nuxtApp.runWithContext(() => setup(props, ctx))).then(r => r || {}) : {}
|
2021-06-30 10:53:10 +00:00
|
|
|
|
2022-08-12 17:47:58 +00:00
|
|
|
const promises: Promise<any>[] = []
|
2021-06-30 10:53:10 +00:00
|
|
|
if (options.asyncData) {
|
|
|
|
promises.push(runLegacyAsyncData(res, options.asyncData))
|
|
|
|
}
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2022-11-15 14:47:17 +00:00
|
|
|
if (options.head) {
|
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
useHead(typeof options.head === 'function' ? () => options.head(nuxtApp) : options.head)
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:03:20 +00:00
|
|
|
return Promise.resolve(res)
|
|
|
|
.then(() => Promise.all(promises))
|
|
|
|
.then(() => res)
|
|
|
|
.finally(() => {
|
|
|
|
promises.length = 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} as DefineComponent
|
|
|
|
}
|