Skip to content

Commit

Permalink
fix(types): add missing NavigationFailure types
Browse files Browse the repository at this point in the history
Close #3293
  • Loading branch information
posva committed Aug 13, 2020
1 parent 3047798 commit fda7067
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/util/errors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// When changing thing, also edit router.d.ts
export const NavigationFailureType = {
redirected: 2,
aborted: 4,
Expand Down
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ export {
Location,
Route,
NavigationGuard,
NavigationGuardNext
NavigationGuardNext,
NavigationFailureType,
NavigationFailure
} from './router'
25 changes: 17 additions & 8 deletions types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export declare class VueRouter {
constructor(options?: RouterOptions)

app: Vue
options: RouterOptions;
options: RouterOptions
mode: RouterMode
currentRoute: Route

Expand Down Expand Up @@ -63,15 +63,24 @@ export declare class VueRouter {
static install: PluginFunction<never>
static version: string

static isNavigationFailure: (error: any, type?: NavigationFailureTypeE) => error is Error
static NavigationFailureType: NavigationFailureTypeE
static isNavigationFailure: (
error: any,
type?: number
) => error is NavigationFailure
static NavigationFailureType: NavigationFailureType
}

export enum NavigationFailureTypeE {
redirected = 1,
aborted = 2,
cancelled = 3,
duplicated = 4
export enum NavigationFailureType {
redirected= 2,
aborted= 4,
cancelled= 8,
duplicated= 16
}

export interface NavigationFailure extends Error {
to: Route
from: Route
type: number
}

type Position = { x: number; y: number }
Expand Down
31 changes: 27 additions & 4 deletions types/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Vue, { ComponentOptions, AsyncComponent } from 'vue'

import VueRouter from '../index'
import { Route, RouteRecord, RedirectOption } from '../index'
import {
Route,
RouteRecord,
RedirectOption,
NavigationFailure,
NavigationFailureType
} from '../index'

Vue.use(VueRouter)

Expand All @@ -11,6 +17,14 @@ const Bar = { template: '<div>bar</div>' }
const Abc = { template: '<div>abc</div>' }
const Async = () => Promise.resolve({ template: '<div>async</div>' })

let err: any
if (VueRouter.isNavigationFailure(err, NavigationFailureType.aborted)) {
err.from.fullPath.split('/')
}

let navigationFailure = new Error() as NavigationFailure
navigationFailure.to.fullPath.split('/')

const Hook: ComponentOptions<Vue> = {
template: '<div>hook</div>',

Expand Down Expand Up @@ -181,8 +195,16 @@ router.push({
})
router.replace({ name: 'home' })

router.push('/', () => {}, () => {})
router.replace('/foo', () => {}, () => {})
router.push(
'/',
() => {},
() => {}
)
router.replace(
'/foo',
() => {},
() => {}
)

// promises

Expand All @@ -204,7 +226,8 @@ router.forward()
const Components: (
| ComponentOptions<Vue>
| typeof Vue
| AsyncComponent)[] = router.getMatchedComponents()
| AsyncComponent
)[] = router.getMatchedComponents()

const vm = new Vue({
router,
Expand Down

0 comments on commit fda7067

Please sign in to comment.