]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: pass meta properties to normalized locations
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 14 Aug 2019 09:22:35 +0000 (11:22 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 14 Aug 2019 09:22:35 +0000 (11:22 +0200)
__tests__/guards/global-beforeEach.spec.js
__tests__/matcher.spec.js
src/errors.ts
src/matcher.ts
src/router.ts
src/types/index.ts

index 4317abeaf990f51735fc57f2125084d664eb4c1d..d0ad04edf7342949931c1e730b4c1959667c9b56 100644 (file)
@@ -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)
         )
       })
     })
index d0af86dc4dc588641b7ac3665c8f4d6a5cbf66e9..3b70f325a2425a1b10a0f186d9f5fdd89b3dfd43 100644 (file)
@@ -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: {},
           }
         )
       })
index 8361c9706f9457ab4bb383656b197a8669523864..b63d2add5ea6ce111bdaa7c8a0ab2a912ce1945e 100644 (file)
@@ -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
+}
index f6d34fd3008c4bce922c11a1ac6201f1768371d4..aaa6ea9c865f5cf099ac93c0e5eeb3a018d7d715 100644 (file)
@@ -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 || {},
     }
   }
 }
index dfafeb5d4acde0ef86ce3658bd9307734d3d8c53..98ad5b800a2c610f9f051e8afa084801fbe495c1 100644 (file)
@@ -209,7 +209,6 @@ export class Router {
         ...matchedRoute,
         ...url,
         redirectedFrom,
-        meta: {},
       }
     }
   }
index 5badd9cb4d93cc2341d828867c23814c0f494340..782b57d84cb5c6cf4e09b92d6414df826b08b216 100644 (file)
@@ -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