Skip to content

Commit 7f07ce4

Browse files
authored
Separate Superagent transport into optional module (#409)
Closes #410 Commit summary: * Begin extracting Superagent / transport-enabled build * Set up integration tests to run against multiple transport layers Converts all integration tests to use `describe.each` so they can be run against the existing superagent-based HTTP transports, as well as hypothetical future transports using isomorphic-fetch or axios. * Move http-transport module into superagent directory * Update README to account for package split * Tag 2.0.0-alpha.0 * Simplify integration test suite names * Update README to clarify upgrading from v1 * Clarify that v2 is not yet out. * Convert superagent to an optional dependency * Move custom transport unit tests back to core wpapi module This functionality is not specific to superagent. * Tag 2.0.0-alpha.1
1 parent fc3c380 commit 7f07ce4

22 files changed

+399
-326
lines changed

README.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ This library is an isomorphic client for the [WordPress REST API](http://develop
1111

1212
- [About](#about)
1313
- [Installation](#installation)
14+
- [Upgrading from v1](#upgrading-from-v1)
1415
- [Using the Client](#using-the-client)
1516
- [Auto-Discovery](#auto-discovery)
1617
- [Creating Posts](#creating-posts)
1718
- [Updating Posts](#updating-posts)
1819
- [Requesting Different Resources](#requesting-different-resources)
1920
- [API Query Parameters & Filtering Collections](#api-query-parameters)
2021
- [Uploading Media](#uploading-media)
21-
- [Custom Routes](#custom-routes)
22+
- [Querying Custom Routes](#custom-routes)
2223
- [Setter Method Naming](#setter-method-naming-for-named-route-components)
2324
- [Query Parameters & Filtering](#query-parameters--filtering-custom-routes)
2425
- [Mixins](#mixins)
@@ -35,7 +36,7 @@ This library is an isomorphic client for the [WordPress REST API](http://develop
3536

3637
`node-wpapi` is an isomorphic JavaScript client for the [WordPress REST API](https://developer.wordpress.org/rest-api) that makes it easy for your JavaScript application to request specific resources from a [WordPress](https://wordpress.org) website. It uses a query builder-style syntax to let you craft the request being made to REST API endpoints, then returns the API's response to your application as a JSON object. And don't let the name fool you: with [Webpack](https://webpack.github.io/) or [Browserify](http://browserify.org/), `node-wpapi` works just as well in the browser as it does on the server!
3738

38-
This library is maintained by K. Adam White at [Bocoup](https://bocoup.com), with contributions from a [great community](https://github.com/WP-API/node-wpapi/graphs/contributors) of WordPress and JavaScript developers.
39+
This library is maintained by K. Adam White at [Human Made](https://humanmade.com), with contributions from a [great community](https://github.com/WP-API/node-wpapi/graphs/contributors) of WordPress and JavaScript developers.
3940

4041
To get started, `npm install wpapi` or [download the browser build](https://wp-api.github.io/node-wpapi/wpapi.zip) and check out "Installation" and "Using the Client" below.
4142

@@ -53,24 +54,51 @@ To use the library from Node, install it with [npm](http://npmjs.org):
5354
npm install --save wpapi
5455
```
5556

56-
Then, within your application's script files, `require` the module to gain access to it:
57+
Then, within your application's script files, `require` the module to gain access to it. As `wpapi` is both a query builder and a transport layer (_i.e._ a tool for getting and sending HTTP requests), we leave it up to you as the author of your application whether you need both parts of this functionality. You may use `wpapi` with [superagent](https://www.npmjs.com/package/superagent) if you wish to send and receive HTTP requests using this library, but you may also use only the query builder part of the library if you intend to submit your HTTP requests with `fetch`, `axios` or other tools.
58+
59+
To import only the query builder (without the `.get()`, `.create()`, `.delete()`, `.update()` or `.then()` chaining methods):
5760

5861
```javascript
5962
var WPAPI = require( 'wpapi' );
6063
```
6164

62-
This library is designed to work in the browser as well, via a build system such as Browserify or Webpack; just install the package and `require( 'wpapi' )` from your application code.
65+
To import the superagent bundle, which contains the full suite of HTTP interaction methods:
66+
67+
```js
68+
var WPAPI = require( 'wpapi/superagent' );
69+
```
70+
71+
This library is designed to work in the browser as well, via a build system such as Browserify or Webpack; just install the package and `require( 'wpapi' )` (or `'wpapi/superagent'`) from your application code.
6372

6473
### Download the UMD Bundle
6574

6675
Alternatively, you may download a [ZIP archive of the bundled library code](https://wp-api.github.io/node-wpapi/wpapi.zip). These files are UMD modules, which may be included directly on a page using a regular `<script>` tag _or_ required via AMD or CommonJS module systems. In the absence of a module system, the UMD modules will export the browser global variable `WPAPI`, which can be used in place of `require( 'wpapi' )` to access the library from your code.
6776

77+
At present this browser bundle tracks the `wpapi/superagent` module, and includes Superagent itself.
78+
79+
### Upgrading from v1
80+
81+
Prior to version 2.0 (currently `alpha` status) this library shipped with built-in HTTP functionality using Superagent.
82+
83+
If you maintain an existing project which uses this library and wish to upgrade to v2, you may do so by manually installing Superagent:
84+
85+
```sh
86+
npm i --save wpapi@alpha superagent
87+
```
88+
89+
and then changing your `require` statements to use the `wpapi/superagent` entrypoint:
90+
91+
```diff
92+
--- const WPAPI = require( 'wpapi' );
93+
+++ const WPAPI = require( 'wpapi/superagent' );
94+
```
95+
6896
## Using the Client
6997

7098
The module is a constructor, so you can create an instance of the API client bound to the endpoint for your WordPress install:
7199

72100
```javascript
73-
var WPAPI = require( 'wpapi' );
101+
var WPAPI = require( 'wpapi/superagent' );
74102
var wp = new WPAPI({ endpoint: 'http://src.wordpress-develop.dev/wp-json' });
75103
```
76104
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!)
@@ -263,8 +291,11 @@ A WPAPI instance object provides the following basic request methods:
263291
* `wp.categories()...`: Get or create categories with the `/categories` endpoint
264292
* `wp.statuses()...`: Get resources within the `/statuses` endpoints
265293
* `wp.users()...`: Get resources within the `/users` endpoints
294+
* `wp.search()...`: Find resources of any [REST-enabled] post type matching a `?search=` string
266295
* `wp.media()...`: Get Media collections and objects from the `/media` endpoints
296+
* `wp.themes()...`: Read information about the active theme from the `/themes` endpoint (always requires authentication)
267297
* `wp.settings()...`: Read or update site settings from the `/settings` endpoint (always requires authentication)
298+
* `wp.blocks()...`: Create queries against the `blocks` endpoint
268299

269300
All of these methods return a customizable request object. The request object can be further refined with chaining methods, and/or sent to the server via `.get()`, `.create()`, `.update()`, `.delete()`, `.headers()`, or `.then()`. (Not all endpoints support all methods; for example, you cannot POST or PUT records on `/types`, as these are defined in WordPress plugin or theme code.)
270301

@@ -921,7 +952,7 @@ add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
921952
And then use this nonce when initializing the library:
922953
923954
```javascript
924-
var WPAPI = require( 'wpapi' );
955+
var WPAPI = require( 'wpapi/superagent' );
925956
var wp = new WPAPI({
926957
endpoint: window.WP_API_Settings.endpoint,
927958
nonce: window.WP_API_Settings.nonce

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"url": "http://www.kadamwhite.com"
55
},
66
"name": "wpapi",
7-
"version": "1.1.2",
7+
"version": "2.0.0-alpha.1",
88
"description": "An isomorphic JavaScript client for interacting with the WordPress REST API",
99
"main": "wpapi.js",
1010
"repository": {
@@ -52,16 +52,18 @@
5252
"lint": "npm run eslint",
5353
"watch": "jest --watch",
5454
"test:all": "jest",
55-
"test:unit": "jest tests/unit",
55+
"test:unit": "jest tests/unit superagent/tests/unit",
5656
"test:integration": "jest tests/integration",
5757
"test:ci": "npm run eslint && jest tests/unit --coverage",
5858
"pretest": "npm run lint || true",
5959
"test": "jest --coverage"
6060
},
6161
"dependencies": {
6262
"parse-link-header": "^1.0.1",
63-
"qs": "^6.6.0",
64-
"superagent": "^4.0.0"
63+
"qs": "^6.6.0"
64+
},
65+
"optionalDependencies": {
66+
"superagent": "^4.1.0"
6567
},
6668
"devDependencies": {
6769
"@babel/core": "^7.2.2",

superagent/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# `wpapi/superagent`
2+
3+
This endpoint returns a version of the WPAPI library configured to use Superagent for HTTP requests.
4+
5+
## Installation & Usage
6+
7+
Install both `wpapi` and `superagent` using the command `npm install --save wpapi superagent`.
8+
9+
```js
10+
import WPAPI from 'wpapi/superagent';
11+
12+
// Configure and use WPAPI as normal
13+
const site = new WPAPI( { /* ... */ } );
14+
```

lib/http-transport.js renamed to superagent/http-transport.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
const agent = require( 'superagent' );
77
const parseLinkHeader = require( 'li' ).parse;
88

9-
const WPRequest = require( './constructors/wp-request' );
10-
const checkMethodSupport = require( './util/check-method-support' );
11-
const objectReduce = require( './util/object-reduce' );
12-
const isEmptyObject = require( './util/is-empty-object' );
9+
const WPRequest = require( '../lib/constructors/wp-request' );
10+
const checkMethodSupport = require( '../lib/util/check-method-support' );
11+
const objectReduce = require( '../lib/util/object-reduce' );
12+
const isEmptyObject = require( '../lib/util/is-empty-object' );
1313

1414
/**
1515
* Set any provided headers on the outgoing request object. Runs after _auth.

superagent/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const WPAPI = require( '../wpapi' );
2+
3+
// Pull in superagent-based HTTP transport
4+
const httpTransport = require( './http-transport' );
5+
6+
/**
7+
* The HTTP transport methods object used by all WPAPI instances
8+
*
9+
* These methods may be extended or replaced on an instance-by-instance basis
10+
*
11+
* @memberof! WPAPI
12+
* @static
13+
* @property transport
14+
* @type {Object}
15+
*/
16+
WPAPI.transport = Object.create( httpTransport );
17+
Object.freeze( WPAPI.transport );
18+
19+
module.exports = WPAPI;

superagent/tests/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
'env': {
3+
jest: true,
4+
},
5+
};

0 commit comments

Comments
 (0)