hashgraph / hedera-mirror-node

Hedera Mirror Node archives data from consensus nodes and serves it via an API
Apache License 2.0
130 stars 108 forks source link

Add query normalization to support REST API Caching #9113

Open edwin-greene opened 1 month ago

edwin-greene commented 1 month ago

Problem

Caching on the REST API can be enhanced by adding logic that handles absent query parameters indentically to default query parameters. For example: /api/v1/accounts and /api/v1/accounts?limit=25

Solution

Add query parameter normalization functionality. It should be easily usable by the caching middleware from #9111

Alternatives

No response

jascks commented 1 month ago

This is kind of tricky to implement. Not only do we need to considere unspecified default query parameter values, but also parameter order, as well as repeated parameters etc.

The Express request query parser (middleware/requestHander.js) might be a good place. I noticed when working in this area recently that Express aggregates repeated parameters when present and defines an array of their respective values. We have code here that forces query parameter names to lowercase, and some parameter values are also forced to lowercase.

After this processing has occurred we are closer to a normalized form of the query parameters. Perhaps after default parameters/values are accounted for we could hash this object? Or maybe we need to create an array from the map with query parameter names in a standard order prior to hashing the array. Not sure, but I'm hopeful.