-
-
Notifications
You must be signed in to change notification settings - Fork 621
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
Custom query parser #3667
Comments
Hi @bompi88 I like the idea of using something like the |
It is unrelated to query parsing, but it would be interesting to allow users to specify a custom JSON serializer as a good example of using something like |
@yusukebe I know that there is currently an ongoing PR regarding the |
Hi @yusukebe, I wanted to follow up as I haven’t heard back from you yet. We’re highly interested in adding a way to override this functionality. Would you be open to accepting contributions on this topic? |
Sorry for the late reply! I am not eager to proceed because the implementation would be complicated. Also, as I said before, I am concerned about the behavior depending on the parameters. If you make a PR, I will review it, but that does not ensure that it will be merged. |
@yusukebe We've started to look into how the code is structured, and we see what concerns you have. I hope that you enable a more plugin approach for the query parser in the future. We have an ugly hack that we currently use to get around the problem using a middleware. // middlewares.ts
import qs from 'qs';
import { createMiddleware } from 'hono/factory';
import { URL } from 'url';
export const queryParseMiddleware = createMiddleware((c, next) => {
const query = qs.parse(new URL(c.req.url).search, { ignoreQueryPrefix: true });
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-explicit-any
c.req.queries = () => query as any;
return next();
});
// utils.ts
import { createRoute as openApiCreateRoute } from '@hono/zod-openapi';
export const createRoute: typeof openApiCreateRoute = (config) =>
openApiCreateRoute({
...config,
middleware: config.middleware
? [...(Array.isArray(config.middleware) ? config.middleware : [config.middleware]), queryParseMiddleware]
: queryParseMiddleware,
});
// routes.ts
import { createRoute } from './utils';
export const list = createRoute({
path: '/whatever',
method: 'get',
request: {
query: z.object({
value: z.object({ lte: z.number() })
})
},
tags,
responses: {
[StatusCodes.OK]: jsonContent(z.any(), 'The list of whatever'),
},
}); |
Setting a value for |
@yusukebe I've been thinking, it does not make sense to me making a PR unless I know you're motivated by the changes. I also do not want to touch more code than necessary. So the part of
And I dont think having more levels than that makes that much sense. And by enabling more levels would complicate things a lot. Would this proposal align with your vision for Hono? Is it something you’d be interested in integrating into the core? |
What is the feature you are proposing?
I want to be able to switch out the default query parser with something like qs.
I am able to use a validator to "hack something together", but if I'm using
@hono/zod-openapi
that will not be able to use the output of that validator.Something like the
getPath
method would be sufficient enough for my use case.EDIT:
Adding more context to the ticket. I'm using a special query "syntax" which is supported by
qs
. For example I can do?amount[lte]=300
which resolves to:For comparison here is how you can change the query parser in
express
. Not that we should follow their approach because I think that one looks messy.The text was updated successfully, but these errors were encountered: