Niedzielski has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/396463 )

Change subject: Update: allow query parameters in URL
......................................................................

Update: allow query parameters in URL

• Permit query parameters to be specified on URLs. e.g.,
  http://localhost:3000/wiki/Ice_cream? no longer fails to load on the
  server.

• Tack query parameters on as an extra parameter to Route.toPath()
  temporarily to enable testing. This will receive a proper typing
  treatment in T178615.

• Add a couple test links to the home page.

• Very minor function refactoring in the route module.

Bug: T178617
Bug: T178615
Change-Id: I74df728def05d5d6be0bd44f6f25e039bf38c3d6
---
M src/common/pages/home.tsx
M src/common/router/route.ts
2 files changed, 41 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/marvin refs/changes/63/396463/1

diff --git a/src/common/pages/home.tsx b/src/common/pages/home.tsx
index 2c38d5f..adb4091 100644
--- a/src/common/pages/home.tsx
+++ b/src/common/pages/home.tsx
@@ -23,7 +23,8 @@
   },
   { title: "Carrot cake", text: "Encoding redirect (301)" },
   { title: "Cheese_cake", text: "Redirect page (302)" },
-  { title: "Nonexistent_title", text: "Missing (404)" }
+  { title: "Nonexistent_title", text: "Missing (404)" },
+  { title: "Pizza", query: "foo=bar", text: "Query string" }
 ];
 
 const testPages = [
@@ -40,7 +41,8 @@
     title: "File:Vanilla_Ice_Cream_Cone_at_Camp_Manitoulin.jpg",
     text: "Redirect (external) and File page"
   },
-  { title: "Nonexistent_title", text: "Missing (404)" }
+  { title: "Nonexistent_title", text: "Missing (404)" },
+  { title: "Pizza", query: "foo=bar", text: "Query string" }
 ];
 
 export default {
@@ -75,15 +77,19 @@
             {testPages.map(
               ({
                 title,
+                query,
                 revision,
                 text
               }: {
                 title: PageTitleID | string;
+                query?: string;
                 revision?: string;
                 text: string;
               }) => (
                 <li>
-                  <Link href={wiki.toPath({ title, revision })}>{text}</Link>
+                  <Link href={wiki.toPath({ title, revision }, query)}>
+                    {text}
+                  </Link>
                 </li>
               )
             )}
@@ -96,11 +102,21 @@
                       be a button. */}
               <Link href={randomSummary.toPath()}>A random summary</Link>
             </li>
-            {testSummaries.map(({ title, text }) => (
-              <li>
-                <Link href={summary.toPath({ title })}>{text}</Link>
-              </li>
-            ))}
+            {testSummaries.map(
+              ({
+                title,
+                query,
+                text
+              }: {
+                title: PageTitleID | string;
+                query?: string;
+                text: string;
+              }) => (
+                <li>
+                  <Link href={summary.toPath({ title }, query)}>{text}</Link>
+                </li>
+              )
+            )}
           </ul>
         </Page>
       </App>
diff --git a/src/common/router/route.ts b/src/common/router/route.ts
index f38343e..7754450 100644
--- a/src/common/router/route.ts
+++ b/src/common/router/route.ts
@@ -74,11 +74,11 @@
   toParams(path: string): Params | void; // eslint-disable-line 
no-use-before-define
 
   /** Generates a URL path from Params. */
-  toPath(params: Params): string;
+  toPath(params: Params, query?: string): string;
 }
 
 export interface NoParamsRoute extends Route<undefined> {
-  toPath(params?: undefined): string;
+  toPath(params?: undefined, query?: string): string;
 }
 
 /**
@@ -90,15 +90,11 @@
  * Note: paramNames is equivalent to manually writing an ordered array of names
  * matching Route.path's encoding. e.g., `/^\/wiki\/([^/]+)$/i` and 
`["title"]`.
  */
-const toParams = ({
-  pathRegExp,
-  paramNames,
-  path
-}: {
-  pathRegExp: RegExp;
-  paramNames: pathToRegExp.Key[];
-  path: string;
-}) => {
+function toParams(
+  pathRegExp: RegExp,
+  paramNames: pathToRegExp.Key[],
+  path: string
+) {
   const matches = pathRegExp.exec(path);
   if (matches) {
     const [, ...paramValues] = matches;
@@ -109,15 +105,21 @@
     return params;
   }
   return undefined;
-};
+}
 
 export function newRoute<Params>({ path, page }: RouteConfig): Route<Params> {
   const paramNames: pathToRegExp.Key[] = [];
-  const pathRegExp = pathToRegExp(path, paramNames);
+  const pathRegExp = pathToRegExp(
+    path,
+    paramNames,
+    // Allow query parameters.
+    { endsWith: "?" }
+  );
+  const toPath = pathToRegExp.compile(path);
   return {
     path,
     page,
-    toParams: (path: string) => toParams({ pathRegExp, paramNames, path }),
-    toPath: pathToRegExp.compile(path)
+    toParams: path => toParams(pathRegExp, paramNames, path),
+    toPath: (path, query) => `${toPath(path)}${query ? `?${query}` : ""}`
   } as Route<Params>;
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/396463
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I74df728def05d5d6be0bd44f6f25e039bf38c3d6
Gerrit-PatchSet: 1
Gerrit-Project: marvin
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <[email protected]>
Gerrit-Reviewer: Sniedzielski <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to