]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
wip: add types for redirect in next
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 20:54:34 +0000 (22:54 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 20:54:34 +0000 (22:54 +0200)
src/types/index.ts
src/utils/guardToPromiseFn.ts
src/utils/index.ts

index cff6a64a349051d6fbf42389231d3e0cbf341820..eab328be41a8013bd60c4ea69d12537c38ccb5bf 100644 (file)
@@ -147,6 +147,7 @@ export interface MatcherLocationNormalized {
 
 export interface NavigationGuardCallback {
   (): void
+  (location: RouteLocation): void
   (valid: false): void
 }
 
index 7dd188481c562dc8e28d25f6be457bb92d90176d..1d5f93772613aedecb849539d18c9fb6660cf76c 100644 (file)
@@ -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<void> {
   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)
index 892c56a966005f4b06a0381d00625e42f48119a2..d8cd3f9d688e875fa70800647472d2a97f7d8176 100644 (file)
@@ -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<T>(array: T[]): T {
   return array[array.length - 1]
 }
+
+export function isRouteLocation(route: any): route is RouteLocation {
+  return typeof route === 'string' || (route && typeof route === 'object')
+}