From: Eduardo San Martin Morote Date: Fri, 3 May 2019 15:21:24 +0000 (+0200) Subject: types: add redirect option to routerecord X-Git-Tag: v4.0.0-alpha.0~399 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5ff1004dfc7aa3d96a7ea8a953801b269eb12a09;p=thirdparty%2Fvuejs%2Frouter.git types: add redirect option to routerecord --- diff --git a/src/matcher.ts b/src/matcher.ts index 75a3b326..1ff6ed46 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -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] diff --git a/src/types/index.ts b/src/types/index.ts index bebd9c20..2c09ecfa 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -39,13 +39,15 @@ export type RouteLocation = | RouteQueryAndHash & LocationAsRelative & RouteLocationOptions // exposed to the user in a very consistant way +export type MatchedRouteRecord = Exclude + export interface RouteLocationNormalized extends Required { 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 } @@ -106,7 +113,10 @@ interface RouteRecordMultipleViews extends RouteRecordCommon { components: Record> } -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 { diff --git a/src/utils/index.ts b/src/utils/index.ts index d8cd3f9d..fb52bad1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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