mirror of
https://github.com/jambonz/jambonz-node.git
synced 2026-10-24 12:14:18 +00:00
add utility function mergeEnvVarsWithDefaults
This commit is contained in:
1 parent
aacc6136c5
commit
ced40a58b9
2 files changed
+33
-2
No files matched your search
+31
-1
@@ -3,7 +3,7 @@ const addFormats = require('ajv-formats');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const schemaPath = path.join(__dirname, 'schema/app-schema.json');
|
||||
const schemaPath = path.join(__dirname, './schema/app-schema.json');
|
||||
const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
|
||||
|
||||
/**
|
||||
@@ -149,8 +149,38 @@ function validateAppConfig(config) {
|
||||
}
|
||||
}
|
||||
|
||||
const mergeEnvVarsWithDefaults = (env_vars, route, schema) => {
|
||||
env_vars = env_vars || {};
|
||||
const merged = {...env_vars};
|
||||
|
||||
// First handle global properties (those not starting with /)
|
||||
Object.entries(schema).forEach(([propName, propSchema]) => {
|
||||
if (!propName.startsWith('/')) {
|
||||
if (!(propName in merged) && 'default' in propSchema) {
|
||||
merged[propName] = propSchema.default;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Then handle route-specific properties
|
||||
const routeSchema = schema[route];
|
||||
if (!routeSchema) {
|
||||
return merged;
|
||||
}
|
||||
|
||||
Object.entries(routeSchema).forEach(([propName, propSchema]) => {
|
||||
if (!(propName in merged) && 'default' in propSchema) {
|
||||
merged[propName] = propSchema.default;
|
||||
}
|
||||
});
|
||||
|
||||
return merged;
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
validateAppConfig,
|
||||
getAppConfig,
|
||||
mergeEnvVarsWithDefaults,
|
||||
schema
|
||||
};
|
||||
Reference in new issue
Block a user