How to get distinct values of a field in strapi via REST API

Hi all,

I have some clothing product data in my Strapi and I want to create some dynamic filters in my frontend for them. I don’t want to hardcode my filters.
E.g. I don’t want to show filters for sizes S, M, L & XL if not everything in the XL size is in stock.

I am trying to find a way to get the distinct values of the field with the size information that are in stock in order to only show filters that will return products if clicked.

If I try /api/products?fields[0]=size I will of course get all sizes in the database as many times as they are in the database.

Of course I could keep distinct values in the frontend after the response from the server gets back and I know I can create a custom route just for that.

Before going custom is there any functionality already in Strapi to get the distinct values from the REST API, or going custom is my only option?

Thanks in advance!

what is the solution?

I did a custom controller that overrides the default functionality.
I found nothing else.

If I understand your question, you want to apply pre-filtering directly on the query ?

If it is what you want, I recommand you to check this documentation, you can addd filters to the query to be able to avoid useless data transfer.

You can combine it with the Populate options, that allow you to select what fields to develop, or not sending etc…

If that is not what you want, I am sorry I didn’t understood your question