Repository: amplify-education/serverless-domain-manager
Pull request #476 (2022-01-21 21:54:39)

Evaluate boolean create route53 record
<!--- Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests --->
Fixes #403 

**Description of Issue Fixed**
Refactoring

<!--- Please remember to allow edits from maintainers: https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork --->



Patch:
diff --git a/src/domain-config.ts b/src/domain-config.ts
index 454a234..071daf1 100644
--- a/src/domain-config.ts
+++ b/src/domain-config.ts
@@ -37,12 +37,12 @@ class DomainConfig {
 
     constructor(config: CustomDomain) {
 
-        this.enabled = this.evaluateEnabled(config.enabled);
+        this.enabled = this.evaluateBoolean(config.enabled, true);
         this.givenDomainName = config.domainName;
         this.hostedZonePrivate = config.hostedZonePrivate;
         this.certificateArn = config.certificateArn;
         this.certificateName = config.certificateName;
-        this.createRoute53Record = config.createRoute53Record;
+        this.createRoute53Record = this.evaluateBoolean(config.createRoute53Record, true);
         this.route53Profile = config.route53Profile;
         this.route53Region = config.route53Region;
         this.hostedZoneId = config.hostedZoneId;
@@ -116,26 +116,28 @@ class DomainConfig {
     }
 
     /**
-     * Determines whether this plug-in is enabled.
+     * Determines whether this boolean config is configured to true or false.
      *
-     * This method reads the customDomain property "enabled" to see if this plug-in should be enabled.
-     * If the property's value is undefined, a default value of true is assumed (for backwards
-     * compatibility).
-     * If the property's value is provided, this should be boolean, otherwise an exception is thrown.
-     * If no customDomain object exists, an exception is thrown.
+     * This method evaluates a customDomain property to see if it's true or false.
+     * If the property's value is undefined, the default value is returned.
+     * If the property's value is provided, this should be boolean, or a string parseable as boolean,
+     * otherwise an exception is thrown.
+     * @param {boolean|string} booleanConfig the config value provided
+     * @param {boolean} defaultValue the default value to return, if config value is undefined
+     * @returns {boolean} the parsed boolean from the config value, or the default value
      */
-    private evaluateEnabled(enabled: any): boolean {
-        if (enabled === undefined) {
-            return true;
+    private evaluateBoolean(booleanConfig: any, defaultValue: boolean): boolean {
+        if (booleanConfig === undefined) {
+            return defaultValue;
         }
-        if (typeof enabled === "boolean") {
-            return enabled;
-        } else if (typeof enabled === "string" && enabled === "true") {
+        if (typeof booleanConfig === "boolean") {
+            return booleanConfig;
+        } else if (typeof booleanConfig === "string" && booleanConfig === "true") {
             return true;
-        } else if (typeof enabled === "string" && enabled === "false") {
+        } else if (typeof booleanConfig === "string" && booleanConfig === "false") {
             return false;
         }
-        throw new Error(`${Globals.pluginName}: Ambiguous enablement boolean: "${enabled}"`);
+        throw new Error(`${Globals.pluginName}: Ambiguous boolean config: "${booleanConfig}"`);
     }
 }
 
diff --git a/src/index.ts b/src/index.ts
index b2f32e5..2e27107 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,5 @@
 "use strict";
 
-import {HTTPOptions} from "aws-sdk";
 import APIGatewayWrapper = require("./aws/api-gateway-wrapper");
 import CloudFormationWrapper = require("./aws/cloud-formation-wrapper");
 import DomainConfig = require("./domain-config");


Test patch:
diff --git a/test/unit-tests/index.test.ts b/test/unit-tests/index.test.ts
index 56a8c7b..34e2616 100644
--- a/test/unit-tests/index.test.ts
+++ b/test/unit-tests/index.test.ts
@@ -1678,7 +1678,7 @@ describe("Custom Domain Plugin", () => {
                 await plugin.hookWrapper(null);
             } catch (err) {
                 errored = true;
-                expect(err.message).to.equal(`${Globals.pluginName}: Ambiguous enablement boolean: \"0\"`);
+                expect(err.message).to.equal(`${Globals.pluginName}: Ambiguous boolean config: \"0\"`);
             }
             expect(errored).to.equal(true);
         });
@@ -1691,7 +1691,7 @@ describe("Custom Domain Plugin", () => {
                 await plugin.hookWrapper(null);
             } catch (err) {
                 errored = true;
-                expect(err.message).to.equal(`${Globals.pluginName}: Ambiguous enablement boolean: \"yes\"`);
+                expect(err.message).to.equal(`${Globals.pluginName}: Ambiguous boolean config: \"yes\"`);
             }
             expect(errored).to.equal(true);
         });