From 4b2cb52f8a74d62871ec3f7cb26ddef84b09ba20 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 14 Mar 2023 13:08:43 +0000 Subject: [PATCH] fix(nuxt): fix default injection type for plugins (#19669) --- packages/nuxt/src/app/composables/error.ts | 3 ++- packages/nuxt/src/app/nuxt.ts | 7 +++---- packages/nuxt/src/pages/runtime/plugins/router.ts | 8 +++----- test/fixtures/basic/types.ts | 4 +++- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/nuxt/src/app/composables/error.ts b/packages/nuxt/src/app/composables/error.ts index 9fac4cd64a..3d1820f139 100644 --- a/packages/nuxt/src/app/composables/error.ts +++ b/packages/nuxt/src/app/composables/error.ts @@ -2,6 +2,7 @@ import type { H3Error } from 'h3' import { createError as _createError } from 'h3' import { toRef } from 'vue' import { useNuxtApp } from '../nuxt' +import { useRouter } from './router' export const useError = () => toRef(useNuxtApp().payload, 'error') @@ -27,7 +28,7 @@ export const clearError = async (options: { redirect?: string } = {}) => { const error = useError() nuxtApp.callHook('app:error:cleared', options) if (options.redirect) { - await nuxtApp.$router.replace(options.redirect) + await useRouter().replace(options.redirect) } error.value = null } diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 34ba740614..78b2b59d6d 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -1,7 +1,7 @@ /* eslint-disable no-use-before-define */ import { getCurrentInstance, reactive } from 'vue' import type { App, onErrorCaptured, VNode, Ref } from 'vue' -import type { RouteLocationNormalizedLoaded, Router } from 'vue-router' +import type { RouteLocationNormalizedLoaded } from 'vue-router' import type { Hookable } from 'hookable' import { createHooks } from 'hookable' import { getContext } from 'unctx' @@ -101,7 +101,6 @@ interface _NuxtApp { // Nuxt injections $config: RuntimeConfig - $router: Router isHydrating?: boolean deferHydration: () => () => void | Promise @@ -133,7 +132,7 @@ interface _NuxtApp { export interface NuxtApp extends _NuxtApp {} export const NuxtPluginIndicator = '__nuxt_plugin' -export interface Plugin = Record> { +export interface Plugin = Record> { (nuxt: _NuxtApp): Promise | Promise<{ provide?: Injections }> | void | { provide?: Injections } [NuxtPluginIndicator]?: true } @@ -313,7 +312,7 @@ export function normalizePlugins (_plugins: Plugin[]) { return plugins as Plugin[] } -export function defineNuxtPlugin> (plugin: Plugin) { +export function defineNuxtPlugin> (plugin: Plugin) { plugin[NuxtPluginIndicator] = true return plugin } diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index 72cb28038f..e193d9e4f5 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -1,8 +1,6 @@ import { computed, isReadonly, reactive, shallowRef } from 'vue' import type { Ref } from 'vue' -import type { - RouteLocation -} from 'vue-router' +import type { RouteLocation, Router } from 'vue-router' import { createRouter, createWebHistory, @@ -12,7 +10,7 @@ import { import { createError } from 'h3' import { withoutBase, isEqual } from 'ufo' -import type { PageMeta, RouteMiddleware } from '#app' +import type { PageMeta, RouteMiddleware, Plugin } from '../../../app/index' import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt' import { showError, clearError, useError } from '#app/composables/error' import { useRequestEvent } from '#app/composables/ssr' @@ -200,4 +198,4 @@ export default defineNuxtPlugin(async (nuxtApp) => { }) return { provide: { router } } -}) +}) as Plugin<{ router: Router }> diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 574d3b8ac7..7fdaa943b1 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -3,7 +3,7 @@ import { describe, it } from 'vitest' import type { Ref } from 'vue' import type { AppConfig, RuntimeValue } from '@nuxt/schema' import type { FetchError } from 'ofetch' -import type { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router' +import type { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter, Router } from 'vue-router' import { defineNuxtConfig } from 'nuxt/config' import { callWithNuxt, isVue3 } from '#app' @@ -130,9 +130,11 @@ describe('modules', () => { describe('nuxtApp', () => { it('types injections provided by plugins', () => { expectTypeOf(useNuxtApp().$asyncPlugin).toEqualTypeOf<() => string>() + expectTypeOf(useNuxtApp().$router).toEqualTypeOf() }) it('marks unknown injections as unknown', () => { expectTypeOf(useNuxtApp().doesNotExist).toEqualTypeOf() + expectTypeOf(useNuxtApp().$random).toEqualTypeOf() }) })