-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[$250] [Cleanup] Utility URL type for getURLWithBackParam, etc... #32143
Comments
Job added to Upwork: https://www.upwork.com/jobs/~01ab405b1ad68ca814 |
Triggered auto assignment to @MitchExpensify ( |
Triggered auto assignment to Contributor-plus team member for initial proposal review - @jjcoffee ( |
Upwork job price has been updated to $250 |
Removing |
Hey! I’m Blazej from Software Mansion, an expert agency, and I’d like to work on this issue! |
ProposalPlease re-state the problem that we are trying to solve in this issue.Making URLs in code is complex and hard to read with the current getUrlWithBackToParam function. What is the root cause of that problem?The existing function is too detailed and prone to mistakes. What changes do you think we should make in order to solve the problem?Introduce a new tool, UrlWithParams, and a simpler function, getUrlWithParams, for easier and clearer URL construction. Will just need to add a only this piece of code will be replaced with two functions.
|
📣 @hamzasaleem2! 📣
|
Contributor details |
✅ Contributor details stored successfully. Thank you for contributing to Expensify! |
@hamzasaleem2 I'll work on this, as Rory mentioned here. Just need a little more time as I have some other priorities atm. |
Not accepting other proposals for this as this issue was deferred feedback from a PR review |
@jjcoffee, @MitchExpensify, @blazejkustra, @roryabraham Eep! 4 days overdue now. Issues have feelings too... |
Not overdue, taking a look today 😄 edit: I have a PoC, will share it tomorrow. |
@roryabraham I took a different approach than the one described in the issue. After examining type URL<TUrl extends string, TQuery extends Record<string, string>> =
| `${TUrl}?${TQuery['key1']}=${string}&${TQuery['key2']}=${string}...`
| `${TUrl}?${TQuery['key2']}=${string}&${TQuery['key1']}=${string}...`
| ... ; I think such granularity isn't needed when it comes to query params. It is far less readable compared to a simple type QueryParams = Record<string, string | number | boolean | undefined>;
declare function getUrlWithQueryParams<TUrl extends string, TQuery extends QueryParams>(url: TUrl, query: TQuery): `${TUrl}?${string}`; Examples: getUrlWithQueryParams('r/123', {backTo: "value", arg: "value2"})
// results in type `r/123?${string}` | `r/123`
getUrlWithQueryParams(`settings/profile/personal-details/address/country`, {backTo, country})
// results in type `settings/profile/personal-details/address/country?${string}` | `settings/profile/personal-details/address/country` cc'ing @BartoszGrajdek @fabioh8010, what do you think? |
@blazejkustra I think your solution is enough for our use cases, I don't see too much value in being super explicit about the query params in the string type, just |
@blazejkustra I think it makes sense. Although we have been quite strict in typing in some cases, this could be an exception. The Navigation is already complicated enough as it is, and adding more advanced and strict types than necessary will make it harder for us to refactor/work later on. |
I personally think that it would be more valuable to be explicit about which URL params, specifically, are expected/allowed for a specific route, and what specifically their type should be. @blazejkustra The first code snippet you showed looked best to me, though I wonder if there's a way to DRY it up / generalize it a bit rather than having to manually list out all possible arrangements when we have potentially |
@roryabraham Maybe I'm missing something, what would be the benefit of such strictness? 🤔
I think it's already handled quite well, each EXAMPLE: 'example', // no query params for example route
DETAILS: {
route: 'details',
getRoute: (login: string) => getUrlWithQueryParams('details', {login}),
}, // details route requires a login query param
PROFILE: {
route: 'a/:accountID',
getRoute: (accountID: string | number, backTo?: string) => getUrlWithQueryParams(`a/${accountID}`, {backTo}),
}, // profile route has an optional backTo query param
Listing all the possible combinations of query parameters in a template-literal string won't be done manually by listing all possibilities. I just wanted to show what would be the resulting type. I didn't code it as it's not trivial 😅 |
My thinking is that you'd be unable to pass unexpected params or miss required params when navigating to a screen. But it sounds like I'm alone in thinking this would bring any additional value, so I'll trust you guys and close this out. |
Coming from #31543 (comment)...
Problem
We've got some types that look like this:
and while that works it's a pretty verbose way to express a pretty common format for something.
Solution
This is a rich representation of what this string could / should look like, instead of just being
string
, but let's clean this up to make it more extensible/reusable. We should make a utility type for URLs that takes:Then the resultant type would be any string matching the pattern of a valid URL with the correct base and query params.
Upwork Automation - Do Not Edit
The text was updated successfully, but these errors were encountered: