var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');
var Auth0RestClient = require('../Auth0RestClient');
var RetryRestClient = require('../RetryRestClient');
/**
* @class ClientGrantsManager
* Auth0 Client Grants Manager.
*
* See {@link https://auth0.com/docs/api/v2#!/Client_Grants Client Grants}
*
* @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 ClientGrantsManager = function (options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide client options');
}
if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}
if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}
/**
* Options object for the Rest Client instance.
*
* @type {Object}
*/
var clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false }
};
/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/v2#!/Client_Grants Auth0 Client Grants endpoint}.
*
* @type {external:RestClient}
*/
var auth0RestClient = new Auth0RestClient(options.baseUrl + '/client-grants/:id', clientOptions, options.tokenProvider);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
};
/**
* Create an Auth0 client grant.
*
* @method create
* @memberOf module:management.ClientGrantsManager.prototype
*
* @example
* management.clientGrants.create(data, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Client grant created.
* });
*
* @param {Object} data The client data object.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ClientGrantsManager, 'create', 'resource.create');
/**
* Get all Auth0 Client Grants.
*
* @method getAll
* @memberOf module:management.ClientGrantsManager.prototype
*
* @example
* management.clientGrants.getAll(function (err, grants) {
* console.log(grants.length);
* });
*
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ClientGrantsManager, 'getAll', 'resource.getAll');
/**
* Update an Auth0 client grant.
*
* @method update
* @memberOf module:management.ClientGrantsManager.prototype
*
* @example
* var data = {
* client_id: CLIENT_ID,
* audience: AUDIENCE,
* scope: []
* };
* var params = { id: CLIENT_GRANT_ID };
*
* management.clientGrants.update(params, data, function (err, grant) {
* if (err) {
* // Handle error.
* }
*
* console.log(grant.id);
* });
*
* @param {Object} params Client parameters.
* @param {String} params.id Client grant ID.
* @param {Object} data Updated client data.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ClientGrantsManager, 'update', 'resource.patch');
/**
* Delete an Auth0 client grant.
*
* @method delete
* @memberOf module:management.ClientGrantsManager.prototype
*
* @example
* management.clientGrants.delete({ id: GRANT_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Grant deleted.
* });
*
* @param {Object} params Client parameters.
* @param {String} params.id Client grant ID.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ClientGrantsManager, 'delete', 'resource.delete');
module.exports = ClientGrantsManager;