Skip to content

Commit

Permalink
refactor: remove old types
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 11, 2024
1 parent 274d245 commit 4e0d730
Show file tree
Hide file tree
Showing 25 changed files with 363 additions and 347 deletions.
13 changes: 6 additions & 7 deletions packages/router/__tests__/RouterLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* @jest-environment jsdom
*/
import { RouterLink } from '../src/RouterLink'
import { RouteQueryAndHash, MatcherLocationRaw } from '../src/types'
import { START_LOCATION_NORMALIZED } from '../src/location'
import {
RouteQueryAndHash,
MatcherLocationRaw,
createMemoryHistory,
RouterOptions,
RouteLocationNormalized,
} from '../src/types'
import { START_LOCATION_NORMALIZED } from '../src/location'
import { createMemoryHistory, RouterOptions } from '../src'
RouteLocationResolved,
} from '../src'
import { createMockedRoute } from './mount'
import { defineComponent, PropType } from 'vue'
import { RouteRecordNormalized } from '../src/matcher/types'
Expand Down Expand Up @@ -37,8 +38,6 @@ records.parentAlias = {
records.childAlias = { aliasOf: records.child } as RouteRecordNormalized
records.childEmptyAlias.aliasOf = records.childEmpty

type RouteLocationResolved = RouteLocationNormalized & { href: string }

function createLocations<
T extends Record<
string,
Expand Down
2 changes: 1 addition & 1 deletion packages/router/__tests__/RouterView.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*/
import { RouterView } from '../src/RouterView'
import { components, RouteLocationNormalizedLoose } from './utils'
import { RouteLocationNormalized } from '../src/types'
import { START_LOCATION_NORMALIZED } from '../src/location'
import { markRaw } from 'vue'
import { createMockedRoute } from './mount'
import { mockWarn } from 'jest-mock-warn'
import { mount } from '@vue/test-utils'
import { RouteLocationNormalized } from '../src'

// to have autocompletion
function createRoutes<T extends Record<string, RouteLocationNormalizedLoose>>(
Expand Down
3 changes: 2 additions & 1 deletion packages/router/__tests__/guards/beforeEach.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fakePromise from 'faked-promise'
import { createDom, tick, noGuard, newRouter as createRouter } from '../utils'
import { RouteRecordRaw, RouteLocationRaw } from '../../src/types'
import { RouteRecordRaw } from '../../src/types'
import { RouteLocationRaw } from '../../src'

const Home = { template: `<div>Home</div>` }
const Foo = { template: `<div>Foo</div>` }
Expand Down
4 changes: 2 additions & 2 deletions packages/router/__tests__/guards/loadRouteLocation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isRouteComponent, loadRouteLocation } from '../../src/navigationGuards'
import { RouteRecordRaw, RouteLocationRaw } from '../../src/types'
import { RouteRecordRaw } from '../../src/types'
import { components } from '../utils'
import { createMemoryHistory, createRouter } from '../../src'
import { RouteLocationRaw, createMemoryHistory, createRouter } from '../../src'
import { FunctionalComponent } from 'vue'

const FunctionalHome: FunctionalComponent = () => null
Expand Down
8 changes: 6 additions & 2 deletions packages/router/__tests__/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
createMemoryHistory,
createWebHistory,
createWebHashHistory,
loadRouteLocation,
RouteLocationRaw,
} from '../src'
import { NavigationFailureType } from '../src/errors'
import { createDom, components, tick, nextNavigation } from './utils'
import { RouteRecordRaw, RouteLocationRaw } from '../src/types'
import { RouteRecordRaw } from '../src/types'
import { mockWarn } from 'jest-mock-warn'
import { START_LOCATION_NORMALIZED } from '../src/location'

