Repository: tatethurston/nextjs-routes
Pull request #28 (2022-07-08 18:50:39)

tidy query formatting

codecov[bot]: # [Codecov](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston) Report
> Merging [#28](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston) (d630061) into [main](https://codecov.io/gh/tatethurston/nextjs-routes/commit/b91e3ffa14a11a58cd7d7492cea6fd1905ef75c2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston) (b91e3ff) will **decrease** coverage by `0.20%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main      #28      +/-   ##
==========================================
- Coverage   98.24%   98.03%   -0.21%     
==========================================
  Files           1        1              
  Lines          57       51       -6     
  Branches       13       13              
==========================================
- Hits           56       50       -6     
  Misses          1        1              
```


| [Impacted Files](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston) | Coverage Δ | |
|---|---|---|
| [src/utils.ts](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston#diff-c3JjL3V0aWxzLnRz) | `98.03% <100.00%> (-0.21%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston). Last update [b91e3ff...d630061](https://codecov.io/gh/tatethurston/nextjs-routes/pull/28?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Tate+Thurston).


Patch:
diff --git a/src/utils.ts b/src/utils.ts
index 2c76802..a281905 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -78,35 +78,31 @@ export function nextRoutes(files: string[]): Route[] {
 function getQueryInterface(
   query: Route["query"]
 ): [query: string, requiredKeys: number] {
-  let res = "{ ";
   let requiredKeys = 0;
-  Object.entries(query).forEach(([key, value]) => {
-    res += key;
-    switch (value) {
-      case "dynamic": {
-        requiredKeys += 1;
-        res += ": string";
-        break;
-      }
-      case "catch-all": {
-        requiredKeys += 1;
-        res += ": string[]";
-        break;
-      }
-      case "optional-catch-all": {
-        res += "?: string[] | undefined";
-        break;
-      }
-      // istanbul ignore next
-      default: {
-        const _exhaust: never = value;
-        return _exhaust;
+  const keys = Object.entries(query)
+    .map(([key, value]) => {
+      switch (value) {
+        case "dynamic": {
+          requiredKeys += 1;
+          return `${key}: string`;
+        }
+        case "catch-all": {
+          requiredKeys += 1;
+          return `${key}: string[]`;
+        }
+        case "optional-catch-all": {
+          return `${key}?: string[] | undefined`;
+        }
+        // istanbul ignore next
+        default: {
+          const _exhaust: never = value;
+          return _exhaust;
+        }
       }
-    }
-    res += ";";
-  });
-  res += " }";
-  return [res, requiredKeys];
+    })
+    .join("; ");
+
+  return [`{ ${keys} }`, requiredKeys];
 }
 
 export function generate(routes: Route[]): string {
@@ -127,7 +123,7 @@ declare module "nextjs-routes" {
           return `{ pathname: "${route.pathname}"; query?: Query | undefined }`;
         }
       })
-      .join("\n    | ")}
+      .join("\n    | ")};
 
   type Query<Params = {}> = Params & { [key: string]: string | undefined };
 }


Test patch:
diff --git a/e2e/nextjs-routes.d.ts b/e2e/nextjs-routes.d.ts
index 49e29f0..b1340d3 100644
--- a/e2e/nextjs-routes.d.ts
+++ b/e2e/nextjs-routes.d.ts
@@ -5,8 +5,8 @@
 // prettier-ignore
 declare module "nextjs-routes" {
   export type Route =
-    | { pathname: "/foos/[foo]"; query: Query<{ foo: string; }> }
-    | { pathname: "/"; query?: Query | undefined }
+    | { pathname: "/foos/[foo]"; query: Query<{ foo: string }> }
+    | { pathname: "/"; query?: Query | undefined };
 
   type Query<Params = {}> = Params & { [key: string]: string | undefined };
 }
diff --git a/src/__snapshots__/test.ts.snap b/src/__snapshots__/test.ts.snap
index ffd99bc..1bdb6d6 100644
--- a/src/__snapshots__/test.ts.snap
+++ b/src/__snapshots__/test.ts.snap
@@ -9,23 +9,23 @@ exports[`route generation typescript 1`] = `
 declare module \\"nextjs-routes\\" {
   export type Route =
     | { pathname: \\"/404\\"; query?: Query | undefined }
-    | { pathname: \\"/[foo]\\"; query: Query<{ foo: string; }> }
-    | { pathname: \\"/[foo]/[bar]/[baz]\\"; query: Query<{ foo: string;bar: string;baz: string; }> }
-    | { pathname: \\"/[foo]/bar/[baz]\\"; query: Query<{ foo: string;baz: string; }> }
-    | { pathname: \\"/[foo]/bar/[baz]/foo/[bar]\\"; query: Query<{ foo: string;baz: string;bar: string; }> }
-    | { pathname: \\"/[foo]/baz\\"; query: Query<{ foo: string; }> }
+    | { pathname: \\"/[foo]\\"; query: Query<{ foo: string }> }
+    | { pathname: \\"/[foo]/[bar]/[baz]\\"; query: Query<{ foo: string; bar: string; baz: string }> }
+    | { pathname: \\"/[foo]/bar/[baz]\\"; query: Query<{ foo: string; baz: string }> }
+    | { pathname: \\"/[foo]/bar/[baz]/foo/[bar]\\"; query: Query<{ foo: string; baz: string; bar: string }> }
+    | { pathname: \\"/[foo]/baz\\"; query: Query<{ foo: string }> }
     | { pathname: \\"/_debug/health-check\\"; query?: Query | undefined }
     | { pathname: \\"/_error\\"; query?: Query | undefined }
     | { pathname: \\"/api/[[...segments]]\\"; query?: Query | undefined }
-    | { pathname: \\"/api/[...segments]\\"; query: Query<{ segments: string[]; }> }
+    | { pathname: \\"/api/[...segments]\\"; query: Query<{ segments: string[] }> }
     | { pathname: \\"/api/bar\\"; query?: Query | undefined }
-    | { pathname: \\"/foo/[slug]\\"; query: Query<{ slug: string; }> }
+    | { pathname: \\"/foo/[slug]\\"; query: Query<{ slug: string }> }
     | { pathname: \\"/\\"; query?: Query | undefined }
     | { pathname: \\"/not-found\\"; query?: Query | undefined }
-    | { pathname: \\"/settings/bars/[bar]\\"; query: Query<{ bar: string; }> }
-    | { pathname: \\"/settings/bars/[bar]/baz\\"; query: Query<{ bar: string; }> }
+    | { pathname: \\"/settings/bars/[bar]\\"; query: Query<{ bar: string }> }
+    | { pathname: \\"/settings/bars/[bar]/baz\\"; query: Query<{ bar: string }> }
     | { pathname: \\"/settings/foo\\"; query?: Query | undefined }
-    | { pathname: \\"/settings\\"; query?: Query | undefined }
+    | { pathname: \\"/settings\\"; query?: Query | undefined };
 
   type Query<Params = {}> = Params & { [key: string]: string | undefined };
 }