Skip to content
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

Extract parameters from path #22

Open
Djaler opened this issue Aug 3, 2022 · 2 comments
Open

Extract parameters from path #22

Djaler opened this issue Aug 3, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@Djaler
Copy link

Djaler commented Aug 3, 2022

It would be nice to have an API to parse parameters back from path.

As example:

const routes = createRouting({
    users: segment`/users/${number('userId')}`,
});

const path = routes.users({ userId: '10' }) // '/users/10'
const params = routes.users.parse(path) // { userId: 10 }
@Djaler
Copy link
Author

Djaler commented Oct 10, 2022

Hello. Are you interested in PR for this feature?

@konowrockis
Copy link
Contributor

Hi, thanks for rising the issue! We were considering this feature, however we came to conclusion that in order to implement that we would need to make too many assumptions and it can't be made type safe without domain knowledge. That suggests that responsibility of parsing paths and ensuring that it's of correct type is entirely on the user of the library.

We'd really welcome a PR or at least a draft which would show your proposal for that.

Our main problem is how to revive the value - for example path-to-regexp's match returns all the parameters as string. That means we either return all the params as string which is only half usefull because you still need to make the conversion yourself or we provide some complex mechanism of reviving in the ts-routes. That however increases complexicity especially for custom segments.
We think it's doable however just not sure if using path-to-regexp match directly and figuring out solutions to those problems should be part of this library. We're open to discussion.

Thanks!

@alszczep alszczep added the enhancement New feature or request label May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants