Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
language: node_js
script: "npm run test:ci"
node_js:
- "stable"
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "iojs"
# Opt-in to travis container infrastructure
sudo: false
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,55 @@ wp.posts().then(function( data ) {
});
```

### Creating Posts

To create posts, use the `.post()` method on a query to POST a data object to the server (POST is the HTTP "verb" equivalent for "create"):

```js
// You must authenticate to be able to POST (create) a post
var wp = new WP({
endpoint: 'http://your-site.com/wp-json',
// This assumes you are using basic auth, as described further below
username: 'someusername',
password: 'password'
});
wp.posts().post({
// "title" and "content" are the only required properties
title: 'Your Post Title',
content: 'Your post content',
// Post will be created as a draft by default if a specific "status"
// is not specified
status: 'publish'
}).then(function( response ) {
// "response" will hold all properties of your newly-created post,
// including the unique `id` the post was assigned on creation
console.log( response.id );
})
```

### Updating Posts

To create posts, use the `.put()` method on a single-item query to PUT a data object to the server (PUT is the HTTP "verb" equivalent for "update"):

```js
// You must authenticate to be able to PUT (update) a post
var wp = new WP({
endpoint: 'http://your-site.com/wp-json',
// This assumes you are using basic auth, as described further below
username: 'someusername',
password: 'password'
});
// .id() must be used to specify the post we are updating
wp.posts().id( 2501 ).post({
// Update the title
title: 'A Better Title',
// Set the post live (assuming it was "draft" before)
status: 'publish'
}).then(function( response ) {
console.log( response );
})
```

### Requesting Different Resources

A WP instance object provides the following basic request methods:
Expand Down
48 changes: 28 additions & 20 deletions lib/shared/wp-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,30 @@ WPRequest.prototype.version = function( version ) {
*
* @method auth
* @chainable
* @param {String} [username] A username string for basic authentication
* @param {String} [password] A user password string for basic authentication
* @param {String|Object} [usrOrObj] A username string for basic authentication,
* or an object with 'username' and 'password'
* string properties
* @param {String} [password] A user password string for basic authentication
* (ignored if usrOrObj is an object)
* @return {WPRequest} The WPRequest instance (for chaining)
*/
WPRequest.prototype.auth = function( username, password ) {
if ( username && typeof username === 'string' ) {
this._options.username = username;
}
WPRequest.prototype.auth = function( usrOrObj, password ) {
if ( typeof usrOrObj === 'object' ) {
if ( typeof usrOrObj.username === 'string' ) {
this._options.username = usrOrObj.username;
}

if ( password && typeof password === 'string' ) {
this._options.password = password;
if ( typeof usrOrObj.password === 'string' ) {
this._options.password = usrOrObj.password;
}
} else {
if ( typeof usrOrObj === 'string' ) {
this._options.username = usrOrObj;
}

if ( typeof password === 'string' ) {
this._options.password = password;
}
}

// Set the "auth" options flag that will force authentication on this request
Expand All @@ -432,8 +445,6 @@ WPRequest.prototype.auth = function( username, password ) {
* @method get
* @async
* @param {Function} [callback] A callback to invoke with the results of the GET request
* @param {Error|Object} callback.err Any errors encountered during the request
* @param {Object} callback.result The body of the server response
* @return {Promise} A promise to the results of the HTTP request
*/
WPRequest.prototype.get = function( callback ) {
Expand All @@ -450,8 +461,6 @@ WPRequest.prototype.get = function( callback ) {
* @async
* @param {Object} data The data for the POST request
* @param {Function} [callback] A callback to invoke with the results of the POST request
* @param {Error|Object} callback.err Any errors encountered during the request
* @param {Object} callback.result The body of the server response
* @return {Promise} A promise to the results of the HTTP request
*/
WPRequest.prototype.post = function( data, callback ) {
Expand All @@ -469,8 +478,6 @@ WPRequest.prototype.post = function( data, callback ) {
* @async
* @param {Object} data The data for the PUT request
* @param {Function} [callback] A callback to invoke with the results of the PUT request
* @param {Error|Object} callback.err Any errors encountered during the request
* @param {Object} callback.result The body of the server response
* @return {Promise} A promise to the results of the HTTP request
*/
WPRequest.prototype.put = function( data, callback ) {
Expand All @@ -486,15 +493,18 @@ WPRequest.prototype.put = function( data, callback ) {
/**
* @method delete
* @async
* @param {Object} [data] Data to send along with the DELETE request
* @param {Function} [callback] A callback to invoke with the results of the DELETE request
* @param {Error|Object} callback.err Any errors encountered during the request
* @param {Object} callback.result The body of the server response
* @return {Promise} A promise to the results of the HTTP request
*/
WPRequest.prototype.delete = function( callback ) {
WPRequest.prototype.delete = function( data, callback ) {
if ( ! callback && typeof data === 'function' ) {
callback = data;
data = null;
}
this._checkMethodSupport( 'delete' );
var url = this._renderURI();
var request = this._auth( agent.del( url ), true );
var request = this._auth( agent.del( url ), true ).send( data );

return invokeAndPromisify( request, callback, returnBody.bind( this ) );
};
Expand All @@ -503,8 +513,6 @@ WPRequest.prototype.delete = function( callback ) {
* @method head
* @async
* @param {Function} [callback] A callback to invoke with the results of the HEAD request
* @param {Error|Object} callback.err Any errors encountered during the request
* @param {Object} callback.result The headers from the server response
* @return {Promise} A promise to the header results of the HTTP request
*/
WPRequest.prototype.head = function( callback ) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
"wordpress"
],
"scripts": {
"prepublish": "grunt",
"docs": "grunt yuidoc",
"lint": "grunt jshint jscs",
"mocha": "_mocha tests --recursive --reporter=nyan",
"watch": "grunt watch",
"test:all": "_mocha tests --recursive --reporter=nyan",
"test:unit": "_mocha tests/unit --recursive --reporter=nyan",
"test:integration": "_mocha tests/integration --recursive --reporter=nyan",
"test:ci": "npm run lint && istanbul cover _mocha -- tests/unit --recursive --reporter=list",
"pretest": "npm run lint",
"test": "istanbul cover _mocha -- tests/unit --recursive --reporter=nyan"
"test": "istanbul cover _mocha -- tests --recursive --reporter=nyan"
},
"engines": {
"node": ">= 0.10.0"
Expand Down
Loading