Skip to content

Conversation

@alexgQQ
Copy link

@alexgQQ alexgQQ commented Sep 26, 2020

Hello 👋 I stumbled across this repo a bit ago and love it! I wanted to try to add some filtering functionality as that seemed to be a missing feature. Mostly I just hated parsing all the entries on the /character response to get character ids for quotes. So I followed your contributing guidelines and gave it a shot.

Description

The changes on here are straightforward:

  • Install mongoose-query-parser package to the backend
  • Parse non-pagination params into a filterable structure for mongoose.

A client can pass any params related to the model schema to filter. Namely for strings we can do regex or exact match lookups like /Gandalf/i or Human.

# Using a delightful curl-like cli named httpie
http :3001/v2/character name=='/Gandalf/i' "Authorization: Bearer $TOKEN"  # Where is that wizard?
http :3001/v2/character name=='/foot/i' race=='Hobbit' limit==1 "Authorization: Bearer $TOKEN"  # First hobbit with foot in the name
http :3001/v2/character name=='/foot/i' race=='Hobbit' limit==2 sort==name:desc "Authorization: Bearer $TOKEN" # Sort it by alphabetically descending name now

Design Issues

The filtering relies on the client to make the proper query, so parameters that do not match the model schemes are still processed and attempted to match, causing no results to be returned. Some APIs follow this paradigm and some don't but we can potentially change the config to blacklist these values or return errors on unmatched keys.

http :3001/v2/character foo=='/bar/i' "Authorization: Bearer $TOKEN"

Let me know your thought on this whenever you get a chance.

@gitfrosh
Copy link
Owner

Hi @alexgQQ,
thanks a LOT for your contribution! 👏👏 I like how this adds so much functionality with just a bit of code. I'll merge your pull request and will ship your changes with the next release (probably by end of #hacktoberfest).
However I'd love to add some explanation in the documentation at the-one-api.dev about filtering too, so every user of the API will know about the enhancement. Maybe you'd like to add that or i'll create an issue for this open task later..
Cheers, rike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants