Re:TinyJS – Shorten JavaScript QuerySelect with $ and $$
by sli on Wednesday October 02, 2024 @07:11PM
(#41725767)
I built a Proxy-based microlib for making fluent REST calls just on a lark a couple years ago. It was never anything production-ready, but it was so handy that I used it in all of my JS projects until I moved away from webdev. The API was basically:
const api = new Proxy({route: baseRoute}, handler); // handler is the microlib's export
const result = await api.get.some.route.invoke(); // GET {baseRoute}/some/route
invoke() is just how it finally fires the call. I didn't feel like spending too much time making it automatic, the benefits just weren't large enough to justify compared to just calling invoke().
You can use it like:
demo: https://codepen.io/anon/pen/RxGgNR