diff --git a/lib/postman-sandbox-fleet.js b/lib/postman-sandbox-fleet.js index 6a2afc50..e67831d5 100644 --- a/lib/postman-sandbox-fleet.js +++ b/lib/postman-sandbox-fleet.js @@ -93,6 +93,38 @@ class PostmanSandboxFleet { this.connectOptions = connectOptions; } + /** + * Check if a template is registered with a given name + * + * @param {string} templateName - + * @returns {boolean} + */ + isRegistered (templateName) { + return _.has(this.templateRegistry, templateName); + } + + /** + * Register a new template + * + * @param {string} templateName - + * @param {template} template - + */ + register (templateName, template) { + if (typeof templateName !== 'string') { + throw new TypeError('sandbox-fleet: template name must be a string'); + } + + if (typeof template !== 'string') { + throw new TypeError('sandbox-fleet: template must be a string'); + } + + if (this.isRegistered(templateName)) { + throw new Error(`sandbox-fleet: template already registered for name ${templateName}`); + } + + this.templateRegistry[templateName] = template; + } + /** * Returns sandbox instance for the given template name * @@ -106,21 +138,21 @@ class PostmanSandboxFleet { } if (typeof templateName !== 'string') { - return callback(new TypeError('sandbox-fleet: template key must be a string')); + return callback(new TypeError('sandbox-fleet: template name must be a string')); } if (this.fleet.has(templateName)) { return callback(null, this.fleet.get(templateName)); } - if (!_.has(this.templateRegistry, templateName)) { - return callback(new Error(`sandbox-fleet: template not found for key ${templateName}`)); + if (!this.isRegistered(templateName)) { + return callback(new Error(`sandbox-fleet: template not found for name ${templateName}`)); } const template = this.templateRegistry[templateName]; if (typeof template !== 'string') { - return callback(new Error(`sandbox-fleet: invalid template for key ${templateName}`)); + return callback(new Error(`sandbox-fleet: invalid template for name ${templateName}`)); } new PostmanSandbox().initialize({