tatethurston / nextjs-routes

Type safe routing for Next.js
MIT License
571 stars 23 forks source link

Question: Other query parameters #17

Closed trevoro closed 2 years ago

trevoro commented 2 years ago

Hi,

We're using this library and it's great for mapping the slug parts of a path to their required values or query parameters. However we also using actual query string values for certain items and it appears as though that isn't possible here without the types throwing an error.

Any recommended way to do something like this?

              <Link
                href={{
                  pathname: '/api//foo/[id]',
                  query: {
                    id: 'bar',
                    mode: 'baz', // this complains
                  },
                }}
              >
                <a>A Thing</a>
              </Link>

To me it would be nice to change the query value to params and then just let people merge in their own query parameters in an un-type-safe kind of way. Otherwise people are prevented from making use of some pretty important browser functionality.

tatethurston commented 2 years ago

Hey @trevoro thanks for opening this. I held off from implementing this, hoping to get https://github.com/vercel/next.js/issues/35791 addressed in Next.js. That seems unlikely to happen, so I'm happy to widen the query types.

Roughly, making the query type something like this for your example:

interface Query {
  id: string;
  [key: string]: string
}

will enforce the required key (id), but allow arbitrary extra keys for query params.