diff --git a/package.json b/package.json index d2c1e12..97d053e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typesafe-routes", - "version": "12.0.0-beta.1", + "version": "12.0.0-beta.2", "repository": "git@github.com:kruschid/typesafe-routes.git", "author": "Denis Kruschinski ", "license": "MIT", diff --git a/src/types.ts b/src/types.ts index e076131..0aa2061 100644 --- a/src/types.ts +++ b/src/types.ts @@ -101,8 +101,8 @@ export type RoutesProps< $render: ( params: ComputeParamRecordMap ) => ReturnType; - $parseParams: (params: Record) => Params["path"]; - $parseQuery: (params: Record) => Params["query"]; + $parseParams: (params: Record | string) => Params["path"]; + $parseQuery: (params: Record | string) => Params["query"]; $bind: ( params: ComputeParamRecordMap ) => RoutesProps; diff --git a/test/test.ts b/test/test.ts index b461449..6a58011 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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", }), { @@ -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(); @@ -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", }), @@ -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(); }); diff --git a/test/types.ts b/test/types.ts index 0099cae..54b229b 100644 --- a/test/types.ts +++ b/test/types.ts @@ -107,36 +107,36 @@ expectType< expectType<(params: Record) => unknown>(r.$parseParams); expectType< - (params: Record) => { + (params: Record | string) => { lang: string; } >(r.language.$parseParams); expectType< - (params: Record) => { + (params: Record | string) => { lang: string; } >(r.language.users.$parseParams); expectType< - (params: Record) => { + (params: Record | string) => { lang: string; userId: number; } >(r.language.users.show.$parseParams); -expectType<(params: Record) => {}>( +expectType<(params: Record | string) => {}>( r.language._.users.$parseParams ); expectType< - (params: Record) => { + (params: Record | string) => { userId: number; } >(r.language._.users.show.$parseParams); expectType< - (params: Record) => { + (params: Record | string) => { userId: number; } >(r.language.users._.show.$parseParams); @@ -144,38 +144,40 @@ expectType< // // query // -expectType<(params: Record) => {}>(r.home.$parseQuery); +expectType<(params: Record | string) => {}>(r.home.$parseQuery); -expectType<(params: Record) => {}>(r.language.$parseQuery); +expectType<(params: Record | string) => {}>( + r.language.$parseQuery +); expectType< - (params: Record) => { + (params: Record | string) => { page: number; } >(r.language.users.$parseQuery); expectType< - (params: Record) => { + (params: Record | string) => { page: number; filter?: boolean | undefined; } >(r.language.users.show.$parseQuery); expectType< - (params: Record) => { + (params: Record | string) => { page: number; } >(r.language._.users.$parseQuery); expectType< - (params: Record) => { + (params: Record | string) => { page: number; filter?: boolean | undefined; } >(r.language._.users.show.$parseQuery); expectType< - (params: Record) => { + (params: Record | string) => { filter?: boolean | undefined; } >(r.language.users._.show.$parseQuery);