chore: improve internal type safety (#19599)

This commit is contained in:
Daniel Roe 2023-03-11 18:22:29 +00:00 committed by GitHub
parent 42986a4669
commit e6bbf71fd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 5 deletions

View File

@ -1,13 +1,17 @@
import { getCurrentInstance, inject, onUnmounted } from 'vue'
import type { Ref } from 'vue'
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure, RouteLocationPathRaw } from 'vue-router'
import { sendRedirect } from 'h3'
import { hasProtocol, joinURL, parseURL } from 'ufo'
import { useNuxtApp, useRuntimeConfig } from '../nuxt'
import type { NuxtError } from './error'
import { createError } from './error'
import { useState } from './state'
import { setResponseStatus } from './ssr'
import type { PageMeta } from '#app'
export const useRouter = () => {
return useNuxtApp()?.$router as Router
}
@ -148,11 +152,11 @@ export const setPageLayout = (layout: string) => {
const inMiddleware = isProcessingMiddleware()
if (inMiddleware || process.server || nuxtApp.isHydrating) {
const unsubscribe = useRouter().beforeResolve((to) => {
to.meta.layout = layout
to.meta.layout = layout as Exclude<PageMeta['layout'], Ref | false>
unsubscribe()
})
}
if (!inMiddleware) {
useRoute().meta.layout = layout
useRoute().meta.layout = layout as Exclude<PageMeta['layout'], Ref | false>
}
}

View File

@ -303,10 +303,10 @@ export function isNuxtPlugin (plugin: unknown) {
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : setup()
if (process.server) {
return nuxtAppCtx.callAsync(nuxt, fn)
return nuxtAppCtx.callAsync(nuxt as NuxtApp, fn)
} else {
// In client side we could assume nuxt app is singleton
nuxtAppCtx.set(nuxt)
nuxtAppCtx.set(nuxt as NuxtApp)
return fn()
}
}

View File

@ -1,4 +1,5 @@
import { computed, isReadonly, reactive, shallowRef } from 'vue'
import type { Ref } from 'vue'
import type {
NavigationGuard,
RouteLocation
@ -11,6 +12,8 @@ import {
} from 'vue-router'
import { createError } from 'h3'
import { withoutBase, isEqual } from 'ufo'
import type { PageMeta } from '#app'
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
import { showError, clearError, useError } from '#app/composables/error'
import { useRequestEvent } from '#app/composables/ssr'
@ -118,7 +121,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
router.beforeEach(async (to, from) => {
to.meta = reactive(to.meta)
if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) {
to.meta.layout = initialLayout.value
to.meta.layout = initialLayout.value as Exclude<PageMeta['layout'], Ref | false>
}
nuxtApp._processingMiddleware = true