refactor: add some more explicit types (#3721)

This commit is contained in:
Daniel Roe 2022-03-16 22:44:22 +00:00 committed by GitHub
parent 99705f77c0
commit e3f0b31925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 24 deletions

View File

@ -38,7 +38,7 @@
"externality": "^0.2.1", "externality": "^0.2.1",
"fs-extra": "^10.0.1", "fs-extra": "^10.0.1",
"globby": "^13.1.1", "globby": "^13.1.1",
"h3": "^0.4.1", "h3": "^0.4.2",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"knitwork": "^0.1.1", "knitwork": "^0.1.1",
"magic-string": "^0.26.1", "magic-string": "^0.26.1",

View File

@ -177,7 +177,7 @@ export interface NavigateToOptions {
replace?: boolean replace?: boolean
} }
export const navigateTo = (to: Route, options: NavigateToOptions = {}) => { export const navigateTo = (to: Route, options: NavigateToOptions = {}): Promise<Route | void> | Route => {
if (isProcessingMiddleware()) { if (isProcessingMiddleware()) {
return to return to
} }

View File

@ -42,7 +42,7 @@
"fs-extra": "^10.0.1", "fs-extra": "^10.0.1",
"globby": "^13.1.1", "globby": "^13.1.1",
"gzip-size": "^7.0.0", "gzip-size": "^7.0.0",
"h3": "^0.4.1", "h3": "^0.4.2",
"hasha": "^5.2.2", "hasha": "^5.2.2",
"hookable": "^5.1.1", "hookable": "^5.1.1",
"http-proxy": "^1.18.1", "http-proxy": "^1.18.1",

View File

@ -45,7 +45,7 @@
"escape-string-regexp": "^5.0.0", "escape-string-regexp": "^5.0.0",
"fs-extra": "^10.0.1", "fs-extra": "^10.0.1",
"globby": "^13.1.1", "globby": "^13.1.1",
"h3": "^0.4.1", "h3": "^0.4.2",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"hookable": "^5.1.1", "hookable": "^5.1.1",
"knitwork": "^0.1.1", "knitwork": "^0.1.1",

View File

@ -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 { sendRedirect } from 'h3'
import { useNuxtApp } from '#app' import { useNuxtApp } from '#app'
@ -50,7 +50,7 @@ export interface NavigateToOptions {
replace?: boolean replace?: boolean
} }
export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}) => { export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => {
if (isProcessingMiddleware()) { if (isProcessingMiddleware()) {
return to return to
} }

View File

@ -183,8 +183,8 @@ export const setNuxtAppInstance = (nuxt: NuxtApp | null) => {
* @param nuxt A Nuxt instance * @param nuxt A Nuxt instance
* @param setup The function to call * @param setup The function to call
*/ */
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp, setup: T, args?: Parameters<T>) { export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
setNuxtAppInstance(nuxt) setNuxtAppInstance(nuxt as NuxtApp)
const p: ReturnType<T> = args ? setup(...args as Parameters<T>) : setup() const p: ReturnType<T> = args ? setup(...args as Parameters<T>) : setup()
if (process.server) { if (process.server) {
// Unset nuxt instance to prevent context-sharing in server-side // Unset nuxt instance to prevent context-sharing in server-side

View File

@ -1,6 +1,5 @@
import { reactive, h } from 'vue' import { reactive, h } from 'vue'
import { parseURL, parseQuery } from 'ufo' import { parseURL, parseQuery } from 'ufo'
import { NuxtApp } from '@nuxt/schema'
import { createError } from 'h3' import { createError } from 'h3'
import { defineNuxtPlugin } from '..' import { defineNuxtPlugin } from '..'
import { callWithNuxt } from '../nuxt' import { callWithNuxt } from '../nuxt'
@ -109,7 +108,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
if (process.client && !nuxtApp.isHydrating) { if (process.client && !nuxtApp.isHydrating) {
// Clear any existing errors // Clear any existing errors
await callWithNuxt(nuxtApp as NuxtApp, clearError) await callWithNuxt(nuxtApp, clearError)
} }
// Run beforeEach hooks // Run beforeEach hooks
@ -198,11 +197,10 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
to.meta = reactive(to.meta || {}) to.meta = reactive(to.meta || {})
nuxtApp._processingMiddleware = true nuxtApp._processingMiddleware = true
type MiddlewareDef = string | RouteGuard const middlewareEntries = new Set<RouteGuard>(nuxtApp._middleware.global)
const middlewareEntries = new Set<MiddlewareDef>(nuxtApp._middleware.global)
for (const middleware of middlewareEntries) { 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 (process.server) {
if (result === false || result instanceof Error) { if (result === false || result instanceof Error) {
const error = result || createError({ const error = result || createError({

View File

@ -7,7 +7,7 @@ import {
} from 'vue-router' } from 'vue-router'
import { createError } from 'h3' import { createError } from 'h3'
import NuxtPage from './page' import NuxtPage from './page'
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, NuxtApp, throwError, clearError } from '#app' import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, throwError, clearError } from '#app'
// @ts-ignore // @ts-ignore
import routes from '#build/routes' import routes from '#build/routes'
// @ts-ignore // @ts-ignore
@ -85,7 +85,7 @@ export default defineNuxtPlugin((nuxtApp) => {
if (process.client && !nuxtApp.isHydrating) { if (process.client && !nuxtApp.isHydrating) {
// Clear any existing errors // Clear any existing errors
await callWithNuxt(nuxtApp as NuxtApp, clearError) await callWithNuxt(nuxtApp, clearError)
} }
for (const entry of middlewareEntries) { 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(', ')}.`) 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 (process.server) {
if (result === false || result instanceof Error) { if (result === false || result instanceof Error) {
const error = result || createError({ const error = result || createError({

View File

@ -51,7 +51,7 @@ describe('middleware', () => {
addRouteMiddleware('example', (to, from) => { addRouteMiddleware('example', (to, from) => {
expectTypeOf(to).toMatchTypeOf<RouteLocationNormalizedLoaded>() expectTypeOf(to).toMatchTypeOf<RouteLocationNormalizedLoaded>()
expectTypeOf(from).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('/') navigateTo('/')
abortNavigation() abortNavigation()
abortNavigation('error string') abortNavigation('error string')

View File

@ -2611,7 +2611,7 @@ __metadata:
externality: ^0.2.1 externality: ^0.2.1
fs-extra: ^10.0.1 fs-extra: ^10.0.1
globby: ^13.1.1 globby: ^13.1.1
h3: ^0.4.1 h3: ^0.4.2
hash-sum: ^2.0.0 hash-sum: ^2.0.0
knitwork: ^0.1.1 knitwork: ^0.1.1
magic-string: ^0.26.1 magic-string: ^0.26.1
@ -3015,7 +3015,7 @@ __metadata:
fs-extra: ^10.0.1 fs-extra: ^10.0.1
globby: ^13.1.1 globby: ^13.1.1
gzip-size: ^7.0.0 gzip-size: ^7.0.0
h3: ^0.4.1 h3: ^0.4.2
hasha: ^5.2.2 hasha: ^5.2.2
hookable: ^5.1.1 hookable: ^5.1.1
http-proxy: ^1.18.1 http-proxy: ^1.18.1
@ -11687,15 +11687,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"h3@npm:^0.4.1": "h3@npm:^0.4.1, h3@npm:^0.4.2":
version: 0.4.1 version: 0.4.2
resolution: "h3@npm:0.4.1" resolution: "h3@npm:0.4.2"
dependencies: dependencies:
cookie-es: ^0.5.0 cookie-es: ^0.5.0
destr: ^1.1.0 destr: ^1.1.0
radix3: ^0.1.1 radix3: ^0.1.1
ufo: ^0.7.11 ufo: ^0.7.11
checksum: d9cd50b9ec08c87588580c9b6847b9be406e784a245e6a1689c894678347edb35a1632496e01c929a40559df185f16aa904e4f3ee0c42e95badba13cd98fc82a checksum: 8467d562a31e782e3456ebad55b90ac6f2a8f44c1857a9e82f9bcb839c5ef9ba7d3eb34c752b21b0498f385dcca5a282e9895353a34347af229143293fef6eed
languageName: node languageName: node
linkType: hard linkType: hard
@ -15458,7 +15458,7 @@ __metadata:
escape-string-regexp: ^5.0.0 escape-string-regexp: ^5.0.0
fs-extra: ^10.0.1 fs-extra: ^10.0.1
globby: ^13.1.1 globby: ^13.1.1
h3: ^0.4.1 h3: ^0.4.2
hash-sum: ^2.0.0 hash-sum: ^2.0.0
hookable: ^5.1.1 hookable: ^5.1.1
knitwork: ^0.1.1 knitwork: ^0.1.1