]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: encode / decode params
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 4 Feb 2020 17:57:31 +0000 (18:57 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 13:08:29 +0000 (14:08 +0100)
src/matcher/index.ts
src/router.ts

index d2b089dec676f3e4f378b631b09d1dd6c702ec65..75278b9f5f3a79321f4d78f15d722a0ea6cf6285 100644 (file)
@@ -27,26 +27,25 @@ function removeTrailingSlash(path: string): string {
   return path.replace(TRAILING_SLASH_RE, '$1')
 }
 
-function applyToParam(
-  fn: (v: string) => string,
-  params: PathParams
-): PathParams {
-  const newParams: PathParams = {}
+// TODO: this should now be used by the router
+// function applyToParam(
+//   fn: (v: string) => string,
+//   params: PathParams
+// ): PathParams {
+//   const newParams: PathParams = {}
 
-  // TODO: could also normalize values like numbers and stuff
-  for (const key in params) {
-    const value = params[key]
-    newParams[key] = Array.isArray(value) ? value.map(fn) : fn(value)
-  }
+//   // TODO: could also normalize values like numbers and stuff
+//   for (const key in params) {
+//     const value = params[key]
+//     newParams[key] = Array.isArray(value) ? value.map(fn) : fn(value)
+//   }
 
-  return newParams
-}
+//   return newParams
+// }
 
 export function createRouterMatcher(
   routes: RouteRecord[],
-  globalOptions: PathParserOptions,
-  encodeParam: (param: string) => string,
-  decodeParam: (param: string) => string
+  globalOptions: PathParserOptions
 ): RouterMatcher {
   const matchers: RouteRecordMatcher[] = []
 
@@ -147,7 +146,7 @@ export function createRouterMatcher(
       params = location.params || currentLocation.params
       // params are automatically encoded
       // TODO: try catch to provide better error messages
-      path = matcher.stringify(applyToParam(encodeParam, params))
+      path = matcher.stringify(params)
 
       if ('redirect' in matcher.record) {
         const { redirect } = matcher.record
@@ -171,7 +170,7 @@ export function createRouterMatcher(
       // TODO: warning of unused params if provided
       if (!matcher) throw new NoRouteMatchError(location)
 
-      params = applyToParam(decodeParam, matcher.parse(location.path)!)
+      params = matcher.parse(location.path)!
       // no need to resolve the path with the matcher as it was provided
       // this also allows the user to control the encoding
       // TODO: check if the note above regarding encoding is still true
@@ -201,7 +200,7 @@ export function createRouterMatcher(
       if (!matcher) throw new NoRouteMatchError(location, currentLocation)
       name = matcher.record.name
       params = location.params || currentLocation.params
-      path = matcher.stringify(applyToParam(encodeParam, params))
+      path = matcher.stringify(params)
     }
 
     // this should never happen because it will mean that the user ended up in a route
index 9a83f689df3b18e1b92113378312229f25b68f89..258938c5c2d47bc48bdc43593d37d64eb25dc99c 100644 (file)
@@ -112,9 +112,7 @@ export function createRouter({
 }: RouterOptions): Router {
   const matcher: ReturnType<typeof createRouterMatcher> = createRouterMatcher(
     routes,
-    {},
-    encodeParam,
-    decode
+    {}
   )
 
   const beforeGuards = useCallbacks<NavigationGuard>()
@@ -130,14 +128,27 @@ export function createRouter({
     return history.base + to.fullPath
   }
 
+  // TODO: move these two somewhere else
   function encodeParams(params: RouteParams): RouteParams {
-    // TODO:
-    return params
+    const newParams: RouteParams = {}
+    for (const key in params) {
+      const value = params[key]
+      newParams[key] = Array.isArray(value)
+        ? value.map(encodeParam)
+        : encodeParam(value)
+    }
+
+    return newParams
   }
 
   function decodeParams(params: RouteParams): RouteParams {
-    // TODO:
-    return params
+    const newParams: RouteParams = {}
+    for (const key in params) {
+      const value = params[key]
+      newParams[key] = Array.isArray(value) ? value.map(decode) : decode(value)
+    }
+
+    return newParams
   }
 
   function resolve(
@@ -167,6 +178,7 @@ export function createRouter({
       return {
         ...locationNormalized,
         ...matchedRoute,
+        params: decodeParams(matchedRoute.params),
         redirectedFrom,
       }
     }