node-wpapi icon indicating copy to clipboard operation
node-wpapi copied to clipboard

Support modifying transport request objects before request is sent

Open unematiii opened this issue 7 years ago • 5 comments

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)?

unematiii avatar Jun 28 '17 12:06 unematiii

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...

unematiii avatar Jun 29 '17 11:06 unematiii

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.

kadamwhite avatar Jan 24 '19 19:01 kadamwhite

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 avatar Jan 24 '19 19:01 kadamwhite

@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 avatar Feb 05 '19 18:02 joshlevinson

@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

kadamwhite avatar Oct 21 '19 21:10 kadamwhite