Skip to content

Commit

Permalink
fix(nuxt): fix default injection type for plugins (#19669)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 14, 2023
1 parent 9d850a2 commit 4b2cb52
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/nuxt/src/app/composables/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -101,7 +101,6 @@ interface _NuxtApp {

// Nuxt injections
$config: RuntimeConfig
$router: Router

isHydrating?: boolean
deferHydration: () => () => void | Promise<void>
Expand Down Expand Up @@ -133,7 +132,7 @@ interface _NuxtApp {
export interface NuxtApp extends _NuxtApp {}

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 }
[NuxtPluginIndicator]?: true
}
Expand Down Expand Up @@ -313,7 +312,7 @@ export function normalizePlugins (_plugins: 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
return plugin
}
Expand Down
8 changes: 3 additions & 5 deletions packages/nuxt/src/pages/runtime/plugins/router.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -200,4 +198,4 @@ export default defineNuxtPlugin(async (nuxtApp) => {
})

return { provide: { router } }
})
}) as Plugin<{ router: Router }>
4 changes: 3 additions & 1 deletion test/fixtures/basic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -130,9 +130,11 @@ describe('modules', () => {
describe('nuxtApp', () => {
it('types injections provided by plugins', () => {
expectTypeOf(useNuxtApp().$asyncPlugin).toEqualTypeOf<() => string>()
expectTypeOf(useNuxtApp().$router).toEqualTypeOf<Router>()
})
it('marks unknown injections as unknown', () => {
expectTypeOf(useNuxtApp().doesNotExist).toEqualTypeOf<unknown>()
expectTypeOf(useNuxtApp().$random).toEqualTypeOf<unknown>()
})
})

Expand Down

0 comments on commit 4b2cb52

Please sign in to comment.