Skip to content

Commit

Permalink
Add support for on-demand template registration
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Dec 1, 2023
1 parent 903677b commit 6ea55d1
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/postman-sandbox-fleet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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({
Expand Down

0 comments on commit 6ea55d1

Please sign in to comment.