From 60850c39d35e3c4eb92997c0cbf644d1decd9eb1 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 10 Aug 2019 23:36:59 +0200 Subject: [PATCH] feat: add empty meta --- __tests__/guards/global-beforeEach.spec.js | 2 +- __tests__/matcher.spec.js | 16 ++++++++-------- __tests__/router.spec.js | 2 ++ src/errors.ts | 14 +++++++++++++- src/router.ts | 2 ++ src/types/index.ts | 3 +++ 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/__tests__/guards/global-beforeEach.spec.js b/__tests__/guards/global-beforeEach.spec.js index a2d288cb..f09837b0 100644 --- a/__tests__/guards/global-beforeEach.spec.js +++ b/__tests__/guards/global-beforeEach.spec.js @@ -28,7 +28,7 @@ const routes = [ { path: '/', component: Home }, { path: '/foo', component: Foo }, { path: '/other', component: Foo }, - { path: '/n/:i', name: 'n', component: Home }, + { path: '/n/:i', name: 'n', component: Home, meta: { requiresLogin: true } }, { path: '/nested', component: Nested, diff --git a/__tests__/matcher.spec.js b/__tests__/matcher.spec.js index 3f5fd9b3..d0af86dc 100644 --- a/__tests__/matcher.spec.js +++ b/__tests__/matcher.spec.js @@ -138,7 +138,7 @@ describe('Router Matcher', () => { expect( assertErrorMatch({ path: '/', component }, { path: '/foo' }) ).toMatchInlineSnapshot( - `[NoRouteMatchError: No match for {"path":"/foo","params":{},"query":{},"hash":"","fullPath":"/"}]` + `[NoRouteMatchError: No match for {"path":"/foo","params":{},"query":{},"hash":"","fullPath":"/","meta":{}}]` ) }) }) @@ -164,7 +164,7 @@ describe('Router Matcher', () => { expect( assertErrorMatch({ path: '/', component }, { name: 'Home' }) ).toMatchInlineSnapshot( - `[NoRouteMatchError: No match for {"path":"/","name":"Home","params":{},"query":{},"hash":"","fullPath":"/"}]` + `[NoRouteMatchError: No match for {"path":"/","name":"Home","params":{},"query":{},"hash":"","fullPath":"/","meta":{}}]` ) }) }) @@ -411,12 +411,12 @@ describe('Router Matcher', () => { { path: '/redirect', params: {}, matched: [], name: undefined } ) ).toMatchInlineSnapshot(` - [InvalidRouteMatch: Cannot redirect using a relative location: - { - "params": {} - } - Use the function redirect and explicitely provide a name] - `) + [InvalidRouteMatch: Cannot redirect using a relative location: + { + "params": {} + } + Use the function redirect and explicitely provide a name] + `) }) it('normalize a location when redirecting', () => { diff --git a/__tests__/router.spec.js b/__tests__/router.spec.js index 1990e9ab..6fc5fc5b 100644 --- a/__tests__/router.spec.js +++ b/__tests__/router.spec.js @@ -37,11 +37,13 @@ describe('Router', () => { const history = new HistoryMock() const router = new Router({ history, routes }) expect(router.currentRoute).toEqual({ + name: undefined, fullPath: '/', hash: '', params: {}, path: '/', query: {}, + meta: {}, }) }) diff --git a/src/errors.ts b/src/errors.ts index 28673583..8361c970 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -52,6 +52,7 @@ export class NoRouteMatchError extends RouterError { private [isNoRouteMatchError] = true constructor(currentLocation: any, location: any) { + // TODO: change the merge to provide information that is useful only super('No match for ' + JSON.stringify({ ...currentLocation, ...location })) } @@ -196,8 +197,19 @@ export class NavigationCancelled extends RouterError { } } +const propertiesToLog: (keyof RouteLocationNormalized)[] = [ + 'params', + 'query', + 'hash', +] + function stringifyRoute(to: RouteLocation): string { if (typeof to === 'string') return to if ('path' in to) return to.path - return JSON.stringify(to, null, 2) + const location: Partial = {} + for (const key of propertiesToLog) { + // @ts-ignore + if (key in to) location[key] = to[key] + } + return JSON.stringify(location, null, 2) } diff --git a/src/router.ts b/src/router.ts index 7180b6de..dfafeb5d 100644 --- a/src/router.ts +++ b/src/router.ts @@ -153,6 +153,7 @@ export class Router { query: this.history.utils.normalizeQuery(location.query || {}), hash: location.hash, redirectedFrom, + meta: {}, } if (typeof redirect === 'string') { @@ -208,6 +209,7 @@ export class Router { ...matchedRoute, ...url, redirectedFrom, + meta: {}, } } } diff --git a/src/types/index.ts b/src/types/index.ts index 0193e990..5badd9cb 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -56,6 +56,7 @@ export interface RouteLocationNormalized name: string | void matched: MatchedRouteRecord[] // non-enumerable redirectedFrom?: RouteLocationNormalized + meta: Record } // interface PropsTransformer { @@ -108,6 +109,7 @@ interface RouteRecordCommon { path: string // | RegExp name?: string beforeEnter?: NavigationGuard | NavigationGuard[] + meta?: Record } export type RouteRecordRedirectOption = @@ -142,6 +144,7 @@ export const START_LOCATION_NORMALIZED: RouteLocationNormalized = { hash: '', fullPath: '/', matched: [], + meta: {}, } // make matched non enumerable for easy printing -- 2.39.5