-
Notifications
You must be signed in to change notification settings - Fork 191
Permit custom functionality to be injected into HTTP transport actions #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }); } } }); ```
Collaborator
Author
Contributor
|
@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. |
Collaborator
Author
|
@edygar Awesome, I'm glad to hear it! Expect this interface to be released by Friday. |
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.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 WPobjects. The default transports themselves are exposed in the (frozen)WP.transportproperty; 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:@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.)