From a4630318cc833c8e0ae44e59cb15de8b88633be6 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 14 Aug 2019 11:22:35 +0200 Subject: [PATCH] feat: pass meta properties to normalized locations --- __tests__/guards/global-beforeEach.spec.js | 4 +- __tests__/matcher.spec.js | 46 +++++++++++++++++----- src/errors.ts | 26 ++++++++++-- src/matcher.ts | 3 ++ src/router.ts | 1 - src/types/index.ts | 1 + 6 files changed, 66 insertions(+), 15 deletions(-) diff --git a/__tests__/guards/global-beforeEach.spec.js b/__tests__/guards/global-beforeEach.spec.js index 4317abea..d0ad04ed 100644 --- a/__tests__/guards/global-beforeEach.spec.js +++ b/__tests__/guards/global-beforeEach.spec.js @@ -234,7 +234,9 @@ describe('router.beforeEach', () => { await router[navigationMethod]('/n/2') expect(spy).toHaveBeenCalledTimes(1) expect(spy).toHaveBeenCalledWith( - expect.objectContaining({ meta: { requiresLogin: true } }) + expect.objectContaining({ meta: { requiresLogin: true } }), + expect.objectContaining({ meta: {} }), + expect.any(Function) ) }) }) diff --git a/__tests__/matcher.spec.js b/__tests__/matcher.spec.js index d0af86dc..3b70f325 100644 --- a/__tests__/matcher.spec.js +++ b/__tests__/matcher.spec.js @@ -34,6 +34,10 @@ describe('Router Matcher', () => { const matcher = new RouterMatcher(record) const targetLocation = {} + if (!('meta' in resolved)) { + resolved.meta = record[0].meta || {} + } + // add location if provided as it should be the same value if ('path' in location) { resolved.path = location.path @@ -138,7 +142,7 @@ describe('Router Matcher', () => { expect( assertErrorMatch({ path: '/', component }, { path: '/foo' }) ).toMatchInlineSnapshot( - `[NoRouteMatchError: No match for {"path":"/foo","params":{},"query":{},"hash":"","fullPath":"/","meta":{}}]` + `[NoRouteMatchError: No match for {"path":"/foo","params":{},"query":{},"hash":"","fullPath":"/"}]` ) }) }) @@ -164,7 +168,7 @@ describe('Router Matcher', () => { expect( assertErrorMatch({ path: '/', component }, { name: 'Home' }) ).toMatchInlineSnapshot( - `[NoRouteMatchError: No match for {"path":"/","name":"Home","params":{},"query":{},"hash":"","fullPath":"/","meta":{}}]` + `[NoRouteMatchError: No match for {"path":"/","name":"Home","params":{},"query":{},"hash":"","fullPath":"/"}]` ) }) }) @@ -181,6 +185,7 @@ describe('Router Matcher', () => { params: {}, path: '/home', matched: [record], + meta: {}, } ) }) @@ -196,6 +201,7 @@ describe('Router Matcher', () => { name: undefined, params: { id: 'ed', role: 'user' }, matched: [record], + meta: {}, } ) }) @@ -215,6 +221,7 @@ describe('Router Matcher', () => { name: 'UserEdit', params: { id: 'ed', role: 'user' }, matched: [], + meta: {}, } ) }) @@ -238,6 +245,7 @@ describe('Router Matcher', () => { name: 'UserEdit', params: { id: 'ed', role: 'user' }, matched: [record], + meta: {}, } ) }) @@ -257,6 +265,7 @@ describe('Router Matcher', () => { name: undefined, params: { id: 'ed', role: 'user' }, matched: [record], + meta: {}, } ) }) @@ -299,6 +308,7 @@ describe('Router Matcher', () => { params: {}, name: undefined, matched: [], + meta: {}, }, } ) @@ -323,6 +333,7 @@ describe('Router Matcher', () => { params: {}, name: undefined, matched: [], + meta: {}, }, } ) @@ -349,6 +360,7 @@ describe('Router Matcher', () => { params: {}, name: undefined, matched: [], + meta: {}, }, } ) @@ -372,6 +384,7 @@ describe('Router Matcher', () => { params: {}, name: undefined, matched: [], + meta: {}, }, } ) @@ -394,6 +407,7 @@ describe('Router Matcher', () => { params: {}, name: 'redirect', matched: [], + meta: {}, }, } ) @@ -408,15 +422,21 @@ describe('Router Matcher', () => { assertErrorMatch( { path: '/redirect', redirect: '/home' }, { params: {} }, - { path: '/redirect', params: {}, matched: [], name: undefined } + { + path: '/redirect', + params: {}, + matched: [], + name: undefined, + meta: {}, + } ) ).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', () => { @@ -443,6 +463,7 @@ describe('Router Matcher', () => { params: { a: 'foo' }, name: 'a', matched: [], + meta: {}, }, } ) @@ -463,7 +484,11 @@ describe('Router Matcher', () => { assertErrorMatch( record, {}, - { ...start, matched: start.matched.map(normalizeRouteRecord) } + { + ...start, + matched: start.matched.map(normalizeRouteRecord), + meta: {}, + } ) ).toMatchInlineSnapshot( `[NoRouteMatchError: No match for {"name":"home","params":{},"path":"/"}]` @@ -647,6 +672,7 @@ describe('Router Matcher', () => { matched: [], params: {}, path: '/foo/nested/a', + meta: {}, } ) }) diff --git a/src/errors.ts b/src/errors.ts index 8361c970..b63d2add 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,4 +1,9 @@ -import { RouteLocationNormalized, RouteLocation } from './types' +import { + RouteLocationNormalized, + RouteLocation, + MatcherLocationNormalized, + MatcherLocation, +} from './types' // we could use symbols, but this is for IE9 only and there is // not Symbol support anyway @@ -51,9 +56,15 @@ export class NoRouteMatchError extends RouterError { // @ts-ignore for IE inheritance support private [isNoRouteMatchError] = true - constructor(currentLocation: any, location: any) { + constructor( + currentLocation: MatcherLocationNormalized, + location: MatcherLocation + ) { // TODO: change the merge to provide information that is useful only - super('No match for ' + JSON.stringify({ ...currentLocation, ...location })) + super( + 'No match for ' + + JSON.stringify(mergeMatcherLocations(currentLocation, location)) + ) } static is(error: Error): error is NoRouteMatchError { @@ -213,3 +224,12 @@ function stringifyRoute(to: RouteLocation): string { } return JSON.stringify(location, null, 2) } + +function mergeMatcherLocations( + currentLocation: MatcherLocationNormalized, + location: MatcherLocation +) { + const merged = { ...currentLocation, ...location } + delete merged.meta + return merged +} diff --git a/src/matcher.ts b/src/matcher.ts index f6d34fd3..aaa6ea9c 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -369,6 +369,7 @@ export class RouterMatcher { path, matched: [], params, + meta: matcher.record.meta || {}, }, } } @@ -392,6 +393,7 @@ export class RouterMatcher { path, matched: [], params, + meta: matcher.record.meta || {}, }, } } @@ -426,6 +428,7 @@ export class RouterMatcher { path, params, matched, + meta: matcher.record.meta || {}, } } } diff --git a/src/router.ts b/src/router.ts index dfafeb5d..98ad5b80 100644 --- a/src/router.ts +++ b/src/router.ts @@ -209,7 +209,6 @@ export class Router { ...matchedRoute, ...url, redirectedFrom, - meta: {}, } } } diff --git a/src/types/index.ts b/src/types/index.ts index 5badd9cb..782b57d8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -166,6 +166,7 @@ export interface MatcherLocationNormalized { params: RouteLocationNormalized['params'] matched: MatchedRouteRecord[] redirectedFrom?: MatcherLocationNormalized + meta: RouteLocationNormalized['meta'] } // used when the route records requires a redirection -- 2.39.5