Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Router parameter naming style conversion #465

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

Closed
tbontb-iaq opened this issue Aug 3, 2024 · 8 comments
Closed

Router parameter naming style conversion #465

tbontb-iaq opened this issue Aug 3, 2024 · 8 comments

Comments

@tbontb-iaq
Copy link

Currently, if I have pages/user/[u-id]/group/[g-id].vue, it will generate params like:

export interface RouteNamedMap {
  "/user/[u-id]/group/[g-id]": RouteRecordInfo<
    "/user/[u-id]/group/[g-id]",
    "/user/:u-id/group/:g-id",
    { u-id: ParamValue<true>; g-id: ParamValue<true> },
    { u-id: ParamValue<false>; g-id: ParamValue<false> }
  >;
}

And u-id and g-id are not valid JavaScript attribute names.

Is it possible to add some kind of automatic conversion? It should also be configurable to convert to multiple naming styles, like camelCase, underscores or raw style.

@posva
Copy link
Owner

posva commented Aug 3, 2024

You can indeed automatically convert them by using the extendRoute option. Something like this:

      extendRoute(route) {
                // transform kebab-case to camelCase
        route.params.forEach((param, i) => {
          const newParam = param.paramName.replace(/-([a-z])/g, (g) =>
            g[1].toUpperCase()
          )
          route.path = route.path.replace(`:${param.paramName}`, `:${newParam}`)
          param.paramName = newParam
        })
	  }

But also, the generated TS should be correct, so it should be 'g-id'.

@tbontb-iaq
Copy link
Author

Thanks for your answer. Will this become a built-in feature?

Copy link
Owner

posva commented Aug 4, 2024

Right now I don’t think it needs a dedicated option

@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Done in unplugin-vue-router Aug 4, 2024
@posva posva reopened this Aug 4, 2024
@github-project-automation github-project-automation bot moved this from ✅ Done to 📋 Backlog in unplugin-vue-router Aug 4, 2024
@tbontb-iaq
Copy link
Author

question/[q-id] will be converted to question/:q-id, which will match question/1234-id instead of question/1234. Any good ideas?

@posva
Copy link
Owner

posva commented Aug 4, 2024

I updated the function, it does require you to change the path too.

@tbontb-iaq
Copy link
Author

Not work, but got warning: [vite] ⚠️ [unplugin-vue-router]: Only absolute paths are supported. Make sure that ":q_id" starts with a slash "/".

@posva
Copy link
Owner

posva commented Aug 5, 2024

That's #341, so you will need a bit more code to handle it right now

@posva posva added 🐞 bug this isn't working as expected and removed 🐞 bug this isn't working as expected labels Aug 5, 2024
@posva
Copy link
Owner

posva commented Aug 5, 2024

I'm still not convinced at all for the need of an option to handle this. Naming the files so they match vue router params seems like a good practice

Repository owner locked and limited conversation to collaborators Aug 5, 2024
@posva posva converted this issue into discussion #468 Aug 5, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in unplugin-vue-router Aug 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants