|
| 1 | +var ArgumentError = require('rest-facade').ArgumentError; |
| 2 | +var utils = require('../utils'); |
| 3 | +var Auth0RestClient = require('../Auth0RestClient'); |
| 4 | +var RetryRestClient = require('../RetryRestClient'); |
| 5 | + |
| 6 | +/** |
| 7 | + * @class CustomDomainsManager |
| 8 | + * Auth0 Custom Domains Manager. |
| 9 | + * |
| 10 | + * {@link https://auth0.com/docs/api/management/v2#!/Custom_Domains/get_custom_domains CustomDomains} represent |
| 11 | + * custom domain names. |
| 12 | + * You can learn more about this in the |
| 13 | + * {@link https://auth0.com/docs/custom-domains CustomDomains} section of the |
| 14 | + * documentation. |
| 15 | + * @constructor |
| 16 | + * @memberOf module:management |
| 17 | + * |
| 18 | + * @param {Object} options The client options. |
| 19 | + * @param {String} options.baseUrl The URL of the API. |
| 20 | + * @param {Object} [options.headers] Headers to be included in all requests. |
| 21 | + * @param {Object} [options.retry] Retry Policy Config |
| 22 | + */ |
| 23 | +var CustomDomainsManager = function(options) { |
| 24 | + if (options === null || typeof options !== 'object') { |
| 25 | + throw new ArgumentError('Must provide manager options'); |
| 26 | + } |
| 27 | + |
| 28 | + if (options.baseUrl === null || options.baseUrl === undefined) { |
| 29 | + throw new ArgumentError('Must provide a base URL for the API'); |
| 30 | + } |
| 31 | + |
| 32 | + if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) { |
| 33 | + throw new ArgumentError('The provided base URL is invalid'); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Options object for the Rest Client instance. |
| 38 | + * |
| 39 | + * @type {Object} |
| 40 | + */ |
| 41 | + var clientOptions = { |
| 42 | + errorFormatter: { message: 'message', name: 'error' }, |
| 43 | + headers: options.headers, |
| 44 | + query: { repeatParams: false } |
| 45 | + }; |
| 46 | + |
| 47 | + /** |
| 48 | + * Provides an abstraction layer for consuming the |
| 49 | + * {@link https://auth0.com/docs/api/v2#!/Custom_Domains Auth0 Custom Domains endpoint}. |
| 50 | + * |
| 51 | + * @type {external:RestClient} |
| 52 | + */ |
| 53 | + var auth0CustomDomainsRestClient = new Auth0RestClient( |
| 54 | + options.baseUrl + '/custom-domains/:id', |
| 55 | + clientOptions, |
| 56 | + options.tokenProvider |
| 57 | + ); |
| 58 | + this.resource = new RetryRestClient(auth0CustomDomainsRestClient, options.retry); |
| 59 | + |
| 60 | + /** |
| 61 | + * Provides an abstraction layer for consuming the |
| 62 | + * {@link https://auth0.com/docs/api/v2#!/Custom_Domains Auth0 Custom Domains Verify endpoint}. |
| 63 | + * |
| 64 | + * @type {external:RestClient} |
| 65 | + */ |
| 66 | + var auth0VerifyRestClient = new Auth0RestClient( |
| 67 | + options.baseUrl + '/custom-domains/:id/verify', |
| 68 | + clientOptions, |
| 69 | + options.tokenProvider |
| 70 | + ); |
| 71 | + this.vefifyResource = new RetryRestClient(auth0VerifyRestClient, options.retry); |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * Create an Auth0 Custom Domain. |
| 76 | + * |
| 77 | + * @method create |
| 78 | + * @memberOf module:management.CustomDomainsManager.prototype |
| 79 | + * |
| 80 | + * @example |
| 81 | + * management.customDomains.create(data, function (err) { |
| 82 | + * if (err) { |
| 83 | + * // Handle error. |
| 84 | + * } |
| 85 | + * |
| 86 | + * // CustomDomain created. |
| 87 | + * }); |
| 88 | + * |
| 89 | + * @param {Object} data The custom domain data object. |
| 90 | + * @param {Function} [cb] Callback function. |
| 91 | + * |
| 92 | + * @return {Promise|undefined} |
| 93 | + */ |
| 94 | +utils.wrapPropertyMethod(CustomDomainsManager, 'create', 'resource.create'); |
| 95 | + |
| 96 | +/** |
| 97 | + * Get all Auth0 CustomDomains. |
| 98 | + * |
| 99 | + * @method getAll |
| 100 | + * @memberOf module:management.CustomDomainsManager.prototype |
| 101 | + * |
| 102 | + * @example |
| 103 | + * management.customDomains.getAll(function (err, customDomains) { |
| 104 | + * console.log(customDomains.length); |
| 105 | + * }); |
| 106 | + * |
| 107 | + * @return {Promise|undefined} |
| 108 | + */ |
| 109 | +utils.wrapPropertyMethod(CustomDomainsManager, 'getAll', 'resource.getAll'); |
| 110 | + |
| 111 | +/** |
| 112 | + * Get a Custom Domain. |
| 113 | + * |
| 114 | + * @method get |
| 115 | + * @memberOf module:management.CustomDomainsManager.prototype |
| 116 | + * |
| 117 | + * @example |
| 118 | + * management.customDomains.get({ id: CUSTOM_DOMAIN_ID }, function (err, customDomain) { |
| 119 | + * if (err) { |
| 120 | + * // Handle error. |
| 121 | + * } |
| 122 | + * |
| 123 | + * console.log(customDomain); |
| 124 | + * }); |
| 125 | + * |
| 126 | + * @param {Object} params Custom Domain parameters. |
| 127 | + * @param {String} params.id Custom Domain ID. |
| 128 | + * @param {Function} [cb] Callback function. |
| 129 | + * |
| 130 | + * @return {Promise|undefined} |
| 131 | + */ |
| 132 | +utils.wrapPropertyMethod(CustomDomainsManager, 'get', 'resource.get'); |
| 133 | + |
| 134 | +/** |
| 135 | + * Verify a Custom Domain. |
| 136 | + * |
| 137 | + * @method verify |
| 138 | + * @memberOf module:management.CustomDomainsManager.prototype |
| 139 | + * |
| 140 | + * @example |
| 141 | + * management.customDomains.verify({ id: CUSTOM_DOMAIN_ID }, function (err, customDomain) { |
| 142 | + * if (err) { |
| 143 | + * // Handle error. |
| 144 | + * } |
| 145 | + * |
| 146 | + * console.log(customDomain); |
| 147 | + * }); |
| 148 | + * |
| 149 | + * @param {Object} params Custom Domain parameters. |
| 150 | + * @param {String} params.id Custom Domain ID. |
| 151 | + * @param {Function} [cb] Callback function. |
| 152 | + * |
| 153 | + * @return {Promise|undefined} |
| 154 | + */ |
| 155 | +CustomDomainsManager.prototype.verify = function(params, cb) { |
| 156 | + if (!params || !params.id) { |
| 157 | + throw new ArgumentError('The custom domain id cannot be null or undefined'); |
| 158 | + } |
| 159 | + |
| 160 | + if (cb && cb instanceof Function) { |
| 161 | + return this.vefifyResource.create(params, {}, cb); |
| 162 | + } |
| 163 | + |
| 164 | + return this.vefifyResource.create(params, {}); |
| 165 | +}; |
| 166 | + |
| 167 | +/** |
| 168 | + * Delete a Custom Domain. |
| 169 | + * |
| 170 | + * @method delete |
| 171 | + * @memberOf module:management.CustomDomainsManager.prototype |
| 172 | + * |
| 173 | + * @example |
| 174 | + * management.customDomains.delete({ id: CUSTOM_DOMAIN_ID }, function (err) { |
| 175 | + * if (err) { |
| 176 | + * // Handle error. |
| 177 | + * } |
| 178 | + * |
| 179 | + * // CustomDomain deleted. |
| 180 | + * }); |
| 181 | + * |
| 182 | + * @param {Object} params Custom Domain parameters. |
| 183 | + * @param {String} params.id Custom Domain ID. |
| 184 | + * @param {Function} [cb] Callback function. |
| 185 | + * |
| 186 | + * @return {Promise|undefined} |
| 187 | + */ |
| 188 | +utils.wrapPropertyMethod(CustomDomainsManager, 'delete', 'resource.delete'); |
| 189 | + |
| 190 | +module.exports = CustomDomainsManager; |
0 commit comments