Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

usergrid

Package Overview
Dependencies
6
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.10.4 to 0.10.5

.idea/.name

9

changelog.md
##Change log
###0.10.5
- Added new class and methods for Groups
- Added serialization / restore methods for entities and collections
- Various bug fixes
- Added function for getting user feed
- Added function for creating user activities with an associated user entity
- Added public facing helper method for signing up users
###0.10.4

@@ -4,0 +13,0 @@

522

lib/usergrid.js

@@ -25,3 +25,3 @@ /*

var Usergrid = {};
Usergrid.SDK_VERSION = '0.10.03';
Usergrid.SDK_VERSION = '0.10.05';

@@ -148,2 +148,37 @@ //authentication type constants

/*
* Main function for creating new groups. Call this directly.
*
* @method createGroup
* @public
* @params {string} path
* @param {function} callback
* @return {callback} callback(err, data)
*/
Usergrid.Client.prototype.createGroup = function(options, callback) {
var getOnExist = options.getOnExist || false;
var options = {
path: options.path,
client: this,
data:options
}
var group = new Usergrid.Group(options);
group.fetch(function(err, data){
var okToSave = (err && 'service_resource_not_found' === data.error || 'no_name_specified' === data.error || 'null_pointer' === data.error) || (!err && getOnExist);
if (okToSave) {
group.save(function(err, data){
if (typeof(callback) === 'function') {
callback(err, group);
}
});
} else {
if(typeof(callback) === 'function') {
callback(err, group);
}
}
});
}
/*
* Main function for creating new entities - should be called directly.

@@ -182,3 +217,3 @@ *

//if the fetch doesn't find what we are looking for, or there is no error, do a save
var okToSave = (err && 'service_resource_not_found' === data.error) || (!err && getOnExist);
var okToSave = (err && 'service_resource_not_found' === data.error || 'no_name_specified' === data.error || 'null_pointer' === data.error) || (!err && getOnExist);
if(okToSave) {

@@ -201,2 +236,21 @@ entity.set(options.data); //add the data again just in case

/*
* Main function for restoring an entity from serialized data.
*
* serializedObject should have come from entityObject.serialize();
*
* @method restoreEntity
* @public
* @param {string} serializedObject
* @return {object} Entity Object
*/
Usergrid.Client.prototype.restoreEntity = function (serializedObject) {
var data = JSON.parse(serializedObject);
var options = {
client:this,
data:data
}
var entity = new Usergrid.Entity(options);
return entity;
}
/*
* Main function for getting existing entities - should be called directly.

@@ -228,3 +282,2 @@ *

/*

@@ -251,2 +304,45 @@ * Main function for creating new collections - should be called directly.

/*
* Main function for restoring a collection from serialized data.
*
* serializedObject should have come from collectionObject.serialize();
*
* @method restoreCollection
* @public
* @param {string} serializedObject
* @return {object} Collection Object
*/
Usergrid.Client.prototype.restoreCollection = function (serializedObject) {
var data = JSON.parse(serializedObject);
data.client = this;
var collection = new Usergrid.Collection(data);
return collection;
}
/*
* Main function for retrieving a user's activity feed.
*
* @method getFeedForUser
* @public
* @params {string} username
* @param {function} callback
* @return {callback} callback(err, data, activities)
*/
Usergrid.Client.prototype.getFeedForUser = function(username, callback) {
var options = {
method: "GET",
endpoint: "users/"+username+"/feed"
}
this.request(options, function(err, data){
if(typeof(callback) === "function") {
if(err) {
callback(err);
} else {
callback(err, data, data.entities);
}
}
});
}
/*
* Function for creating new activities for the current user - should be called directly.

@@ -302,2 +398,38 @@ *

/*
* Function for creating user activities with an associated user entity.
*
* user object:
* The user object passed into this function is an instance of Usergrid.Entity.
*
* @method createUserActivityWithEntity
* @public
* @params {object} user
* @params {string} content
* @param {function} callback
* @return {callback} callback(err, data)
*/
Usergrid.Client.prototype.createUserActivityWithEntity = function(user, content, callback) {
var username = user.get("username");
var options = {
actor: {
"displayName":username,
"uuid":user.get("uuid"),
"username":username,
"email":user.get("email"),
"picture":user.get("picture"),
"image": {
"duration":0,
"height":80,
"url":user.get("picture"),
"width":80
},
},
"verb":"post",
"content":content };
this.createUserActivity(username, options, callback);
}
/*
* A private method to get call timing of last call

@@ -341,2 +473,28 @@ */

