wpapi
Advanced tools
Comparing version 1.0.2 to 1.0.3-0
@@ -17,3 +17,3 @@ # Contributing | ||
In order to run the integration tests you will need to run a local WP REST API instance in a virtual machine as described in [wpapi-vagrant-varietal](https://github.com/kadamwhite/wpapi-vagrant-varietal). Full instructions are provided there, and once that VM is running the integration tests will pass. (You can run the integration suite specifically with the command `npm test:integration`). | ||
In order to run the integration tests you will need to run a specifically-configured local WordPress instance in a virtual machine as described in [wpapi-vagrant-varietal](https://github.com/kadamwhite/wpapi-vagrant-varietal). Full instructions are provided there, and once that VM is booted and running the integration tests will pass. (You can run the integration suite specifically with the command `npm test:integration`). | ||
@@ -20,0 +20,0 @@ ### Adding Tests |
@@ -20,3 +20,3 @@ --- | ||
If you don't have a plugin already, let's make a quick one together. First, we'll need the [WordPress REST API v2 plugin](https://wordpress.org/plugins/rest-api/) to be installed and activated. Then in your `wp-content/plugins` directory, create the folder `wpapi-demo`, then make an empty `wpapi-demo.php` file inside that folder. | ||
If you don't have a plugin already, let's make a quick one together. First, we'll need to ensure our site is running WordPress 4.7.3 or higher, so that the API endpoints are available. Then in your `wp-content/plugins` directory, create the folder `wpapi-demo`, then make an empty `wpapi-demo.php` file inside that folder. | ||
@@ -23,0 +23,0 @@ Next, download and copy over the script files from the [wpapi.js download bundle](https://wp-api.github.io/node-wpapi/wpapi.zip) into your plugin directory. At a minimum, you should now have this: |
@@ -16,2 +16,3 @@ 'use strict'; | ||
var objectReduce = require( './util/object-reduce' ); | ||
var isEmptyObject = require( './util/is-empty-object' ); | ||
@@ -83,2 +84,25 @@ /** | ||
/** | ||
* Extract the body property from the superagent response, or else try to parse | ||
* the response text to get a JSON object. | ||
* | ||
* @param {Object} response The response object from the HTTP request | ||
* @param {String} response.text The response content as text | ||
* @param {Object} response.body The response content as a JS object | ||
* @returns {Object} The response content as a JS object | ||
*/ | ||
function extractResponseBody( response ) { | ||
var responseBody = response.body; | ||
if ( isEmptyObject( responseBody ) && response.type === 'text/html' ) { | ||
// Response may have come back as HTML due to caching plugin; try to parse | ||
// the response text into JSON | ||
try { | ||
responseBody = JSON.parse( response.text ); | ||
} catch ( e ) { | ||
// Swallow errors, it's OK to fall back to returning the body | ||
} | ||
} | ||
return responseBody; | ||
} | ||
/** | ||
* If the response is not paged, return the body as-is. If pagination | ||
@@ -103,6 +127,8 @@ * information is present in the response headers, parse those headers into | ||
*/ | ||
function paginateResponse( result, endpoint, httpTransport ) { | ||
function createPaginationObject( result, endpoint, httpTransport ) { | ||
var _paging = null; | ||
if ( ! result.headers || ! result.headers[ 'x-wp-totalpages' ] ) { | ||
// No headers: return as-is | ||
return result; | ||
return _paging; | ||
} | ||
@@ -114,3 +140,3 @@ | ||
// No paging: return as-is | ||
return result; | ||
return _paging; | ||
} | ||
@@ -122,3 +148,3 @@ | ||
// Store pagination data from response headers on the response collection | ||
result.body._paging = { | ||
_paging = { | ||
total: result.headers[ 'x-wp-total' ], | ||
@@ -131,3 +157,3 @@ totalPages: totalPages, | ||
if ( links.next ) { | ||
result.body._paging.next = new WPRequest({ | ||
_paging.next = new WPRequest({ | ||
transport: httpTransport, | ||
@@ -140,3 +166,3 @@ endpoint: mergeUrl( endpoint, links.next ) | ||
if ( links.prev ) { | ||
result.body._paging.prev = new WPRequest({ | ||
_paging.prev = new WPRequest({ | ||
transport: httpTransport, | ||
@@ -147,3 +173,3 @@ endpoint: mergeUrl( endpoint, links.prev ) | ||
return result; | ||
return _paging; | ||
} | ||
@@ -164,3 +190,2 @@ | ||
function invokeAndPromisify( request, callback, transform ) { | ||
return new Promise(function( resolve, reject ) { | ||
@@ -218,3 +243,8 @@ // Fire off the result | ||
var httpTransport = wpreq.transport; | ||
return paginateResponse( result, endpoint, httpTransport ).body; | ||
var body = extractResponseBody( result ); | ||
var _paging = createPaginationObject( result, endpoint, httpTransport ); | ||
if ( _paging ) { | ||
body._paging = _paging; | ||
} | ||
return body; | ||
} | ||
@@ -221,0 +251,0 @@ |
@@ -7,3 +7,3 @@ { | ||
"name": "wpapi", | ||
"version": "1.0.2", | ||
"version": "1.0.3-0", | ||
"description": "An isomorphic JavaScript client for interacting with the WordPress REST API", | ||
@@ -10,0 +10,0 @@ "main": "wpapi.js", |
@@ -203,3 +203,3 @@ A WordPress REST API client for JavaScript | ||
This will work in the same manner for resources other than `post`: you can see the list of required data parameters for each resource on the [WP REST API Documentation Website](https://developer.wordpress.org/rest-api/reference/). | ||
This will work in the same manner for resources other than `post`: you can see the list of required data parameters for each resource on the [REST API Developer Handbook](https://developer.wordpress.org/rest-api/reference/). | ||
@@ -229,3 +229,3 @@ ### Updating Posts | ||
This will work in the same manner for resources other than `post`: you can see the list of required data parameters for each resource on the [WP REST API Documentation Website](https://developer.wordpress.org/rest-api/reference/). | ||
This will work in the same manner for resources other than `post`: you can see the list of required data parameters for each resource in the [REST API Developer Handbook](https://developer.wordpress.org/rest-api/reference/). | ||
@@ -554,3 +554,3 @@ ### Requesting Different Resources | ||
Support for Custom Post Types is provided via the `.registerRoute` method. This method returns a handler function which can be assigned to your site instance as a method, and takes the [same namespace and route string arguments as `rest_register_route`](http://v2.wp-api.org/extending/adding/#bare-basics): | ||
Support for Custom Post Types is provided via the `.registerRoute` method. This method returns a handler function which can be assigned to your site instance as a method, and takes the [same namespace and route string arguments as `rest_register_route`](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/): | ||
@@ -857,3 +857,3 @@ ```js | ||
When the library is loaded from the frontend of the WordPress site you are querying against, you can utilize the build in [Cookie authentication](https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#cookie-authentication) supported by WP REST API. | ||
When the library is loaded from the frontend of the WordPress site you are querying against, you may authenticate your REST API requests using the built in WordPress [Cookie authentication](https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#cookie-authentication) by creating and passing a Nonce with your API requests. | ||
@@ -860,0 +860,0 @@ First localize your scripts with an object with root-url and nonce in your theme's `functions.php` or your plugin: |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
81
1811166
13814
1