Skip to content

Conversation

@kadamwhite
Copy link
Collaborator

WPRequest currently does two things: It builds a string, then it allows HTTP requests to be submitted against the resource that URI string represents. The latter part of that behavior will benefit from being pluggable, as described in #211 and #213.

Exposing WPRequest itself is something I would like to avoid to keep the core workings of the library (its URI-builder behavior) safe from unexpected modification; but exposing the HTTP behavior and enabling it to be customized or extended not only solves the issue expressed in the linked issues around cache implementation, but also provides a better interface for specifying bespoke authentication behavior.

To that end, this commit moves WPRequest's HTTP logic into a separate http-transport.js module, and injects a copy of that module's exports into new WP objects. The default transports themselves are exposed in the (frozen) WP.transport property; they can be referenced from there to extend the transport logic to permit additional functionality to be injected into the request process, for example caching behavior (as suggested in #211). Example:

var site = new WP({
  endpoint: 'http://my-site.com/wp-json',
  transport: {
    get: function( wpquery, cb ) {
      var result = cache[ wpquery ];
      // If a cache hit is found, return it via the same callback/promise
      // signature as the default transport method
      if ( result ) {
        if ( cb && typeof cb === 'function' ) {
          cb( null, result );
        }
        return Promise.resolve( result );
      }

      // Delegate to default transport if no cached data was found
      return WP.transport.get( wpquery, cb ).then(function( result ) {
        cache[ wpquery ] = result;
        return result;
      });
    }
  }
});

@edygar please review and see whether this API makes sense; this provisionally closes #211. (Note however that this PR does not yet address #213 in any substantive way.)

WPRequest currently does two things: It builds a string, then it allows
HTTP requests to be submitted against the resource that URI string
represents. The latter part of that behavior will benefit from being
pluggable, as described in #211 and #213.

Exposing WPRequest itself is something I would like to avoid to keep
the core workings of the library (its URI-builder behavior) safe from
unexpected modification; but exposing the HTTP behavior and enabling
it to be customized or extended not only solves the issue expressed
in the linked issues around cache implementation, but also provides
a better interface for specifying bespoke authentication behavior.

To that end, this commit moves WPRequest's HTTP logic into a separate
http-transport.js module, and injects a _copy_ of that module's
exports into `new WP` objects. The next step will be to make `transport`
a property which may be passed in, and to expose the default transport
on the `WP` object.
This extends the work of the previous commits by exposing the default
HTTP transport methods in the (frozen) `WP.transport` property; they
can be referenced from there to extend the transport logic to permit
additional functionality to be injected into the request process, for
example caching behavior (as suggested in #211). Example:

```js
var site = new WP({
  endpoint: 'http://my-site.com/wp-json',
  transport: {
    get: function( wpquery, cb ) {
      var result = cache[ wpquery ];
      // If a cache hit is found, return it via the same callback/promise
      // signature as the default transport method
      if ( result ) {
        if ( cb && typeof cb === 'function' ) {
          cb( null, result );
        }
        return Promise.resolve( result );
      }

      // Delegate to default transport if no cached data was found
      return WP.transport.get( wpquery, cb ).then(function( result ) {
        cache[ wpquery ] = result;
        return result;
      });
    }
  }
});
```
@kadamwhite
Copy link
Collaborator Author

@edygar I'd love to know if you have any feedback on this patch, whenever you have a moment! If it looks like it will resolve #211 I'll merge it in and get it in the next release later this week.

@edygar
Copy link
Contributor

edygar commented Sep 13, 2016

@kadamwhite I've been playing this all day! I'm sorry to let you waiting for so long for a feedbacks. So far so good! It allowed me to hook in the way I would like to. #211 is resolved with this patch.

@kadamwhite
Copy link
Collaborator Author

@edygar Awesome, I'm glad to hear it! Expect this interface to be released by Friday.

@kadamwhite kadamwhite deleted the separate-request-from-stringbuilder branch September 13, 2016 17:41
kadamwhite added a commit that referenced this pull request Sep 13, 2016
…ization

This follows #220 by providing a method by which transport functions can
be set on a preexisting `WP` instance object, such as one returned through
the auto-discovery flow. Documentation around HTTP behavior customization
and extension has been added to the README.
kadamwhite added a commit that referenced this pull request Sep 13, 2016
…ization

This follows #220 by providing a method by which transport functions can
be set on a preexisting `WP` instance object, such as one returned through
the auto-discovery flow. Documentation around HTTP behavior customization
and extension has been added to the README.
@kadamwhite
Copy link
Collaborator Author

Released today in v0.10!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EndpointRequest customization

3 participants