/*
* A public facing helper method for signing up users
*
* @method signup
* @public
* @params {string} username
* @params {string} password
* @params {string} email
* @params {string} name
* @param {function} callback
* @return {callback} callback(err, data)
*/
Usergrid.Client.prototype.signup = function(username, password, email, name, callback) {
var self = this;
var options = {
type:"users",
username:username,
password:password,
email:email,
name:name
};
this.createEntity(options, callback);
}
/*
*
* A public method to log in an app user - stores the token for later use

@@ -367,3 +525,7 @@ *

} else {
user = new Usergrid.Entity('users', data.user);
var options = {
client:self,
data:data.user
}
user = new Usergrid.Entity(options);
self.setToken(data.access_token);

@@ -401,3 +563,7 @@ }

} else {
user = new Usergrid.Entity('users', data.user);
var options = {
client: self,
data: data.user
}
user = new Usergrid.Entity(options);
self.setToken(data.access_token);

@@ -426,3 +592,3 @@ }

method:'GET',
endpoint:'users/me',
endpoint:'users/me'
};

@@ -522,7 +688,21 @@ this.request(options, function(err, data) {

Usergrid.Entity = function(options) {
this._client = options.client;
this._data = options.data || {};
if(options){
this._client = options.client;
this._data = options.data || {};
}
};
/*
* returns a serialized version of the entity object
*
* Note: use the client.restoreEntity() function to restore
*
* @method serialize
* @return {string} data
*/
Usergrid.Entity.prototype.serialize = function () {
return JSON.stringify(this._data);
}
/*
* gets a specific field or the entire data object. If null or no argument

@@ -595,3 +775,3 @@ * passed, will return all data, else, will return a specific field

if (item === 'metadata' || item === 'created' || item === 'modified' ||
item === 'type' || item === 'activatted' || item ==='uuid') { continue; }
item === 'type' || item === 'activated' || item ==='uuid') { continue; }
data[item] = entityData[item];

@@ -679,3 +859,3 @@ }

if (this.get('name')) {
type += '/' + this.get('name');
type += '/' + encodeURIComponent(this.get('name'));
} else {

@@ -813,3 +993,12 @@ if (typeof(callback) === 'function') {

/*
* returns a unique identifier for an entity
*
* @method connect
* @public
* @param {object} entity
* @param {function} callback
* @return {callback} callback(err, data)
*
*/
Usergrid.Entity.prototype.getEntityId = function (entity) {

@@ -837,3 +1026,3 @@ var id = false;

* @param {function} callback
* @return {callback} callback(err, data)
* @return {callback} callback(err, data, connections)
*

@@ -882,3 +1071,3 @@ */

if (typeof(callback) === 'function') {
callback(err, data);
callback(err, data, data.entities);
}

@@ -958,20 +1147,66 @@ });

