mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-07 08:34:15 +01:00
bugfix: resolve env variables in headers when generating the code
This commit is contained in:
parent
d007feb3d1
commit
612820ddb5
@ -9,7 +9,6 @@ import { CopyToClipboard } from 'react-copy-to-clipboard';
|
|||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
import { IconCopy } from '@tabler/icons';
|
import { IconCopy } from '@tabler/icons';
|
||||||
import { findCollectionByItemUid, getGlobalEnvironmentVariables } from '../../../../../../../utils/collections/index';
|
import { findCollectionByItemUid, getGlobalEnvironmentVariables } from '../../../../../../../utils/collections/index';
|
||||||
import { getAuthHeaders } from '../../../../../../../utils/codegenerator/auth';
|
|
||||||
import { cloneDeep } from 'lodash';
|
import { cloneDeep } from 'lodash';
|
||||||
|
|
||||||
const CodeView = ({ language, item }) => {
|
const CodeView = ({ language, item }) => {
|
||||||
@ -17,7 +16,6 @@ const CodeView = ({ language, item }) => {
|
|||||||
const preferences = useSelector((state) => state.app.preferences);
|
const preferences = useSelector((state) => state.app.preferences);
|
||||||
const { globalEnvironments, activeGlobalEnvironmentUid } = useSelector((state) => state.globalEnvironments);
|
const { globalEnvironments, activeGlobalEnvironmentUid } = useSelector((state) => state.globalEnvironments);
|
||||||
const { target, client, language: lang } = language;
|
const { target, client, language: lang } = language;
|
||||||
const requestHeaders = item.draft ? get(item, 'draft.request.headers') : get(item, 'request.headers');
|
|
||||||
let _collection = findCollectionByItemUid(
|
let _collection = findCollectionByItemUid(
|
||||||
useSelector((state) => state.collections.collections),
|
useSelector((state) => state.collections.collections),
|
||||||
item.uid
|
item.uid
|
||||||
@ -29,18 +27,9 @@ const CodeView = ({ language, item }) => {
|
|||||||
const globalEnvironmentVariables = getGlobalEnvironmentVariables({ globalEnvironments, activeGlobalEnvironmentUid });
|
const globalEnvironmentVariables = getGlobalEnvironmentVariables({ globalEnvironments, activeGlobalEnvironmentUid });
|
||||||
collection.globalEnvironmentVariables = globalEnvironmentVariables;
|
collection.globalEnvironmentVariables = globalEnvironmentVariables;
|
||||||
|
|
||||||
const collectionRootAuth = collection?.root?.request?.auth;
|
|
||||||
const requestAuth = item.draft ? get(item, 'draft.request.auth') : get(item, 'request.auth');
|
|
||||||
|
|
||||||
const headers = [
|
|
||||||
...getAuthHeaders(collectionRootAuth, requestAuth),
|
|
||||||
...(collection?.root?.request?.headers || []),
|
|
||||||
...(requestHeaders || [])
|
|
||||||
];
|
|
||||||
|
|
||||||
let snippet = '';
|
let snippet = '';
|
||||||
try {
|
try {
|
||||||
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers, type: item.type })).convert(
|
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers: item.request.headers, type: item.type })).convert(
|
||||||
target,
|
target,
|
||||||
client
|
client
|
||||||
);
|
);
|
||||||
|
@ -2,13 +2,14 @@ import Modal from 'components/Modal/index';
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import CodeView from './CodeView';
|
import CodeView from './CodeView';
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
import { isValidUrl } from 'utils/url';
|
import { interpolateHeaders, isValidUrl } from 'utils/url';
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
import { findEnvironmentInCollection } from 'utils/collections';
|
import { findEnvironmentInCollection } from 'utils/collections';
|
||||||
import { interpolateUrl, interpolateUrlPathParams } from 'utils/url/index';
|
import { interpolateUrl, interpolateUrlPathParams } from 'utils/url/index';
|
||||||
import { getLanguages } from 'utils/codegenerator/targets';
|
import { getLanguages } from 'utils/codegenerator/targets';
|
||||||
import { useSelector } from 'react-redux';
|
import { useSelector } from 'react-redux';
|
||||||
import { getGlobalEnvironmentVariables } from 'utils/collections/index';
|
import { getGlobalEnvironmentVariables } from 'utils/collections/index';
|
||||||
|
import { getAuthHeaders } from 'utils/codegenerator/auth';
|
||||||
|
|
||||||
const GenerateCodeItem = ({ collection, item, onClose }) => {
|
const GenerateCodeItem = ({ collection, item, onClose }) => {
|
||||||
const languages = getLanguages();
|
const languages = getLanguages();
|
||||||
@ -44,6 +45,25 @@ const GenerateCodeItem = ({ collection, item, onClose }) => {
|
|||||||
get(item, 'draft.request.params') !== undefined ? get(item, 'draft.request.params') : get(item, 'request.params')
|
get(item, 'draft.request.params') !== undefined ? get(item, 'draft.request.params') : get(item, 'request.params')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
const collectionRootAuth = collection?.root?.request?.auth;
|
||||||
|
const requestAuth = item.draft ? get(item, 'draft.request.auth') : get(item, 'request.auth');
|
||||||
|
const requestHeaders = item.draft ? get(item, 'draft.request.headers') : get(item, 'request.headers');
|
||||||
|
const headers = [
|
||||||
|
...getAuthHeaders(collectionRootAuth, requestAuth),
|
||||||
|
...(collection?.root?.request?.headers || []),
|
||||||
|
...(requestHeaders || [])
|
||||||
|
];
|
||||||
|
|
||||||
|
const finalHeaders = interpolateHeaders({
|
||||||
|
header: headers,
|
||||||
|
globalEnvironmentVariables,
|
||||||
|
envVars,
|
||||||
|
runtimeVariables: collection.runtimeVariables,
|
||||||
|
processEnvVars: collection.processEnvVariables
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
const [selectedLanguage, setSelectedLanguage] = useState(languages[0]);
|
const [selectedLanguage, setSelectedLanguage] = useState(languages[0]);
|
||||||
return (
|
return (
|
||||||
<Modal size="lg" title="Generate Code" handleCancel={onClose} hideFooter={true}>
|
<Modal size="lg" title="Generate Code" handleCancel={onClose} hideFooter={true}>
|
||||||
@ -76,11 +96,13 @@ const GenerateCodeItem = ({ collection, item, onClose }) => {
|
|||||||
item.request.url !== ''
|
item.request.url !== ''
|
||||||
? {
|
? {
|
||||||
...item.request,
|
...item.request,
|
||||||
url: finalUrl
|
url: finalUrl,
|
||||||
|
headers: finalHeaders
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
...item.draft.request,
|
...item.draft.request,
|
||||||
url: finalUrl
|
url: finalUrl,
|
||||||
|
headers: finalHeaders
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import isEmpty from 'lodash/isEmpty';
|
import isEmpty from 'lodash/isEmpty';
|
||||||
import trim from 'lodash/trim';
|
import trim from 'lodash/trim';
|
||||||
import each from 'lodash/each';
|
import each from 'lodash/each';
|
||||||
import filter from 'lodash/filter';
|
|
||||||
import find from 'lodash/find';
|
import find from 'lodash/find';
|
||||||
|
|
||||||
import brunoCommon from '@usebruno/common';
|
import brunoCommon from '@usebruno/common';
|
||||||
@ -125,6 +124,44 @@ export const interpolateUrl = ({ url, globalEnvironmentVariables = {}, envVars,
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const interpolateHeaders = ({ header, globalEnvironmentVariables = {}, envVars, runtimeVariables, processEnvVars }) => {
|
||||||
|
if (!header || !header.length ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return header.map((h) => {
|
||||||
|
if (!h || !h.enabled || !h.name || !h.value || !h.uid) {
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
let newH = {
|
||||||
|
enabled: h.enabled,
|
||||||
|
uid: h.uid,
|
||||||
|
name: h.name,
|
||||||
|
value: h.value
|
||||||
|
};
|
||||||
|
newH.name = interpolate(h.name, {
|
||||||
|
...globalEnvironmentVariables,
|
||||||
|
...envVars,
|
||||||
|
...runtimeVariables,
|
||||||
|
process: {
|
||||||
|
env: {
|
||||||
|
...processEnvVars
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
newH.value = interpolate(h.value, {
|
||||||
|
...globalEnvironmentVariables,
|
||||||
|
...envVars,
|
||||||
|
...runtimeVariables,
|
||||||
|
process: {
|
||||||
|
env: {
|
||||||
|
...processEnvVars
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return newH;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export const interpolateUrlPathParams = (url, params) => {
|
export const interpolateUrlPathParams = (url, params) => {
|
||||||
const getInterpolatedBasePath = (pathname, params) => {
|
const getInterpolatedBasePath = (pathname, params) => {
|
||||||
return pathname
|
return pathname
|
||||||
|
Loading…
Reference in New Issue
Block a user