mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 00:23:53 +00:00
fix(nuxt): await async callWithNuxt
calls (#18443)
This commit is contained in:
parent
022c95269e
commit
c5d6db7fd0
@ -33,7 +33,8 @@ const error = useError()
|
||||
onErrorCaptured((err, target, info) => {
|
||||
nuxtApp.hooks.callHook('vue:error', err, target, info).catch(hookError => console.error('[nuxt] Error in `vue:error` hook', hookError))
|
||||
if (process.server || (isNuxtError(err) && (err.fatal || err.unhandled))) {
|
||||
callWithNuxt(nuxtApp, showError, [err])
|
||||
const p = callWithNuxt(nuxtApp, showError, [err])
|
||||
onServerPrefetch(() => p)
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -284,9 +284,9 @@ export function isNuxtPlugin (plugin: unknown) {
|
||||
* @param setup The function to call
|
||||
*/
|
||||
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
|
||||
const fn = () => args ? setup(...args as Parameters<T>) : setup()
|
||||
const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : setup()
|
||||
if (process.server) {
|
||||
return nuxtAppCtx.callAsync<ReturnType<T>>(nuxt, fn)
|
||||
return nuxtAppCtx.callAsync(nuxt, fn)
|
||||
} else {
|
||||
// In client side we could assume nuxt app is singleton
|
||||
nuxtAppCtx.set(nuxt)
|
||||
|
@ -113,7 +113,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
||||
await router.isReady()
|
||||
} catch (error: any) {
|
||||
// We'll catch 404s here
|
||||
callWithNuxt(nuxtApp, showError, [error])
|
||||
await callWithNuxt(nuxtApp, showError, [error])
|
||||
}
|
||||
|
||||
const initialLayout = useState('_layout')
|
||||
@ -171,7 +171,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
||||
await callWithNuxt(nuxtApp, clearError)
|
||||
}
|
||||
if (to.matched.length === 0) {
|
||||
callWithNuxt(nuxtApp, showError, [createError({
|
||||
await callWithNuxt(nuxtApp, showError, [createError({
|
||||
statusCode: 404,
|
||||
fatal: false,
|
||||
statusMessage: `Page not found: ${to.fullPath}`
|
||||
@ -193,7 +193,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
||||
})
|
||||
} catch (error: any) {
|
||||
// We'll catch middleware errors or deliberate exceptions here
|
||||
callWithNuxt(nuxtApp, showError, [error])
|
||||
await callWithNuxt(nuxtApp, showError, [error])
|
||||
}
|
||||
})
|
||||
|
||||
|
9
test/fixtures/basic/types.ts
vendored
9
test/fixtures/basic/types.ts
vendored
@ -5,7 +5,7 @@ import type { AppConfig } from '@nuxt/schema'
|
||||
|
||||
import type { FetchError } from 'ofetch'
|
||||
import type { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router'
|
||||
import { isVue3 } from '#app'
|
||||
import { callWithNuxt, isVue3 } from '#app'
|
||||
import type { NavigateToOptions } from '~~/../../../packages/nuxt/dist/app/composables/router'
|
||||
import { defineNuxtConfig } from '~~/../../../packages/nuxt/config'
|
||||
import { useRouter } from '#imports'
|
||||
@ -224,3 +224,10 @@ describe('extends type declarations', () => {
|
||||
expectTypeOf<import('bing').BingInterface>().toEqualTypeOf<{ foo: 'bar' }>()
|
||||
})
|
||||
})
|
||||
|
||||
describe('composables inference', () => {
|
||||
it('callWithNuxt', () => {
|
||||
const bob = callWithNuxt({} as any, () => true)
|
||||
expectTypeOf<typeof bob>().toEqualTypeOf<boolean | Promise<boolean>>()
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user