Expand Down Expand Up @@ -465,7 +467,9 @@ describe('Router', () => {
expect(
router.resolve(
{ params: { p: 1 } },
router.resolve({ name: 'Param', params: { p: 2 } })
await loadRouteLocation(
router.resolve({ name: 'Param', params: { p: 2 } })
)
)
).toMatchObject({
name: 'Param',
Expand Down
2 changes: 1 addition & 1 deletion packages/router/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
MatcherLocation,
RouteComponent,
RouteRecordRaw,
_RouteRecordProps,
} from '../src/types'
import { h, ComponentOptions } from 'vue'
import {
Expand All @@ -17,6 +16,7 @@ import {
NavigationGuard,
RouteLocationNormalized,
} from '../src'
import { _RouteRecordProps } from '../src/typed-routes'

export const tick = (time?: number) =>
new Promise(resolve => {
Expand Down
12 changes: 4 additions & 8 deletions packages/router/src/RouterLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import {
// @ts-ignore
ComponentOptionsMixin,
} from 'vue'
import { isRouteLocation, type VueUseOptions } from './types'
import { isSameRouteLocationParams, isSameRouteRecord } from './location'
import { routerKey, routeLocationKey } from './injectionSymbols'
import { RouteRecord } from './matcher/types'
import { NavigationFailure } from './errors'
import { isArray, isBrowser, noop } from './utils'
import { warn } from './warning'
import { isRouteLocation, type VueUseOptions } from './types'
import {
RouteLocation,
RouteLocationNormalized,
RouteLocationRaw,
RouteLocationResolved,
} from './typed-routes'

export interface RouterLinkOptions {
Expand Down Expand Up @@ -81,8 +81,7 @@ export interface RouterLinkProps extends RouterLinkOptions {
}

export interface UseLinkDevtoolsContext {
// TODO: loaded type ?
route: RouteLocationNormalized & { href: string }
route: RouteLocationResolved
isActive: boolean
isExactActive: boolean
error: string | null
Expand Down Expand Up @@ -127,10 +126,7 @@ export function useLink(props: UseLinkOptions) {
hasPrevious = true
}

return router.resolve(
// @ts-expect-error: FIXME: errors on the name because of typed routes
to
)
return router.resolve(to)
})

const activeRecordIndex = computed<number>(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { PathParser } from './matcher/pathParserRanker'
import { Router } from './router'
import { UseLinkDevtoolsContext } from './RouterLink'
import { RouterViewDevtoolsContext } from './RouterView'
import { RouteLocationNormalized } from './types'
import { assign, isArray } from './utils'
import { RouteLocationNormalized } from './typed-routes'

/**
* Copies a route location and removes any problematic properties that cannot be shown in devtools (e.g. Vue instances).
Expand Down
4 changes: 2 additions & 2 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export type {
RouteMeta,
RouteComponent,
// RawRouteComponent,
NavigationGuardNext,
} from './types'

// Experimental Type Safe API
Expand All @@ -72,7 +71,7 @@ export type {
RouteLocationNormalized,
RouteLocationNormalizedLoaded,
RouteLocationResolved,
RouteLocationAsRelativePath,
RouteLocationAsRelative,

// route records
RouteRecordInfo,
Expand All @@ -92,6 +91,7 @@ export type {
NavigationGuardWithThis,
NavigationHookAfter,
NavigationGuardReturn,
NavigationGuardNext,
} from './typed-routes'

export { createRouter } from './router'
Expand Down
7 changes: 3 additions & 4 deletions packages/router/src/location.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LocationQuery, LocationQueryRaw } from './query'
import { RouteLocationNormalized, RouteParamValue } from './types'
import { RouteParamValue, RouteParamsGeneric } from './types'
import { RouteRecord } from './matcher/types'
import { warn } from './warning'
import { isArray } from './utils'
Expand Down Expand Up @@ -156,8 +156,8 @@ export function isSameRouteRecord(a: RouteRecord, b: RouteRecord): boolean {
}

export function isSameRouteLocationParams(
a: RouteLocationNormalized['params'],
b: RouteLocationNormalized['params']
a: RouteParamsGeneric,
b: RouteParamsGeneric
): boolean {
if (Object.keys(a).length !== Object.keys(b).length) return false

Expand Down Expand Up @@ -262,7 +262,6 @@ export function resolveRelativePath(to: string, from: string): string {
*/
export const START_LOCATION_NORMALIZED: RouteLocationNormalizedLoaded = {
path: '/',
// @ts-expect-error: internal name for compatibility
name: undefined,
// TODO: could we use a symbol in the future?
params: {},
Expand Down
13 changes: 8 additions & 5 deletions packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
MatcherLocationRaw,
MatcherLocation,
isRouteName,
_RouteRecordProps,
} from '../types'
import { createRouterError, ErrorTypes, MatcherError } from '../errors'
import { createRouteRecordMatcher, RouteRecordMatcher } from './pathMatcher'
Expand All @@ -19,7 +18,7 @@ import { comparePathParserScore } from './pathParserRanker'

import { warn } from '../warning'
import { assign, noop } from '../utils'
import type { RouteRecordName } from '../typed-routes'
import type { RouteRecordName, _RouteRecordProps } from '../typed-routes'

/**
* Internal RouterMatcher
Expand All @@ -30,10 +29,12 @@ export interface RouterMatcher {
addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void

removeRoute(matcher: RouteRecordMatcher): void
removeRoute(name: RouteRecordName): void
removeRoute(name: NonNullable<RouteRecordName>): void

getRoutes: () => RouteRecordMatcher[]
getRecordMatcher: (name: RouteRecordName) => RouteRecordMatcher | undefined
getRecordMatcher: (
name: NonNullable<RouteRecordName>
) => RouteRecordMatcher | undefined

/**
* Resolves a location. Gives access to the route record that corresponds to the actual path as well as filling the corresponding params objects
Expand Down Expand Up @@ -193,7 +194,9 @@ export function createRouterMatcher(
: noop
}

function removeRoute(matcherRef: RouteRecordName | RouteRecordMatcher) {
function removeRoute(
matcherRef: NonNullable<RouteRecordName> | RouteRecordMatcher
) {
if (isRouteName(matcherRef)) {
const matcher = matcherMap.get(matcherRef)
if (matcher) {
Expand Down
8 changes: 5 additions & 3 deletions packages/router/src/matcher/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { NavigationGuard } from '../typed-routes'
import type {
NavigationGuard,
NavigationGuardNextCallback,
_RouteRecordProps,
} from '../typed-routes'
import {
RouteRecordMultipleViews,
_RouteRecordBase,
_RouteRecordProps,
NavigationGuardNextCallback,
RouteRecordRaw,
} from '../types'
import { ComponentPublicInstance } from 'vue'
Expand Down
18 changes: 6 additions & 12 deletions packages/router/src/navigationGuards.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {
NavigationGuardNext,
NavigationGuardNextCallback,
isRouteLocation,
Lazy,
RouteComponent,
RawRouteComponent,

// NOTE: Still need to use some old types while migrating
RouteLocationRaw as RouteLocationRaw_OLD,
} from './types'

import type {
RouteLocationNormalized,
RouteLocationNormalizedLoaded,
NavigationGuard,
RouteLocation,
RouteLocationRaw,
NavigationGuardNext,
NavigationGuardNextCallback,
} from './typed-routes'

import {
Expand Down Expand Up @@ -142,12 +141,7 @@ export function guardToPromiseFn(
return () =>
new Promise((resolve, reject) => {
const next: NavigationGuardNext = (
valid?:
| boolean
// TODO: remove
| RouteLocationRaw_OLD
| NavigationGuardNextCallback
| Error
valid?: boolean | RouteLocationRaw | NavigationGuardNextCallback | Error
) => {
if (valid === false) {
reject(
Expand Down Expand Up @@ -378,7 +372,7 @@ export function isRouteComponent(
* @param route - resolved route to load
*/
export function loadRouteLocation(
route: RouteLocationNormalized
route: RouteLocation | RouteLocationNormalized
): Promise<RouteLocationNormalizedLoaded> {
return route.matched.every(record => record.redirect)
? Promise.reject(new Error('Cannot load a route that redirects.'))
Expand Down
14 changes: 8 additions & 6 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ export interface Router {
* @param parentName - Parent Route Record where `route` should be appended at
* @param route - Route Record to add
*/
addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void
addRoute(
parentName: NonNullable<RouteRecordName>,
route: RouteRecordRaw
): () => void
/**
* Add a new {@link RouteRecordRaw | route record} to the router.
*
Expand All @@ -230,7 +233,7 @@ export interface Router {
*
* @param name - Name of the route to check
*/
hasRoute(name: RouteRecordName): boolean
hasRoute(name: NonNullable<RouteRecordName>): boolean
/**
* Get a full list of all the {@link RouteRecord | route records}.
*/
Expand Down Expand Up @@ -404,7 +407,7 @@ export function createRouter(options: RouterOptions): Router {
applyToParams.bind(null, decode)

function addRoute(
parentOrRoute: RouteRecordName | RouteRecordRaw,
parentOrRoute: NonNullable<RouteRecordName> | RouteRecordRaw,
route?: RouteRecordRaw
) {
let parent: Parameters<(typeof matcher)['addRoute']>[1] | undefined
Expand All @@ -427,7 +430,7 @@ export function createRouter(options: RouterOptions): Router {
return matcher.addRoute(record, parent)
}

function removeRoute(name: RouteRecordName) {
function removeRoute(name: NonNullable<RouteRecordName>) {
const recordMatcher = matcher.getRecordMatcher(name)
if (recordMatcher) {
matcher.removeRoute(recordMatcher)
Expand All @@ -440,7 +443,7 @@ export function createRouter(options: RouterOptions): Router {
return matcher.getRoutes().map(routeMatcher => routeMatcher.record)
}

function hasRoute(name: RouteRecordName): boolean {
function hasRoute(name: NonNullable<RouteRecordName>): boolean {
return !!matcher.getRecordMatcher(name)
}

Expand Down Expand Up @@ -1225,7 +1228,6 @@ export function createRouter(options: RouterOptions): Router {
removeRoute,
hasRoute,
getRoutes,
// @ts-expect-error: FIXME: types do not match
resolve,
options,

Expand Down
5 changes: 4 additions & 1 deletion packages/router/src/scrollBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { RouteLocationNormalized, RouteLocationNormalizedLoaded } from './types'
import type {
RouteLocationNormalized,
RouteLocationNormalizedLoaded,
} from './typed-routes'
import { warn } from './warning'

// we use types instead of interfaces to make it work with HistoryStateValue type
Expand Down
Loading

0 comments on commit 4e0d730

Please sign in to comment.