Skip to content

Improved types | Handling for Boolean and Float route params#2177

Merged
dac09 merged 8 commits intoredwoodjs:mainfrom
dac09:feature/better-named-route-params
Apr 2, 2021
Merged

Improved types | Handling for Boolean and Float route params#2177
dac09 merged 8 commits intoredwoodjs:mainfrom
dac09:feature/better-named-route-params

Conversation

@dac09
Copy link
Copy Markdown
Contributor

@dac09 dac09 commented Apr 1, 2021

Fixes #2169

What?

Lots of improvements to type generation, and handling params for routes

  • You can now specify Float and Boolean types in routes e.g.
      <Route path="/version/{version:Float}" page={VersionPage} name="version" />
      <Route path="/subscribed/{status:Boolean}" page={SubscribePage} name="subscribe" />

Not 100% sure how useful the boolean one is, but hey.

  • Generated named route types now respect the Int, Float and Boolean types passed in the route string
  • Named route types are now less restrictive i.e. gives you typechecking for params in your string, but also lets you add additional params (which are used as query params by router). Query params must be number, string or boolean

@dac09 dac09 requested review from Tobbe and peterp April 1, 2021 20:22
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2021

📦 PR Packages

Click to Show Package Download Links

https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/create-redwood-app-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-api-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-api-server-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-auth-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-cli-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-core-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-dev-server-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-eslint-config-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-eslint-plugin-redwood-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-forms-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-internal-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-prerender-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-router-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-structure-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-testing-0.28.4-d864ec4.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2177/redwoodjs-web-0.28.4-d864ec4.tgz

Install this PR by running yarn rw upgrade --pr 2177:0.28.4-d864ec4

@cannikin
Copy link
Copy Markdown
Contributor

cannikin commented Apr 1, 2021

Now, if you want to be more like RAILS (and who doesn't) then you may want Boolean to respond to "true" / "false" as well as 0 / 1... Rails specifically likes 0 / 1 for when you submit checkboxes in forms. The values submitted are 0 or 1 but when they come in as params Rails will automatically convert to real true or false.

If you want to get really crazy, what about "yes" / "no"? ;)

@dac09
Copy link
Copy Markdown
Contributor Author

dac09 commented Apr 1, 2021

Now, if you want to be more like RAILS (and who doesn't) then you may want Boolean to respond to "true" / "false" as well as 0 / 1... Rails specifically likes 0 / 1 for when you submit checkboxes in forms. The values submitted are 0 or 1 but when they come in as params Rails will automatically convert to real true or false.

If you want to get really crazy, what about "yes" / "no"? ;)

I'd say its a good idea, but I've had enough of named route params

mild_panic

@cannikin
Copy link
Copy Markdown
Contributor

cannikin commented Apr 1, 2021

HAHAHAHA <Close Pull Request>

).toEqual({
match: true,
params: {
floatyMcFloat: 1.58,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These params are for you @cannikin to enjoy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha Approved!

'type ParseRouteParameters<Route> = Route extends `${string}/{${infer Param}:${string}}/${infer Rest}` ? { [Entry in Param | keyof ParseRouteParameters<`/${Rest}`>]: string } : Route extends `${string}/{${infer Param}:${string}}` ? { [Entry in Param]: string } : Route extends `${string}/{${infer Param}}` ? { [Entry in Param]: string } : Record<string, string>'
'type ParamType<constraint> = constraint extends "Int" ? number : constraint extends "Boolean" ? boolean : constraint extends "Float" ? number : string;' +
'\n' +
'type RouteParams<Route> = Route extends `${string}/{${infer Param}:${infer Constraint}}/${infer Rest}` ? { [Entry in Param | keyof RouteParams<`/${Rest}`>]: ParamType<Constraint> } : Route extends `${string}/{${infer Param}:${infer Constraint}}` ? { [Entry in Param]: ParamType<Constraint> } : Route extends `${infer Constraint}/{${infer Param}}` ? { [Entry in Param]: ParamType<Constraint> } : Record<string, string | number>'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

Comment thread packages/router/src/util.ts Outdated
Comment thread packages/router/src/util.ts Outdated
@dac09
Copy link
Copy Markdown
Contributor Author

dac09 commented Apr 2, 2021

Now, if you want to be more like RAILS (and who doesn't) then you may want Boolean to respond to "true" / "false" as well as 0 / 1... Rails specifically likes 0 / 1 for when you submit checkboxes in forms. The values submitted are 0 or 1 but when they come in as params Rails will automatically convert to real true or false.

If you want to get really crazy, what about "yes" / "no"? ;)

Hmm in all seriousness should we implement 0/1? Would it actually be a legit case in a url parameter (and not a query param?). Query accepting 0/1 makes sense, but the path itself... dunno. Typescript for example would complain that you're trying to set a number instead of a bool

@cannikin
Copy link
Copy Markdown
Contributor

cannikin commented Apr 2, 2021

OHHHH good point, these are path params, not query params. Nevermind!

@Tobbe
Copy link
Copy Markdown
Contributor

Tobbe commented Apr 2, 2021

Just so we don't forget to document this: https://github.com/redwoodjs/redwoodjs.com/issues/658

@dac09 dac09 merged commit 4642797 into redwoodjs:main Apr 2, 2021
@dac09 dac09 deleted the feature/better-named-route-params branch April 2, 2021 17:22
@thedavidprice thedavidprice added this to the next release milestone Apr 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Route parameter type provides wrong type for named routes

4 participants