bugfix: resolve env variables in headers when generating the code

This commit is contained in:
Nook 2024-11-06 16:40:38 +08:00
parent d007feb3d1
commit 612820ddb5
3 changed files with 64 additions and 16 deletions

View File

@ -9,7 +9,6 @@ import { CopyToClipboard } from 'react-copy-to-clipboard';
import toast from 'react-hot-toast';
import { IconCopy } from '@tabler/icons';
import { findCollectionByItemUid, getGlobalEnvironmentVariables } from '../../../../../../../utils/collections/index';
import { getAuthHeaders } from '../../../../../../../utils/codegenerator/auth';
import { cloneDeep } from 'lodash';
const CodeView = ({ language, item }) => {
@ -17,7 +16,6 @@ const CodeView = ({ language, item }) => {
const preferences = useSelector((state) => state.app.preferences);
const { globalEnvironments, activeGlobalEnvironmentUid } = useSelector((state) => state.globalEnvironments);
const { target, client, language: lang } = language;
const requestHeaders = item.draft ? get(item, 'draft.request.headers') : get(item, 'request.headers');
let _collection = findCollectionByItemUid(
useSelector((state) => state.collections.collections),
item.uid
@ -29,18 +27,9 @@ const CodeView = ({ language, item }) => {
const globalEnvironmentVariables = getGlobalEnvironmentVariables({ globalEnvironments, activeGlobalEnvironmentUid });
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 = '';
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,
client
);

View File

@ -2,13 +2,14 @@ import Modal from 'components/Modal/index';
import { useState } from 'react';
import CodeView from './CodeView';
import StyledWrapper from './StyledWrapper';
import { isValidUrl } from 'utils/url';
import { interpolateHeaders, isValidUrl } from 'utils/url';
import { get } from 'lodash';
import { findEnvironmentInCollection } from 'utils/collections';
import { interpolateUrl, interpolateUrlPathParams } from 'utils/url/index';
import { getLanguages } from 'utils/codegenerator/targets';
import { useSelector } from 'react-redux';
import { getGlobalEnvironmentVariables } from 'utils/collections/index';
import { getAuthHeaders } from 'utils/codegenerator/auth';
const GenerateCodeItem = ({ collection, item, onClose }) => {
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')
);
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]);
return (
<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: finalUrl
url: finalUrl,
headers: finalHeaders
}
: {
...item.draft.request,
url: finalUrl
url: finalUrl,
headers: finalHeaders
}
}}
/>

View File

@ -1,7 +1,6 @@
import isEmpty from 'lodash/isEmpty';
import trim from 'lodash/trim';
import each from 'lodash/each';
import filter from 'lodash/filter';
import find from 'lodash/find';
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) => {
const getInterpolatedBasePath = (pathname, params) => {
return pathname