node-wpapi
node-wpapi copied to clipboard
Support modifying transport request objects before request is sent
How to access superagent's request obj?
I need to call withCredentials()
on the request obj to deal with CORS. How to do it without overriding all the methods that http-transport
exports (delete, get, head, post, put)?
Workaround: Cloned the contents of http-transport.js
+ added defaults for superagent
:
var agentDefaults = require( 'superagent-defaults' );
var agent = agentDefaults();
agent.withCredentials();
/**
* Conditionally set basic authentication on a server request object
...
import transport from './http-transport'
const api = new WPAPI({ endpoint: 'example.com/wp-json', transport })
But... updates to lib most likely to break this anytime soon...
Re-purposing this ticket to generically support a way to modify or call additional methods on the request object before it is sent. This should address the withCredentials
example above, but also handle #396 by providing a generic way to call any methods on the agent's request object.
A good solution to this problem should permit an application author to introduce any of the following into their request chain:
- Proxy options
- Credentials / CORS configuration
- Request Logging
This may also be useful for custom authentication providers.
@kadamwhite Your proposed solution sounds 💯. I see this "in progress" for the 2.0 project. Is there a WIP branch anywhere one could review? Or is this not quite started and open for contribution?
@joshlevinson Sorry for the delayed response; between day job and the core WordPress REST API component, I haven't given this project as much love as it deserves this year. You're correct, I have yet to implement anything; I was focusing previously on figuring out how to introduce a new transport using fetch
instead of superagent, which just merged in #432