forked from extern/bruno
feat: interpolate environment vats while sending request
This commit is contained in:
parent
510e549d34
commit
abc00b810f
@ -104,7 +104,7 @@ const Sidebar = () => {
|
|||||||
{/* Need to ut github stars link here */}
|
{/* Need to ut github stars link here */}
|
||||||
</div>
|
</div>
|
||||||
<div className='pl-1'>
|
<div className='pl-1'>
|
||||||
<iframe src="https://ghbtns.com/github-btn.html?user=usebruno&repo=bruno&type=star&count=true" frameborder="0" scrolling="0" width="100" height="20" title="GitHub"></iframe>
|
<iframe src="https://ghbtns.com/github-btn.html?user=usebruno&repo=bruno&type=star&count=true" frameBorder="0" scrolling="0" width="100" height="20" title="GitHub"></iframe>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-grow items-center justify-end text-xs mr-2">
|
<div className="flex flex-grow items-center justify-end text-xs mr-2">
|
||||||
v1.25.2
|
v1.25.2
|
||||||
|
@ -35,7 +35,9 @@ export default function Main() {
|
|||||||
const isDragging = useSelector((state) => state.app.isDragging);
|
const isDragging = useSelector((state) => state.app.isDragging);
|
||||||
const showHomePage = useSelector((state) => state.app.showHomePage);
|
const showHomePage = useSelector((state) => state.app.showHomePage);
|
||||||
|
|
||||||
console.log(useSelector((state) => state.collections.collections));
|
// Todo: write a better logging flow that can be used to log by turning on debug flag
|
||||||
|
// Enable for debugging.
|
||||||
|
// console.log(useSelector((state) => state.collections.collections));
|
||||||
|
|
||||||
const className = classnames({
|
const className = classnames({
|
||||||
'is-dragging': isDragging
|
'is-dragging': isDragging
|
||||||
|
@ -15,7 +15,8 @@ import {
|
|||||||
findParentItemInCollection,
|
findParentItemInCollection,
|
||||||
findEnvironmentInCollection,
|
findEnvironmentInCollection,
|
||||||
isItemAFolder,
|
isItemAFolder,
|
||||||
refreshUidsInItem
|
refreshUidsInItem,
|
||||||
|
interpolateEnvironmentVars
|
||||||
} from 'utils/collections';
|
} from 'utils/collections';
|
||||||
import { collectionSchema, itemSchema } from '@usebruno/schema';
|
import { collectionSchema, itemSchema } from '@usebruno/schema';
|
||||||
import { waitForNextTick } from 'utils/common';
|
import { waitForNextTick } from 'utils/common';
|
||||||
@ -213,16 +214,33 @@ export const saveRequest = (itemUid, collectionUid) => (dispatch, getState) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sendRequest = (item, collectionUid) => (dispatch) => {
|
export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
|
||||||
|
const state = getState();
|
||||||
|
const collection = findCollectionByUid(state.collections.collections, collectionUid);
|
||||||
const cancelTokenUid = uuid();
|
const cancelTokenUid = uuid();
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if(!collection) {
|
||||||
|
return reject(new Error('Collection not found'));
|
||||||
|
}
|
||||||
|
|
||||||
dispatch(requestSent({
|
dispatch(requestSent({
|
||||||
itemUid: item.uid,
|
itemUid: item.uid,
|
||||||
collectionUid: collectionUid,
|
collectionUid: collectionUid,
|
||||||
cancelTokenUid: cancelTokenUid
|
cancelTokenUid: cancelTokenUid
|
||||||
}));
|
}));
|
||||||
|
|
||||||
sendNetworkRequest(item, {cancelTokenUid: cancelTokenUid})
|
const itemCopy = cloneDeep(item);
|
||||||
|
const collectionCopy = cloneDeep(collection);
|
||||||
|
|
||||||
|
if(collection.activeEnvironmentUid) {
|
||||||
|
const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid);
|
||||||
|
if(environment) {
|
||||||
|
interpolateEnvironmentVars(itemCopy, environment.variables)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendNetworkRequest(itemCopy, {cancelTokenUid: cancelTokenUid})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if(response && response.status !== -1) {
|
if(response && response.status !== -1) {
|
||||||
return dispatch(responseReceived({
|
return dispatch(responseReceived({
|
||||||
@ -232,7 +250,9 @@ export const sendRequest = (item, collectionUid) => (dispatch) => {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => console.log(err));
|
.then(resolve)
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cancelRequest = (cancelTokenUid, item, collection) => (dispatch) => {
|
export const cancelRequest = (cancelTokenUid, item, collection) => (dispatch) => {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import template from 'lodash/template';
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
import each from 'lodash/each';
|
import each from 'lodash/each';
|
||||||
import find from 'lodash/find';
|
import find from 'lodash/find';
|
||||||
@ -352,3 +353,54 @@ export const refreshUidsInItem = (item) => {
|
|||||||
export const isLocalCollection = (collection) => {
|
export const isLocalCollection = (collection) => {
|
||||||
return collection.pathname ? true : false;
|
return collection.pathname ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const interpolateEnvironmentVars = (item, variables) => {
|
||||||
|
const envVars = {};
|
||||||
|
const { request } = item;
|
||||||
|
each(variables, (variable) => {
|
||||||
|
envVars[variable.name] = variable.value;
|
||||||
|
});
|
||||||
|
|
||||||
|
const templateOpts = {
|
||||||
|
interpolate: /{{([\s\S]+?)}}/g, //interpolate content using markers `{{}}`
|
||||||
|
evaluate: null, // prevent any js evaluation
|
||||||
|
escape: null, // disable any escaping
|
||||||
|
};
|
||||||
|
|
||||||
|
const interpolate = (str) => template(str, templateOpts)(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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user