#Add Sort and Filter to API

5 messages · Page 1 of 1 (latest)

candid juniper
#

I'm looking for the Nest way to handle adding URL params to your endpoints for sort and filter. In a very old express web app, we used the hpp module to do this. I'm hoping to not use this module if there is something already built in.

// Prevent paramater pollution
app.use(
  hpp({
    whitelist: [
      'duration',
      'ratingsAverage',
      'price'
    ]
  })
);

Then, we could create the endpoint logic to be something like this.

exports.topTours = (req, res, next) => {
  req.query.limit = '5';
  req.query.sort = '-ratingsAverage,price';
  req.query.fields = 'name,price,ratingsAverage';
  next();
};
#

I understand that Nest runs Express by default under the hood. I'm just not sure if the team at Nest has already put something in place to simplify this.

pastel river
#

since you already are using url queries instead of params this makes it actually pretty easy
i would suggest to split some of your queries

http://example.com/?category=price?sort=rating?order=asc?limit=5

the fields query this is more complex
i suggest to use encoding

a "," character isn't url safe, luckly more people realized this and the "url encoding" or "Percent Encoding" was created for this exact reason

you can even go some what further
lets take a look at youtube as a example for this

when searching anything and going to the filter and selecting for example "4k"
you get this odd string "EgJwAQ%253D%253D"
this isn't documented anywhere but by doing some quick tests this seems to be using multiple encoding methods

we can split this string into "EgJwAQ" and "%253D%253D"
lets take at a look at the second part for now
the %3D stands for the = character
%25 is the % character it self but

so when you see %253D it stands for %3D
but hey %3D actually means = so when you see %253D it actuelly means url encoded =
so %253D%253D actuelly means ==

now to the first part of that strange string EgJwAQ
on its own it means nothing
but EgJwAQ== seems odd isn't it?

well this is actuelly base64 encoding and could actuelly be decoded
decoding this as plain text decodes to p
decoding this to to binairy instead you get 00100110 00000000
from here things get wierd as things get confusing trying to decode
and is prob not right
this P is prob some internal stuff

but hope this gets you the gist

pastel river
pastel river
#

i think this issue has been answered by now
and is safe to close now

if anybody wants to add on to this topic feel free