dafortune / hapi-qs

Bring back qs support for hapi 12
10 stars 7 forks source link

Doesn't work with hapi v18 #22

Open mweibel opened 5 years ago

mweibel commented 5 years ago

Release notes: https://github.com/hapijs/hapi/issues/3871

look for request.setUrl() references in your code and ensure you are only passing valid arguments.
if you use request.setUrl() to override query processing (e.g. using the qs module), consider switching to the much simpler server.options.query.parser option.

I assume this package can be deprecated and alternatively the change to the query parser suggested?

The v18 way would be:

query: {
  parser: (query) => qs.parse(query)
}
dafortune commented 5 years ago

Hey @mweibel thanks for the pointer and sorry about the delay, I had not time to look at this deeply today, but if the integration with QS in Hapi v18 is so easy I'd say that does not worth having this plugin anymore and so it worth deprecating it. Will look into it in the following weeks.

Thanks a lot.

petermilan commented 2 years ago

I think this module is still needed.

From https://hapi.dev/api?v=20.2.0#-serveroptionsqueryparser

the method must return an object where each key is a parameter and matching value is the parameter value. If the method throws, the error is used as the response or returned when request.setUrl() is called.

so it looks like hapi doeas not allow nested objects. It just allows format like this:

  'q[query][search][firstname]': 'John',
  'q[query][search][lastname]': 'Doe',
petermilan commented 2 years ago

this seems to work

    parser: (query) => {
      if (!query) {
        return query;
      }
      const parsedQuery = Object.entries(query).reduce((acc, [key, value]) => {
        _.set(acc, key, value);
        return acc;
      }, {});

      return parsedQuery;
    },