fix(nuxt): fix default injection type for plugins (#19669)

This commit is contained in:
Daniel Roe 2023-03-14 13:08:43 +00:00 committed by GitHub
parent 9d850a2a12
commit 4b2cb52f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 11 deletions

View File

@ -2,6 +2,7 @@ import type { H3Error } from 'h3'
import { createError as _createError } from 'h3' import { createError as _createError } from 'h3'
import { toRef } from 'vue' import { toRef } from 'vue'
import { useNuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt'
import { useRouter } from './router'
export const useError = () => toRef(useNuxtApp().payload, 'error') export const useError = () => toRef(useNuxtApp().payload, 'error')
@ -27,7 +28,7 @@ export const clearError = async (options: { redirect?: string } = {}) => {
const error = useError() const error = useError()
nuxtApp.callHook('app:error:cleared', options) nuxtApp.callHook('app:error:cleared', options)
if (options.redirect) { if (options.redirect) {
await nuxtApp.$router.replace(options.redirect) await useRouter().replace(options.redirect)
} }
error.value = null error.value = null
} }

View File

@ -1,7 +1,7 @@
/* eslint-disable no-use-before-define */ /* eslint-disable no-use-before-define */
import { getCurrentInstance, reactive } from 'vue' import { getCurrentInstance, reactive } from 'vue'
import type { App, onErrorCaptured, VNode, Ref } 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 type { Hookable } from 'hookable'
import { createHooks } from 'hookable' import { createHooks } from 'hookable'
import { getContext } from 'unctx' import { getContext } from 'unctx'
@ -101,7 +101,6 @@ interface _NuxtApp {
// Nuxt injections // Nuxt injections
$config: RuntimeConfig $config: RuntimeConfig
$router: Router
isHydrating?: boolean isHydrating?: boolean
deferHydration: () => () => void | Promise<void> deferHydration: () => () => void | Promise<void>
@ -133,7 +132,7 @@ interface _NuxtApp {
export interface NuxtApp extends _NuxtApp {} export interface NuxtApp extends _NuxtApp {}
export const NuxtPluginIndicator = '__nuxt_plugin' export const NuxtPluginIndicator = '__nuxt_plugin'
export interface Plugin<Injections extends Record<string, any> = Record<string, any>> { export interface Plugin<Injections extends Record<string, unknown> = Record<string, unknown>> {
(nuxt: _NuxtApp): Promise<void> | Promise<{ provide?: Injections }> | void | { provide?: Injections } (nuxt: _NuxtApp): Promise<void> | Promise<{ provide?: Injections }> | void | { provide?: Injections }
[NuxtPluginIndicator]?: true [NuxtPluginIndicator]?: true
} }
@ -313,7 +312,7 @@ export function normalizePlugins (_plugins: Plugin[]) {
return plugins as Plugin[] return plugins as Plugin[]
} }
export function defineNuxtPlugin<T extends Record<string, any>> (plugin: Plugin<T>) { export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plugin<T>) {
plugin[NuxtPluginIndicator] = true plugin[NuxtPluginIndicator] = true
return plugin return plugin
} }

View File

@ -1,8 +1,6 @@
import { computed, isReadonly, reactive, shallowRef } from 'vue' import { computed, isReadonly, reactive, shallowRef } from 'vue'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { import type { RouteLocation, Router } from 'vue-router'
RouteLocation
} from 'vue-router'
import { import {
createRouter, createRouter,
createWebHistory, createWebHistory,
@ -12,7 +10,7 @@ import {
import { createError } from 'h3' import { createError } from 'h3'
import { withoutBase, isEqual } from 'ufo' 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 { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
import { showError, clearError, useError } from '#app/composables/error' import { showError, clearError, useError } from '#app/composables/error'
import { useRequestEvent } from '#app/composables/ssr' import { useRequestEvent } from '#app/composables/ssr'
@ -200,4 +198,4 @@ export default defineNuxtPlugin(async (nuxtApp) => {
}) })
return { provide: { router } } return { provide: { router } }
}) }) as Plugin<{ router: Router }>

View File

@ -3,7 +3,7 @@ import { describe, it } from 'vitest'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { AppConfig, RuntimeValue } from '@nuxt/schema' import type { AppConfig, RuntimeValue } from '@nuxt/schema'
import type { FetchError } from 'ofetch' 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 { defineNuxtConfig } from 'nuxt/config'
import { callWithNuxt, isVue3 } from '#app' import { callWithNuxt, isVue3 } from '#app'
@ -130,9 +130,11 @@ describe('modules', () => {
describe('nuxtApp', () => { describe('nuxtApp', () => {
it('types injections provided by plugins', () => { it('types injections provided by plugins', () => {
expectTypeOf(useNuxtApp().$asyncPlugin).toEqualTypeOf<() => string>() expectTypeOf(useNuxtApp().$asyncPlugin).toEqualTypeOf<() => string>()
expectTypeOf(useNuxtApp().$router).toEqualTypeOf<Router>()
}) })
it('marks unknown injections as unknown', () => { it('marks unknown injections as unknown', () => {
expectTypeOf(useNuxtApp().doesNotExist).toEqualTypeOf<unknown>() expectTypeOf(useNuxtApp().doesNotExist).toEqualTypeOf<unknown>()
expectTypeOf(useNuxtApp().$random).toEqualTypeOf<unknown>()
}) })
}) })