From d76c6aae115110e2d9c4c072748bd9403080c8bd Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 17 Mar 2020 10:59:57 +0100 Subject: [PATCH] feat: allow true in `next` --- .../guards/component-beforeRouteEnter.spec.ts | 7 +++++-- playground/views/User.vue | 8 ++++++++ src/types/index.ts | 4 +++- src/utils/guardToPromiseFn.ts | 16 ++++++++++++---- src/utils/index.ts | 3 ++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/__tests__/guards/component-beforeRouteEnter.spec.ts b/__tests__/guards/component-beforeRouteEnter.spec.ts index f807b67bc..5f4f48bb2 100644 --- a/__tests__/guards/component-beforeRouteEnter.spec.ts +++ b/__tests__/guards/component-beforeRouteEnter.spec.ts @@ -215,8 +215,8 @@ describe('beforeRouteEnter', () => { expect(router.currentRoute.value.fullPath).toBe('/foo') }) - // not implemented yet as it depends on Vue 3 Suspense - it.skip('calls next callback', done => { + // TODO: + it.skip('calls next callback', async done => { const router = createRouter({ routes }) beforeRouteEnter.mockImplementationOnce((to, from, next) => { next(vm => { @@ -225,6 +225,9 @@ describe('beforeRouteEnter', () => { done() }) }) + + await router.push('/') + await router.push('/guard/2') }) it.skip('calls next callback after waiting', async done => { diff --git a/playground/views/User.vue b/playground/views/User.vue index 8384bf8ee..86ccd0c04 100644 --- a/playground/views/User.vue +++ b/playground/views/User.vue @@ -10,5 +10,13 @@ export default defineComponent({ props: { id: String, }, + + beforeRouteUpdate(to, from, next) { + // TODO: these do not work yet + console.log('this', this) + next(vm => { + console.log('in next', vm) + }) + }, }) diff --git a/src/types/index.ts b/src/types/index.ts index 74636cc0d..b42faf5af 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -217,10 +217,12 @@ export interface MatcherLocationRedirect { export interface NavigationGuardCallback { (): void (location: RouteLocation): void - (valid: false): void + (valid: boolean): void (cb: (vm: any) => void): void } +export type NavigationGuardNextCallback = (vm: any) => any + export interface NavigationGuard { ( this: V, diff --git a/src/utils/guardToPromiseFn.ts b/src/utils/guardToPromiseFn.ts index 0cd49c641..29bf6e6fd 100644 --- a/src/utils/guardToPromiseFn.ts +++ b/src/utils/guardToPromiseFn.ts @@ -3,6 +3,8 @@ import { RouteLocationNormalized, NavigationGuardCallback, RouteLocation, + RouteLocationNormalizedResolved, + NavigationGuardNextCallback, } from '../types' import { isRouteLocation } from '../types' @@ -11,18 +13,24 @@ import { NavigationGuardRedirect, NavigationAborted } from '../errors' export function guardToPromiseFn( guard: NavigationGuard, to: RouteLocationNormalized, - from: RouteLocationNormalized + from: RouteLocationNormalizedResolved + // record?: RouteRecordNormalized ): () => Promise { return () => new Promise((resolve, reject) => { const next: NavigationGuardCallback = ( - valid?: boolean | RouteLocation + valid?: boolean | RouteLocation | NavigationGuardNextCallback ) => { - // TODO: handle callback if (valid === false) reject(new NavigationAborted(to, from)) else if (isRouteLocation(valid)) { reject(new NavigationGuardRedirect(to, valid)) - } else resolve() + } else if (!valid || valid === true) { + resolve() + } else { + // TODO: call the in component enter callbacks. Maybe somewhere else + // record && record.enterCallbacks.push(valid) + resolve() + } } guard(to, from, next) diff --git a/src/utils/index.ts b/src/utils/index.ts index 60c9d019c..6167d396a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,6 +3,7 @@ import { RouteParams, Immutable, RouteComponent, + RouteLocationNormalizedResolved, } from '../types' import { guardToPromiseFn } from './guardToPromiseFn' import { RouteRecordNormalized } from '../matcher/types' @@ -23,7 +24,7 @@ export function extractComponentsGuards( matched: RouteRecordNormalized[], guardType: GuardType, to: RouteLocationNormalized, - from: RouteLocationNormalized + from: RouteLocationNormalizedResolved ) { const guards: Array<() => Promise> = []