usergrid
Advanced tools
Comparing version 0.10.0 to 0.10.1
@@ -24,2 +24,4 @@ /** | ||
var request = require('request'); | ||
var Usergrid = {}; | ||
Usergrid.SDK_VERSION = '0.10.01'; | ||
@@ -31,3 +33,3 @@ //authentication type constants | ||
Client = function(options) { | ||
Usergrid.Client = function(options) { | ||
//usergrid enpoint | ||
@@ -52,5 +54,5 @@ this.URI = 'https://api.usergrid.com'; | ||
//timeout and callbacks | ||
this._callTimeout = options.callTimeout || 30000; //default to 30 seconds | ||
this._callTimeoutCallback = options.callTimeoutCallback || null; | ||
this.loggingoutCallback = options.logoutCallback || null; | ||
this._callTimeout = options.callTimeout || 30000; //default to 30 seconds | ||
this._callTimeoutCallback = options.callTimeoutCallback || null; | ||
this.logoutCallback = options.logoutCallback || null; | ||
@@ -75,3 +77,3 @@ }; | ||
*/ | ||
Client.prototype.request = function (options, callback) { | ||
Usergrid.Client.prototype.request = function (options, callback) { | ||
var self = this; | ||
@@ -127,7 +129,7 @@ var method = options.method || 'GET'; | ||
if (self.logging) { | ||
console.log('Error ('+ r.statusCode+')(' + error + '): ' + errorDesc) | ||
console.log('Error (' + r.statusCode + ')(' + error + '): ' + errorDesc) | ||
} | ||
//if the user has specified a logout callback: | ||
if (typeof(self.loggingoutCallback) === 'function') { | ||
self.loggingoutCallback(err, data); | ||
if (typeof(self.logoutCallback) === 'function') { | ||
self.logoutCallback(err, data); | ||
} else if (typeof(callback) === 'function') { | ||
@@ -140,3 +142,3 @@ callback(err, data); | ||
if (self.logging) { | ||
console.log('Error ('+ r.statusCode +')(' + error + '): ' + errorDesc); | ||
console.log('Error (' + r.statusCode + ')(' + error + '): ' + errorDesc); | ||
} | ||
@@ -151,8 +153,19 @@ if (typeof(callback) === 'function') { | ||
Client.prototype.createEntity = function (data, callback) { | ||
/** | ||
* Main function for creating new entities - should be called directly. | ||
* | ||
* options object: options {client:client, data:{'type':'collection_type', 'key':'value'}, uuid:uuid}} | ||
* | ||
* @method createEntity | ||
* @public | ||
* @params {object} options | ||
* @param {function} callback | ||
* @return {callback} callback(err, data) | ||
*/ | ||
Usergrid.Client.prototype.createEntity = function (options, callback) { | ||
var options = { | ||
client:this, | ||
data:data | ||
data:options | ||
} | ||
var entity = new Entity(options); | ||
var entity = new Usergrid.Entity(options); | ||
entity.save(function(err, data) { | ||
@@ -165,6 +178,16 @@ if (typeof(callback) === 'function') { | ||
Client.prototype.createCollection = function (options, callback) { | ||
/** | ||
* Main function for creating new collections - should be called directly. | ||
* | ||
* options object: options {client:client, type: type, qs:qs} | ||
* | ||
* @method createCollection | ||
* @public | ||
* @params {object} options | ||
* @param {function} callback | ||
* @return {callback} callback(err, data) | ||
*/ | ||
Usergrid.Client.prototype.createCollection = function (options, callback) { | ||
options.client = this; | ||
var collection = new Collection(options, function(err, data) { | ||
var collection = new Usergrid.Collection(options, function(err, data) { | ||
if (typeof(callback) === 'function') { | ||
@@ -174,10 +197,8 @@ callback(err, collection); | ||
}); | ||
} | ||
/** | ||
* A private method to get call timing of last call | ||
*/ | ||
Client.prototype.calcTimeDiff = function () { | ||
Usergrid.Client.prototype.calcTimeDiff = function () { | ||
var seconds = 0; | ||
@@ -201,3 +222,3 @@ var time = this._end - this._start; | ||
*/ | ||
Client.prototype.login = function (username, password, callback) { | ||
Usergrid.Client.prototype.login = function (username, password, callback) { | ||
var self = this; | ||
@@ -218,3 +239,3 @@ var options = { | ||
} else { | ||
user = new Entity('users'); | ||
user = new Usergrid.Entity('users'); | ||
user.set(data.user); | ||
@@ -230,2 +251,37 @@ self.user = user; | ||
/* | ||
* A public method to log in an app user with facebook - stores the token for later use | ||
* | ||
* @method loginFacebook | ||
* @public | ||
* @params {string} username | ||
* @params {string} password | ||
* @param {function} callback | ||
* @return {callback} callback(err, data) | ||
*/ | ||
Usergrid.Client.prototype.loginFacebook = function (facebookToken, callback) { | ||
var self = this; | ||
var options = { | ||
method:'GET', | ||
endpoint:'auth/facebook', | ||
qs:{ | ||
fb_access_token: facebookToken | ||
} | ||
}; | ||
this.request(options, function(err, data) { | ||
var user = {}; | ||
if (err && self.logging) { | ||
console.log('error trying to log user in'); | ||
} else { | ||
user = new Usergrid.Entity('users'); | ||
user.set(data.user); | ||
self.user = user; | ||
self.token = data.access_token; | ||
} | ||
if (typeof(callback) === 'function') { | ||
callback(err, data, user); | ||
} | ||
}); | ||
} | ||
/** | ||
@@ -239,3 +295,3 @@ * A public method to test if a user is logged in - does not guarantee that the token is still valid, | ||
*/ | ||
Client.prototype.isLoggedIn = function () { | ||
Usergrid.Client.prototype.isLoggedIn = function () { | ||
var user = this.user; | ||
@@ -260,3 +316,3 @@ var haveUser = (user && this.token); | ||
*/ | ||
Client.prototype.buildCurlCall = function (options) { | ||
Usergrid.Client.prototype.buildCurlCall = function (options) { | ||
var curl = 'curl'; | ||
@@ -278,3 +334,3 @@ var method = (options.method || 'GET').toUpperCase(); | ||
body = JSON.stringify(body) | ||
if (body !== '{}') { | ||
if (body !== '"{}"' && method !== 'GET' && method !== 'DELETE') { | ||
//curl - add in the json obj | ||
@@ -297,3 +353,3 @@ curl += " -d '" + body + "'"; | ||
*/ | ||
Client.prototype.logout = function () { | ||
Usergrid.Client.prototype.logout = function () { | ||
this.user = null; | ||
@@ -303,2 +359,4 @@ this.token = null; | ||
/** | ||
@@ -311,3 +369,3 @@ * A class to Model a Usergrid Entity. | ||
*/ | ||
Entity = function(options) { | ||
Usergrid.Entity = function(options) { | ||
this._client = options.client; | ||
@@ -325,3 +383,3 @@ this._data = options.data || {}; | ||
*/ | ||
Entity.prototype.get = function (field) { | ||
Usergrid.Entity.prototype.get = function (field) { | ||
if (field) { | ||
@@ -344,3 +402,3 @@ return this._data[field]; | ||
*/ | ||
Entity.prototype.set = function (key, value) { | ||
Usergrid.Entity.prototype.set = function (key, value) { | ||
if (typeof key === 'object') { | ||
@@ -369,3 +427,3 @@ for(var field in key) { | ||
*/ | ||
Entity.prototype.save = function (callback) { | ||
Usergrid.Entity.prototype.save = function (callback) { | ||
//TODO: API will be changed soon to accomodate PUTs via name which create new entities | ||
@@ -449,3 +507,3 @@ // This function should be changed to PUT only at that time, and updated to use | ||
*/ | ||
Entity.prototype.fetch = function (callback) { | ||
Usergrid.Entity.prototype.fetch = function (callback) { | ||
var type = this.get('type'); | ||
@@ -515,3 +573,3 @@ var self = this; | ||
*/ | ||
Entity.prototype.destroy = function (callback) { | ||
Usergrid.Entity.prototype.destroy = function (callback) { | ||
var type = this.get('type'); | ||
@@ -558,6 +616,5 @@ if (isUUID(this.get('uuid'))) { | ||
*/ | ||
Collection = function(options, callback) { | ||
Usergrid.Collection = function(options, callback) { | ||
this._client = options.client; | ||
this._type = options.type; | ||
this._uuid = options.uuid; | ||
this.qs = options.qs || {}; | ||
@@ -628,3 +685,3 @@ | ||
*/ | ||
Collection.prototype.fetch = function (callback) { | ||
Usergrid.Collection.prototype.fetch = function (callback) { | ||
var self = this; | ||
@@ -655,2 +712,3 @@ var qs = this.qs; | ||
//save entities locally | ||
self._list = []; //clear the local list first | ||
for (var i=0;i<count;i++) { | ||
@@ -666,3 +724,3 @@ var uuid = data.entities[i].uuid; | ||
}; | ||
var ent = new Entity(entityOptions); | ||
var ent = new Usergrid.Entity(entityOptions); | ||
var ct = self._list.length; | ||
@@ -688,3 +746,3 @@ self._list[ct] = ent; | ||
*/ | ||
Collection.prototype.addEntity = function (options, callback) { | ||
Usergrid.Collection.prototype.addEntity = function (options, callback) { | ||
var self = this; | ||
@@ -714,3 +772,3 @@ options.type = this._type; | ||
*/ | ||
Collection.prototype.destroyEntity = function (entity, callback) { | ||
Usergrid.Collection.prototype.destroyEntity = function (entity, callback) { | ||
var self = this; | ||
@@ -740,3 +798,3 @@ entity.destroy(function(err, data) { | ||
*/ | ||
Collection.prototype.getEntityByUUID = function (uuid, callback) { | ||
Usergrid.Collection.prototype.getEntityByUUID = function (uuid, callback) { | ||
//get the entity from the database | ||
@@ -750,3 +808,3 @@ var options = { | ||
} | ||
var entity = new Entity(options); | ||
var entity = new Usergrid.Entity(options); | ||
entity.fetch(callback); | ||
@@ -761,3 +819,3 @@ } | ||
*/ | ||
Collection.prototype.getFirstEntity = function () { | ||
Usergrid.Collection.prototype.getFirstEntity = function () { | ||
var count = this._list.length; | ||
@@ -776,3 +834,3 @@ if (count > 0) { | ||
*/ | ||
Collection.prototype.getLastEntity = function () { | ||
Usergrid.Collection.prototype.getLastEntity = function () { | ||
var count = this._list.length; | ||
@@ -794,3 +852,3 @@ if (count > 0) { | ||
*/ | ||
Collection.prototype.hasNextEntity = function () { | ||
Usergrid.Collection.prototype.hasNextEntity = function () { | ||
var next = this._iterator + 1; | ||
@@ -813,3 +871,3 @@ var hasNextElement = (next >=0 && next < this._list.length); | ||
*/ | ||
Collection.prototype.getNextEntity = function () { | ||
Usergrid.Collection.prototype.getNextEntity = function () { | ||
this._iterator++; | ||
@@ -830,3 +888,3 @@ var hasNextElement = (this._iterator >= 0 && this._iterator <= this._list.length); | ||
*/ | ||
Collection.prototype.hasPrevEntity = function () { | ||
Usergrid.Collection.prototype.hasPrevEntity = function () { | ||
var previous = this._iterator - 1; | ||
@@ -846,3 +904,3 @@ var hasPreviousElement = (previous >=0 && previous < this._list.length); | ||
*/ | ||
Collection.prototype.getPrevEntity = function () { | ||
Usergrid.Collection.prototype.getPrevEntity = function () { | ||
this._iterator--; | ||
@@ -863,3 +921,3 @@ var hasPreviousElement = (this._iterator >= 0 && this._iterator <= this._list.length); | ||
*/ | ||
Collection.prototype.resetEntityPointer = function () { | ||
Usergrid.Collection.prototype.resetEntityPointer = function () { | ||
this._iterator = -1; | ||
@@ -875,3 +933,3 @@ } | ||
*/ | ||
Collection.prototype.saveCursor = function(cursor) { | ||
Usergrid.Collection.prototype.saveCursor = function(cursor) { | ||
//if current cursor is different, grab it for next cursor | ||
@@ -890,3 +948,3 @@ if (this._next !== cursor) { | ||
*/ | ||
Collection.prototype.resetPaging = function() { | ||
Usergrid.Collection.prototype.resetPaging = function() { | ||
this._previous = []; | ||
@@ -903,3 +961,3 @@ this._next = null; | ||
*/ | ||
Collection.prototype.hasNextPage = function () { | ||
Usergrid.Collection.prototype.hasNextPage = function () { | ||
return (this._next); | ||
@@ -917,3 +975,3 @@ } | ||
*/ | ||
Collection.prototype.getNextPage = function (callback) { | ||
Usergrid.Collection.prototype.getNextPage = function (callback) { | ||
if (this.hasNextPage()) { | ||
@@ -935,3 +993,3 @@ //set the cursor to the next page of data | ||
*/ | ||
Collection.prototype.hasPreviousPage = function () { | ||
Usergrid.Collection.prototype.hasPreviousPage = function () { | ||
return (this._previous.length > 0); | ||
@@ -949,3 +1007,3 @@ } | ||
*/ | ||
Collection.prototype.getPreviousPage = function (callback) { | ||
Usergrid.Collection.prototype.getPreviousPage = function (callback) { | ||
if (this.hasPreviousPage()) { | ||
@@ -974,7 +1032,7 @@ this._next=null; //clear out next so the comparison will find the next item | ||
exports.client = Client; | ||
exports.entity = Entity; | ||
exports.collection = Collection; | ||
exports.client = Usergrid.Client; | ||
exports.entity = Usergrid.Entity; | ||
exports.collection = Usergrid.Collection; | ||
exports.AUTH_CLIENT_ID = AUTH_CLIENT_ID; | ||
exports.AUTH_APP_USER = AUTH_APP_USER; | ||
exports.AUTH_NONE = AUTH_NONE; |
{ | ||
"name": "usergrid", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "A Node.js module for making API calls to App Services (Usergrid) from within Node.js", | ||
@@ -5,0 +5,0 @@ "main": "./lib/usergrid.js", |
##Version | ||
Current Version: **0.10.0** | ||
Current Version: **0.10.1** | ||
@@ -21,3 +21,9 @@ See change log: | ||
##Client side Javascript | ||
Want to make calls to App Services (Usergrid) client-side? No problem - just head over to the Usergrid Javascript SDK: | ||
<https://github.com/apigee/usergrid-javascript-sdk> | ||
The syntax for this Node module and the Javascript SDK are almost exactly the same so you can easily transition between them. | ||
##Installing | ||
@@ -84,2 +90,6 @@ Use npm: | ||
var options = { | ||
method:'GET', | ||
endpoint:'users' | ||
}; | ||
client.request(options, function (err, data) { | ||
@@ -115,3 +125,3 @@ if (err) { | ||
endpoint:'users/fred', | ||
body:{ newkey:'newvalue' } | ||
body:{ newkey:'newvalue' } | ||
}; | ||
@@ -530,4 +540,4 @@ client.request(options, function (err, data) { | ||
d | ||
## Contributing | ||
@@ -534,0 +544,0 @@ We welcome your enhancements! |
104
test.js
@@ -46,78 +46,86 @@ /** | ||
case 1: | ||
console.log('-----running step 1: GET test'); | ||
notice('-----running step '+step+': GET test'); | ||
testGET(step); | ||
break; | ||
case 2: | ||
console.log('-----running step 2: POST test'); | ||
notice('-----running step '+step+': POST test'); | ||
testPOST(step); | ||
break; | ||
case 3: | ||
console.log('-----running step 3: PUT test'); | ||
notice('-----running step '+step+': PUT test'); | ||
testPUT(step); | ||
break; | ||
case 4: | ||
console.log('-----running step 4: DELETE test'); | ||
notice('-----running step '+step+': DELETE test'); | ||
testDELETE(step); | ||
break; | ||
case 5: | ||
console.log('-----running step 5: make a new dog'); | ||
notice('-----running step '+step+': prepare database - remove all dogs (no real dogs harmed here!!)'); | ||
cleanupAllDogs(step); | ||
break; | ||
case 6: | ||
notice('-----running step '+step+': make a new dog'); | ||
makeNewDog(step); | ||
break; | ||
case 6: | ||
console.log('-----running step 6: update our dog'); | ||
case 7: | ||
notice('-----running step '+step+': update our dog'); | ||
updateDog(step, arg); | ||
break; | ||
case 7: | ||
console.log('-----running step 7: refresh our dog'); | ||
case 8: | ||
notice('-----running step '+step+': refresh our dog'); | ||
refreshDog(step, arg); | ||
break; | ||
case 8: | ||
console.log('-----running step 8: remove our dog from database (no real dogs harmed here!!)'); | ||
case 9: | ||
notice('-----running step '+step+': remove our dog from database (no real dogs harmed here!!)'); | ||
removeDogFromDatabase(step, arg); | ||
break; | ||
case 9: | ||
console.log('-----running step 9: make lots of dogs!'); | ||
case 10: | ||
notice('-----running step '+step+': make lots of dogs!'); | ||
makeSampleData(step, arg); | ||
break; | ||
case 10: | ||
console.log('-----running step 10: make a dogs collection and show each dog'); | ||
case 11: | ||
notice('-----running step '+step+': make a dogs collection and show each dog'); | ||
testDogsCollection(step); | ||
break; | ||
case 11: | ||
console.log('-----running step 11: get the next page of the dogs collection and show each dog'); | ||
case 12: | ||
notice('-----running step '+step+': get the next page of the dogs collection and show each dog'); | ||
getNextDogsPage(step, arg); | ||
break; | ||
case 12: | ||
console.log('-----running step 12: get the previous page of the dogs collection and show each dog'); | ||
case 13: | ||
notice('-----running step '+step+': get the previous page of the dogs collection and show each dog'); | ||
getPreviousDogsPage(step, arg); | ||
break; | ||
case 13: | ||
console.log('-----running step 13: remove all dogs from the database (no real dogs harmed here!!)'); | ||
case 14: | ||
notice('-----running step '+step+': remove all dogs from the database (no real dogs harmed here!!)'); | ||
cleanupAllDogs(step); | ||
break; | ||
case 14: | ||
console.log('-----running step 14: create a new user'); | ||
case 15: | ||
notice('-----running step '+step+': prepare database (remove existing user if present)'); | ||
prepareDatabaseForNewUser(step); | ||
break; | ||
case 16: | ||
notice('-----running step '+step+': create a new user'); | ||
createUser(step); | ||
break; | ||
case 15: | ||
console.log('-----running step 15: update the user'); | ||
case 17: | ||
notice('-----running step '+step+': update the user'); | ||
updateUser(step, arg); | ||
break; | ||
case 16: | ||
console.log('-----running step 16: refresh the user from the database'); | ||
case 18: | ||
notice('-----running step '+step+': refresh the user from the database'); | ||
refreshUser(step, arg); | ||
break; | ||
case 17: | ||
console.log('-----running step 17: refresh the user from the database'); | ||
case 19: | ||
notice('-----running step '+step+': refresh the user from the database'); | ||
loginUser(step, arg); | ||
break; | ||
case 18: | ||
console.log('-----running step 18: remove the user from the database'); | ||
case 20: | ||
notice('-----running step '+step+': remove the user from the database'); | ||
destroyUser(step, arg); | ||
break; | ||
default: | ||
console.log('-----test complete!-----'); | ||
console.log('Success count= ' + successCount); | ||
console.log('Error count= ' + errorCount); | ||
console.log('-----thank you for playing!-----'); | ||
notice('-----test complete!-----'); | ||
notice('Success count= ' + successCount); | ||
notice('Error count= ' + errorCount); | ||
notice('-----thank you for playing!-----'); | ||
} | ||
@@ -454,9 +462,9 @@ } | ||
var dog = dogs.getNextEntity(); | ||
var name = dog.get('name'); | ||
notice('removing dog ' + name + ' from database'); | ||
var dogname = dog.get('name'); | ||
notice('removing dog ' + dogname + ' from database'); | ||
dog.destroy(function(err, data) { | ||
if (err) { | ||
error('dog ' + name + ' not removed'); | ||
error('dog not removed'); | ||
} else { | ||
success('dog ' + name + ' removed'); | ||
success('dog removed'); | ||
} | ||
@@ -472,2 +480,19 @@ }); | ||
function prepareDatabaseForNewUser(step) { | ||
var options = { | ||
method:'DELETE', | ||
endpoint:'users/marty' | ||
}; | ||
client.request(options, function (err, data) { | ||
if (err) { | ||
notice('database ready - no user to delete'); | ||
runner(step); | ||
} else { | ||
//data will contain raw results from API call | ||
success('database ready - user deleted worked'); | ||
runner(step); | ||
} | ||
}); | ||
} | ||
function createUser(step) { | ||
@@ -487,2 +512,3 @@ | ||
error('user not saved'); | ||
runner(step, marty); | ||
} else { | ||
@@ -489,0 +515,0 @@ success('user saved'); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
72049
1859
566
0