Usergrid.Collection = function(options, callback) {
this._client = options.client;
this._type = options.type;
this.qs = options.qs || {};
//iteration
this._list = [];
this._iterator = -1; //first thing we do is increment, so set to -1
if (options) {
this._client = options.client;
this._type = options.type;
this.qs = options.qs || {};
//paging
this._previous = [];
this._next = null;
this._cursor = null
//iteration
this._list = options.list || [];
this._iterator = options.iterator || -1; //first thing we do is increment, so set to -1
//populate the collection
this.fetch(callback);
//paging
this._previous = options.previous || [];
this._next = options.next || null;
this._cursor = options.cursor || null;
//restore entities if available
if (options.list) {
var count = options.list.length;
for(var i=0;i<count;i++){
//make new entity with
var entity = this._client.restoreEntity(options.list[i]);
this._list[i] = entity;
}
}
}
if (callback) {
//populate the collection
this.fetch(callback);
}
}
/*
* gets the data from the collection object for serialization
*
* @method serialize
* @return {object} data
*/
Usergrid.Collection.prototype.serialize = function () {
//pull out the state from this object and return it
var data = {}
data.type = this._type;
data.qs = this.qs;
data.iterator = this._iterator;
data.previous = this._previous;
data.next = this._next;
data.cursor = this._cursor;
this.resetEntityPointer();
var i=0;
data.list = [];
while(this.hasNextEntity()) {
var entity = this.getNextEntity();
data.list[i] = entity.serialize();
i++;
}
data = JSON.stringify(data);
return data;
}
/*
* Populates the collection from the server

@@ -1296,3 +1531,239 @@ *

/*
* A class to model a Usergrid group.
* Set the path in the options object.
*
* @constructor
* @param {object} options {client:client, data: {'key': 'value'}, path:'path'}
*/
Usergrid.Group = function(options, callback) {
this._path = options.path;
this._list = [];
this._client = options.client;
this._data = options.data || {};
this._data.type = "groups";
}
/*
* Inherit from Usergrid.Entity.
* Note: This only accounts for data on the group object itself.
* You need to use add and remove to manipulate group membership.
*/
Usergrid.Group.prototype = new Usergrid.Entity();
/*
* Fetches current group data, and members.
*
* @method fetch
* @public
* @param {function} callback
* @returns {function} callback(err, data)
*/
Usergrid.Group.prototype.fetch = function(callback) {
var self = this;
var groupEndpoint = 'groups/'+this._path;
var memberEndpoint = 'groups/'+this._path+'/users';
var groupOptions = {
method:'GET',
endpoint:groupEndpoint
}
var memberOptions = {
method:'GET',
endpoint:memberEndpoint
}
this._client.request(groupOptions, function(err, data){
if(err) {
if(self._client.logging) {
console.log('error getting group');
}
if(typeof(callback) === 'function') {
callback(err, data);
}
} else {
if(data.entities) {
var groupData = data.entities[0];
self._data = groupData || {};
self._client.request(memberOptions, function(err, data) {
if(err && self._client.logging) {
console.log('error getting group users');
} else {
if(data.entities) {
var count = data.entities.length;
self._list = [];
for (var i = 0; i < count; i++) {
var uuid = data.entities[i].uuid;
if(uuid) {
var entityData = data.entities[i] || {};
var entityOptions = {
type: entityData.type,
client: self._client,
uuid:uuid,
data:entityData
};
var entity = new Usergrid.Entity(entityOptions);
self._list.push(entity);
}
}
}
}
if(typeof(callback) === 'function') {
callback(err, data, self._list);
}
});
}
}
});
}
/*
* Retrieves the members of a group.
*
* @method members
* @public
* @param {function} callback
* @return {function} callback(err, data);
*/
Usergrid.Group.prototype.members = function(callback) {
if(typeof(callback) === 'function') {
callback(null, this._list);
}
}
/*
* Adds a user to the group, and refreshes the group object.
*
* Options object: {user: user_entity}
*
* @method add
* @public
* @params {object} options
* @param {function} callback
* @return {function} callback(err, data)
*/
Usergrid.Group.prototype.add = function(options, callback) {
var self = this;
var options = {
method:"POST",
endpoint:"groups/"+this._path+"/users/"+options.user.get('username')
}
this._client.request(options, function(error, data){
if(error) {
if(typeof(callback) === 'function') {
callback(error, data, data.entities);
}
} else {
self.fetch(callback);
}
});
}
/*
* Removes a user from a group, and refreshes the group object.
*
* Options object: {user: user_entity}
*
* @method remove
* @public
* @params {object} options
* @param {function} callback
* @return {function} callback(err, data)
*/
Usergrid.Group.prototype.remove = function(options, callback) {
var self = this;
var options = {
method:"DELETE",
endpoint:"groups/"+this._path+"/users/"+options.user.get('username')
}
this._client.request(options, function(error, data){
if(error) {
if(typeof(callback) === 'function') {
callback(error, data);
}
} else {
self.fetch(callback);
}
});
}
/*
* Gets feed for a group.
*
* @public
* @method feed
* @param {function} callback
* @returns {callback} callback(err, data, activities)
*/
Usergrid.Group.prototype.feed = function(callback) {
var self = this;
var endpoint = "groups/"+this._path+"/feed";
var options = {
method:"GET",
endpoint:endpoint
}
this._client.request(options, function(err, data){
if (err && self.logging) {
console.log('error trying to log user in');
}
if(typeof(callback) === 'function') {
callback(err, data, data.entities);
}
});
}
/*
* Creates activity and posts to group feed.
*
* options object: {user: user_entity, content: "activity content"}
*
* @public
* @method createGroupActivity
* @params {object} options
* @param {function} callback
* @returns {callback} callback(err, entity)
*/
Usergrid.Group.prototype.createGroupActivity = function(options, callback){
var user = options.user;
var options = {
actor: {
"displayName":user.get("username"),
"uuid":user.get("uuid"),
"username":user.get("username"),
"email":user.get("email"),
"picture":user.get("picture"),
"image": {
"duration":0,
"height":80,
"url":user.get("picture"),
"width":80
},
},
"verb":"post",
"content":options.content };
options.type = 'groups/'+this._path+'/activities';
var options = {
client:this._client,
data:options
}
var entity = new Usergrid.Entity(options);
entity.save(function(err, data) {
if (typeof(callback) === 'function') {
callback(err, entity);
}
});
}
/*
* Tests if the string is a uuid

@@ -1314,4 +1785,5 @@ *

exports.collection = Usergrid.Collection;
exports.group = Usergrid.Group;
exports.AUTH_CLIENT_ID = AUTH_CLIENT_ID;
exports.AUTH_APP_USER = AUTH_APP_USER;
exports.AUTH_NONE = AUTH_NONE;

2

package.json
{
"name": "usergrid",
"version": "0.10.4",
"version": "0.10.5",
"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.4**
Current Version: **0.10.5**

@@ -196,3 +196,3 @@ See change log:

or
var state = dog.get('state');

@@ -217,3 +217,3 @@

During a client.createEntity call, there are two ways that you can choose to handle this situation. The question is, what should the client do if an entity with the same name, username, or uuid already exists on the server?
1. Give you back an error.

@@ -470,7 +470,7 @@ 2. Give you back the pre-existing entity.

Connections are a way to connect to entities with some verb. This is called an entity relationship. For example, if you have a user entity with username of marty, and a dog entity with a name of einstein, then using our RESTful API, you could make a call like this:
POST users/marty/likes/dogs/einstein
This creates a one-way connection between marty and einstein, where marty "likes" einstein.
POST users/marty/likes/dogs/einstein
This creates a one-way connection between marty and einstein, where marty "likes" einstein.
Complete documentation on the entity relationships API can be found here:

@@ -617,2 +617,4 @@

##Groups
This module provides an easy way to make new groups. They follow the same syntax as Entities

@@ -619,0 +621,0 @@ ##Making generic calls

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc