honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.35k stars 582 forks source link

Custom query parser #3667

Open bompi88 opened 1 day ago

bompi88 commented 1 day ago

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:

{
  amount: {
    lte: 300
  }
}

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.

var express = require('express');
var qs = require('qs');
var app = express();

app.set('query parser', function (str) {
  return qs.parse(str, opts);
});
yusukebe commented 16 hours ago

Hi @bompi88

I like the idea of using something like the getPath method. I will also consider this issue.

yusukebe commented 16 hours ago

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 getPath.

bompi88 commented 16 hours ago

@yusukebe thanks! I've updated the ticket with the example from the https://github.com/honojs/hono/pull/3565 + example how it is done in for example express (please not get too inspired by that approach).

bompi88 commented 16 hours ago

@yusukebe I know that there is currently an ongoing PR regarding the URLSearchParams that is kind of related (and not), but let me know if there anything I can facilitate regarding this ticket.