Skip to content

Commit

Permalink
[sdks-7615] Add configs logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Zamora committed Oct 18, 2023
1 parent ebccc87 commit c45da60
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
28 changes: 23 additions & 5 deletions environmentManager/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const settings = require('../utils/parserConfigs')();
const { validEnvironment, validEnvironmentConfig, isString, throwError } = require('../utils/parserConfigs/validators');
const { validEnvironment, validEnvironmentConfig, isString, throwError, validFlagSets } = require('../utils/parserConfigs/validators');
const { getSplitFactory } = require('../sdk');
const { obfuscate } = require('../utils/utils');
const SPLIT_EVALUATOR_ENVIRONMENTS = 'SPLIT_EVALUATOR_ENVIRONMENTS';
const SPLIT_EVALUATOR_AUTH_TOKEN = 'SPLIT_EVALUATOR_AUTH_TOKEN';
const SPLIT_EVALUATOR_API_KEY = 'SPLIT_EVALUATOR_API_KEY';
const DEFAULT_AUTH_TOKEN = 'DEFAULT_AUTH_TOKEN';

const EnvironmentManagerFactory = (function(){
/**
Expand All @@ -29,13 +30,16 @@ const EnvironmentManagerFactory = (function(){
}

_initializeEnvironments(){

let defaultEnvironment = false;
// If environments envVar is not defined, it creates an environment with auth_token and api_key envVars
if (!process.env.SPLIT_EVALUATOR_ENVIRONMENTS) {
defaultEnvironment = true;
const AUTH_TOKEN = process.env.SPLIT_EVALUATOR_AUTH_TOKEN;
// If auth_token envVar is not defined, means that openapi security tag should not be added
if (!AUTH_TOKEN) {
this.requireAuth = false;
process.env.SPLIT_EVALUATOR_AUTH_TOKEN = 'splitToken';
process.env.SPLIT_EVALUATOR_AUTH_TOKEN = DEFAULT_AUTH_TOKEN;
}
process.env.SPLIT_EVALUATOR_ENVIRONMENTS = `[{
"AUTH_TOKEN": "${process.env[SPLIT_EVALUATOR_AUTH_TOKEN]}",
Expand All @@ -45,11 +49,17 @@ const EnvironmentManagerFactory = (function(){

const environmentConfigs = validEnvironmentConfig(SPLIT_EVALUATOR_ENVIRONMENTS);

if (!defaultEnvironment && settings.sync && settings.sync.splitFilters) {
throwError('Flag sets must be defined in SPLIT_EVALUATOR_ENVIRONMENTS, ignoring filters in GLOBAL_CONFIG');
process.exit(0);
}

environmentConfigs.forEach(environment => {

validEnvironment(environment);
const authToken = environment['AUTH_TOKEN'];
const apiKey = environment['API_KEY'];
const maybeFlagSets = environment['FLAG_SET_FILTER'];
settings.core.authorizationKey = apiKey;

if(!isString(authToken)) {
Expand All @@ -60,6 +70,14 @@ const EnvironmentManagerFactory = (function(){
throwError(`There are two or more environments with the same authToken '${authToken}' `);
}

if (!defaultEnvironment) {
const flagSets = validFlagSets(maybeFlagSets);
settings.sync = {
...settings.sync,
splitFilters: flagSets,
};
}

const { factory, telemetry, impressionsMode} = getSplitFactory(settings);

// Creates an environment for authToken
Expand Down Expand Up @@ -105,7 +123,7 @@ const EnvironmentManagerFactory = (function(){
}

getFactory(authToken) {
if (!this.requireAuth) authToken = 'splitToken';
if (!this.requireAuth) authToken = DEFAULT_AUTH_TOKEN;
return this._environments[authToken].factory;
}

Expand All @@ -122,7 +140,7 @@ const EnvironmentManagerFactory = (function(){
}

getTelemetry(authToken) {
if (!this.requireAuth) authToken = 'splitToken';
if (!this.requireAuth) authToken = DEFAULT_AUTH_TOKEN;
const environment = this.getEnvironment(authToken);
const telemetry = environment.telemetry;
const stats = {
Expand Down Expand Up @@ -152,7 +170,7 @@ const EnvironmentManagerFactory = (function(){
}

updateLastEvaluation(authToken) {
if (!this.requireAuth) authToken = 'splitToken';
if (!this.requireAuth) authToken = DEFAULT_AUTH_TOKEN;
this._environments[authToken].lastEvaluation = new Date().toJSON();
}

Expand Down
4 changes: 0 additions & 4 deletions utils/mocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ const storage = {
};

const sync = {
splitFilters: [{
type: 'byName',
values: ['split_name_1', 'split_name_2'],
}],
impressionsMode: 'NONE',
enabled: false,
};
Expand Down
10 changes: 10 additions & 0 deletions utils/parserConfigs/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ const validGlobalConfig = (globalParam) => {
}
};

const validFlagSets = (maybeFlagSets) => {
if (!maybeFlagSets) return;
if (!isString(maybeFlagSets)) {
throwError('you passed an invalid flag set, flag sets must be comma separated a string list');
return;
}
return [{type: 'bySet', values: maybeFlagSets.split(',')}];
};

module.exports = {
throwError,
validUrl,
Expand All @@ -96,4 +105,5 @@ module.exports = {
validEnvironment,
validEnvironmentConfig,
validGlobalConfig,
validFlagSets,
};

0 comments on commit c45da60

Please sign in to comment.