From: Eduardo San Martin Morote Date: Thu, 2 May 2019 20:54:34 +0000 (+0200) Subject: wip: add types for redirect in next X-Git-Tag: v4.0.0-alpha.0~401 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20549b4b6965c1cecef30219bd6934a81623450c;p=thirdparty%2Fvuejs%2Frouter.git wip: add types for redirect in next --- diff --git a/src/types/index.ts b/src/types/index.ts index cff6a64a..eab328be 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -147,6 +147,7 @@ export interface MatcherLocationNormalized { export interface NavigationGuardCallback { (): void + (location: RouteLocation): void (valid: false): void } diff --git a/src/utils/guardToPromiseFn.ts b/src/utils/guardToPromiseFn.ts index 7dd18848..1d5f9377 100644 --- a/src/utils/guardToPromiseFn.ts +++ b/src/utils/guardToPromiseFn.ts @@ -2,8 +2,11 @@ import { NavigationGuard, RouteLocationNormalized, NavigationGuardCallback, + RouteLocation, } from '../types' +import { isRouteLocation } from './index' + export function guardToPromiseFn( guard: NavigationGuard, to: RouteLocationNormalized, @@ -11,11 +14,15 @@ export function guardToPromiseFn( ): () => Promise { return () => new Promise((resolve, reject) => { - const next: NavigationGuardCallback = (valid?: boolean) => { + const next: NavigationGuardCallback = ( + valid?: boolean | RouteLocation + ) => { // TODO: better error // TODO: handle callback if (valid === false) reject(new Error('Aborted')) - else resolve() + else if (isRouteLocation(valid)) { + // TODO: redirect + } else resolve() } guard(to, from, next) diff --git a/src/utils/index.ts b/src/utils/index.ts index 892c56a9..d8cd3f9d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,4 @@ -import { RouteRecord, RouteLocationNormalized } from '../types' +import { RouteRecord, RouteLocationNormalized, RouteLocation } from '../types' import { guardToPromiseFn } from './guardToPromiseFn' export * from './guardToPromiseFn' @@ -46,3 +46,7 @@ export async function extractComponentsGuards( export function last(array: T[]): T { return array[array.length - 1] } + +export function isRouteLocation(route: any): route is RouteLocation { + return typeof route === 'string' || (route && typeof route === 'object') +}