-
-
Notifications
You must be signed in to change notification settings - Fork 543
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
Add Url2Json
type
#262
Conversation
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"
} |
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. |
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. |
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 |
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. |
FI: @voxpelli I have made a Fastify tRPC adapter to make this process more easier ;) |
Is a type that gets the query string from the URL