]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
types: add redirect option to routerecord
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 3 May 2019 15:21:24 +0000 (17:21 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 3 May 2019 15:21:24 +0000 (17:21 +0200)
src/matcher.ts
src/types/index.ts
src/utils/index.ts

index 75a3b32655d5ce393969ed78bc2ad74f426eb9b3..1ff6ed464b1ce5a29884aa1e58f4076b1f3c9eb8 100644 (file)
@@ -15,7 +15,7 @@ interface RouteMatcher {
   keys: string[]
 }
 
-function generateMatcher(record: RouteRecord) {
+function generateMatcher(record: RouteRecord): RouteMatcher {
   const keys: pathToRegexp.Key[] = []
   // TODO: if children use option end: false ?
   const re = pathToRegexp(record.path, keys)
@@ -57,6 +57,9 @@ export class RouterMatcher {
 
       if (!matcher) throw new NoRouteMatchError(currentLocation, location)
       // TODO: build up the array with children based on current location
+
+      if ('redirect' in matcher.record) throw new Error('TODO')
+
       const matched = [matcher.record]
 
       const params: RouteParams = {}
@@ -92,6 +95,8 @@ export class RouterMatcher {
       matcher = this.matchers.find(m => m.record.name === location.name)
 
       if (!matcher) throw new NoRouteMatchError(currentLocation, location)
+      if ('redirect' in matcher.record) throw new Error('TODO')
+
       // TODO: build up the array with children based on current location
       const matched = [matcher.record]
 
@@ -115,6 +120,8 @@ export class RouterMatcher {
     }
 
     if (!matcher) throw new NoRouteMatchError(currentLocation, location)
+    if ('redirect' in matcher.record) throw new Error('TODO')
+
     // TODO: build up the array with children based on current location
     const matched = [matcher.record]
 
index bebd9c20540072f7744d74623d058abd4dcc1744..2c09ecfaa50d70ca0b7e45c36c9d7614fe371100 100644 (file)
@@ -39,13 +39,15 @@ export type RouteLocation =
   | RouteQueryAndHash & LocationAsRelative & RouteLocationOptions
 
 // exposed to the user in a very consistant way
+export type MatchedRouteRecord = Exclude<RouteRecord, RouteRecordRedirect>
+
 export interface RouteLocationNormalized
   extends Required<RouteQueryAndHash & LocationAsRelative & LocationAsPath> {
   fullPath: string
   query: HistoryQuery // the normalized version cannot have numbers
   // TODO: do the same for params
   name: string | void
-  matched: RouteRecord[] // non-enumerable
+  matched: MatchedRouteRecord[] // non-enumerable
 }
 
 // interface PropsTransformer {
@@ -98,6 +100,11 @@ interface RouteRecordCommon {
   beforeEnter?: NavigationGuard
 }
 
+type DynamicRedirect = (to: RouteLocationNormalized) => RouteLocation
+interface RouteRecordRedirect extends RouteRecordCommon {
+  redirect: RouteLocation | DynamicRedirect
+}
+
 interface RouteRecordSingleView extends RouteRecordCommon {
   component: RouteComponent | Lazy<RouteComponent>
 }
@@ -106,7 +113,10 @@ interface RouteRecordMultipleViews extends RouteRecordCommon {
   components: Record<string, RouteComponent | Lazy<RouteComponent>>
 }
 
-export type RouteRecord = RouteRecordSingleView | RouteRecordMultipleViews
+export type RouteRecord =
+  | RouteRecordSingleView
+  | RouteRecordMultipleViews
+  | RouteRecordRedirect
 
 export const START_RECORD: RouteRecord = {
   path: '/',
@@ -142,7 +152,7 @@ export interface MatcherLocationNormalized {
   path: string
   // record?
   params: RouteLocationNormalized['params']
-  matched: RouteRecord[]
+  matched: MatchedRouteRecord[]
 }
 
 export interface NavigationGuardCallback {
index d8cd3f9d688e875fa70800647472d2a97f7d8176..fb52bad1a34bb25d8c1023500109ef0b6ddd6f8d 100644 (file)
@@ -1,11 +1,15 @@
-import { RouteRecord, RouteLocationNormalized, RouteLocation } from '../types'
+import {
+  RouteLocationNormalized,
+  RouteLocation,
+  MatchedRouteRecord,
+} from '../types'
 import { guardToPromiseFn } from './guardToPromiseFn'
 
 export * from './guardToPromiseFn'
 
 type GuardType = 'beforeRouteEnter' | 'beforeRouteUpdate' | 'beforeRouteLeave'
 export async function extractComponentsGuards(
-  matched: RouteRecord[],
+  matched: MatchedRouteRecord[],
   guardType: GuardType,
   to: RouteLocationNormalized,
   from: RouteLocationNormalized