From 7f07ce4d48a2dbc63f37ea649506e86fd82c3094 Mon Sep 17 00:00:00 2001 From: K Adam White Date: Tue, 22 Jan 2019 21:11:28 -0500 Subject: [PATCH 01/31] 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 --- README.md | 43 +++- package.json | 10 +- superagent/README.md | 14 ++ {lib => superagent}/http-transport.js | 8 +- superagent/index.js | 19 ++ superagent/tests/.eslintrc.js | 5 + superagent/tests/unit/superagent.js | 256 +++++++++++++++++++ tests/integration/autodiscovery.js | 5 +- tests/integration/categories.js | 5 +- tests/integration/comments.js | 5 +- tests/integration/custom-http-headers.js | 6 +- tests/integration/custom-http-transport.js | 8 +- tests/integration/error-states.js | 6 +- tests/integration/media.js | 5 +- tests/integration/pages.js | 5 +- tests/integration/posts.js | 5 +- tests/integration/settings.js | 6 +- tests/integration/tags.js | 5 +- tests/integration/taxonomies.js | 6 +- tests/integration/types.js | 6 +- tests/unit/wpapi.js | 277 ++------------------- wpapi.js | 20 +- 22 files changed, 399 insertions(+), 326 deletions(-) create mode 100644 superagent/README.md rename {lib => superagent}/http-transport.js (97%) create mode 100644 superagent/index.js create mode 100644 superagent/tests/.eslintrc.js create mode 100644 superagent/tests/unit/superagent.js diff --git a/README.md b/README.md index 349408c3..b0105429 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ This library is an isomorphic client for the [WordPress REST API](http://develop - [About](#about) - [Installation](#installation) + - [Upgrading from v1](#upgrading-from-v1) - [Using the Client](#using-the-client) - [Auto-Discovery](#auto-discovery) - [Creating Posts](#creating-posts) @@ -18,7 +19,7 @@ This library is an isomorphic client for the [WordPress REST API](http://develop - [Requesting Different Resources](#requesting-different-resources) - [API Query Parameters & Filtering Collections](#api-query-parameters) - [Uploading Media](#uploading-media) -- [Custom Routes](#custom-routes) +- [Querying Custom Routes](#custom-routes) - [Setter Method Naming](#setter-method-naming-for-named-route-components) - [Query Parameters & Filtering](#query-parameters--filtering-custom-routes) - [Mixins](#mixins) @@ -35,7 +36,7 @@ This library is an isomorphic client for the [WordPress REST API](http://develop `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! -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. +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. 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. @@ -53,24 +54,51 @@ To use the library from Node, install it with [npm](http://npmjs.org): npm install --save wpapi ``` -Then, within your application's script files, `require` the module to gain access to it: +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. + +To import only the query builder (without the `.get()`, `.create()`, `.delete()`, `.update()` or `.then()` chaining methods): ```javascript var WPAPI = require( 'wpapi' ); ``` -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. +To import the superagent bundle, which contains the full suite of HTTP interaction methods: + +```js +var WPAPI = require( 'wpapi/superagent' ); +``` + +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. ### Download the UMD Bundle 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 `