Skip to content

Commit

Permalink
Merge branch 'master' into feature-pagination-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge authored May 29, 2018
2 parents 49425a0 + fbb9bdd commit 6fe68f7
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 6 deletions.
129 changes: 129 additions & 0 deletions src/management/EmailTemplatesManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');
var Auth0RestClient = require('../Auth0RestClient');
var RetryRestClient = require('../RetryRestClient');

/**
* Simple facade for consuming a REST API endpoint.
* @external RestClient
* @see https://github.com/ngonzalvez/rest-facade
*/

/**
* @class EmailTemplatesManager
* This class provides a simple abstraction for performing CRUD operations
* on Auth0's Email Templates. {@see https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName}
* @constructor
* @memberOf module:management
*
* @param {Object} options The client options.
* @param {String} options.baseUrl The URL of the API.
* @param {Object} [options.headers] Headers to be included in all requests.
* @param {Object} [options.retry] Retry Policy Config
*/
var EmailTemplatesManager = function(options) {
if (!options || 'object' !== typeof options) {
throw new ArgumentError('Must provide manager options');
}

if (!options.baseUrl || 'string' !== typeof options.baseUrl) {
throw new ArgumentError('Must provide a valid string as base URL for the API');
}

/**
* Options object for the Rest Client instance.
*
* @type {Object}
*/
var clientOptions = {
headers: options.headers,
query: { repeatParams: false }
};

/**
* Provides an abstraction layer for performing CRUD operations on
* {@link https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName Auth0's Email Templates}.
*
* @type {external:RestClient}
*/
var auth0RestClient = new Auth0RestClient(
options.baseUrl + '/email-templates/:name',
clientOptions,
options.tokenProvider
);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
};

/**
* Create a new Email Template.
*
* @method create
* @memberOf module:management.EmailTemplatesManager.prototype
*
* @example
* management.emailTemplates.create(data, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Email Template created.
* });
*
* @param {Object} data Email Template data object.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(EmailTemplatesManager, 'create', 'resource.create');

/**
* Get an Auth0 Email Template.
*
* @method get
* @memberOf module:management.EmailTemplatesManager.prototype
*
* @example
* management.emailTemplates.get({ name: EMAIL_TEMPLATE_NAME }, function (err, emailTemplate) {
* if (err) {
* // Handle error.
* }
*
* console.log(emailTemplate);
* });
*
* @param {Object} params Email Template parameters.
* @param {String} params.name Template Name
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(EmailTemplatesManager, 'get', 'resource.get');

/**
* Update an existing Email Template.
*
* @method update
* @memberOf module:management.EmailTemplatesManager.prototype
*
* @example
* var data = { from: '[email protected]' };
* var params = { name: EMAIL_TEMPLATE_NAME };
*
* management.emailTemplates.update(params, data, function (err, emailTemplate) {
* if (err) {
* // Handle error.
* }
*
* console.log(emailTemplate.from); // '[email protected]'
* });
*
* @param {Object} params Email Template parameters.
* @param {String} params.name Template Name
* @param {Object} data Updated Email Template data.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(EmailTemplatesManager, 'update', 'resource.patch');

module.exports = EmailTemplatesManager;
8 changes: 5 additions & 3 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ JobsManager.prototype.get = function(params, cb) {

/**
* Given a path to a file and a connection id, create a new job that imports the
* users contained in the file and associate them with the given connection.
* users contained in the file or JSON string and associate them with the given
* connection.
*
* @method importUsers
* @memberOf module:management.JobsManager.prototype
Expand All @@ -120,6 +121,7 @@ JobsManager.prototype.get = function(params, cb) {
* @param {Object} data Users import data.
* @param {String} data.connectionId Connection for the users insertion.
* @param {String} data.users Path to the users data file.
* @param {String} data.users_json JSON data for the users.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
Expand All @@ -141,9 +143,9 @@ JobsManager.prototype.importUsers = function(data, cb) {
headers: headers,
formData: {
users: {
value: fs.createReadStream(data.users),
value: data.users_json ? Buffer.from(data.users_json) : fs.createReadStream(data.users),
options: {
filename: data.users
filename: data.users_json ? 'users.json' : data.users,
}
},
connection_id: data.connection_id
Expand Down
9 changes: 9 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var TicketsManager = require('./TicketsManager');
var LogsManager = require('./LogsManager');
var ResourceServersManager = require('./ResourceServersManager');
var ManagementTokenProvider = require('./ManagementTokenProvider');
var EmailTemplatesManager = require('./EmailTemplatesManager');

var BASE_URL_FORMAT = 'https://%s/api/v2';
var MANAGEMENT_API_AUD_FORMAT = 'https://%s/api/v2/';
Expand Down Expand Up @@ -235,6 +236,14 @@ var ManagementClient = function(options) {
* @type {ResourceServersManager}
*/
this.resourceServers = new ResourceServersManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on
* Auth0's Email Templates
*
* @type {EmailTemplatesManager}
*/
this.emailTemplates = new EmailTemplatesManager(managerOptions);
};

/**
Expand Down
27 changes: 26 additions & 1 deletion test/data/users.json
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
[]
[
{
"email": "[email protected]",
"email_verified": true,
"app_metadata": {
"roles": [
"admin"
],
"plan": "premium"
},
"user_metadata": {
"theme": "light"
}
},
{
"email": "[email protected]",
"email_verified": false,
"app_metadata": {
"roles": [],
"plan": "basic"
},
"user_metadata": {
"theme": "dark"
}
}
]
Loading

0 comments on commit 6fe68f7

Please sign in to comment.