Repository: auth0/node-auth0
Pull request #577 (2021-01-21 20:09:21)

Additional options on getByEmail [SDK-2268]
Adds the ability to specify additional options to be passed into getUsersByEmail -- https://auth0.com/docs/api/management/v2#!/Users_By_Email/get_users_by_email

Fixes https://github.com/auth0/node-auth0/issues/564


Patch:
diff --git a/src/management/UsersManager.js b/src/management/UsersManager.js
index a6647c32c..a9c18ff0f 100644
--- a/src/management/UsersManager.js
+++ b/src/management/UsersManager.js
@@ -1,6 +1,7 @@
 var ArgumentError = require('rest-facade').ArgumentError;
 var Auth0RestClient = require('../Auth0RestClient');
 var RetryRestClient = require('../RetryRestClient');
+var sanitizeArguments = require('../utils').sanitizeArguments;
 
 /**
  * Simple facade for consuming a REST API endpoint.
@@ -238,13 +239,18 @@ UsersManager.prototype.getAll = function(params) {
  *   console.log(users);
  * });
  *
- * @param   {String}    [email]           Email address of user(s) to find
- * @param   {Function}  [cb]              Callback function.
+ * @param   {String}    [email]                     Email address of user(s) to find
+ * @param   {Object}    [options]                   Additional options to pass to the endpoint
+ * @param   {String}    [options.fields]            Comma-separated list of fields to include or exclude in the result
+ * @param   {Boolean}   [options.include_fields]    Whether specified fields are to be included (true) or excluded (false). Defaults to true.
+ * @param   {Function}  [cb]                        Callback function.
  *
  * @return  {Promise|undefined}
  */
-UsersManager.prototype.getByEmail = function(email, callback) {
-  return this.usersByEmail.getAll({ email }, callback);
+UsersManager.prototype.getByEmail = function(email, options, cb) {
+  var { options, cb } = sanitizeArguments(options, cb);
+
+  return this.usersByEmail.getAll({ email, ...options }, cb);
 };
 
 /**
diff --git a/src/management/index.js b/src/management/index.js
index 092ec8ac7..1dfac641d 100644
--- a/src/management/index.js
+++ b/src/management/index.js
@@ -1019,8 +1019,11 @@ utils.wrapPropertyMethod(ManagementClient, 'getUsers', 'users.getAll');
  *   console.log(users);
  * });
  *
- * @param   {String}    [email]           Email Address of users to locate
- * @param   {Function}  [cb]              Callback function.
+ * @param   {String}    [email]                     Email address of user(s) to find
+ * @param   {Object}    [options]                   Additional options to pass to the endpoint
+ * @param   {String}    [options.fields]            Comma-separated list of fields to include or exclude in the result
+ * @param   {Boolean}   [options.include_fields]    Whether specified fields are to be included (true) or excluded (false). Defaults to true.
+ * @param   {Function}  [cb]                        Callback function.
  *
  * @return  {Promise|undefined}
  */


Test patch:
diff --git a/test/management/users.tests.js b/test/management/users.tests.js
index 16714a6ae..4c3d2b94d 100644
--- a/test/management/users.tests.js
+++ b/test/management/users.tests.js
@@ -259,6 +259,30 @@ describe('UsersManager', function() {
         done();
       });
     });
+
+    it('should pass additional options into the query string', function(done) {
+      nock.cleanAll();
+
+      var additionalOptions = {
+        fields: 'user_id, email, email_verified',
+        include_fields: true
+      };
+      var params = {
+        email: 'email@example.com',
+        ...additionalOptions
+      };
+
+      var request = nock(API_URL)
+        .get('/users-by-email')
+        .query(params)
+        .reply(200);
+
+      this.users.getByEmail(params.email, additionalOptions).then(function() {
+        expect(request.isDone()).to.be.true;
+
+        done();
+      });
+    });
   });
 
   describe('#get', function() {