Skip to content

Commit

Permalink
Fix #206 (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatethurston authored Nov 23, 2024
1 parent 8231dbd commit 11c98bc
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 104 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.2.4

- CLI invocation now reads next.config.js or next.config.mjs.
- Fix `route`'s handling of query keys whose value is `undefined`. Fixes #206. Thanks @sleepdotexe!

## 2.2.3

Expand Down
2 changes: 1 addition & 1 deletion examples/app/@types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */

// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion examples/cjs/types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */

// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion examples/intl/@types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */

// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */

// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/@types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */

// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-routes/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-routes",
"version": "2.2.4-rc.1",
"version": "2.2.4",
"description": "Type safe routing for Next.js",
"license": "MIT",
"author": "Tate <tatethurston@gmail.com>",
Expand Down
26 changes: 13 additions & 13 deletions packages/nextjs-routes/src/__snapshots__/core.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`route generation app directory generates routes 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -261,7 +261,7 @@ exports[`route generation app directory handles intercept routes 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -510,7 +510,7 @@ exports[`route generation app directory handles parallel routes 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -760,7 +760,7 @@ exports[`route generation app directory handles windows paths 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -1009,7 +1009,7 @@ exports[`route generation configuration i18n 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -1209,7 +1209,7 @@ exports[`route generation configuration outDir 1`] = `
"src/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -1385,7 +1385,7 @@ exports[`route generation configuration pageExtensions configured 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -1563,7 +1563,7 @@ exports[`route generation configuration pageExtensions default 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -1739,7 +1739,7 @@ exports[`route generation dedupes 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -1915,7 +1915,7 @@ exports[`route generation no routes 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -2091,7 +2091,7 @@ exports[`route generation pages and app directory generates routes 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -2342,7 +2342,7 @@ exports[`route generation transforms windows paths 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down Expand Up @@ -2518,7 +2518,7 @@ exports[`route generation typescript 1`] = `
"@types/nextjs-routes.d.ts",
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file will be automatically regenerated when your Next.js server is running.
// nextjs-routes version: 2.2.2
// nextjs-routes version: 2.2.4
/* eslint-disable */
// prettier-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-routes/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { findFiles, getAppDirectory, getPagesDirectory } from "./utils.js";
// by node 17+
// import pkg from "../package.json" assert { type: "json" };
const pkg = {
version: "2.2.2",
version: "2.2.4",
};

type QueryType = "dynamic" | "catch-all" | "optional-catch-all";
Expand Down
121 changes: 39 additions & 82 deletions packages/nextjs-routes/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,49 @@
import { route } from "./index.js";

describe(route, () => {
it("generates paths", () => {
// static
expect(route({ pathname: "/404" })).toEqual("/404");
expect(route({ pathname: "/settings/about" })).toEqual("/settings/about");
// dynamic
expect(route({ pathname: "/foos/[foo]", query: { foo: "bar" } })).toEqual(
"/foos/bar",
);
expect(
route({
pathname: "/foos/[foo]/bars/[bar]",
query: { foo: "bar", bar: "baz" },
}),
).toEqual("/foos/bar/bars/baz");
expect(
route({
pathname: "/[foo]/[bar]/[baz]",
query: { foo: "foo", bar: "bar", baz: "baz" },
}),
).toEqual("/foo/bar/baz");
// catch all
expect(
route({ pathname: "/[...segments]", query: { segments: ["foo"] } }),
).toEqual("/foo");
expect(
route({
pathname: "/[...segments]",
query: { segments: ["foo", "bar"] },
}),
).toEqual("/foo/bar");
// optional catch all
expect(
route({ pathname: "/[[...segments]]", query: { segments: [] } }),
).toEqual("/");
expect(
route({ pathname: "/[[...segments]]", query: { segments: undefined } }),
).toEqual("/");
expect(
route({
pathname: "/[[...segments]]/foos",
query: { segments: undefined },
}),
).toEqual("/foos");
// query params
expect(
route({ pathname: "/foos/[foo]", query: { foo: "foo", bar: "bar" } }),
).toEqual("/foos/foo?bar=bar");
expect(
route({
pathname: "/foos/[foo]",
query: { foo: "foo", bar: "bar", baz: ["1", "2", "3"] },
}),
).toEqual("/foos/foo?bar=bar&baz=1&baz=2&baz=3");
expect(
route({
pathname: "/foos/[foo]",
query: { foo: "foo", bar: "bar", baz: ["1", "2", "3"] },
hash: "foo",
}),
).toEqual("/foos/foo?bar=bar&baz=1&baz=2&baz=3#foo");
describe("generates paths", () => {
// prettier-ignore
test.each([
[{ pathname: "/404" }, "/404"],
[{ pathname: "/settings/about" }, "/settings/about"],
// dynamic
[{ pathname: "/foos/[foo]", query: { foo: "bar" } }, "/foos/bar"],
[{ pathname: "/foos/[foo]/bars/[bar]", query: { foo: "bar", bar: "baz" } }, "/foos/bar/bars/baz"],
[{ pathname: "/[foo]/[bar]/[baz]", query: { foo: "foo", bar: "bar", baz: "baz" } }, "/foo/bar/baz"],
// catch all
[{ pathname: "/[...segments]", query: { segments: ["foo"] } }, "/foo"],
[{ pathname: "/[...segments]", query: { segments: ["foo", "bar"] } }, "/foo/bar"],
// optional catch all
[{ pathname: "/[[...segments]]", query: { segments: [] } }, "/"],
[{ pathname: "/[[...segments]]", query: { segments: undefined } }, "/"],
[{ pathname: "/[[...segments]]/foos", query: { segments: undefined } }, "/foos"],
// query params
[{ pathname: "/foos/[foo]", query: { foo: "foo", bar: "bar" } }, "/foos/foo?bar=bar"],
[{ pathname: "/foos/[foo]", query: { foo: "foo", bar: "bar", baz: ["1", "2", "3"] } }, "/foos/foo?bar=bar&baz=1&baz=2&baz=3"],
[{ pathname: "/foos/[foo]", query: { foo: "foo", bar: "bar", baz: ["1", "2", "3"] }, hash: "foo" }, "/foos/foo?bar=bar&baz=1&baz=2&baz=3#foo"],
[{ pathname: "/foos/[foo]", query: { foo: "foo", bar: undefined, baz: '', foobar: '' } }, "/foos/foo?baz=&foobar="],
])("generates paths for %o", (input, expected) => {
expect(route(input)).toEqual(expected);
});
});

describe("options", () => {
describe("trailingSlash", () => {
it("when true", () => {
expect(
route({ pathname: "/settings/about" }, { trailingSlash: true }),
).toEqual("/settings/about/");
expect(
route(
{ pathname: "/foos/[foo]", query: { foo: "bar" } },
{ trailingSlash: true },
),
).toEqual("/foos/bar/");
});
it("when false", () => {
expect(
route({ pathname: "/settings/about" }, { trailingSlash: false }),
).toEqual("/settings/about");
expect(
route(
{ pathname: "/foos/[foo]", query: { foo: "bar" } },
{ trailingSlash: false },
),
).toEqual("/foos/bar");
});
describe.each([
["/settings/about", undefined, true, "/settings/about/"],
["/settings/about", undefined, false, "/settings/about"],
["/foos/[foo]", { foo: "bar" }, true, "/foos/bar/"],
["/foos/[foo]", { foo: "bar" }, false, "/foos/bar"],
])(
"route(%p, { trailingSlash: %p })",
(pathname, query, trailingSlash, expectedResult) => {
it(`returns ${expectedResult}`, () => {
expect(route({ pathname, query }, { trailingSlash })).toEqual(
expectedResult,
);
});
},
);
});
});
});
4 changes: 2 additions & 2 deletions packages/nextjs-routes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export function route(
const value = r.query[key];
if (Array.isArray(value)) {
value.forEach((val) => search.append(key, val));
} else {
search.append(key, value as string);
} else if (value !== undefined) {
search.append(key, value);
}
}
}
Expand Down

0 comments on commit 11c98bc

Please sign in to comment.