Skip to content

Commit

Permalink
fixes #56 $parseParams can accept string
Browse files Browse the repository at this point in the history
  • Loading branch information
kruschid committed Sep 6, 2024
1 parent 128b249 commit 307dd96
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typesafe-routes",
"version": "12.0.0-beta.1",
"version": "12.0.0-beta.2",
"repository": "[email protected]:kruschid/typesafe-routes.git",
"author": "Denis Kruschinski <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export type RoutesProps<
$render: (
params: ComputeParamRecordMap<Params>
) => ReturnType<Context["renderPath"]>;
$parseParams: (params: Record<string, any>) => Params["path"];
$parseQuery: (params: Record<string, any>) => Params["query"];
$parseParams: (params: Record<string, any> | string) => Params["path"];
$parseQuery: (params: Record<string, any> | string) => Params["query"];
$bind: (
params: ComputeParamRecordMap<Params>
) => RoutesProps<Routes, Context>;
Expand Down
77 changes: 70 additions & 7 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,21 @@ test("parsing path params", (t) => {
}
);

t.deepEqual(
routes.blog.category.date.$parseParams(
"blog/false/category/24/date/2024-11-29"
),
{
lang: false,
cid: 24,
date: new Date("2024-11-29T00:00:00.000Z"),
}
);

t.deepEqual(
routes.blog.category.date.$parseParams({
lang: "true",
cid: "0",
cid: "0", // potentially falsy
date: "2023-12-28",
}),
{
Expand All @@ -213,17 +224,45 @@ test("parsing path params", (t) => {
);

t.deepEqual(
routes.blog._.category.date.$parseParams({
cid: "42",
}),
{ cid: 42 }
routes.blog.category.date.$parseParams(
"blog/true/category/0/date/2024-11-29"
),
{
lang: true,
cid: 0,
date: new Date("2024-11-29T00:00:00.000Z"),
}
);

t.deepEqual(
routes.blog._.category.date.$parseParams({
cid: "42",
}),
{ cid: 42 }
{ cid: 42 },
"relative path with optional params"
);

t.deepEqual(
routes.blog._.category.date.$parseParams("category/42/date"),
{
cid: 42,
},
"relative path with omitted optional params in string path"
);

t.deepEqual(
routes.blog._.category.date.$parseParams("category/244/date/2024-10-29"),
{
cid: 244,
date: new Date("2024-10-29T00:00:00.000Z"),
},
"relative path with all optional params in string path"
);

t.throws(
() =>
routes.blog.category._.date.$parseParams("category/244/date/2024-10-29"),
"string path mismatch"
);

t.end();
Expand Down Expand Up @@ -266,9 +305,21 @@ test("parsing query params", (t) => {
);

t.deepEqual(
routes.blog._.category.date.$parseQuery({
routes.blog.category.date.$parseQuery(
"lang=en&category=drama&shortmovie=true&month=feb"
),
{
lang: "en",
category: "drama",
shortmovie: true,
month: "feb",
}
);

t.deepEqual(
routes.blog._.category.date.$parseQuery({
lang: "en", // ignores additional params
category: "drama",
shortmovie: "true",
month: "feb",
}),
Expand All @@ -279,8 +330,20 @@ test("parsing query params", (t) => {
}
);

t.deepEqual(
routes.blog._.category.date.$parseQuery(
"category=drama&shortmovie=true&month=feb"
),
{
category: "drama",
shortmovie: true,
month: "feb",
}
);

t.throws(() => routes.blog.category._.date.$parseQuery({}));
t.throws(() => routes.blog.category._.date.$parseQuery({ month: "jun" }));
t.throws(() => routes.blog._.category.date.$parseQuery("lang=en&category"));

t.end();
});
Expand Down
28 changes: 15 additions & 13 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,75 +107,77 @@ expectType<
expectType<(params: Record<string, any>) => unknown>(r.$parseParams);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
lang: string;
}
>(r.language.$parseParams);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
lang: string;
}
>(r.language.users.$parseParams);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
lang: string;
userId: number;
}
>(r.language.users.show.$parseParams);

expectType<(params: Record<string, any>) => {}>(
expectType<(params: Record<string, any> | string) => {}>(
r.language._.users.$parseParams
);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
userId: number;
}
>(r.language._.users.show.$parseParams);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
userId: number;
}
>(r.language.users._.show.$parseParams);

//
// query
//
expectType<(params: Record<string, any>) => {}>(r.home.$parseQuery);
expectType<(params: Record<string, any> | string) => {}>(r.home.$parseQuery);

expectType<(params: Record<string, any>) => {}>(r.language.$parseQuery);
expectType<(params: Record<string, any> | string) => {}>(
r.language.$parseQuery
);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
page: number;
}
>(r.language.users.$parseQuery);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
page: number;
filter?: boolean | undefined;
}
>(r.language.users.show.$parseQuery);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
page: number;
}
>(r.language._.users.$parseQuery);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
page: number;
filter?: boolean | undefined;
}
>(r.language._.users.show.$parseQuery);

expectType<
(params: Record<string, any>) => {
(params: Record<string, any> | string) => {
filter?: boolean | undefined;
}
>(r.language.users._.show.$parseQuery);
Expand Down

0 comments on commit 307dd96

Please sign in to comment.