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

Add Url2Json type #262

Closed
wants to merge 4 commits into from
Closed

Add Url2Json type #262

wants to merge 4 commits into from

Conversation

Asarua
Copy link

@Asarua Asarua commented Sep 10, 2021

Is a type that gets the query string from the URL

@sindresorhus
Copy link
Owner

What's the use-case?

@Asarua
Copy link
Author

Asarua commented Sep 10, 2021

What's the use-case?

Sorry, let me complete it

Required: Typescript > 4.1

Here is the demo code

const url = 'https://google.com?a=1&b=2'

type QueryString = Url2Json<typeof url>

it will be

{
    a: "1",
    b: "2"
}

@sindresorhus
Copy link
Owner

It's a cute idea, but I don't see how it will work in real-life scenarios. I doesn't and cannot handle escaping, which most URLs have:

const url = 'https://google.com?a=1%20foo&b=2'

Also, with most request libraries, you pass the query parameters as an object, not in the actual URL.

@Asarua
Copy link
Author

Asarua commented Sep 13, 2021

I use this method to obtain URL parameters, because many projects I contact need this step. I think I should not be the only one with this requirement.

Similarly, it can also make the type of our request library more perfect.

@voxpelli
Copy link
Collaborator

I think question is:

Why in that use case is a URL string literal the basis for the types?

Take fastify as an example:

interface IQuerystring {
  a: string;
  b: number;
}

server.get<{
  Querystring: IQuerystring,
}>('/auth', async (request, reply) => {
  const { a, b } = request.query
  // The a and b will have the correct types inferred
})

The only thing you can infer from a URL string literal query parameters that themselves are string literals, and static query parameters are not that different from the path sections in the URL?

Most query parameters are dynamic, so an approach like Fastify's seems like the more typical approach to this for me (but I'll happily hear more examples to prove me wrong)

(For the sake of completeness, my own approach with Fastify was actually a bit more complex, I sent in a JSON Schema to validate the query parameters and then used json-schema-to-typescript to generate types from those schemas, thus actually knowing for a fact that my routes never got any other data than the ones my types expected)

@sindresorhus
Copy link
Owner

We are unfortunately not going to accept this type.

Next time, I recommend opening an issue first for discussion so you don't have to have your effort go in vain.

@skarab42
Copy link
Collaborator

skarab42 commented Apr 5, 2022

(For the sake of completeness, my own approach with Fastify was actually a bit more complex, I sent in a JSON Schema to validate the query parameters and then used json-schema-to-typescript to generate types from those schemas, thus actually knowing for a fact that my routes never got any other data than the ones my types expected)

FI: @voxpelli I have made a Fastify tRPC adapter to make this process more easier ;)

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.

4 participants