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
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
@@ -35,7 +36,7 @@ This library is an isomorphic client for the [WordPress REST API](http://develop
35
36
36
37
`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!
37
38
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.
39
40
40
41
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.
41
42
@@ -53,24 +54,51 @@ To use the library from Node, install it with [npm](http://npmjs.org):
53
54
npm install --save wpapi
54
55
```
55
56
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):
57
60
58
61
```javascript
59
62
varWPAPI=require( 'wpapi' );
60
63
```
61
64
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
+
varWPAPI=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.
63
72
64
73
### Download the UMD Bundle
65
74
66
75
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.
67
76
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
+
68
96
## Using the Client
69
97
70
98
The module is a constructor, so you can create an instance of the API client bound to the endpoint for your WordPress install:
71
99
72
100
```javascript
73
-
varWPAPI=require( 'wpapi' );
101
+
varWPAPI=require( 'wpapi/superagent' );
74
102
var wp =newWPAPI({ endpoint:'http://src.wordpress-develop.dev/wp-json' });
75
103
```
76
104
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:
263
291
*`wp.categories()...`: Get or create categories with the `/categories` endpoint
264
292
*`wp.statuses()...`: Get resources within the `/statuses` endpoints
265
293
*`wp.users()...`: Get resources within the `/users` endpoints
294
+
*`wp.search()...`: Find resources of any [REST-enabled] post type matching a `?search=` string
266
295
*`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)
267
297
*`wp.settings()...`: Read or update site settings from the `/settings` endpoint (always requires authentication)
298
+
*`wp.blocks()...`: Create queries against the `blocks` endpoint
268
299
269
300
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.)
0 commit comments