mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-22 07:53:34 +01:00
parent
b432e94a4e
commit
a0df5138b3
@ -9,6 +9,7 @@ import { CopyToClipboard } from 'react-copy-to-clipboard';
|
||||
import toast from 'react-hot-toast';
|
||||
import { IconCopy } from '@tabler/icons';
|
||||
import { findCollectionByItemUid } from '../../../../../../../utils/collections/index';
|
||||
import { getAuthHeaders } from '../../../../../../../utils/codegenerator/auth';
|
||||
|
||||
const CodeView = ({ language, item }) => {
|
||||
const { displayedTheme } = useTheme();
|
||||
@ -20,10 +21,16 @@ const CodeView = ({ language, item }) => {
|
||||
item.uid
|
||||
);
|
||||
|
||||
const headers = [...(collection?.root?.request?.headers || []), ...(requestHeaders || [])];
|
||||
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 })).convert(target, client);
|
||||
} catch (e) {
|
||||
|
30
packages/bruno-app/src/utils/codegenerator/auth.js
Normal file
30
packages/bruno-app/src/utils/codegenerator/auth.js
Normal file
@ -0,0 +1,30 @@
|
||||
import get from 'lodash/get';
|
||||
|
||||
export const getAuthHeaders = (collectionRootAuth, requestAuth) => {
|
||||
const auth = collectionRootAuth && ['inherit', 'none'].includes(requestAuth.mode) ? collectionRootAuth : requestAuth;
|
||||
|
||||
switch (auth.mode) {
|
||||
case 'basic':
|
||||
const username = get(auth, 'basic.username');
|
||||
const password = get(auth, 'basic.password');
|
||||
const basicToken = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
|
||||
return [
|
||||
{
|
||||
enabled: true,
|
||||
name: 'Authorization',
|
||||
value: `Basic ${basicToken}`
|
||||
}
|
||||
];
|
||||
case 'bearer':
|
||||
return [
|
||||
{
|
||||
enabled: true,
|
||||
name: 'Authorization',
|
||||
value: `Bearer ${get(auth, 'bearer.token')}`
|
||||
}
|
||||
];
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user