mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-15 20:01:28 +00:00
72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
const { forOwn, cloneDeep } = require('lodash');
|
|
const { interpolate, interpolateObject: interpolateObjectCommon } = require('@usebruno/common');
|
|
|
|
const buildCombinedVars = ({
|
|
collectionVariables,
|
|
envVars,
|
|
folderVariables,
|
|
requestVariables,
|
|
runtimeVariables,
|
|
processEnvVars,
|
|
globalEnvVars
|
|
}) => {
|
|
processEnvVars = processEnvVars || {};
|
|
runtimeVariables = runtimeVariables || {};
|
|
collectionVariables = collectionVariables || {};
|
|
folderVariables = folderVariables || {};
|
|
requestVariables = requestVariables || {};
|
|
globalEnvVars = globalEnvVars || {};
|
|
|
|
// we clone envVars because we don't want to modify the original object
|
|
envVars = envVars ? cloneDeep(envVars) : {};
|
|
|
|
// envVars can inturn have values as {{process.env.VAR_NAME}}
|
|
// so we need to interpolate envVars first with processEnvVars
|
|
forOwn(envVars, (value, key) => {
|
|
envVars[key] = interpolate(value, {
|
|
process: {
|
|
env: {
|
|
...processEnvVars
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
// runtimeVariables take precedence over envVars
|
|
return {
|
|
...globalEnvVars,
|
|
...collectionVariables,
|
|
...envVars,
|
|
...folderVariables,
|
|
...requestVariables,
|
|
...runtimeVariables,
|
|
process: {
|
|
env: {
|
|
...processEnvVars
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
const interpolateString = (str, interpolationOptions) => {
|
|
if (!str || !str.length || typeof str !== 'string') {
|
|
return str;
|
|
}
|
|
|
|
const combinedVars = buildCombinedVars(interpolationOptions);
|
|
return interpolate(str, combinedVars);
|
|
};
|
|
|
|
/**
|
|
* recursively interpolating all string values in a object
|
|
*/
|
|
const interpolateObject = (obj, interpolationOptions) => {
|
|
const combinedVars = buildCombinedVars(interpolationOptions);
|
|
return interpolateObjectCommon(obj, combinedVars);
|
|
};
|
|
|
|
module.exports = {
|
|
interpolateString,
|
|
interpolateObject
|
|
};
|