You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Breaking: Remove support for "Node-style" error-first callbacks (#428)
Our original HTTP transport `superagent` does not use Promises by default, so all versions of this library to date have supported Node-style error-first callbacks, _e.g._ `.get( ( err, data ) => { if ( err ) {} ... } )`. However, Promises are the preferred and recommended way of using this library, and the vast majority of library consumers do not utilize the callback method signature. While evaluating additional transport providers, particularly `fetch`, it became obvious that continuing to support callback-style asynchronous methods would be cumbersome with Promise-aware, modern transport methods like `fetch` or libraries like `axios`. As part of the slimming-down of our API for Version 2, this PR removes support for the node-style callback signature on all transport methods. * Refactor superagent transport: extract bind-transport method * Remove error-first callback support from superagent transport Maintaining this node-style interface, which support threads indicate is rarely used (especially given the rising popularity of `async`/`await`), no longer outweights the benefit of providing a consistent interface regardless of the transport library used. * Update WPRequest & WPAPI for promise-only method signatures This completes the work to phase out the "node-style" error-first callbacks * Update CHANGELOG with breaking transport method signature change * Add note on callback support removal in v2 upgrade guide
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Changelog
2
2
3
3
## v2.0.0 [**alpha**]_Second Toughest in the Infants_
4
-
4
+
-**BREAKING**: "Node-style" error-first callbacks (_e.g._`.get( ( err, data ) => {} )`) are no longer supported. All transport request methods return Promises.
5
5
-**BREAKING**: The module exported as `wpapi` no longer includes HTTP methods. Install `superagent` as a peer dependency and `require( 'wpapi/superagent' )` in order to make HTTP requests.
6
6
-**BREAKING**: Autodiscovery now either succeeds or fails; a WPAPI instance configured with default routes will no longer be returned.
7
7
@@ -11,13 +11,11 @@
11
11
- Throw an error early when `.file()` is passed a Buffer object without an accompanying name string, props @cungminh2710 & @mvhirsch
12
12
13
13
14
-
15
14
## v1.2.1 _Colomb_
16
15
17
16
- Fix issue where `li` was improperly declared as a dev-only dependency, props @el-lsan & @jerolan.
18
17
19
18
20
-
21
19
## v1.2.0 _Space Is Only Noise_
22
20
23
21
-**BREAKING**: The minimum supported node version is now v8.6, or v8.2.1 with the `--harmony` flag.
Copy file name to clipboardExpand all lines: README.md
+34-26Lines changed: 34 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,6 +78,8 @@ At present this browser bundle tracks the `wpapi/superagent` module, and include
78
78
79
79
### Upgrading from v1
80
80
81
+
### Require `wpapi/superagent`
82
+
81
83
Prior to version 2.0 (currently `alpha` status) this library shipped with built-in HTTP functionality using Superagent.
82
84
83
85
If you maintain an existing project which uses this library and wish to upgrade to v2, you may do so by manually installing Superagent:
@@ -93,6 +95,21 @@ and then changing your `require` statements to use the `wpapi/superagent` entryp
93
95
+++ const WPAPI = require( 'wpapi/superagent' );
94
96
```
95
97
98
+
### Use Promises instead of Callbacks
99
+
100
+
In version 1, you could use "Node-style" error-first callback functions instead of chaining promises. As of version 2, this callback style is no longer supported; use Promise `.then` syntax or [`async`/`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) instead.
The module is a constructor, so you can create an instance of the API client bound to the endpoint for your WordPress install:
@@ -103,23 +120,17 @@ var wp = new WPAPI({ endpoint: 'http://src.wordpress-develop.dev/wp-json' });
103
120
```
104
121
Once an instance is constructed, you can chain off of it to construct a specific request. (Think of it as a query-builder for WordPress!)
105
122
106
-
We support requesting posts using either a callback-style or promise-style syntax:
123
+
**Compatibility Note:** As of Version 2.0, Node-style error-first callbacks are no longer supported by this library. All request methods return Promises.
107
124
108
125
```javascript
109
-
// Callbacks
110
-
wp.posts().get(function( err, data ) {
111
-
if ( err ) {
112
-
// handle err
113
-
}
114
-
// do something with the returned posts
115
-
});
116
-
117
-
// Promises
118
-
wp.posts().then(function( data ) {
119
-
// do something with the returned posts
120
-
}).catch(function( err ) {
121
-
// handle error
122
-
});
126
+
// Request methods return Promises.
127
+
wp.posts().get()
128
+
.then(function( data ) {
129
+
// do something with the returned posts
130
+
})
131
+
.catch(function( err ) {
132
+
// handle error
133
+
});
123
134
```
124
135
The `wp` object has endpoint handler methods for every endpoint that ships with the default WordPress REST API plugin.
125
136
@@ -798,7 +809,9 @@ By default `node-wpapi` uses the [superagent](https://www.npmjs.com/package/supe
798
809
799
810
**This is advanced behavior; you will only need to utilize this functionality if your application has very specific HTTP handling or caching requirements.**
800
811
801
-
In order to maintain consistency with the rest of the API, custom transport methods should take in a WordPress API route handler query object (_e.g._ the result of calling `wp.posts()...` or any of the other chaining resource handlers), a `data` object (for POST, PUT and DELETE requests), and an optional callback function (as `node-wpapi` transport methods both return Promise objects _and_ support traditional `function( err, response )` callbacks).
812
+
In order to maintain consistency with the rest of the API, custom transport methods should take in a WordPress API route handler query object (_e.g._ the result of calling `wp.posts()...` or any of the other chaining resource handlers)and a `data` object (for POST, PUT and DELETE requests).
813
+
814
+
**Note:** Node-style error-first callbacks are no longer supported by this library as of version 2.0. Custom transport methods should therefore not accept or expect a third optional callback parameter.
802
815
803
816
The default HTTP transport methods are available as `WPAPI.transport` (a property of the constructor object) and may be called within your transports if you wish to extend the existing behavior, as in the example below.
804
817
@@ -809,16 +822,11 @@ var site = new WPAPI({
809
822
endpoint:'http://my-site.com/wp-json',
810
823
transport: {
811
824
// Only override the transport for the GET method, in this example
812
-
// Transport methods should take a wpreq object and a callback:
813
-
get:function( wpreq, cb ) {
825
+
// Transport methods should take a wpreq object:
826
+
get:function( wpreq ) {
814
827
var result = cache[ wpreq ];
815
-
// If a cache hit is found, return it via the same callback/promise
816
-
// signature as the default transport method:
828
+
// If a cache hit is found, return it wrapped in a Promise:
817
829
if ( result ) {
818
-
if ( cb &&typeof cb ==='function' ) {
819
-
// Invoke the callback function, if one was provided
820
-
cb( null, result );
821
-
}
822
830
// Return the data as a promise
823
831
returnPromise.resolve( result );
824
832
}
@@ -837,8 +845,8 @@ You may set one or many custom HTTP transport methods on an existing WP site cli
0 commit comments