mirror of
https://github.com/jambonz/jambonz-node.git
synced 2025-12-19 05:17:49 +00:00
add utility function mergeEnvVarsWithDefaults
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const Jambonz = require('./rest/jambonz');
|
||||
const { validateAppConfig, getAppConfig, schema } = require('./validator');
|
||||
const { validateAppConfig, getAppConfig, mergeEnvVarsWithDefaults, schema } = require('./validator');
|
||||
|
||||
const initializer = (accountSid, apiKey, opts) => {
|
||||
return new Jambonz(accountSid, apiKey, opts);
|
||||
@@ -12,6 +12,7 @@ initializer.WsSession = require('./jambonz/ws-session');
|
||||
initializer.validateAppConfig = validateAppConfig;
|
||||
initializer.getAppConfig = getAppConfig;
|
||||
initializer.appSchema = schema;
|
||||
initializer.mergeEnvVarsWithDefaults = mergeEnvVarsWithDefaults;
|
||||
initializer.handleProtocols = (protocols) => {
|
||||
if (!protocols.has('ws.jambonz.org')) return false;
|
||||
return 'ws.jambonz.org';
|
||||
|
||||
@@ -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