mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 17:33:28 +01:00
feat: moved env var interpolation logic to electron
This commit is contained in:
parent
977637e556
commit
a45628dd85
@ -108,14 +108,9 @@ export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
|
||||
const itemCopy = cloneDeep(item);
|
||||
const collectionCopy = cloneDeep(collection);
|
||||
|
||||
if (collection.activeEnvironmentUid) {
|
||||
const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid);
|
||||
if (environment) {
|
||||
interpolateEnvironmentVars(itemCopy, environment.variables);
|
||||
}
|
||||
}
|
||||
const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid);
|
||||
|
||||
sendNetworkRequest(itemCopy, collectionUid)
|
||||
sendNetworkRequest(itemCopy, collectionUid, environment)
|
||||
.then((response) => {
|
||||
return dispatch(
|
||||
responseReceived({
|
||||
|
@ -454,61 +454,6 @@ export const isLocalCollection = (collection) => {
|
||||
return collection.pathname ? true : false;
|
||||
};
|
||||
|
||||
export const interpolateEnvironmentVars = (item, variables) => {
|
||||
const envVars = {
|
||||
interpolation: { escapeValue: true }
|
||||
};
|
||||
const _item = item.draft ? item.draft : item;
|
||||
const { request } = _item;
|
||||
each(variables, (variable) => {
|
||||
envVars[variable.name] = variable.value;
|
||||
});
|
||||
|
||||
// TODO: Find a better interpolation library
|
||||
const interpolate = (str) => {
|
||||
if(!str || !str.length || typeof str !== "string") {
|
||||
return str;
|
||||
}
|
||||
|
||||
return str.reckon(envVars);
|
||||
};
|
||||
|
||||
request.url = interpolate(request.url);
|
||||
|
||||
each(request.headers, (header) => {
|
||||
header.value = interpolate(header.value);
|
||||
});
|
||||
each(request.params, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
});
|
||||
|
||||
// Todo: Make interpolation work with body mode json
|
||||
switch (request.body.mode) {
|
||||
case 'text': {
|
||||
request.body.text = interpolate(request.body.text);
|
||||
break;
|
||||
}
|
||||
case 'xml': {
|
||||
request.body.text = interpolate(request.body.text);
|
||||
break;
|
||||
}
|
||||
case 'multipartForm': {
|
||||
each(request.body.multipartForm, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'formUrlEncoded': {
|
||||
each(request.body.formUrlEncoded, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return request;
|
||||
};
|
||||
|
||||
export const deleteUidsInItem = (item) => {
|
||||
delete item.uid;
|
||||
const params = get(item, 'request.params', []);
|
||||
|
@ -1,8 +1,8 @@
|
||||
export const sendNetworkRequest = async (item, collectionUid) => {
|
||||
export const sendNetworkRequest = async (item, collectionUid, environment) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (['http-request', 'graphql-request'].includes(item.type)) {
|
||||
const timeStart = Date.now();
|
||||
sendHttpRequest(item, collectionUid)
|
||||
sendHttpRequest(item, collectionUid, environment)
|
||||
.then((response) => {
|
||||
const timeEnd = Date.now();
|
||||
resolve({
|
||||
@ -20,12 +20,12 @@ export const sendNetworkRequest = async (item, collectionUid) => {
|
||||
});
|
||||
};
|
||||
|
||||
const sendHttpRequest = async (item, collectionUid) => {
|
||||
const sendHttpRequest = async (item, collectionUid, environment) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { ipcRenderer } = window;
|
||||
|
||||
ipcRenderer
|
||||
.invoke('send-http-request', item, collectionUid)
|
||||
.invoke('send-http-request', item, collectionUid, environment)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
|
@ -25,6 +25,7 @@
|
||||
"fs-extra": "^10.1.0",
|
||||
"is-valid-path": "^0.1.1",
|
||||
"lodash": "^4.17.21",
|
||||
"mustache": "^4.2.0",
|
||||
"nanoid": "3.3.4",
|
||||
"vm2": "^3.9.13",
|
||||
"yup": "^0.32.11"
|
||||
|
@ -9,12 +9,12 @@ const { uuid } = require('../../utils/common');
|
||||
|
||||
const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
|
||||
// handler for sending http request
|
||||
ipcMain.handle('send-http-request', async (event, item, collectionUid) => {
|
||||
ipcMain.handle('send-http-request', async (event, item, collectionUid, environment) => {
|
||||
const cancelTokenUid = uuid();
|
||||
|
||||
try {
|
||||
const _request = item.draft ? item.draft.request : item.request;
|
||||
const request = prepareRequest(_request);
|
||||
const request = prepareRequest(_request, environment);
|
||||
|
||||
// make axios work in node using form data
|
||||
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
||||
|
68
packages/bruno-electron/src/ipc/network/interpolate-vars.js
Normal file
68
packages/bruno-electron/src/ipc/network/interpolate-vars.js
Normal file
@ -0,0 +1,68 @@
|
||||
const Mustache = require('mustache');
|
||||
const { each } = require('lodash');
|
||||
|
||||
// override the default escape function to prevent escaping
|
||||
Mustache.escape = function (value) {
|
||||
return value;
|
||||
};
|
||||
|
||||
const interpolateVars = (request, environment) => {
|
||||
if(!environment) {
|
||||
return request;
|
||||
}
|
||||
|
||||
const variables = environment.variables;
|
||||
if(!variables || !variables.length) {
|
||||
return request;
|
||||
}
|
||||
|
||||
const envVars = {};
|
||||
each(variables, (variable) => {
|
||||
envVars[variable.name] = Mustache.escape(variable.value);
|
||||
});
|
||||
|
||||
const interpolate = (str) => {
|
||||
if(!str || !str.length || typeof str !== "string") {
|
||||
return str;
|
||||
}
|
||||
|
||||
return Mustache.render(str, envVars);
|
||||
};
|
||||
|
||||
request.url = interpolate(request.url);
|
||||
|
||||
each(request.headers, (header) => {
|
||||
header.value = interpolate(header.value);
|
||||
});
|
||||
each(request.params, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
});
|
||||
|
||||
// Todo: Make interpolation work with body mode json
|
||||
switch (request.body.mode) {
|
||||
case 'text': {
|
||||
request.body.text = interpolate(request.body.text);
|
||||
break;
|
||||
}
|
||||
case 'xml': {
|
||||
request.body.text = interpolate(request.body.text);
|
||||
break;
|
||||
}
|
||||
case 'multipartForm': {
|
||||
each(request.body.multipartForm, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'formUrlEncoded': {
|
||||
each(request.body.formUrlEncoded, (param) => {
|
||||
param.value = interpolate(param.value);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return request;
|
||||
};
|
||||
|
||||
module.exports = interpolateVars;
|
@ -1,7 +1,9 @@
|
||||
const { get, each, filter } = require('lodash');
|
||||
const qs = require('qs');
|
||||
const interpolateVars = require('./interpolate-vars');
|
||||
|
||||
const prepareRequest = (request) => {
|
||||
const prepareRequest = (request, environment) => {
|
||||
interpolateVars(request, environment);
|
||||
const headers = {};
|
||||
each(request.headers, (h) => {
|
||||
if (h.enabled) {
|
||||
|
Loading…
Reference in New Issue
Block a user