mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
refactor: add some more explicit types (#3721)
This commit is contained in:
parent
99705f77c0
commit
e3f0b31925
@ -38,7 +38,7 @@
|
||||
"externality": "^0.2.1",
|
||||
"fs-extra": "^10.0.1",
|
||||
"globby": "^13.1.1",
|
||||
"h3": "^0.4.1",
|
||||
"h3": "^0.4.2",
|
||||
"hash-sum": "^2.0.0",
|
||||
"knitwork": "^0.1.1",
|
||||
"magic-string": "^0.26.1",
|
||||
|
@ -177,7 +177,7 @@ export interface NavigateToOptions {
|
||||
replace?: boolean
|
||||
}
|
||||
|
||||
export const navigateTo = (to: Route, options: NavigateToOptions = {}) => {
|
||||
export const navigateTo = (to: Route, options: NavigateToOptions = {}): Promise<Route | void> | Route => {
|
||||
if (isProcessingMiddleware()) {
|
||||
return to
|
||||
}
|
||||
|
@ -42,7 +42,7 @@
|
||||
"fs-extra": "^10.0.1",
|
||||
"globby": "^13.1.1",
|
||||
"gzip-size": "^7.0.0",
|
||||
"h3": "^0.4.1",
|
||||
"h3": "^0.4.2",
|
||||
"hasha": "^5.2.2",
|
||||
"hookable": "^5.1.1",
|
||||
"http-proxy": "^1.18.1",
|
||||
|
@ -45,7 +45,7 @@
|
||||
"escape-string-regexp": "^5.0.0",
|
||||
"fs-extra": "^10.0.1",
|
||||
"globby": "^13.1.1",
|
||||
"h3": "^0.4.1",
|
||||
"h3": "^0.4.2",
|
||||
"hash-sum": "^2.0.0",
|
||||
"hookable": "^5.1.1",
|
||||
"knitwork": "^0.1.1",
|
||||
|
@ -1,4 +1,4 @@
|
||||
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw } from 'vue-router'
|
||||
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure } from 'vue-router'
|
||||
import { sendRedirect } from 'h3'
|
||||
import { useNuxtApp } from '#app'
|
||||
|
||||
@ -50,7 +50,7 @@ export interface NavigateToOptions {
|
||||
replace?: boolean
|
||||
}
|
||||
|
||||
export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}) => {
|
||||
export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => {
|
||||
if (isProcessingMiddleware()) {
|
||||
return to
|
||||
}
|
||||
|
@ -183,8 +183,8 @@ export const setNuxtAppInstance = (nuxt: NuxtApp | null) => {
|
||||
* @param nuxt A Nuxt instance
|
||||
* @param setup The function to call
|
||||
*/
|
||||
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp, setup: T, args?: Parameters<T>) {
|
||||
setNuxtAppInstance(nuxt)
|
||||
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
|
||||
setNuxtAppInstance(nuxt as NuxtApp)
|
||||
const p: ReturnType<T> = args ? setup(...args as Parameters<T>) : setup()
|
||||
if (process.server) {
|
||||
// Unset nuxt instance to prevent context-sharing in server-side
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { reactive, h } from 'vue'
|
||||
import { parseURL, parseQuery } from 'ufo'
|
||||
import { NuxtApp } from '@nuxt/schema'
|
||||
import { createError } from 'h3'
|
||||
import { defineNuxtPlugin } from '..'
|
||||
import { callWithNuxt } from '../nuxt'
|
||||
@ -109,7 +108,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
|
||||
|
||||
if (process.client && !nuxtApp.isHydrating) {
|
||||
// Clear any existing errors
|
||||
await callWithNuxt(nuxtApp as NuxtApp, clearError)
|
||||
await callWithNuxt(nuxtApp, clearError)
|
||||
}
|
||||
|
||||
// Run beforeEach hooks
|
||||
@ -198,11 +197,10 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
|
||||
to.meta = reactive(to.meta || {})
|
||||
nuxtApp._processingMiddleware = true
|
||||
|
||||
type MiddlewareDef = string | RouteGuard
|
||||
const middlewareEntries = new Set<MiddlewareDef>(nuxtApp._middleware.global)
|
||||
const middlewareEntries = new Set<RouteGuard>(nuxtApp._middleware.global)
|
||||
|
||||
for (const middleware of middlewareEntries) {
|
||||
const result = await callWithNuxt(nuxtApp as NuxtApp, middleware, [to, from])
|
||||
const result = await callWithNuxt(nuxtApp, middleware, [to, from])
|
||||
if (process.server) {
|
||||
if (result === false || result instanceof Error) {
|
||||
const error = result || createError({
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
} from 'vue-router'
|
||||
import { createError } from 'h3'
|
||||
import NuxtPage from './page'
|
||||
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, NuxtApp, throwError, clearError } from '#app'
|
||||
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, throwError, clearError } from '#app'
|
||||
// @ts-ignore
|
||||
import routes from '#build/routes'
|
||||
// @ts-ignore
|
||||
@ -85,7 +85,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
|
||||
if (process.client && !nuxtApp.isHydrating) {
|
||||
// Clear any existing errors
|
||||
await callWithNuxt(nuxtApp as NuxtApp, clearError)
|
||||
await callWithNuxt(nuxtApp, clearError)
|
||||
}
|
||||
|
||||
for (const entry of middlewareEntries) {
|
||||
@ -95,7 +95,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
console.warn(`Unknown middleware: ${entry}. Valid options are ${Object.keys(namedMiddleware).join(', ')}.`)
|
||||
}
|
||||
|
||||
const result = await callWithNuxt(nuxtApp as NuxtApp, middleware, [to, from])
|
||||
const result = await callWithNuxt(nuxtApp, middleware, [to, from])
|
||||
if (process.server) {
|
||||
if (result === false || result instanceof Error) {
|
||||
const error = result || createError({
|
||||
|
2
test/fixtures/basic/types.ts
vendored
2
test/fixtures/basic/types.ts
vendored
@ -51,7 +51,7 @@ describe('middleware', () => {
|
||||
addRouteMiddleware('example', (to, from) => {
|
||||
expectTypeOf(to).toMatchTypeOf<RouteLocationNormalizedLoaded>()
|
||||
expectTypeOf(from).toMatchTypeOf<RouteLocationNormalizedLoaded>()
|
||||
expectTypeOf(navigateTo).toMatchTypeOf<(to: RouteLocationRaw) => RouteLocationRaw | Promise<void | unknown | NavigationFailure>>()
|
||||
expectTypeOf(navigateTo).toMatchTypeOf<(to: RouteLocationRaw) => RouteLocationRaw | Promise<void | NavigationFailure>>()
|
||||
navigateTo('/')
|
||||
abortNavigation()
|
||||
abortNavigation('error string')
|
||||
|
14
yarn.lock
14
yarn.lock
@ -2611,7 +2611,7 @@ __metadata:
|
||||
externality: ^0.2.1
|
||||
fs-extra: ^10.0.1
|
||||
globby: ^13.1.1
|
||||
h3: ^0.4.1
|
||||
h3: ^0.4.2
|
||||
hash-sum: ^2.0.0
|
||||
knitwork: ^0.1.1
|
||||
magic-string: ^0.26.1
|
||||
@ -3015,7 +3015,7 @@ __metadata:
|
||||
fs-extra: ^10.0.1
|
||||
globby: ^13.1.1
|
||||
gzip-size: ^7.0.0
|
||||
h3: ^0.4.1
|
||||
h3: ^0.4.2
|
||||
hasha: ^5.2.2
|
||||
hookable: ^5.1.1
|
||||
http-proxy: ^1.18.1
|
||||
@ -11687,15 +11687,15 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"h3@npm:^0.4.1":
|
||||
version: 0.4.1
|
||||
resolution: "h3@npm:0.4.1"
|
||||
"h3@npm:^0.4.1, h3@npm:^0.4.2":
|
||||
version: 0.4.2
|
||||
resolution: "h3@npm:0.4.2"
|
||||
dependencies:
|
||||
cookie-es: ^0.5.0
|
||||
destr: ^1.1.0
|
||||
radix3: ^0.1.1
|
||||
ufo: ^0.7.11
|
||||
checksum: d9cd50b9ec08c87588580c9b6847b9be406e784a245e6a1689c894678347edb35a1632496e01c929a40559df185f16aa904e4f3ee0c42e95badba13cd98fc82a
|
||||
checksum: 8467d562a31e782e3456ebad55b90ac6f2a8f44c1857a9e82f9bcb839c5ef9ba7d3eb34c752b21b0498f385dcca5a282e9895353a34347af229143293fef6eed
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@ -15458,7 +15458,7 @@ __metadata:
|
||||
escape-string-regexp: ^5.0.0
|
||||
fs-extra: ^10.0.1
|
||||
globby: ^13.1.1
|
||||
h3: ^0.4.1
|
||||
h3: ^0.4.2
|
||||
hash-sum: ^2.0.0
|
||||
hookable: ^5.1.1
|
||||
knitwork: ^0.1.1
|
||||
|
Loading…
Reference in New Issue
Block a user