]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: add empty meta
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 10 Aug 2019 21:36:59 +0000 (23:36 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 10 Aug 2019 21:37:12 +0000 (23:37 +0200)
__tests__/guards/global-beforeEach.spec.js
__tests__/matcher.spec.js
__tests__/router.spec.js
src/errors.ts
src/router.ts
src/types/index.ts

index a2d288cb1e00708ff466d95940c9632602d939e8..f09837b0336501bbafb353bec9a85700cdd24021 100644 (file)
@@ -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,
index 3f5fd9b3dc9de7215aae7072e8eef6b21186dc09..d0af86dc4dc588641b7ac3665c8f4d6a5cbf66e9 100644 (file)
@@ -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', () => {
index 1990e9abefc124101410fbd9952002d55620b2c5..6fc5fc5b19b565a47335ae7f6bfc4aa576249377 100644 (file)
@@ -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: {},
     })
   })
 
index 286735831fb79e3e4dddfb1829c9590ae6949393..8361c9706f9457ab4bb383656b197a8669523864 100644 (file)
@@ -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<RouteLocationNormalized> = {}
+  for (const key of propertiesToLog) {
+    // @ts-ignore
+    if (key in to) location[key] = to[key]
+  }
+  return JSON.stringify(location, null, 2)
 }
index 7180b6de7d632798713f17eb089b87e745e0bf80..dfafeb5d4acde0ef86ce3658bd9307734d3d8c53 100644 (file)
@@ -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: {},
       }
     }
   }
index 0193e990cdfc1530c620c65e2364b27fce84824e..5badd9cb4d93cc2341d828867c23814c0f494340 100644 (file)
@@ -56,6 +56,7 @@ export interface RouteLocationNormalized
   name: string | void
   matched: MatchedRouteRecord[] // non-enumerable
   redirectedFrom?: RouteLocationNormalized
+  meta: Record<string | number | symbol, any>
 }
 
 // interface PropsTransformer {
@@ -108,6 +109,7 @@ interface RouteRecordCommon {
   path: string // | RegExp
   name?: string
   beforeEnter?: NavigationGuard | NavigationGuard[]
+  meta?: Record<string | number | symbol, any>
 }
 
 export type RouteRecordRedirectOption =
@@ -142,6 +144,7 @@ export const START_LOCATION_NORMALIZED: RouteLocationNormalized = {
   hash: '',
   fullPath: '/',
   matched: [],
+  meta: {},
 }
 
 // make matched non enumerable for easy printing