Created
February 26, 2020 22:25
-
-
Save zodman/f0b08795cbe18aacfb56cc92b20a93f2 to your computer and use it in GitHub Desktop.
Simple featureflag in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simple featureFlag | |
* */ | |
const YAML = require('yaml'); | |
const fs = require("fs"); | |
class FeatureFlag { | |
constructor() { | |
let filepath = 'featureflag.yaml'; | |
let fileContent= fs.readFileSync(filepath, 'utf8'); | |
let yamlLoad = YAML.parse(fileContent); | |
//console.log(JSON.stringify(yaml_load)); | |
this.config = yamlLoad; | |
} | |
isFeatureEnabled(component, featureFlag) { | |
let isEnabled = this.isComponentEnabled(component) | |
if(!isEnabled) { | |
return false; | |
} | |
return this.checkFeature(component, featureFlag); | |
} | |
checkFeature(component, featureFlag) { | |
let comp = this.getComponent(component) | |
//console.log("checkFeature", comp.featureflags[featureFlag]) | |
return comp.featureflags[featureFlag].enabled | |
} | |
isComponentEnabled(component) { | |
return this.checkComponent(component); | |
} | |
getComponent(component) { | |
let comp = {}; | |
this.config.Components.forEach(e =>{ | |
if( component in e) { | |
comp = e[component]; | |
return | |
} | |
}) | |
return comp | |
} | |
checkComponent(component) { | |
let e = this.getComponent(component); | |
try { | |
return e.enabled; | |
} catch (e) { | |
if( e instanceof TypeError) { | |
throw Error("component \""+ component + "\" missing in yaml"); | |
} | |
} | |
} | |
} | |
module.exports=FeatureFlag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment