mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-08 06:28:33 +00:00
feat(#1460): use new interpolation lib in app, electron, cli
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
"package.json"
|
||||
],
|
||||
"dependencies": {
|
||||
"@usebruno/common": "0.1.0",
|
||||
"@usebruno/js": "0.9.4",
|
||||
"@usebruno/lang": "0.9.0",
|
||||
"axios": "^1.5.1",
|
||||
@@ -32,7 +33,6 @@
|
||||
"decomment": "^0.9.5",
|
||||
"form-data": "^4.0.0",
|
||||
"fs-extra": "^10.1.0",
|
||||
"handlebars": "^4.7.8",
|
||||
"http-proxy-agent": "^7.0.0",
|
||||
"https-proxy-agent": "^7.0.2",
|
||||
"inquirer": "^9.1.4",
|
||||
|
||||
@@ -1,21 +1,5 @@
|
||||
const Handlebars = require('handlebars');
|
||||
const { forOwn, cloneDeep } = require('lodash');
|
||||
|
||||
const interpolateEnvVars = (str, processEnvVars) => {
|
||||
if (!str || !str.length || typeof str !== 'string') {
|
||||
return str;
|
||||
}
|
||||
|
||||
const template = Handlebars.compile(str, { noEscape: true });
|
||||
|
||||
return template({
|
||||
process: {
|
||||
env: {
|
||||
...processEnvVars
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
const { interpolate } = require('@usebruno/common');
|
||||
|
||||
const interpolateString = (str, { envVars, collectionVariables, processEnvVars }) => {
|
||||
if (!str || !str.length || typeof str !== 'string') {
|
||||
@@ -31,11 +15,15 @@ const interpolateString = (str, { envVars, collectionVariables, processEnvVars }
|
||||
// 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] = interpolateEnvVars(value, processEnvVars);
|
||||
envVars[key] = interpolate(value, {
|
||||
process: {
|
||||
env: {
|
||||
...processEnvVars
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const template = Handlebars.compile(str, { noEscape: true });
|
||||
|
||||
// collectionVariables take precedence over envVars
|
||||
const combinedVars = {
|
||||
...envVars,
|
||||
@@ -47,7 +35,7 @@ const interpolateString = (str, { envVars, collectionVariables, processEnvVars }
|
||||
}
|
||||
};
|
||||
|
||||
return template(combinedVars);
|
||||
return interpolate(str, combinedVars);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Handlebars = require('handlebars');
|
||||
const { interpolate } = require('@usebruno/common');
|
||||
const { each, forOwn, cloneDeep } = require('lodash');
|
||||
|
||||
const getContentType = (headers = {}) => {
|
||||
@@ -12,24 +12,6 @@ const getContentType = (headers = {}) => {
|
||||
return contentType;
|
||||
};
|
||||
|
||||
const interpolateEnvVars = (str, processEnvVars) => {
|
||||
if (!str || !str.length || typeof str !== 'string') {
|
||||
return str;
|
||||
}
|
||||
|
||||
const template = Handlebars.compile(str, { noEscape: true });
|
||||
|
||||
return template({
|
||||
process: {
|
||||
env: {
|
||||
...processEnvVars
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const varsRegex = /(?<!\\)\{\{(?!process\.env\.\w+)(.*\..*)\}\}/g;
|
||||
|
||||
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
||||
// we clone envVars because we don't want to modify the original object
|
||||
envVars = cloneDeep(envVars);
|
||||
@@ -37,20 +19,20 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
||||
// 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] = interpolateEnvVars(value, processEnvVars);
|
||||
envVars[key] = interpolate(value, {
|
||||
process: {
|
||||
env: {
|
||||
...processEnvVars
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const interpolate = (str) => {
|
||||
const _interpolate = (str) => {
|
||||
if (!str || !str.length || typeof str !== 'string') {
|
||||
return str;
|
||||
}
|
||||
|
||||
if (varsRegex.test(str)) {
|
||||
// Handlebars doesn't allow dots as identifiers, so we need to use literal segments
|
||||
str = str.replaceAll(varsRegex, '{{[$1]}}');
|
||||
}
|
||||
const template = Handlebars.compile(str, { noEscape: true });
|
||||
|
||||
// collectionVariables take precedence over envVars
|
||||
const combinedVars = {
|
||||
...envVars,
|
||||
@@ -62,14 +44,14 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
||||
}
|
||||
};
|
||||
|
||||
return template(combinedVars);
|
||||
return interpolate(str, combinedVars);
|
||||
};
|
||||
|
||||
request.url = interpolate(request.url);
|
||||
request.url = _interpolate(request.url);
|
||||
|
||||
forOwn(request.headers, (value, key) => {
|
||||
delete request.headers[key];
|
||||
request.headers[interpolate(key)] = interpolate(value);
|
||||
request.headers[_interpolate(key)] = _interpolate(value);
|
||||
});
|
||||
|
||||
const contentType = getContentType(request.headers);
|
||||
@@ -78,40 +60,40 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
||||
if (typeof request.data === 'object') {
|
||||
try {
|
||||
let parsed = JSON.stringify(request.data);
|
||||
parsed = interpolate(parsed);
|
||||
parsed = _interpolate(parsed);
|
||||
request.data = JSON.parse(parsed);
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
if (typeof request.data === 'string') {
|
||||
if (request.data.length) {
|
||||
request.data = interpolate(request.data);
|
||||
request.data = _interpolate(request.data);
|
||||
}
|
||||
}
|
||||
} else if (contentType === 'application/x-www-form-urlencoded') {
|
||||
if (typeof request.data === 'object') {
|
||||
try {
|
||||
let parsed = JSON.stringify(request.data);
|
||||
parsed = interpolate(parsed);
|
||||
parsed = _interpolate(parsed);
|
||||
request.data = JSON.parse(parsed);
|
||||
} catch (err) {}
|
||||
}
|
||||
} else {
|
||||
request.data = interpolate(request.data);
|
||||
request.data = _interpolate(request.data);
|
||||
}
|
||||
|
||||
each(request.params, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
param.value = _interpolate(param.value);
|
||||
});
|
||||
|
||||
if (request.proxy) {
|
||||
request.proxy.protocol = interpolate(request.proxy.protocol);
|
||||
request.proxy.hostname = interpolate(request.proxy.hostname);
|
||||
request.proxy.port = interpolate(request.proxy.port);
|
||||
request.proxy.protocol = _interpolate(request.proxy.protocol);
|
||||
request.proxy.hostname = _interpolate(request.proxy.hostname);
|
||||
request.proxy.port = _interpolate(request.proxy.port);
|
||||
|
||||
if (request.proxy.auth) {
|
||||
request.proxy.auth.username = interpolate(request.proxy.auth.username);
|
||||
request.proxy.auth.password = interpolate(request.proxy.auth.password);
|
||||
request.proxy.auth.username = _interpolate(request.proxy.auth.username);
|
||||
request.proxy.auth.password = _interpolate(request.proxy.auth.password);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,8 +101,8 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
||||
// need to refactor this in the future
|
||||
// the request.auth (basic auth) object gets set inside the prepare-request.js file
|
||||
if (request.auth) {
|
||||
const username = interpolate(request.auth.username) || '';
|
||||
const password = interpolate(request.auth.password) || '';
|
||||
const username = _interpolate(request.auth.username) || '';
|
||||
const password = _interpolate(request.auth.password) || '';
|
||||
|
||||
// use auth header based approach and delete the request.auth object
|
||||
request.headers['authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
|
||||
|
||||
Reference in New Issue
Block a user