outlandishideas/kasia

How to expose express req/res objects to node-wpapi

MickeyKay opened this issue · 6 comments

Is there a way to pass our express app's req/res objects to Kasia, specifically within our node-wpapi instance?

We've trying to hook up node-redis middleware for server-side caching, and the cache instance gets passed through on the req object. I'm thinking we can create a custom transport method ala https://github.com/WP-API/node-wpapi#customizing-http-request-behavior and then check for the existence of the cache instance to bypass doing an actual fetch, however to do this we need to be able to check the req object. Any ideas? Thanks!

This isn't possible currently, but wouldn't take much to implement, at least for SSR anyway. Creating a dummy request object on the client would be bit more work, if desirable. For the former only (access to req during SSR), this would require a simple change to the preload function, which could be passed the req object as an argument and pass this into the queryFn, where you could do whatever you like with it. Happy to take PRs for that, otherwise I will take a look into it ASAP. We are well overdue for the release of v4.2 as well, so we could bundle it into that.

Thanks @sdgluck, I probably don't have the bandwidth to take this on any time soon, but will definitely circle back if time opens up for me. Thanks for the quick response, excited to see the continued development of this great project.

@MickeyKay Can I confirm that when you say "pass req/res objects to Kasia", you mean in order to access them in a connected component's query function?

Possibly. More specifically, the idea would be to instantiate our backing node-wpapi object with a custom transport method hooked into a node-redis instance. That way, we have a secondary cache that can short-circuit external requests across sessions, as opposed to Kasia's single-session "caching" logic (in which objects already in the store are not re-requested).

In that case I'm not sure I understand what functionality you are asking to see in Kasia. Could you give an example of the kind of API you're looking for?

Yeah, I'm thinking this is probably less a Kasia-specific question and more a general wp-api question. This is newer territory for me, so thanks for your patience :)