Repository: auth0/node-auth0
Pull request #29 (2015-04-13 14:51:01)

Changed API


Patch:
diff --git a/README.md b/README.md
index 753c2e274..a5b15dd34 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ Node.js client library for the [Auth0](https://auth0.com) platform.
 
 ## Installation
 
-	npm install auth0@2.0.0-alpha.3
+	npm install auth0@2.0.0-alpha.5
 
 ## Usage
 
@@ -42,7 +42,7 @@ You can store data about a user that does not come from the user's identity prov
 To update the value of a property or create it if it does not exist:
 ~~~js
 var user_id = '...'
-auth0.users(user_id).appMetadata.update({
+auth0.users.updateAppMetadata(user_id, {
   roles: ['reader']
 }).then(function(user){
   // assert.equal(user.app_metadata.roles.length, 1);
@@ -55,7 +55,7 @@ auth0.users(user_id).appMetadata.update({
 To delete a property:
 ~~~js
 var user_id = '...'
-auth0.users(user_id).appMetadata.update({
+auth0.users.updateAppMetadata(user_id, {
   roles: null
 }, function(err, user){
   // assert.equal(typeof user.app_metadata.roles, 'undefined');
@@ -68,7 +68,7 @@ auth0.users(user_id).appMetadata.update({
 To update the value of a property or create it if it does not exist:
 ~~~js
 var user_id = '...'
-auth0.users(user_id).userMetadata.update({
+auth0.users.updateUserMetadata(user_id,{
   hobby: 'surf'
 }).then(function(user){
   // assert.equal(user.user_metadata.hobby, 'surf');
@@ -80,7 +80,7 @@ auth0.users(user_id).userMetadata.update({
 To delete a property:
 ~~~js
 var user_id = '...'
-auth0.users(user_id).userMetadata.update({
+auth0.users.updateUserMetadata(user_id,{
   hobby: null
 }, function(err, user){
   // assert.equal(typeof user.user_metadata.hobby, 'undefined');
diff --git a/examples/updateAppMetadata.js b/examples/updateAppMetadata.js
index dfd45c47d..f6928bce3 100644
--- a/examples/updateAppMetadata.js
+++ b/examples/updateAppMetadata.js
@@ -1,7 +1,7 @@
 var token = '{YOUR_API_JSON_WEB_TOKEN}';
 var auth0 = require('..')({ token: token });
 
-auth0.user('google-oauth2|1234').appMetadata.update({
+auth0.users.updateAppMetadata('google-oauth2|1234', {
   rules: ['writer', 'reader'],
   plan: null
 }).then(function(body){
diff --git a/examples/updateUserMetadata.js b/examples/updateUserMetadata.js
index 43430e748..4476af3c9 100644
--- a/examples/updateUserMetadata.js
+++ b/examples/updateUserMetadata.js
@@ -1,7 +1,7 @@
 var token = '{YOUR_API_JSON_WEB_TOKEN}';
 var auth0 = require('..')({ token: token });
 
-auth0.user('google-oauth2|1234').userMetadata.update({
+auth0.users.updateUserMetadata('google-oauth2|1234', {
   hobby: 'surf',
   theme: 'green'
 }).then(function(body){
diff --git a/package.json b/package.json
index 8482fe945..f34c5b720 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "auth0",
-  "version": "2.0.0-alpha.4",
+  "version": "2.0.0-alpha.5",
   "description": "node.js SDK for Auth0 API v2",
   "main": "index.js",
   "scripts": {
diff --git a/src/client.js b/src/client.js
index 0e173a8e7..bcab9476f 100644
--- a/src/client.js
+++ b/src/client.js
@@ -1,5 +1,7 @@
-var User = require('./user');
+var Users = require('./users');
 var util = require('util');
+var utils = require('./utils');
+
 var DEFAULT_REGION = 'us';
 var BASE_URL_FORMAT = 'https://%s/api/v2';
 var ArgumentError = require('./errors').ArgumentError;
@@ -30,9 +32,6 @@ function Client(options){
   this.baseUrl = util.format(BASE_URL_FORMAT, domain);
 }
 
-Client.prototype.user = function(id){
-  // TODO: caching
-  return new User(this, id);
-};
+utils.subEntity(Client, 'users', Users);
 
 module.exports = Client;
\ No newline at end of file
diff --git a/src/metadata.js b/src/metadata.js
deleted file mode 100644
index cd0fbe6f0..000000000
--- a/src/metadata.js
+++ /dev/null
@@ -1,52 +0,0 @@
-var request = require('superagent');
-var Promise = require('bluebird');
-var util = require('util');
-var urlJoin  = require('url-join');
-var utils = require('./utils');
-
-var USER_PATCH_URL = '/users/%s';
-
-function Metadata(user){
-  var client = user.client;
-  this.user = user;
-  this.accessToken = client.accessToken;
-}
-
-Metadata.prototype.update = function(updatedMetadata, cb) {
-  var partialUrl = util.format(USER_PATCH_URL, this.user.id);
-  var fullUrl = urlJoin(this.user.client.baseUrl, partialUrl);
-
-  var body = {};
-  body[this.type] = updatedMetadata;
-
-  var self = this;
-
-  return new Promise(function(res, rej){
-    var done = utils.successCallback(cb, res);
-    request
-    .patch(fullUrl)
-    .set('Authorization', 'Bearer ' + self.accessToken)
-    .send(body)
-    .end(utils.responseHandler(cb || rej, function(resp){
-      done(resp.body);
-    }));
-  });
-};
-
-var AppMetadata = function(user){
-  AppMetadata.super_.call(this, user);
-  this.type = 'app_metadata';
-};
-
-util.inherits(AppMetadata, Metadata);
-
-var UserMetadata = function(user){
-  UserMetadata.super_.call(this, user);
-  this.type = 'user_metadata';
-};
-
-util.inherits(UserMetadata, Metadata);
-
-module.exports.AppMetadata = AppMetadata;
-
-module.exports.UserMetadata = UserMetadata;
\ No newline at end of file
diff --git a/src/metadataUpdater.js b/src/metadataUpdater.js
new file mode 100644
index 000000000..df84271f5
--- /dev/null
+++ b/src/metadataUpdater.js
@@ -0,0 +1,35 @@
+var request = require('superagent');
+var Promise = require('bluebird');
+var util = require('util');
+var urlJoin  = require('url-join');
+var utils = require('./utils');
+
+var USER_PATCH_URL = '/users/%s';
+
+function MetadataUpdater(client){
+  this.baseUrl = client.baseUrl;
+  this.accessToken = client.accessToken;
+}
+
+MetadataUpdater.prototype.update = function(id, params, cb) {
+  var partialUrl = util.format(USER_PATCH_URL, id);
+  var fullUrl = urlJoin(this.baseUrl, partialUrl);
+
+  var body = {};
+  body[params.type] = params.updatedMetadata;
+
+  var self = this;
+
+  return new Promise(function(res, rej){
+    var done = utils.successCallback(cb, res);
+    request
+    .patch(fullUrl)
+    .set('Authorization', 'Bearer ' + self.accessToken)
+    .send(body)
+    .end(utils.responseHandler(cb || rej, function(resp){
+      done(resp.body);
+    }));
+  });
+};
+
+module.exports = MetadataUpdater;
\ No newline at end of file
diff --git a/src/user.js b/src/user.js
deleted file mode 100644
index ffc7350d8..000000000
--- a/src/user.js
+++ /dev/null
@@ -1,12 +0,0 @@
-var metadata = require('./metadata');
-var utils = require('./utils');
-
-function User(client, id){
-  this.id = id;
-  this.client = client;
-}
-
-utils.subEntity(User, 'appMetadata', metadata.AppMetadata);
-utils.subEntity(User, 'userMetadata', metadata.UserMetadata);
-
-module.exports = User;
\ No newline at end of file
diff --git a/src/users.js b/src/users.js
new file mode 100644
index 000000000..19699bfb2
--- /dev/null
+++ b/src/users.js
@@ -0,0 +1,23 @@
+var MetadataUpdater = require('./metadataUpdater');
+
+function Users(client, id){
+  this.id = id;
+  this.client = client;
+  this.metadataUpdater = new MetadataUpdater(client);
+}
+
+Users.prototype.updateAppMetadata = function(id, updatedMetadata, cb){
+  return this.metadataUpdater.update(id, {
+    updatedMetadata: updatedMetadata,
+    type: 'app_metadata'
+  }, cb);
+};
+
+Users.prototype.updateUserMetadata = function(id, updatedMetadata, cb){
+  return this.metadataUpdater.update(id, {
+    updatedMetadata: updatedMetadata,
+    type: 'user_metadata'
+  }, cb);
+};
+
+module.exports = Users;
\ No newline at end of file


Test patch:
diff --git a/test/appMetadata.tests.js b/test/appMetadata.tests.js
index 743316bd4..489445310 100644
--- a/test/appMetadata.tests.js
+++ b/test/appMetadata.tests.js
@@ -37,7 +37,7 @@ describe('app metadata', function(){
         'message': 'Invalid token'
       });
 
-      auth0.user(user_id).appMetadata.update(update, function(err){
+      auth0.users.updateAppMetadata(user_id, update, function(err){
         expect(err).to.not.be.undefined;
         expect(err.statusCode).to.equal(401);
         expect(err.error).to.equal('Unauthorized');
@@ -55,7 +55,7 @@ describe('app metadata', function(){
         id: user_id
       });
 
-      return auth0.user(user_id).appMetadata.update(update)
+      return auth0.users.updateAppMetadata(user_id, update)
         .then(function(body){
           expect(body.id).to.equal(user_id);
           expect(body.app_metadata.roles).to.have.length(2);
diff --git a/test/domains.tests.js b/test/domains.tests.js
index 5ebb3784e..d5b314f45 100644
--- a/test/domains.tests.js
+++ b/test/domains.tests.js
@@ -42,7 +42,7 @@ describe('regions', function(){
   after(nock.cleanAll);
 
   it ('should perform request against correct domain', function(){
-    return auth0.user(user_id).appMetadata.update(update).catch(function(err){
+    return auth0.users.updateAppMetadata(user_id, update).catch(function(err){
       expect(err).to.not.be.undefined;
       expect(err.statusCode).to.equal(401);
       expect(err.error).to.equal('Unauthorized');
@@ -88,7 +88,7 @@ describe('domains', function(){
   after(nock.cleanAll);
 
   it ('should perform request against correct domain', function(){
-    return auth0.user(user_id).appMetadata.update(update).catch(function(err){
+    return auth0.users.updateAppMetadata(user_id, update).catch(function(err){
       expect(err).to.not.be.undefined;
       expect(err.statusCode).to.equal(401);
       expect(err.error).to.equal('Unauthorized');
diff --git a/test/userMetadata.tests.js b/test/userMetadata.tests.js
index 7da5e92e8..ce7112e77 100644
--- a/test/userMetadata.tests.js
+++ b/test/userMetadata.tests.js
@@ -37,7 +37,7 @@ describe('user metadata', function(){
         'message': 'Invalid token'
       });
 
-      auth0.user(user_id).userMetadata.update(update, function(err){
+      auth0.users.updateUserMetadata(user_id, update, function(err){
         expect(err).to.not.be.undefined;
         expect(err.statusCode).to.equal(401);
         expect(err.error).to.equal('Unauthorized');
@@ -53,7 +53,7 @@ describe('user metadata', function(){
         id: user_id
       });
 
-      return auth0.user(user_id).userMetadata.update(update)
+      return auth0.users.updateUserMetadata(user_id, update)
         .then(function(body){
           expect(body.id).to.equal(user_id);
           expect(body.user_metadata.hobby).to.equal('surf');