Skip to content

Commit

Permalink
Replaced custom code with safe-flat
Browse files Browse the repository at this point in the history
  • Loading branch information
denysoblohin-okta committed Feb 22, 2021
1 parent df2cb2b commit 2e9807b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 45 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"mocha-junit-reporter": "^2.0.0",
"njwt": "^1.0.0",
"parse-link-header": "^1.0.1",
"rasha": "^1.2.5"
"rasha": "^1.2.5",
"safe-flat": "^2.0.2"
},
"devDependencies": {
"@okta/openapi": "^2.1.6",
Expand Down
47 changes: 3 additions & 44 deletions src/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const yaml = require('js-yaml');
const flat = require('safe-flat');

class ConfigLoader {

Expand Down Expand Up @@ -56,8 +57,7 @@ class ConfigLoader {
applyEnvVars() {
const delimiter = '_';
const prefix = this.prefix;
const flatConfig = { delimiter: delimiter };
const flattendDefaultConfig = this.flatten(this.config, flatConfig);
const flattendDefaultConfig = flat.flatten(this.config, delimiter);

var flatEnvValues = Object.keys(flattendDefaultConfig)
.reduce((envVarMap, key) => {
Expand All @@ -70,55 +70,14 @@ class ConfigLoader {
return envVarMap;
}, {});

const envConfig = this.unflatten(flatEnvValues, flatConfig);
const envConfig = flat.unflatten(flatEnvValues, delimiter);
this.apply(envConfig);
}

applyYamlFile(path) {
this.apply(yaml.safeLoad(fs.readFileSync(path)).okta);
}

flatten(config, flatConfig) {
let res = {};
const step = (key, val) => {
if (_.isPlainObject(val)) {
const prefix = key ? key + flatConfig.delimiter : '';
Object.keys(val).forEach(function (k) {
step(prefix + k, val[k]);
});
} else {
res[key] = val;
}
};
step(null, config);
return res;
}

unflatten(config, flatConfig) {
const keysToIgnore = ['__proto__', 'constructor', 'prototype'];
let res = {};
for (const key in config) {
const val = config[key];
const keyParts = key.split(flatConfig.delimiter);
let target = res;
for (let i = 0 ; i < keyParts.length ; i++) {
const k = keyParts[i];
if (keysToIgnore.includes(k)) {
break;
}
if (i < keyParts.length - 1) {
if (target[k] === undefined) {
target[k] = {};
}
target = target[k];
} else {
target[k] = val;
}
}
}
return res;
}

apply(config) {
_.mergeWith(this.config, config, (dest, src) => {
if (typeof dest !== 'string') {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4291,6 +4291,11 @@ safe-buffer@~5.1.1:
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==

safe-flat@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/safe-flat/-/safe-flat-2.0.2.tgz#8fc26e0c5addcf3b294efbdb602e95633f44ee5c"
integrity sha512-4plwd0jT25AB/gnVhEsjM9dt4wOZhp3Lk8xOmJ8tW8niJ2gB/QSKSvhqVY8zFQukAnPJhvn8BNc8mCSZxoVQ+A==

safe-regex@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e"
Expand Down

0 comments on commit 2e9807b

Please sign in to comment.