mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-28 15:14:06 +00:00
116 lines
3.3 KiB
JavaScript
116 lines
3.3 KiB
JavaScript
const { interpolate } = require('@usebruno/common');
|
|
const { each, forOwn, cloneDeep } = require('lodash');
|
|
|
|
const getContentType = (headers = {}) => {
|
|
let contentType = '';
|
|
forOwn(headers, (value, key) => {
|
|
if (key && key.toLowerCase() === 'content-type') {
|
|
contentType = value;
|
|
}
|
|
});
|
|
|
|
return contentType;
|
|
};
|
|
|
|
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
|
// we clone envVars because we don't want to modify the original object
|
|
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
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
const _interpolate = (str) => {
|
|
if (!str || !str.length || typeof str !== 'string') {
|
|
return str;
|
|
}
|
|
|
|
// collectionVariables take precedence over envVars
|
|
const combinedVars = {
|
|
...envVars,
|
|
...collectionVariables,
|
|
process: {
|
|
env: {
|
|
...processEnvVars
|
|
}
|
|
}
|
|
};
|
|
|
|
return interpolate(str, combinedVars);
|
|
};
|
|
|
|
request.url = _interpolate(request.url);
|
|
|
|
forOwn(request.headers, (value, key) => {
|
|
delete request.headers[key];
|
|
request.headers[_interpolate(key)] = _interpolate(value);
|
|
});
|
|
|
|
const contentType = getContentType(request.headers);
|
|
|
|
if (contentType.includes('json')) {
|
|
if (typeof request.data === 'object') {
|
|
try {
|
|
let parsed = JSON.stringify(request.data);
|
|
parsed = _interpolate(parsed);
|
|
request.data = JSON.parse(parsed);
|
|
} catch (err) {}
|
|
}
|
|
|
|
if (typeof request.data === 'string') {
|
|
if (request.data.length) {
|
|
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);
|
|
request.data = JSON.parse(parsed);
|
|
} catch (err) {}
|
|
}
|
|
} else {
|
|
request.data = _interpolate(request.data);
|
|
}
|
|
|
|
each(request.params, (param) => {
|
|
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);
|
|
|
|
if (request.proxy.auth) {
|
|
request.proxy.auth.username = _interpolate(request.proxy.auth.username);
|
|
request.proxy.auth.password = _interpolate(request.proxy.auth.password);
|
|
}
|
|
}
|
|
|
|
// todo: we have things happening in two places w.r.t basic auth
|
|
// 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) || '';
|
|
|
|
// use auth header based approach and delete the request.auth object
|
|
request.headers['authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
|
|
delete request.auth;
|
|
}
|
|
|
|
return request;
|
|
};
|
|
|
|
module.exports = interpolateVars;
|