Skip to content

Commit

Permalink
DXEX-273: Consistent method signatures (#5)
Browse files Browse the repository at this point in the history
* Made getAll hooks consistent.

* Made get single hook signature consistent.

* Made update hook signature consistent.

* Made update method signature consistent.

* Add sample test script.

* Rename package to original name and bumped version.
  • Loading branch information
shawnmclean authored Sep 21, 2019
1 parent 39d51d9 commit 8cca40b
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth0-ext-alpha",
"version": "2.19.2",
"name": "auth0",
"version": "3.0.1",
"description": "SDK for Auth0 API v2",
"main": "src/index.js",
"files": [
Expand Down
122 changes: 81 additions & 41 deletions src/management/HooksManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,63 @@ var HooksManager = function(options) {
};

/**
* Get all rules.
* Create a new hook.
*
* @method getAll
* @method create
* @memberOf module:management.HooksManager.prototype
*
* @example <caption>
* This method takes an optional object as first argument that may be used to
* specify pagination settings. If pagination options are not present,
* the first page of a limited number of results will be returned.
* </caption>
* @example
* management.hooks.create(data, function (err) {
* if (err) {
* // Handle error.
* }
*
* management.hooks.getAll(0, 100, function (err, hooks) {
* console.log(hooks.length);
* // Hook created.
* });
*
* @param {Number} [offset] The number of records to skip
* @param {Number} [limit] Number of records to return
* @param {Function} [cb] Callback function.
* @param {Object} data Hook data object.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(HooksManager, 'getAll', 'resource.getHooks');
utils.wrapPropertyMethod(HooksManager, 'create', 'resource.createHook');

/**
* Create a new hook.
* Get all hooks.
*
* @method create
* @method getAll
* @memberOf module:management.HooksManager.prototype
*
* @example
* management.hooks.create(data, function (err) {
* if (err) {
* // Handle error.
* }
* @example <caption>
* This method takes an optional object as first argument that may be used to
* specify pagination settings. If pagination options are not present,
* the first page of a limited number of results will be returned.
* </caption>
*
* // Rule created.
* // Pagination settings.
* var params = {
* offset: 0,
* limit: 100
* };
* management.hooks.getAll(params, function (err, hooks) {
* console.log(hooks.length);
* });
*
* @param {Object} data Hook data object.
* @param {Function} [cb] Callback function.
* @param {Object} [params] Hooks parameters.
* @param {Number} [params.offset] Number of results per page.
* @param {Number} [params.limit] Page number, zero indexed.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(HooksManager, 'create', 'resource.createHook');
HooksManager.prototype.getAll = function(params = {}, cb) {
if (cb && cb instanceof Function) {
throw Error('Callback not supported in this iteration.');
}

// Return a promise.
return this.resource.getHooks(params.offset || 0, params.limit || 100);
};

/**
* Get a hook by id.
Expand All @@ -76,62 +89,89 @@ utils.wrapPropertyMethod(HooksManager, 'create', 'resource.createHook');
* @memberOf module:management.HooksManager.prototype
*
* @example
* management.hooks.get(id, function (err) {
* management.hooks.get({ id: HOOK_ID }, function (err, hook) {
* if (err) {
* // Handle error.
* }
*
* // Rule created.
* console.log(hook);
* });
*
* @param {string} id Hook data object.
* @param {Object} params Hook parameters.
* @param {String} params.id Hook ID.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(HooksManager, 'get', 'resource.getHookById');
HooksManager.prototype.get = function(params, cb) {
if (cb && cb instanceof Function) {
throw Error('Callback not supported in this iteration.');
}

// Return a promise.
return this.resource.getHookById(params.id);
};

/**
* Delete an existing hook.
* Update an existing hook.
*
* @method delete
* @method update
* @memberOf module:management.HooksManager.prototype
*
* @example
* management.hooks.delete(id, function (err) {
* var data = { name: 'New name' };
* var params = { id: HOOK_ID };
* management.hooks.update(params, data, function (err, hook) {
* if (err) {
* // Handle error.
* }
*
* // Hook deleted.
* // Hook updated.
* console.log(hook.name) // 'New name'
* });
*
* @param {String} id Hook ID.
* @param {Object} params Hook parameters.
* @param {String} params.id Hook ID.
* @param {Object} data Updated hook data.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(HooksManager, 'delete', 'resource.deleteHookById');
HooksManager.prototype.update = function(params, data, cb) {
if (cb && cb instanceof Function) {
throw Error('Callback not supported in this iteration.');
}

// Return a promise.
return this.resource.updateHook({ ...data, id: params.id });
};

/**
* Update an existing hook.
* Delete an existing hook.
*
* @method update
* @method delete
* @memberOf module:management.HooksManager.prototype
*
* @example
* management.hooks.update(data, function (err) {
* management.hooks.delete({ id : HOOK_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Hook updated.
* // Hook deleted.
* });
*
* @param {Object} data Hook data object.
* @param {Function} [cb] Callback function.
* @param {Object} params Hook parameters.
* @param {String} params.id Hook ID.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(HooksManager, 'update', 'resource.updateHook');
HooksManager.prototype.delete = function(params, cb) {
if (cb && cb instanceof Function) {
throw Error('Callback not supported in this iteration.');
}

// Return a promise.
return this.resource.deleteHookById(params.id);
};

module.exports = HooksManager;
108 changes: 108 additions & 0 deletions test.sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const ManagementClient = require('./src/index').ManagementClient;

process.env.WEBTASK_API_TOKEN = '<webtask_api>';
process.env.WEBTASK_API_URL = 'https://sandbox8-us.it.auth0.com';

var management = new ManagementClient({
token: '<management api token>',
domain: '<some_domain>'
});

const hook_temlate = `/**
@param {object} client - information about the client
@param {string} client.name - name of client
@param {string} client.id - client id
@param {string} client.tenant - Auth0 tenant name
@param {object} client.metadata - client metadata
@param {array|undefined} scope - array of strings representing the scope claim or undefined
@param {string} audience - token's audience claim
@param {object} context - additional authorization context
@param {object} context.webtask - webtask context
@param {function} cb - function (error, accessTokenClaims)
*/
module.exports = function(client, scope, audience, context, cb) {
var access_token = {};
access_token.scope = scope;
// Modify scopes or add extra claims
// access_token['https://example.com/claim'] = 'bar';
// access_token.scope.push('extra');
cb(null, access_token);
};
`;
function createHook() {
management.hooks
.create({
triggerId: 'post-user-registration',
name: 'post-user-reg', // Also testing that kebab casing works
active: true,
code: hook_temlate,
secrets: {
'api-key': 'my custom api key'
},
dependencies: {
bcrypt: '3.0.6'
}
})
.then(hook => {
console.log('hook created');
console.log(hook);
return updateHook(hook.id, hook);
})
.catch(err => {
console.error(err);
});
}
function updateHook(id, hook) {
hook.name = 'new name';
return management.hooks
.update({ id }, hook)
.then(hook => {
console.log('hook updated');
console.log(hook);
return getHook(hook.id);
})
.catch(err => {
console.error(err);
});
}

function getHook(id) {
return management.hooks
.get({ id })
.then(hook => {
console.log('hook retreived');
console.log(hook);
return deleteHook(hook.id);
})
.catch(err => {
console.error(err);
});
}
function deleteHook(id) {
console.log('deleting hook');
return management.hooks
.delete({ id })
.then(() => {
console.log('hook deleted');
})
.catch(err => {
console.error(err);
});
}

function getAllHooks() {
return management.hooks
.getAll({})
.then(hook => {
console.log('hook retreived');
console.log(hook);
})
.catch(err => {
console.error(err);
});
}

createHook();
//getAllHooks();

0 comments on commit 8cca40b

Please sign in to comment.