Merge branch 'main' into one

This commit is contained in:
Anoop M D 2023-10-13 07:03:55 +05:30 committed by GitHub
commit 09f67abad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
94 changed files with 7991 additions and 7262 deletions

46
.github/workflows/release-snap.yml vendored Normal file
View File

@ -0,0 +1,46 @@
name: Publish to Snapcraft
on:
workflow_dispatch:
inputs:
build:
description: 'Build and publish to Snapcraft'
required: true
default: 'true'
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- name: Check package-lock.json
run: npm ci
- name: Install dependencies
run: npm install --legacy-peer-deps
- name: Build Electron app
run: npm run build:electron:snap
- name: Install Snapcraft
run: |
sudo snap install snapcraft --classic
continue-on-error: true
- name: Configure Snapcraft
run: snapcraft whoami
env:
SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_API_KEY }}
- name: Publish to Snapcraft
run: |
snapcraft upload --release=stable packages/bruno-electron/out/*.snap
env:
SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_API_KEY }}

11598
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,7 @@
"dev:electron": "npm run dev --workspace=packages/bruno-electron", "dev:electron": "npm run dev --workspace=packages/bruno-electron",
"build:bruno-query": "npm run build --workspace=packages/bruno-query", "build:bruno-query": "npm run build --workspace=packages/bruno-query",
"build:graphql-docs": "npm run build --workspace=packages/bruno-graphql-docs", "build:graphql-docs": "npm run build --workspace=packages/bruno-graphql-docs",
"build:electron": "./scripts/build-electron.sh", "build:electron:snap": "./scripts/build-electron.sh snap",
"test:e2e": "npx playwright test", "test:e2e": "npx playwright test",
"test:report": "npx playwright show-report", "test:report": "npx playwright show-report",
"prepare": "husky install" "prepare": "husky install"

View File

@ -27,6 +27,7 @@
"file-dialog": "^0.0.8", "file-dialog": "^0.0.8",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"formik": "^2.2.9", "formik": "^2.2.9",
"github-markdown-css": "^5.2.0",
"graphiql": "^1.5.9", "graphiql": "^1.5.9",
"graphql": "^16.6.0", "graphql": "^16.6.0",
"graphql-request": "^3.7.0", "graphql-request": "^3.7.0",
@ -36,7 +37,7 @@
"immer": "^9.0.15", "immer": "^9.0.15",
"know-your-http-well": "^0.5.0", "know-your-http-well": "^0.5.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"markdown-it": "^13.0.1", "markdown-it": "^13.0.2",
"mousetrap": "^1.6.5", "mousetrap": "^1.6.5",
"nanoid": "3.3.4", "nanoid": "3.3.4",
"next": "12.3.3", "next": "12.3.3",

View File

@ -0,0 +1,28 @@
import styled from 'styled-components';
const Wrapper = styled.div`
font-size: 0.8125rem;
.auth-mode-selector {
background: transparent;
.auth-mode-label {
color: ${(props) => props.theme.colors.text.yellow};
}
.dropdown-item {
padding: 0.2rem 0.6rem !important;
}
.label-item {
padding: 0.2rem 0.6rem !important;
}
}
.caret {
color: rgb(140, 140, 140);
fill: rgb(140 140 140);
}
`;
export default Wrapper;

View File

@ -0,0 +1,78 @@
import React, { useRef, forwardRef } from 'react';
import get from 'lodash/get';
import { IconCaretDown } from '@tabler/icons';
import Dropdown from 'components/Dropdown';
import { useDispatch } from 'react-redux';
import { updateCollectionAuthMode } from 'providers/ReduxStore/slices/collections';
import { humanizeRequestAuthMode } from 'utils/collections';
import StyledWrapper from './StyledWrapper';
const AuthMode = ({ collection }) => {
const dispatch = useDispatch();
const dropdownTippyRef = useRef();
const onDropdownCreate = (ref) => (dropdownTippyRef.current = ref);
const authMode = get(collection, 'root.request.auth.mode');
const Icon = forwardRef((props, ref) => {
return (
<div ref={ref} className="flex items-center justify-center auth-mode-label select-none">
{humanizeRequestAuthMode(authMode)} <IconCaretDown className="caret ml-1 mr-1" size={14} strokeWidth={2} />
</div>
);
});
const onModeChange = (value) => {
dispatch(
updateCollectionAuthMode({
collectionUid: collection.uid,
mode: value
})
);
};
return (
<StyledWrapper>
<div className="inline-flex items-center cursor-pointer auth-mode-selector">
<Dropdown onCreate={onDropdownCreate} icon={<Icon />} placement="bottom-end">
<div
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('awsv4');
}}
>
AWS Sig v4
</div>
<div
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('basic');
}}
>
Basic Auth
</div>
<div
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('bearer');
}}
>
Bearer Token
</div>
<div
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('none');
}}
>
No Auth
</div>
</Dropdown>
</div>
</StyledWrapper>
);
};
export default AuthMode;

View File

@ -0,0 +1,16 @@
import styled from 'styled-components';
const Wrapper = styled.div`
label {
font-size: 0.8125rem;
}
.single-line-editor-wrapper {
padding: 0.15rem 0.4rem;
border-radius: 3px;
border: solid 1px ${(props) => props.theme.input.border};
background-color: ${(props) => props.theme.input.bg};
}
`;
export default Wrapper;

View File

@ -0,0 +1,192 @@
import React from 'react';
import get from 'lodash/get';
import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux';
import SingleLineEditor from 'components/SingleLineEditor';
import { updateCollectionAuth } from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
const AwsV4Auth = ({ collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const awsv4Auth = get(collection, 'root.request.auth.awsv4', {});
console.log('saved auth', awsv4Auth);
const handleSave = () => dispatch(saveCollectionRoot(collection.uid));
const handleAccessKeyIdChange = (accessKeyId) => {
dispatch(
updateCollectionAuth({
mode: 'awsv4',
collectionUid: collection.uid,
content: {
accessKeyId: accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleSecretAccessKeyChange = (secretAccessKey) => {
dispatch(
updateCollectionAuth({
mode: 'awsv4',
collectionUid: collection.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleSessionTokenChange = (sessionToken) => {
dispatch(
updateCollectionAuth({
mode: 'awsv4',
collectionUid: collection.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleServiceChange = (service) => {
dispatch(
updateCollectionAuth({
mode: 'awsv4',
collectionUid: collection.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleRegionChange = (region) => {
dispatch(
updateCollectionAuth({
mode: 'awsv4',
collectionUid: collection.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleProfileNameChange = (profileName) => {
dispatch(
updateCollectionAuth({
mode: 'awsv4',
collectionUid: collection.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: profileName
}
})
);
};
return (
<StyledWrapper className="mt-2 w-full">
<label className="block font-medium mb-2">Access Key ID</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.accessKeyId || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleAccessKeyIdChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Secret Access Key</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.secretAccessKey || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleSecretAccessKeyChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Session Token</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.sessionToken || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleSessionTokenChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Service</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.service || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleServiceChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Region</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.region || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleRegionChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Profile Name</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.profileName || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleProfileNameChange(val)}
collection={collection}
/>
</div>
</StyledWrapper>
);
};
export default AwsV4Auth;

View File

@ -0,0 +1,16 @@
import styled from 'styled-components';
const Wrapper = styled.div`
label {
font-size: 0.8125rem;
}
.single-line-editor-wrapper {
padding: 0.15rem 0.4rem;
border-radius: 3px;
border: solid 1px ${(props) => props.theme.input.border};
background-color: ${(props) => props.theme.input.bg};
}
`;
export default Wrapper;

View File

@ -0,0 +1,71 @@
import React from 'react';
import get from 'lodash/get';
import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux';
import SingleLineEditor from 'components/SingleLineEditor';
import { updateCollectionAuth } from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
const BasicAuth = ({ collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const basicAuth = get(collection, 'root.request.auth.basic', {});
const handleSave = () => dispatch(saveCollectionRoot(collection.uid));
const handleUsernameChange = (username) => {
dispatch(
updateCollectionAuth({
mode: 'basic',
collectionUid: collection.uid,
content: {
username: username,
password: basicAuth.password
}
})
);
};
const handlePasswordChange = (password) => {
dispatch(
updateCollectionAuth({
mode: 'basic',
collectionUid: collection.uid,
content: {
username: basicAuth.username,
password: password
}
})
);
};
return (
<StyledWrapper className="mt-2 w-full">
<label className="block font-medium mb-2">Username</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={basicAuth.username || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleUsernameChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Password</label>
<div className="single-line-editor-wrapper">
<SingleLineEditor
value={basicAuth.password || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handlePasswordChange(val)}
collection={collection}
/>
</div>
</StyledWrapper>
);
};
export default BasicAuth;

View File

@ -0,0 +1,16 @@
import styled from 'styled-components';
const Wrapper = styled.div`
label {
font-size: 0.8125rem;
}
.single-line-editor-wrapper {
padding: 0.15rem 0.4rem;
border-radius: 3px;
border: solid 1px ${(props) => props.theme.input.border};
background-color: ${(props) => props.theme.input.bg};
}
`;
export default Wrapper;

View File

@ -0,0 +1,46 @@
import React from 'react';
import get from 'lodash/get';
import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux';
import SingleLineEditor from 'components/SingleLineEditor';
import { updateCollectionAuth } from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
const BearerAuth = ({ collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const bearerToken = get(collection, 'root.request.auth.bearer.token');
const handleSave = () => dispatch(saveCollectionRoot(collection.uid));
const handleTokenChange = (token) => {
dispatch(
updateCollectionAuth({
mode: 'bearer',
collectionUid: collection.uid,
content: {
token: token
}
})
);
};
return (
<StyledWrapper className="mt-2 w-full">
<label className="block font-medium mb-2">Token</label>
<div className="single-line-editor-wrapper">
<SingleLineEditor
value={bearerToken}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleTokenChange(val)}
collection={collection}
/>
</div>
</StyledWrapper>
);
};
export default BearerAuth;

View File

@ -0,0 +1,5 @@
import styled from 'styled-components';
const Wrapper = styled.div``;
export default Wrapper;

View File

@ -0,0 +1,46 @@
import React from 'react';
import get from 'lodash/get';
import { useDispatch } from 'react-redux';
import AuthMode from './AuthMode';
import AwsV4Auth from './AwsV4Auth';
import BearerAuth from './BearerAuth';
import BasicAuth from './BasicAuth';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
const Auth = ({ collection }) => {
const authMode = get(collection, 'root.request.auth.mode');
const dispatch = useDispatch();
const handleSave = () => dispatch(saveCollectionRoot(collection.uid));
const getAuthView = () => {
switch (authMode) {
case 'awsv4': {
return <AwsV4Auth collection={collection} />;
}
case 'basic': {
return <BasicAuth collection={collection} />;
}
case 'bearer': {
return <BearerAuth collection={collection} />;
}
}
};
return (
<StyledWrapper className="w-full mt-2">
<div className="flex flex-grow justify-start items-center">
<AuthMode collection={collection} />
</div>
{getAuthView()}
<div className="mt-6">
<button type="submit" className="submit btn btn-sm btn-secondary" onClick={handleSave}>
Save
</button>
</div>
</StyledWrapper>
);
};
export default Auth;

View File

@ -0,0 +1,56 @@
import styled from 'styled-components';
const Wrapper = styled.div`
table {
width: 100%;
border-collapse: collapse;
font-weight: 600;
table-layout: fixed;
thead,
td {
border: 1px solid ${(props) => props.theme.table.border};
}
thead {
color: ${(props) => props.theme.table.thead.color};
font-size: 0.8125rem;
user-select: none;
}
td {
padding: 6px 10px;
&:nth-child(1) {
width: 30%;
}
&:nth-child(3) {
width: 70px;
}
}
}
.btn-add-header {
font-size: 0.8125rem;
}
input[type='text'] {
width: 100%;
border: solid 1px transparent;
outline: none !important;
background-color: inherit;
&:focus {
outline: none !important;
border: solid 1px transparent;
}
}
input[type='checkbox'] {
cursor: pointer;
position: relative;
top: 1px;
}
`;
export default Wrapper;

View File

@ -0,0 +1,151 @@
import React from 'react';
import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
import { IconTrash } from '@tabler/icons';
import { useDispatch } from 'react-redux';
import { useTheme } from 'providers/Theme';
import {
addCollectionHeader,
updateCollectionHeader,
deleteCollectionHeader
} from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import SingleLineEditor from 'components/SingleLineEditor';
import StyledWrapper from './StyledWrapper';
import { headers as StandardHTTPHeaders } from 'know-your-http-well';
const headerAutoCompleteList = StandardHTTPHeaders.map((e) => e.header);
const Headers = ({ collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const headers = get(collection, 'root.request.headers', []);
const addHeader = () => {
dispatch(
addCollectionHeader({
collectionUid: collection.uid
})
);
};
const handleSave = () => dispatch(saveCollectionRoot(collection.uid));
const handleHeaderValueChange = (e, _header, type) => {
const header = cloneDeep(_header);
switch (type) {
case 'name': {
header.name = e.target.value;
break;
}
case 'value': {
header.value = e.target.value;
break;
}
case 'enabled': {
header.enabled = e.target.checked;
break;
}
}
dispatch(
updateCollectionHeader({
header: header,
collectionUid: collection.uid
})
);
};
const handleRemoveHeader = (header) => {
dispatch(
deleteCollectionHeader({
headerUid: header.uid,
collectionUid: collection.uid
})
);
};
return (
<StyledWrapper className="w-full">
<table>
<thead>
<tr>
<td>Name</td>
<td>Value</td>
<td></td>
</tr>
</thead>
<tbody>
{headers && headers.length
? headers.map((header) => {
return (
<tr key={header.uid}>
<td>
<SingleLineEditor
value={header.name}
theme={storedTheme}
onSave={handleSave}
onChange={(newValue) =>
handleHeaderValueChange(
{
target: {
value: newValue
}
},
header,
'name'
)
}
autocomplete={headerAutoCompleteList}
collection={collection}
/>
</td>
<td>
<SingleLineEditor
value={header.value}
theme={storedTheme}
onSave={handleSave}
onChange={(newValue) =>
handleHeaderValueChange(
{
target: {
value: newValue
}
},
header,
'value'
)
}
collection={collection}
/>
</td>
<td>
<div className="flex items-center">
<input
type="checkbox"
checked={header.enabled}
tabIndex="-1"
className="mr-3 mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'enabled')}
/>
<button tabIndex="-1" onClick={() => handleRemoveHeader(header)}>
<IconTrash strokeWidth={1.5} size={20} />
</button>
</div>
</td>
</tr>
);
})
: null}
</tbody>
</table>
<button className="btn-add-header text-link pr-2 py-3 mt-2 select-none" onClick={addHeader}>
+ Add Header
</button>
<div className="mt-6">
<button type="submit" className="submit btn btn-sm btn-secondary" onClick={handleSave}>
Save
</button>
</div>
</StyledWrapper>
);
};
export default Headers;

View File

@ -19,7 +19,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
}, },
validationSchema: Yup.object({ validationSchema: Yup.object({
enabled: Yup.boolean(), enabled: Yup.boolean(),
protocol: Yup.string().oneOf(['http', 'https']), protocol: Yup.string().oneOf(['http', 'https', 'socks5']),
hostname: Yup.string().max(1024), hostname: Yup.string().max(1024),
port: Yup.number().min(0).max(65535), port: Yup.number().min(0).max(65535),
auth: Yup.object({ auth: Yup.object({
@ -49,20 +49,19 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
return ( return (
<StyledWrapper> <StyledWrapper>
<h1 className="font-medium mb-3">Proxy Settings</h1>
<form className="bruno-form" onSubmit={formik.handleSubmit}> <form className="bruno-form" onSubmit={formik.handleSubmit}>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="enabled"> <label className="settings-label" htmlFor="enabled">
Enabled Enabled
</label> </label>
<input type="checkbox" name="enabled" checked={formik.values.enabled} onChange={formik.handleChange} /> <input type="checkbox" name="enabled" checked={formik.values.enabled} onChange={formik.handleChange} />
</div> </div>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="protocol"> <label className="settings-label" htmlFor="protocol">
Protocol Protocol
</label> </label>
<div className="flex items-center"> <div className="flex items-center">
<label className="flex items-center mr-4"> <label className="flex items-center">
<input <input
type="radio" type="radio"
name="protocol" name="protocol"
@ -73,7 +72,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
/> />
http http
</label> </label>
<label className="flex items-center"> <label className="flex items-center ml-4">
<input <input
type="radio" type="radio"
name="protocol" name="protocol"
@ -84,9 +83,20 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
/> />
https https
</label> </label>
<label className="flex items-center ml-4">
<input
type="radio"
name="protocol"
value="socks5"
checked={formik.values.protocol === 'socks5'}
onChange={formik.handleChange}
className="mr-1"
/>
socks5
</label>
</div> </div>
</div> </div>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="hostname"> <label className="settings-label" htmlFor="hostname">
Hostname Hostname
</label> </label>
@ -106,7 +116,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
<div className="text-red-500">{formik.errors.hostname}</div> <div className="text-red-500">{formik.errors.hostname}</div>
) : null} ) : null}
</div> </div>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="port"> <label className="settings-label" htmlFor="port">
Port Port
</label> </label>
@ -124,7 +134,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
/> />
{formik.touched.port && formik.errors.port ? <div className="text-red-500">{formik.errors.port}</div> : null} {formik.touched.port && formik.errors.port ? <div className="text-red-500">{formik.errors.port}</div> : null}
</div> </div>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="auth.enabled"> <label className="settings-label" htmlFor="auth.enabled">
Auth Auth
</label> </label>
@ -136,7 +146,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
/> />
</div> </div>
<div> <div>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="auth.username"> <label className="settings-label" htmlFor="auth.username">
Username Username
</label> </label>
@ -156,7 +166,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
<div className="text-red-500">{formik.errors.auth.username}</div> <div className="text-red-500">{formik.errors.auth.username}</div>
) : null} ) : null}
</div> </div>
<div className="ml-4 mb-3 flex items-center"> <div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="auth.password"> <label className="settings-label" htmlFor="auth.password">
Password Password
</label> </label>
@ -178,7 +188,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
</div> </div>
</div> </div>
<div className="mt-6"> <div className="mt-6">
<button type="submit" className="submit btn btn-md btn-secondary"> <button type="submit" className="submit btn btn-sm btn-secondary">
Save Save
</button> </button>
</div> </div>

View File

@ -0,0 +1,13 @@
import styled from 'styled-components';
const StyledWrapper = styled.div`
div.CodeMirror {
height: inherit;
}
div.title {
color: var(--color-tab-inactive);
}
`;
export default StyledWrapper;

View File

@ -0,0 +1,73 @@
import React from 'react';
import get from 'lodash/get';
import { useDispatch } from 'react-redux';
import CodeEditor from 'components/CodeEditor';
import { updateCollectionRequestScript, updateCollectionResponseScript } from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import { useTheme } from 'providers/Theme';
import StyledWrapper from './StyledWrapper';
const Script = ({ collection }) => {
const dispatch = useDispatch();
const requestScript = get(collection, 'root.request.script.req', '');
const responseScript = get(collection, 'root.request.script.res', '');
const { storedTheme } = useTheme();
const onRequestScriptEdit = (value) => {
dispatch(
updateCollectionRequestScript({
script: value,
collectionUid: collection.uid
})
);
};
const onResponseScriptEdit = (value) => {
dispatch(
updateCollectionResponseScript({
script: value,
collectionUid: collection.uid
})
);
};
const handleSave = () => {
dispatch(saveCollectionRoot(collection.uid));
};
return (
<StyledWrapper className="w-full flex flex-col">
<div className="flex-1 mt-2">
<div className="mb-1 title text-xs">Pre Request</div>
<CodeEditor
collection={collection}
value={requestScript || ''}
theme={storedTheme}
onEdit={onRequestScriptEdit}
mode="javascript"
onSave={handleSave}
/>
</div>
<div className="flex-1 mt-6">
<div className="mt-1 mb-1 title text-xs">Post Response</div>
<CodeEditor
collection={collection}
value={responseScript || ''}
theme={storedTheme}
onEdit={onResponseScriptEdit}
mode="javascript"
onSave={handleSave}
/>
</div>
<div className="mt-12">
<button type="submit" className="submit btn btn-sm btn-secondary" onClick={handleSave}>
Save
</button>
</div>
</StyledWrapper>
);
};
export default Script;

View File

@ -1,6 +1,32 @@
import styled from 'styled-components'; import styled from 'styled-components';
const StyledWrapper = styled.div` const StyledWrapper = styled.div`
max-width: 800px;
div.tabs {
div.tab {
padding: 6px 0px;
border: none;
border-bottom: solid 2px transparent;
margin-right: 1.25rem;
color: var(--color-tab-inactive);
cursor: pointer;
&:focus,
&:active,
&:focus-within,
&:focus-visible,
&:target {
outline: none !important;
box-shadow: none !important;
}
&.active {
color: ${(props) => props.theme.tabs.active.color} !important;
border-bottom: solid 2px ${(props) => props.theme.tabs.active.border} !important;
}
}
}
table { table {
thead, thead,
td { td {

View File

@ -0,0 +1,5 @@
import styled from 'styled-components';
const StyledWrapper = styled.div``;
export default StyledWrapper;

View File

@ -0,0 +1,47 @@
import React from 'react';
import get from 'lodash/get';
import { useDispatch } from 'react-redux';
import CodeEditor from 'components/CodeEditor';
import { updateCollectionTests } from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import { useTheme } from 'providers/Theme';
import StyledWrapper from './StyledWrapper';
const Tests = ({ collection }) => {
const dispatch = useDispatch();
const tests = get(collection, 'root.request.tests', '');
const { storedTheme } = useTheme();
const onEdit = (value) => {
dispatch(
updateCollectionTests({
tests: value,
collectionUid: collection.uid
})
);
};
const handleSave = () => dispatch(saveCollectionRoot(collection.uid));
return (
<StyledWrapper className="w-full flex flex-col h-full">
<CodeEditor
collection={collection}
value={tests || ''}
theme={storedTheme}
onEdit={onEdit}
mode="javascript"
onSave={handleSave}
/>
<div className="mt-6">
<button type="submit" className="submit btn btn-sm btn-secondary" onClick={handleSave}>
Save
</button>
</div>
</StyledWrapper>
);
};
export default Tests;

View File

@ -1,14 +1,29 @@
import React from 'react'; import React from 'react';
import classnames from 'classnames';
import get from 'lodash/get'; import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import { updateBrunoConfig } from 'providers/ReduxStore/slices/collections/actions'; import { updateBrunoConfig } from 'providers/ReduxStore/slices/collections/actions';
import { updateSettingsSelectedTab } from 'providers/ReduxStore/slices/collections';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import ProxySettings from './ProxySettings'; import ProxySettings from './ProxySettings';
import Headers from './Headers';
import Auth from './Auth';
import Script from './Script';
import Test from './Tests';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
const CollectionSettings = ({ collection }) => { const CollectionSettings = ({ collection }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const tab = collection.settingsSelectedTab;
const setTab = (tab) => {
dispatch(
updateSettingsSelectedTab({
collectionUid: collection.uid,
tab
})
);
};
const proxyConfig = get(collection, 'brunoConfig.proxy', {}); const proxyConfig = get(collection, 'brunoConfig.proxy', {});
@ -22,11 +37,52 @@ const CollectionSettings = ({ collection }) => {
.catch((err) => console.log(err) && toast.error('Failed to update collection settings')); .catch((err) => console.log(err) && toast.error('Failed to update collection settings'));
}; };
return ( const getTabPanel = (tab) => {
<StyledWrapper className="px-4 py-4"> switch (tab) {
<h1 className="font-semibold mb-4">Collection Settings</h1> case 'headers': {
return <Headers collection={collection} />;
}
case 'auth': {
return <Auth collection={collection} />;
}
case 'script': {
return <Script collection={collection} />;
}
case 'tests': {
return <Test collection={collection} />;
}
case 'proxy': {
return <ProxySettings proxyConfig={proxyConfig} onUpdate={onProxySettingsUpdate} />;
}
}
};
<ProxySettings proxyConfig={proxyConfig} onUpdate={onProxySettingsUpdate} /> const getTabClassname = (tabName) => {
return classnames(`tab select-none ${tabName}`, {
active: tabName === tab
});
};
return (
<StyledWrapper className="flex flex-col h-full relative px-4 py-4">
<div className="flex flex-wrap items-center tabs" role="tablist">
<div className={getTabClassname('headers')} role="tab" onClick={() => setTab('headers')}>
Headers
</div>
<div className={getTabClassname('auth')} role="tab" onClick={() => setTab('auth')}>
Auth
</div>
<div className={getTabClassname('script')} role="tab" onClick={() => setTab('script')}>
Script
</div>
<div className={getTabClassname('tests')} role="tab" onClick={() => setTab('tests')}>
Tests
</div>
<div className={getTabClassname('proxy')} role="tab" onClick={() => setTab('proxy')}>
Proxy
</div>
</div>
<section className={`flex ${['auth', 'script'].includes(tab) ? '' : 'mt-4'}`}>{getTabPanel(tab)}</section>
</StyledWrapper> </StyledWrapper>
); );
}; };

View File

@ -0,0 +1,18 @@
import styled from 'styled-components';
const StyledWrapper = styled.div`
div.CodeMirror {
/* todo: find a better way */
height: calc(100vh - 240px);
.CodeMirror-scroll {
padding-bottom: 0px;
}
}
.editing-mode {
cursor: pointer;
color: ${(props) => props.theme.colors.text.yellow};
}
`;
export default StyledWrapper;

View File

@ -0,0 +1,60 @@
import 'github-markdown-css/github-markdown.css';
import get from 'lodash/get';
import { updateRequestDocs } from 'providers/ReduxStore/slices/collections';
import { useTheme } from 'providers/Theme/index';
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { saveRequest } from 'providers/ReduxStore/slices/collections/actions';
import Markdown from 'components/MarkDown';
import CodeEditor from 'components/CodeEditor';
import StyledWrapper from './StyledWrapper';
const Documentation = ({ item, collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const [isEditing, setIsEditing] = useState(false);
const docs = item.draft ? get(item, 'draft.request.docs') : get(item, 'request.docs');
const toggleViewMode = () => {
setIsEditing((prev) => !prev);
};
const onEdit = (value) => {
dispatch(
updateRequestDocs({
itemUid: item.uid,
collectionUid: collection.uid,
docs: value
})
);
};
const onSave = () => dispatch(saveRequest(item.uid, collection.uid));
if (!item) {
return null;
}
return (
<StyledWrapper className="mt-1 h-full w-full relative">
<div className="editing-mode mb-2" role="tab" onClick={toggleViewMode}>
{isEditing ? 'Preview' : 'Edit'}
</div>
{isEditing ? (
<CodeEditor
collection={collection}
theme={storedTheme}
value={docs || ''}
onEdit={onEdit}
onSave={onSave}
mode="application/text"
/>
) : (
<Markdown onDoubleClick={toggleViewMode} content={docs} />
)}
</StyledWrapper>
);
};
export default Documentation;

View File

@ -0,0 +1,83 @@
import styled from 'styled-components';
const StyledMarkdownBodyWrapper = styled.div`
background: transparent;
height: inherit;
.markdown-body {
background: transparent;
overflow-y: auto;
color: ${(props) => props.theme.text};
box-sizing: border-box;
height: 100%;
margin: 0 auto;
padding-top: 0.5rem;
font-size: 0.875rem;
h1 {
margin: 0.67em 0;
font-weight: var(--base-text-weight-semibold, 600);
padding-bottom: 0.3em;
font-size: 1.3;
border-bottom: 1px solid var(--color-border-muted);
}
h2 {
font-weight: var(--base-text-weight-semibold, 600);
padding-bottom: 0.3em;
font-size: 1.2;
border-bottom: 1px solid var(--color-border-muted);
}
h3 {
font-weight: var(--base-text-weight-semibold, 600);
font-size: 1.1em;
}
h4 {
font-weight: var(--base-text-weight-semibold, 600);
font-size: 1em;
}
h5 {
font-weight: var(--base-text-weight-semibold, 600);
font-size: 0.95em;
}
h6 {
font-weight: var(--base-text-weight-semibold, 600);
font-size: 0.9em;
color: var(--color-fg-muted);
}
hr {
box-sizing: content-box;
overflow: hidden;
border-bottom: 1px solid var(--color-border-muted);
height: 1px;
padding: 0;
margin: 24px 0;
background-color: var(--color-border-default);
border: 0;
}
ul {
list-style-type: disc;
}
ol {
list-style-type: decimal;
}
pre {
background: ${(props) => props.theme.sidebar.bg};
}
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
`;
export default StyledMarkdownBodyWrapper;

View File

@ -0,0 +1,32 @@
import MarkdownIt from 'markdown-it';
import StyledWrapper from './StyledWrapper';
const md = new MarkdownIt();
const Markdown = ({ onDoubleClick, content }) => {
const handleOnDoubleClick = (event) => {
switch (event.detail) {
case 2: {
onDoubleClick();
break;
}
case 1:
default: {
break;
}
}
};
const htmlFromMarkdown = md.render(content || '');
return (
<StyledWrapper>
<div
className="markdown-body"
dangerouslySetInnerHTML={{ __html: htmlFromMarkdown }}
onClick={handleOnDoubleClick}
/>
</StyledWrapper>
);
};
export default Markdown;

View File

@ -178,7 +178,7 @@ const AssertionRow = ({
handleAssertionChange( handleAssertionChange(
{ {
target: { target: {
value: newValue value: `${operator} ${newValue}`
} }
}, },
assertion, assertion,

View File

@ -35,6 +35,15 @@ const AuthMode = ({ item, collection }) => {
<StyledWrapper> <StyledWrapper>
<div className="inline-flex items-center cursor-pointer auth-mode-selector"> <div className="inline-flex items-center cursor-pointer auth-mode-selector">
<Dropdown onCreate={onDropdownCreate} icon={<Icon />} placement="bottom-end"> <Dropdown onCreate={onDropdownCreate} icon={<Icon />} placement="bottom-end">
<div
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('awsv4');
}}
>
AWS Sig v4
</div>
<div <div
className="dropdown-item" className="dropdown-item"
onClick={() => { onClick={() => {

View File

@ -0,0 +1,16 @@
import styled from 'styled-components';
const Wrapper = styled.div`
label {
font-size: 0.8125rem;
}
.single-line-editor-wrapper {
padding: 0.15rem 0.4rem;
border-radius: 3px;
border: solid 1px ${(props) => props.theme.input.border};
background-color: ${(props) => props.theme.input.bg};
}
`;
export default Wrapper;

View File

@ -0,0 +1,206 @@
import React, { useState } from 'react';
import get from 'lodash/get';
import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux';
import SingleLineEditor from 'components/SingleLineEditor';
import { updateAuth } from 'providers/ReduxStore/slices/collections';
import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
import { update } from 'lodash';
const AwsV4Auth = ({ onTokenChange, item, collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const awsv4Auth = item.draft ? get(item, 'draft.request.auth.awsv4', {}) : get(item, 'request.auth.awsv4', {});
console.log('saved auth', awsv4Auth);
const handleRun = () => dispatch(sendRequest(item, collection.uid));
const handleSave = () => dispatch(saveRequest(item.uid, collection.uid));
const handleAccessKeyIdChange = (accessKeyId) => {
dispatch(
updateAuth({
mode: 'awsv4',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
accessKeyId: accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleSecretAccessKeyChange = (secretAccessKey) => {
dispatch(
updateAuth({
mode: 'awsv4',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleSessionTokenChange = (sessionToken) => {
dispatch(
updateAuth({
mode: 'awsv4',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleServiceChange = (service) => {
dispatch(
updateAuth({
mode: 'awsv4',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: service,
region: awsv4Auth.region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleRegionChange = (region) => {
dispatch(
updateAuth({
mode: 'awsv4',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: region,
profileName: awsv4Auth.profileName
}
})
);
};
const handleProfileNameChange = (profileName) => {
dispatch(
updateAuth({
mode: 'awsv4',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
accessKeyId: awsv4Auth.accessKeyId,
secretAccessKey: awsv4Auth.secretAccessKey,
sessionToken: awsv4Auth.sessionToken,
service: awsv4Auth.service,
region: awsv4Auth.region,
profileName: profileName
}
})
);
};
return (
<StyledWrapper className="mt-2 w-full">
<label className="block font-medium mb-2">Access Key ID</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.accessKeyId || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleAccessKeyIdChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Secret Access Key</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.secretAccessKey || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleSecretAccessKeyChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Session Token</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.sessionToken || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleSessionTokenChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Service</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.service || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleServiceChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Region</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.region || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleRegionChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Profile Name</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={awsv4Auth.profileName || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleProfileNameChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
</StyledWrapper>
);
};
export default AwsV4Auth;

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import get from 'lodash/get'; import get from 'lodash/get';
import AuthMode from './AuthMode'; import AuthMode from './AuthMode';
import AwsV4Auth from './AwsV4Auth';
import BearerAuth from './BearerAuth'; import BearerAuth from './BearerAuth';
import BasicAuth from './BasicAuth'; import BasicAuth from './BasicAuth';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
@ -10,6 +11,9 @@ const Auth = ({ item, collection }) => {
const getAuthView = () => { const getAuthView = () => {
switch (authMode) { switch (authMode) {
case 'awsv4': {
return <AwsV4Auth collection={collection} item={item} />;
}
case 'basic': { case 'basic': {
return <BasicAuth collection={collection} item={item} />; return <BasicAuth collection={collection} item={item} />;
} }

View File

@ -19,6 +19,7 @@ import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collection
import { findEnvironmentInCollection } from 'utils/collections'; import { findEnvironmentInCollection } from 'utils/collections';
import useGraphqlSchema from './useGraphqlSchema'; import useGraphqlSchema from './useGraphqlSchema';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
import Documentation from 'components/Documentation/index';
const GraphQLRequestPane = ({ item, collection, leftPaneWidth, onSchemaLoad, toggleDocs, handleGqlClickReference }) => { const GraphQLRequestPane = ({ item, collection, leftPaneWidth, onSchemaLoad, toggleDocs, handleGqlClickReference }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -113,6 +114,9 @@ const GraphQLRequestPane = ({ item, collection, leftPaneWidth, onSchemaLoad, tog
case 'tests': { case 'tests': {
return <Tests item={item} collection={collection} />; return <Tests item={item} collection={collection} />;
} }
case 'docs': {
return <Documentation item={item} collection={collection} />;
}
default: { default: {
return <div className="mt-4">404 | Not found</div>; return <div className="mt-4">404 | Not found</div>;
} }
@ -161,6 +165,9 @@ const GraphQLRequestPane = ({ item, collection, leftPaneWidth, onSchemaLoad, tog
<div className={getTabClassname('tests')} role="tab" onClick={() => selectTab('tests')}> <div className={getTabClassname('tests')} role="tab" onClick={() => selectTab('tests')}>
Tests Tests
</div> </div>
<div className={getTabClassname('docs')} role="tab" onClick={() => selectTab('docs')}>
Docs
</div>
<div className="flex flex-grow justify-end items-center" style={{ fontSize: 13 }}> <div className="flex flex-grow justify-end items-center" style={{ fontSize: 13 }}>
<div className="flex items-center cursor-pointer hover:underline" onClick={loadGqlSchema}> <div className="flex items-center cursor-pointer hover:underline" onClick={loadGqlSchema}>
{isSchemaLoading ? <IconLoader2 className="animate-spin" size={18} strokeWidth={1.5} /> : null} {isSchemaLoading ? <IconLoader2 className="animate-spin" size={18} strokeWidth={1.5} /> : null}

View File

@ -15,6 +15,7 @@ import Script from 'components/RequestPane/Script';
import Tests from 'components/RequestPane/Tests'; import Tests from 'components/RequestPane/Tests';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
import { get } from 'lodash'; import { get } from 'lodash';
import Documentation from 'components/Documentation/index';
const HttpRequestPane = ({ item, collection, leftPaneWidth }) => { const HttpRequestPane = ({ item, collection, leftPaneWidth }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -56,6 +57,9 @@ const HttpRequestPane = ({ item, collection, leftPaneWidth }) => {
case 'tests': { case 'tests': {
return <Tests item={item} collection={collection} />; return <Tests item={item} collection={collection} />;
} }
case 'docs': {
return <Documentation item={item} collection={collection} />;
}
default: { default: {
return <div className="mt-4">404 | Not found</div>; return <div className="mt-4">404 | Not found</div>;
} }
@ -122,6 +126,9 @@ const HttpRequestPane = ({ item, collection, leftPaneWidth }) => {
<div className={getTabClassname('tests')} role="tab" onClick={() => selectTab('tests')}> <div className={getTabClassname('tests')} role="tab" onClick={() => selectTab('tests')}>
Tests Tests
</div> </div>
<div className={getTabClassname('docs')} role="tab" onClick={() => selectTab('docs')}>
Docs
</div>
{focusedTab.requestPaneTab === 'body' ? ( {focusedTab.requestPaneTab === 'body' ? (
<div className="flex flex-grow justify-end items-center"> <div className="flex flex-grow justify-end items-center">
<RequestBodyMode item={item} collection={collection} /> <RequestBodyMode item={item} collection={collection} />
@ -129,7 +136,9 @@ const HttpRequestPane = ({ item, collection, leftPaneWidth }) => {
) : null} ) : null}
</div> </div>
<section <section
className={`flex w-full ${['script', 'vars', 'auth'].includes(focusedTab.requestPaneTab) ? '' : 'mt-5'}`} className={`flex w-full ${
['script', 'vars', 'auth', 'docs'].includes(focusedTab.requestPaneTab) ? '' : 'mt-5'
}`}
> >
{getTabPanel(focusedTab.requestPaneTab)} {getTabPanel(focusedTab.requestPaneTab)}
</section> </section>

View File

@ -82,6 +82,15 @@ const RequestBodyMode = ({ item, collection }) => {
> >
TEXT TEXT
</div> </div>
<div
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('sparql');
}}
>
SPARQL
</div>
<div className="label-item font-medium">Other</div> <div className="label-item font-medium">Other</div>
<div <div
className="dropdown-item" className="dropdown-item"

View File

@ -28,17 +28,19 @@ const RequestBody = ({ item, collection }) => {
const onRun = () => dispatch(sendRequest(item, collection.uid)); const onRun = () => dispatch(sendRequest(item, collection.uid));
const onSave = () => dispatch(saveRequest(item.uid, collection.uid)); const onSave = () => dispatch(saveRequest(item.uid, collection.uid));
if (['json', 'xml', 'text'].includes(bodyMode)) { if (['json', 'xml', 'text', 'sparql'].includes(bodyMode)) {
let codeMirrorMode = { let codeMirrorMode = {
json: 'application/ld+json', json: 'application/ld+json',
text: 'application/text', text: 'application/text',
xml: 'application/xml' xml: 'application/xml',
sparql: 'application/sparql-query'
}; };
let bodyContent = { let bodyContent = {
json: body.json, json: body.json,
text: body.text, text: body.text,
xml: body.xml xml: body.xml,
sparql: body.sparql
}; };
return ( return (

View File

@ -8,7 +8,7 @@ const SpecialTab = ({ handleCloseClick, type }) => {
return ( return (
<> <>
<IconSettings size={18} strokeWidth={1.5} className="text-yellow-600" /> <IconSettings size={18} strokeWidth={1.5} className="text-yellow-600" />
<span className="ml-1">Settings</span> <span className="ml-1">Collection</span>
</> </>
); );
} }

View File

@ -2,16 +2,12 @@ import styled from 'styled-components';
const StyledWrapper = styled.div` const StyledWrapper = styled.div`
position: absolute; position: absolute;
height: 100%;
z-index: 1; z-index: 1;
height: 100vh;
background-color: ${(props) => props.theme.requestTabPanel.responseOverlayBg}; background-color: ${(props) => props.theme.requestTabPanel.responseOverlayBg};
div.overlay { div.overlay {
position: absolute; height: 100%;
top: 0;
right: 0;
left: 0;
bottom: 0;
z-index: 9; z-index: 9;
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View File

@ -1,8 +1,14 @@
import React from 'react'; import React from 'react';
import { IconSend } from '@tabler/icons'; import { IconSend } from '@tabler/icons';
import StyledWrapper from './StyledWrapper'; import StyledWrapper from './StyledWrapper';
import { isMacOS } from 'utils/common/platform';
const Placeholder = () => { const Placeholder = () => {
const isMac = isMacOS();
const sendRequestShortcut = isMac ? 'Cmd + Enter' : 'Ctrl + Enter';
const newRequestShortcut = isMac ? 'Cmd + B' : 'Ctrl + B';
const editEnvironmentShortcut = isMac ? 'Cmd + E' : 'Ctrl + E';
return ( return (
<StyledWrapper> <StyledWrapper>
<div className="send-icon flex justify-center" style={{ fontSize: 200 }}> <div className="send-icon flex justify-center" style={{ fontSize: 200 }}>
@ -15,9 +21,9 @@ const Placeholder = () => {
<div className="px-1 py-2">Edit Environments</div> <div className="px-1 py-2">Edit Environments</div>
</div> </div>
<div className="flex flex-1 flex-col px-1"> <div className="flex flex-1 flex-col px-1">
<div className="px-1 py-2">Cmd + Enter</div> <div className="px-1 py-2">{sendRequestShortcut}</div>
<div className="px-1 py-2">Cmd + B</div> <div className="px-1 py-2">{newRequestShortcut}</div>
<div className="px-1 py-2">Cmd + E</div> <div className="px-1 py-2">{editEnvironmentShortcut}</div>
</div> </div>
</div> </div>
</StyledWrapper> </StyledWrapper>

View File

@ -45,6 +45,10 @@ const QueryResult = ({ item, collection, data, width, disableRunEventListener, h
return safeStringifyJSON(data); return safeStringifyJSON(data);
} }
if (mode.includes('image')) {
return item.requestSent.url;
}
// final fallback // final fallback
if (typeof data === 'string') { if (typeof data === 'string') {
return data; return data;
@ -87,7 +91,13 @@ const QueryResult = ({ item, collection, data, width, disableRunEventListener, h
}; };
const activeResult = useMemo(() => { const activeResult = useMemo(() => {
if (tab === 'preview' && mode.includes('html') && item.requestSent && item.requestSent.url) { if (
tab === 'preview' &&
mode.includes('html') &&
item.requestSent &&
item.requestSent.url &&
typeof data === 'string'
) {
// Add the Base tag to the head so content loads properly. This also needs the correct CSP settings // Add the Base tag to the head so content loads properly. This also needs the correct CSP settings
const webViewSrc = data.replace('<head>', `<head><base href="${item.requestSent.url}">`); const webViewSrc = data.replace('<head>', `<head><base href="${item.requestSent.url}">`);
return ( return (
@ -97,13 +107,15 @@ const QueryResult = ({ item, collection, data, width, disableRunEventListener, h
className="h-full bg-white" className="h-full bg-white"
/> />
); );
} else if (mode.includes('image')) {
return <img src={item.requestSent.url} alt="image" />;
} }
return <CodeEditor collection={collection} theme={storedTheme} onRun={onRun} value={value} mode={mode} readOnly />; return <CodeEditor collection={collection} theme={storedTheme} onRun={onRun} value={value} mode={mode} readOnly />;
}, [tab, collection, storedTheme, onRun, value, mode]); }, [tab, collection, storedTheme, onRun, value, mode]);
return ( return (
<StyledWrapper className="px-3 w-full h-full" style={{ maxWidth: width }}> <StyledWrapper className="w-full h-full" style={{ maxWidth: width }}>
<div className="flex justify-end gap-2 text-xs" role="tablist"> <div className="flex justify-end gap-2 text-xs" role="tablist">
{getTabs()} {getTabs()}
</div> </div>

View File

@ -3,7 +3,7 @@ import StyledWrapper from './StyledWrapper';
const ResponseHeaders = ({ headers }) => { const ResponseHeaders = ({ headers }) => {
return ( return (
<StyledWrapper className="px-3 pb-4 w-full"> <StyledWrapper className="pb-4 w-full">
<table> <table>
<thead> <thead>
<tr> <tr>

View File

@ -15,7 +15,7 @@ const TestResults = ({ results, assertionResults }) => {
const failedAssertions = assertionResults.filter((result) => result.status === 'fail'); const failedAssertions = assertionResults.filter((result) => result.status === 'fail');
return ( return (
<StyledWrapper className="flex flex-col px-3"> <StyledWrapper className="flex flex-col">
<div className="pb-2 font-medium test-summary"> <div className="pb-2 font-medium test-summary">
Tests ({results.length}/{results.length}), Passed: {passedTests.length}, Failed: {failedTests.length} Tests ({results.length}/{results.length}), Passed: {passedTests.length}, Failed: {failedTests.length}
</div> </div>

View File

@ -20,7 +20,7 @@ const Timeline = ({ request, response }) => {
let requestData = safeStringifyJSON(request.data); let requestData = safeStringifyJSON(request.data);
return ( return (
<StyledWrapper className="px-3 pb-4 w-full"> <StyledWrapper className="pb-4 w-full">
<div> <div>
<pre className="line request font-bold"> <pre className="line request font-bold">
<span className="arrow">{'>'}</span> {request.method} {request.url} <span className="arrow">{'>'}</span> {request.method} {request.url}

View File

@ -94,7 +94,7 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
return ( return (
<StyledWrapper className="flex flex-col h-full relative"> <StyledWrapper className="flex flex-col h-full relative">
<div className="flex flex-wrap items-center px-3 tabs" role="tablist"> <div className="flex flex-wrap items-center pl-3 pr-4 tabs" role="tablist">
<div className={getTabClassname('response')} role="tab" onClick={() => selectTab('response')}> <div className={getTabClassname('response')} role="tab" onClick={() => selectTab('response')}>
Response Response
</div> </div>
@ -115,7 +115,9 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
</div> </div>
) : null} ) : null}
</div> </div>
<section className={`flex flex-grow ${focusedTab.responsePaneTab === 'response' ? '' : 'mt-4'}`}> <section
className={`flex flex-grow relative pl-3 pr-4 ${focusedTab.responsePaneTab === 'response' ? '' : 'mt-4'}`}
>
{isLoading ? <Overlay item={item} collection={collection} /> : null} {isLoading ? <Overlay item={item} collection={collection} /> : null}
{getTabPanel(focusedTab.responsePaneTab)} {getTabPanel(focusedTab.responsePaneTab)}
</section> </section>

View File

@ -88,14 +88,18 @@ const CollectionItem = ({ item, collection, searchText }) => {
}); });
const handleClick = (event) => { const handleClick = (event) => {
switch (event.button) {
case 0: // left click
if (isItemARequest(item)) { if (isItemARequest(item)) {
dispatch(hideHomePage());
if (itemIsOpenedInTabs(item, tabs)) { if (itemIsOpenedInTabs(item, tabs)) {
dispatch( dispatch(
focusTab({ focusTab({
uid: item.uid uid: item.uid
}) })
); );
} else { return;
}
dispatch( dispatch(
addTab({ addTab({
uid: item.uid, uid: item.uid,
@ -103,15 +107,25 @@ const CollectionItem = ({ item, collection, searchText }) => {
requestPaneTab: getDefaultRequestPaneTab(item) requestPaneTab: getDefaultRequestPaneTab(item)
}) })
); );
return;
} }
dispatch(hideHomePage());
} else {
dispatch( dispatch(
collectionFolderClicked({ collectionFolderClicked({
itemUid: item.uid, itemUid: item.uid,
collectionUid: collection.uid collectionUid: collection.uid
}) })
); );
return;
case 2: // right click
const _menuDropdown = dropdownTippyRef.current;
if (_menuDropdown) {
let menuDropdownBehavior = 'show';
if (_menuDropdown.state.isShown) {
menuDropdownBehavior = 'hide';
}
_menuDropdown[menuDropdownBehavior]();
}
return;
} }
}; };
@ -189,7 +203,7 @@ const CollectionItem = ({ item, collection, searchText }) => {
? indents.map((i) => { ? indents.map((i) => {
return ( return (
<div <div
onClick={handleClick} onMouseUp={handleClick}
onDoubleClick={handleDoubleClick} onDoubleClick={handleDoubleClick}
className="indent-block" className="indent-block"
key={i} key={i}
@ -205,7 +219,7 @@ const CollectionItem = ({ item, collection, searchText }) => {
}) })
: null} : null}
<div <div
onClick={handleClick} onMouseUp={handleClick}
onDoubleClick={handleDoubleClick} onDoubleClick={handleDoubleClick}
className="flex flex-grow items-center h-full overflow-hidden" className="flex flex-grow items-center h-full overflow-hidden"
style={{ style={{

View File

@ -0,0 +1,36 @@
import React from 'react';
import exportBrunoCollection from 'utils/collections/export';
import exportPostmanCollection from 'utils/exporters/postman-collection';
import { toastError } from 'utils/common/error';
import cloneDeep from 'lodash/cloneDeep';
import Modal from 'components/Modal';
import { transformCollectionToSaveToExportAsFile } from 'utils/collections/index';
const ExportCollection = ({ onClose, collection }) => {
const handleExportBrunoCollection = () => {
const collectionCopy = cloneDeep(collection);
exportBrunoCollection(transformCollectionToSaveToExportAsFile(collectionCopy));
onClose();
};
const handleExportPostmanCollection = () => {
const collectionCopy = cloneDeep(collection);
exportPostmanCollection(collectionCopy);
onClose();
};
return (
<Modal size="sm" title="Export Collection" hideFooter={true} handleConfirm={onClose} handleCancel={onClose}>
<div>
<div className="text-link hover:underline cursor-pointer" onClick={handleExportBrunoCollection}>
Bruno Collection
</div>
<div className="text-link hover:underline cursor-pointer mt-2" onClick={handleExportPostmanCollection}>
Postman Collection
</div>
</div>
</Modal>
);
};
export default ExportCollection;

View File

@ -14,6 +14,7 @@ import NewRequest from 'components/Sidebar/NewRequest';
import NewFolder from 'components/Sidebar/NewFolder'; import NewFolder from 'components/Sidebar/NewFolder';
import CollectionItem from './CollectionItem'; import CollectionItem from './CollectionItem';
import RemoveCollection from './RemoveCollection'; import RemoveCollection from './RemoveCollection';
import ExportCollection from './ExportCollection';
import CollectionProperties from './CollectionProperties'; import CollectionProperties from './CollectionProperties';
import { doesCollectionHaveItemsMatchingSearchText } from 'utils/collections/search'; import { doesCollectionHaveItemsMatchingSearchText } from 'utils/collections/search';
import { isItemAFolder, isItemARequest, transformCollectionToSaveToExportAsFile } from 'utils/collections'; import { isItemAFolder, isItemARequest, transformCollectionToSaveToExportAsFile } from 'utils/collections';
@ -26,6 +27,7 @@ const Collection = ({ collection, searchText }) => {
const [showNewFolderModal, setShowNewFolderModal] = useState(false); const [showNewFolderModal, setShowNewFolderModal] = useState(false);
const [showNewRequestModal, setShowNewRequestModal] = useState(false); const [showNewRequestModal, setShowNewRequestModal] = useState(false);
const [showRenameCollectionModal, setShowRenameCollectionModal] = useState(false); const [showRenameCollectionModal, setShowRenameCollectionModal] = useState(false);
const [showExportCollectionModal, setShowExportCollectionModal] = useState(false);
const [showRemoveCollectionModal, setShowRemoveCollectionModal] = useState(false); const [showRemoveCollectionModal, setShowRemoveCollectionModal] = useState(false);
const [collectionPropertiesModal, setCollectionPropertiesModal] = useState(false); const [collectionPropertiesModal, setCollectionPropertiesModal] = useState(false);
const [collectionIsCollapsed, setCollectionIsCollapsed] = useState(collection.collapsed); const [collectionIsCollapsed, setCollectionIsCollapsed] = useState(collection.collapsed);
@ -64,7 +66,21 @@ const Collection = ({ collection, searchText }) => {
}); });
const handleClick = (event) => { const handleClick = (event) => {
const _menuDropdown = menuDropdownTippyRef.current;
switch (event.button) {
case 0: // left click
dispatch(collectionClicked(collection.uid)); dispatch(collectionClicked(collection.uid));
return;
case 2: // right click
if (_menuDropdown) {
let menuDropdownBehavior = 'show';
if (_menuDropdown.state.isShown) {
menuDropdownBehavior = 'hide';
}
_menuDropdown[menuDropdownBehavior]();
}
return;
}
}; };
const handleExportClick = () => { const handleExportClick = () => {
@ -115,11 +131,14 @@ const Collection = ({ collection, searchText }) => {
{showRemoveCollectionModal && ( {showRemoveCollectionModal && (
<RemoveCollection collection={collection} onClose={() => setShowRemoveCollectionModal(false)} /> <RemoveCollection collection={collection} onClose={() => setShowRemoveCollectionModal(false)} />
)} )}
{showExportCollectionModal && (
<ExportCollection collection={collection} onClose={() => setShowExportCollectionModal(false)} />
)}
{collectionPropertiesModal && ( {collectionPropertiesModal && (
<CollectionProperties collection={collection} onClose={() => setCollectionPropertiesModal(false)} /> <CollectionProperties collection={collection} onClose={() => setCollectionPropertiesModal(false)} />
)} )}
<div className="flex py-1 collection-name items-center" ref={drop}> <div className="flex py-1 collection-name items-center" ref={drop}>
<div className="flex flex-grow items-center overflow-hidden" onClick={handleClick}> <div className="flex flex-grow items-center overflow-hidden" onMouseUp={handleClick}>
<IconChevronRight <IconChevronRight
size={16} size={16}
strokeWidth={2} strokeWidth={2}
@ -172,7 +191,7 @@ const Collection = ({ collection, searchText }) => {
className="dropdown-item" className="dropdown-item"
onClick={(e) => { onClick={(e) => {
menuDropdownTippyRef.current.hide(); menuDropdownTippyRef.current.hide();
handleExportClick(true); setShowExportCollectionModal(true);
}} }}
> >
Export Export

View File

@ -16,6 +16,7 @@ const NewFolder = ({ collection, item, onClose }) => {
}, },
validationSchema: Yup.object({ validationSchema: Yup.object({
folderName: Yup.string() folderName: Yup.string()
.trim()
.min(1, 'must be at least 1 character') .min(1, 'must be at least 1 character')
.required('name is required') .required('name is required')
.test({ .test({
@ -32,7 +33,7 @@ const NewFolder = ({ collection, item, onClose }) => {
onSubmit: (values) => { onSubmit: (values) => {
dispatch(newFolder(values.folderName, collection.uid, item ? item.uid : null)) dispatch(newFolder(values.folderName, collection.uid, item ? item.uid : null))
.then(() => onClose()) .then(() => onClose())
.catch((err) => toast.error(err ? err.message : 'An error occurred while adding the request')); .catch((err) => toast.error(err ? err.message : 'An error occurred while adding the folder'));
} }
}); });

View File

@ -25,12 +25,16 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => {
}, },
validationSchema: Yup.object({ validationSchema: Yup.object({
requestName: Yup.string() requestName: Yup.string()
.trim()
.min(1, 'must be at least 1 character') .min(1, 'must be at least 1 character')
.required('name is required') .required('name is required')
.test({ .test({
name: 'requestName', name: 'requestName',
message: 'The request name "index" is reserved in bruno', message: `The request names - collection and folder is reserved in bruno`,
test: (value) => value && !value.trim().toLowerCase().includes('index') test: (value) => {
const trimmedValue = value ? value.trim().toLowerCase() : '';
return !['collection', 'folder'].includes(trimmedValue);
}
}) })
}), }),
onSubmit: (values) => { onSubmit: (values) => {

View File

@ -105,7 +105,7 @@ const Sidebar = () => {
Star Star
</GitHubButton> </GitHubButton>
</div> </div>
<div className="flex flex-grow items-center justify-end text-xs mr-2">v0.21.1</div> <div className="flex flex-grow items-center justify-end text-xs mr-2">v0.23.0</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -9,29 +9,32 @@ import StyledWrapper from './StyledWrapper';
import 'codemirror/theme/material.css'; import 'codemirror/theme/material.css';
import 'codemirror/theme/monokai.css'; import 'codemirror/theme/monokai.css';
import 'codemirror/addon/scroll/simplescrollbars.css'; import 'codemirror/addon/scroll/simplescrollbars.css';
import Documentation from 'components/Documentation';
const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true; const SERVER_RENDERED = typeof navigator === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
if (!SERVER_RENDERED) { if (!SERVER_RENDERED) {
require('codemirror/mode/javascript/javascript'); require('codemirror/mode/javascript/javascript');
require('codemirror/mode/xml/xml'); require('codemirror/mode/xml/xml');
require('codemirror/addon/scroll/simplescrollbars'); require('codemirror/mode/sparql/sparql');
require('codemirror/addon/comment/comment');
require('codemirror/addon/dialog/dialog');
require('codemirror/addon/edit/closebrackets');
require('codemirror/addon/edit/matchbrackets'); require('codemirror/addon/edit/matchbrackets');
require('codemirror/addon/fold/brace-fold'); require('codemirror/addon/fold/brace-fold');
require('codemirror/addon/fold/foldgutter'); require('codemirror/addon/fold/foldgutter');
require('codemirror/addon/mode/overlay');
require('codemirror/addon/hint/show-hint'); require('codemirror/addon/hint/show-hint');
require('codemirror/keymap/sublime'); require('codemirror/addon/lint/lint');
require('codemirror/addon/comment/comment'); require('codemirror/addon/mode/overlay');
require('codemirror/addon/edit/closebrackets'); require('codemirror/addon/scroll/simplescrollbars');
require('codemirror/addon/search/jump-to-line');
require('codemirror/addon/search/search'); require('codemirror/addon/search/search');
require('codemirror/addon/search/searchcursor'); require('codemirror/addon/search/searchcursor');
require('codemirror/addon/search/jump-to-line'); require('codemirror/keymap/sublime');
require('codemirror/addon/dialog/dialog');
require('codemirror-graphql/hint'); require('codemirror-graphql/hint');
require('codemirror-graphql/lint');
require('codemirror-graphql/info'); require('codemirror-graphql/info');
require('codemirror-graphql/jump'); require('codemirror-graphql/jump');
require('codemirror-graphql/lint');
require('codemirror-graphql/mode'); require('codemirror-graphql/mode');
require('utils/codemirror/brunoVarInfo'); require('utils/codemirror/brunoVarInfo');

View File

@ -51,7 +51,8 @@ export const HotkeysProvider = (props) => {
if (item && item.uid) { if (item && item.uid) {
dispatch(saveRequest(activeTab.uid, activeTab.collectionUid)); dispatch(saveRequest(activeTab.uid, activeTab.collectionUid));
} else { } else {
setShowSaveRequestModal(true); // todo: when ephermal requests go live
// setShowSaveRequestModal(true);
} }
} }
} }

View File

@ -45,6 +45,8 @@ import {
import { closeAllCollectionTabs } from 'providers/ReduxStore/slices/tabs'; import { closeAllCollectionTabs } from 'providers/ReduxStore/slices/tabs';
import { resolveRequestFilename } from 'utils/common/platform'; import { resolveRequestFilename } from 'utils/common/platform';
import { parseQueryParams, splitOnFirst } from 'utils/url/index';
import { each } from 'lodash';
const PATH_SEPARATOR = path.sep; const PATH_SEPARATOR = path.sep;
@ -91,6 +93,29 @@ export const saveRequest = (itemUid, collectionUid) => (dispatch, getState) => {
}); });
}; };
export const saveCollectionRoot = (collectionUid) => (dispatch, getState) => {
const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid);
console.log(collection.root);
return new Promise((resolve, reject) => {
if (!collection) {
return reject(new Error('Collection not found'));
}
const { ipcRenderer } = window;
ipcRenderer
.invoke('renderer:save-collection-root', collection.pathname, collection.root)
.then(() => toast.success('Collection Settings saved successfully'))
.then(resolve)
.catch((err) => {
toast.error('Failed to save collection settings!');
reject(err);
});
});
};
export const sendRequest = (item, collectionUid) => (dispatch, getState) => { export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
const state = getState(); const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid); const collection = findCollectionByUid(state.collections.collections, collectionUid);
@ -565,6 +590,12 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
return reject(new Error('Collection not found')); return reject(new Error('Collection not found'));
} }
const parts = splitOnFirst(requestUrl, '?');
const params = parseQueryParams(parts[1]);
each(params, (urlParam) => {
urlParam.enabled = true;
});
const collectionCopy = cloneDeep(collection); const collectionCopy = cloneDeep(collection);
const item = { const item = {
uid: uuid(), uid: uuid(),
@ -574,11 +605,13 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
method: requestMethod, method: requestMethod,
url: requestUrl, url: requestUrl,
headers: [], headers: [],
params,
body: { body: {
mode: 'none', mode: 'none',
json: null, json: null,
text: null, text: null,
xml: null, xml: null,
sparql: null,
multipartForm: null, multipartForm: null,
formUrlEncoded: null formUrlEncoded: null
} }

View File

@ -7,6 +7,8 @@ import concat from 'lodash/concat';
import filter from 'lodash/filter'; import filter from 'lodash/filter';
import each from 'lodash/each'; import each from 'lodash/each';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import get from 'lodash/get';
import set from 'lodash/set';
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import { splitOnFirst } from 'utils/url'; import { splitOnFirst } from 'utils/url';
import { import {
@ -40,6 +42,8 @@ export const collectionsSlice = createSlice({
const collectionUids = map(state.collections, (c) => c.uid); const collectionUids = map(state.collections, (c) => c.uid);
const collection = action.payload; const collection = action.payload;
collection.settingsSelectedTab = 'headers';
// TODO: move this to use the nextAction approach // TODO: move this to use the nextAction approach
// last action is used to track the last action performed on the collection // last action is used to track the last action performed on the collection
// this is optional // this is optional
@ -107,6 +111,15 @@ export const collectionsSlice = createSlice({
collection.nextAction = nextAction; collection.nextAction = nextAction;
} }
}, },
updateSettingsSelectedTab: (state, action) => {
const { collectionUid, tab } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
collection.settingsSelectedTab = tab;
}
},
collectionUnlinkEnvFileEvent: (state, action) => { collectionUnlinkEnvFileEvent: (state, action) => {
const { data: environment, meta } = action.payload; const { data: environment, meta } = action.payload;
const collection = findCollectionByUid(state.collections, meta.collectionUid); const collection = findCollectionByUid(state.collections, meta.collectionUid);
@ -273,6 +286,12 @@ export const collectionsSlice = createSlice({
const collection = findCollectionByUid(state.collections, action.payload.collectionUid); const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection && collection.items && collection.items.length) { if (collection && collection.items && collection.items.length) {
const parts = splitOnFirst(action.payload.requestUrl, '?');
const params = parseQueryParams(parts[1]);
each(params, (urlParam) => {
urlParam.enabled = true;
});
const item = { const item = {
uid: action.payload.uid, uid: action.payload.uid,
name: action.payload.requestName, name: action.payload.requestName,
@ -280,7 +299,7 @@ export const collectionsSlice = createSlice({
request: { request: {
url: action.payload.requestUrl, url: action.payload.requestUrl,
method: action.payload.requestMethod, method: action.payload.requestMethod,
params: [], params,
headers: [], headers: [],
body: { body: {
mode: null, mode: null,
@ -360,6 +379,10 @@ export const collectionsSlice = createSlice({
item.draft.request.auth = item.draft.request.auth || {}; item.draft.request.auth = item.draft.request.auth || {};
switch (action.payload.mode) { switch (action.payload.mode) {
case 'awsv4':
item.draft.request.auth.mode = 'awsv4';
item.draft.request.auth.awsv4 = action.payload.content;
break;
case 'bearer': case 'bearer':
item.draft.request.auth.mode = 'bearer'; item.draft.request.auth.mode = 'bearer';
item.draft.request.auth.bearer = action.payload.content; item.draft.request.auth.bearer = action.payload.content;
@ -679,6 +702,10 @@ export const collectionsSlice = createSlice({
item.draft.request.body.xml = action.payload.content; item.draft.request.body.xml = action.payload.content;
break; break;
} }
case 'sparql': {
item.draft.request.body.sparql = action.payload.content;
break;
}
case 'formUrlEncoded': { case 'formUrlEncoded': {
item.draft.request.body.formUrlEncoded = action.payload.content; item.draft.request.body.formUrlEncoded = action.payload.content;
break; break;
@ -930,10 +957,104 @@ export const collectionsSlice = createSlice({
} }
} }
}, },
updateCollectionAuthMode: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
set(collection, 'root.request.auth.mode', action.payload.mode);
}
},
updateCollectionAuth: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
switch (action.payload.mode) {
case 'awsv4':
set(collection, 'root.request.auth.awsv4', action.payload.content);
console.log('set auth awsv4', action.payload.content);
break;
case 'bearer':
set(collection, 'root.request.auth.bearer', action.payload.content);
break;
case 'basic':
set(collection, 'root.request.auth.basic', action.payload.content);
break;
}
}
},
updateCollectionRequestScript: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
set(collection, 'root.request.script.req', action.payload.script);
}
},
updateCollectionResponseScript: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
set(collection, 'root.request.script.res', action.payload.script);
}
},
updateCollectionTests: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
set(collection, 'root.request.tests', action.payload.tests);
}
},
addCollectionHeader: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
const headers = get(collection, 'root.request.headers', []);
headers.push({
uid: uuid(),
name: '',
value: '',
description: '',
enabled: true
});
set(collection, 'root.request.headers', headers);
}
},
updateCollectionHeader: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
const headers = get(collection, 'root.request.headers', []);
const header = find(headers, (h) => h.uid === action.payload.header.uid);
if (header) {
header.name = action.payload.header.name;
header.value = action.payload.header.value;
header.description = action.payload.header.description;
header.enabled = action.payload.header.enabled;
}
}
},
deleteCollectionHeader: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
let headers = get(collection, 'root.request.headers', []);
headers = filter(headers, (h) => h.uid !== action.payload.headerUid);
set(collection, 'root.request.headers', headers);
}
},
collectionAddFileEvent: (state, action) => { collectionAddFileEvent: (state, action) => {
const file = action.payload.file; const file = action.payload.file;
const isCollectionRoot = file.meta.collectionRoot ? true : false;
const collection = findCollectionByUid(state.collections, file.meta.collectionUid); const collection = findCollectionByUid(state.collections, file.meta.collectionUid);
if (isCollectionRoot) {
if (collection) {
collection.root = file.data;
}
console.log('collectionAddFileEvent', file);
return;
}
if (collection) { if (collection) {
const dirname = getDirectoryName(file.meta.pathname); const dirname = getDirectoryName(file.meta.pathname);
const subDirectories = getSubdirectoriesFromRoot(collection.pathname, dirname); const subDirectories = getSubdirectoriesFromRoot(collection.pathname, dirname);
@ -1018,6 +1139,12 @@ export const collectionsSlice = createSlice({
const { file } = action.payload; const { file } = action.payload;
const collection = findCollectionByUid(state.collections, file.meta.collectionUid); const collection = findCollectionByUid(state.collections, file.meta.collectionUid);
// check and update collection root
if (collection && file.meta.collectionRoot) {
collection.root = file.data;
return;
}
if (collection) { if (collection) {
const item = findItemInCollection(collection, file.data.uid); const item = findItemInCollection(collection, file.data.uid);
@ -1210,6 +1337,20 @@ export const collectionsSlice = createSlice({
if (collection) { if (collection) {
collection.runnerResult = null; collection.runnerResult = null;
} }
},
updateRequestDocs: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
const item = findItemInCollection(collection, action.payload.itemUid);
if (item && isItemARequest(item)) {
if (!item.draft) {
item.draft = cloneDeep(item);
}
item.draft.request.docs = action.payload.docs;
}
}
} }
} }
}); });
@ -1222,6 +1363,7 @@ export const {
sortCollections, sortCollections,
updateLastAction, updateLastAction,
updateNextAction, updateNextAction,
updateSettingsSelectedTab,
collectionUnlinkEnvFileEvent, collectionUnlinkEnvFileEvent,
saveEnvironment, saveEnvironment,
selectEnvironment, selectEnvironment,
@ -1267,6 +1409,14 @@ export const {
addVar, addVar,
updateVar, updateVar,
deleteVar, deleteVar,
addCollectionHeader,
updateCollectionHeader,
deleteCollectionHeader,
updateCollectionAuthMode,
updateCollectionAuth,
updateCollectionRequestScript,
updateCollectionResponseScript,
updateCollectionTests,
collectionAddFileEvent, collectionAddFileEvent,
collectionAddDirectoryEvent, collectionAddDirectoryEvent,
collectionChangeFileEvent, collectionChangeFileEvent,
@ -1277,7 +1427,8 @@ export const {
resetRunResults, resetRunResults,
runRequestEvent, runRequestEvent,
runFolderEvent, runFolderEvent,
resetCollectionRunner resetCollectionRunner,
updateRequestDocs
} = collectionsSlice.actions; } = collectionsSlice.actions;
export default collectionsSlice.reducer; export default collectionsSlice.reducer;

View File

@ -54,8 +54,11 @@ body::-webkit-scrollbar-thumb,
border-radius: 5rem; border-radius: 5rem;
} }
/* making all the checkboxes and radios bigger */ /*
input[type='checkbox'], * todo: this will be supported in the future to be changed via applying a theme
input[type='radio'] { * making all the checkboxes and radios bigger
transform: scale(1.25); * input[type='checkbox'],
} * input[type='radio'] {
* transform: scale(1.1);
* }
*/

View File

@ -2,7 +2,7 @@ import * as FileSaver from 'file-saver';
import get from 'lodash/get'; import get from 'lodash/get';
import each from 'lodash/each'; import each from 'lodash/each';
const deleteUidsInItems = (items) => { export const deleteUidsInItems = (items) => {
each(items, (item) => { each(items, (item) => {
delete item.uid; delete item.uid;
@ -26,7 +26,7 @@ const deleteUidsInItems = (items) => {
* Some of the models in the app are not consistent with the Collection Json format * Some of the models in the app are not consistent with the Collection Json format
* This function is used to transform the models to the Collection Json format * This function is used to transform the models to the Collection Json format
*/ */
const transformItem = (items = []) => { export const transformItem = (items = []) => {
each(items, (item) => { each(items, (item) => {
if (['http-request', 'graphql-request'].includes(item.type)) { if (['http-request', 'graphql-request'].includes(item.type)) {
item.request.query = item.request.params; item.request.query = item.request.params;
@ -47,14 +47,14 @@ const transformItem = (items = []) => {
}); });
}; };
const deleteUidsInEnvs = (envs) => { export const deleteUidsInEnvs = (envs) => {
each(envs, (env) => { each(envs, (env) => {
delete env.uid; delete env.uid;
each(env.variables, (variable) => delete variable.uid); each(env.variables, (variable) => delete variable.uid);
}); });
}; };
const deleteSecretsInEnvs = (envs) => { export const deleteSecretsInEnvs = (envs) => {
each(envs, (env) => { each(envs, (env) => {
each(env.variables, (variable) => { each(env.variables, (variable) => {
if (variable.secret) { if (variable.secret) {
@ -64,9 +64,13 @@ const deleteSecretsInEnvs = (envs) => {
}); });
}; };
const exportCollection = (collection) => { export const exportCollection = (collection) => {
// delete uids // delete uids
delete collection.uid; delete collection.uid;
// delete process variables
delete collection.processEnvVariables;
deleteUidsInItems(collection.items); deleteUidsInItems(collection.items);
deleteUidsInEnvs(collection.environments); deleteUidsInEnvs(collection.environments);
deleteSecretsInEnvs(collection.environments); deleteSecretsInEnvs(collection.environments);

View File

@ -284,6 +284,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
text: si.draft.request.body.text, text: si.draft.request.body.text,
xml: si.draft.request.body.xml, xml: si.draft.request.body.xml,
graphql: si.draft.request.body.graphql, graphql: si.draft.request.body.graphql,
sparql: si.draft.request.body.sparql,
formUrlEncoded: copyFormUrlEncodedParams(si.draft.request.body.formUrlEncoded), formUrlEncoded: copyFormUrlEncodedParams(si.draft.request.body.formUrlEncoded),
multipartForm: copyMultipartFormParams(si.draft.request.body.multipartForm) multipartForm: copyMultipartFormParams(si.draft.request.body.multipartForm)
}, },
@ -316,6 +317,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
text: si.request.body.text, text: si.request.body.text,
xml: si.request.body.xml, xml: si.request.body.xml,
graphql: si.request.body.graphql, graphql: si.request.body.graphql,
sparql: si.request.body.sparql,
formUrlEncoded: copyFormUrlEncodedParams(si.request.body.formUrlEncoded), formUrlEncoded: copyFormUrlEncodedParams(si.request.body.formUrlEncoded),
multipartForm: copyMultipartFormParams(si.request.body.multipartForm) multipartForm: copyMultipartFormParams(si.request.body.multipartForm)
}, },
@ -382,7 +384,8 @@ export const transformRequestToSaveToFilesystem = (item) => {
script: _item.request.script, script: _item.request.script,
vars: _item.request.vars, vars: _item.request.vars,
assertions: _item.request.assertions, assertions: _item.request.assertions,
tests: _item.request.tests tests: _item.request.tests,
docs: _item.request.docs
} }
}; };
@ -459,6 +462,10 @@ export const humanizeRequestBodyMode = (mode) => {
label = 'XML'; label = 'XML';
break; break;
} }
case 'sparql': {
label = 'SPARQL';
break;
}
case 'formUrlEncoded': { case 'formUrlEncoded': {
label = 'Form URL Encoded'; label = 'Form URL Encoded';
break; break;
@ -475,6 +482,10 @@ export const humanizeRequestBodyMode = (mode) => {
export const humanizeRequestAuthMode = (mode) => { export const humanizeRequestAuthMode = (mode) => {
let label = 'No Auth'; let label = 'No Auth';
switch (mode) { switch (mode) {
case 'awsv4': {
label = 'AWS Sig V4';
break;
}
case 'basic': { case 'basic': {
label = 'Basic Auth'; label = 'Basic Auth';
break; break;

View File

@ -60,6 +60,8 @@ export const getCodeMirrorModeBasedOnContentType = (contentType) => {
return 'application/xml'; return 'application/xml';
} else if (contentType.includes('yaml')) { } else if (contentType.includes('yaml')) {
return 'application/yaml'; return 'application/yaml';
} else if (contentType.includes('image')) {
return 'application/image';
} else { } else {
return 'application/text'; return 'application/text';
} }

View File

@ -0,0 +1,215 @@
import map from 'lodash/map';
import * as FileSaver from 'file-saver';
import { deleteSecretsInEnvs, deleteUidsInEnvs, deleteUidsInItems } from 'utils/collections/export';
export const exportCollection = (collection) => {
delete collection.uid;
delete collection.processEnvVariables;
deleteUidsInItems(collection.items);
deleteUidsInEnvs(collection.environments);
deleteSecretsInEnvs(collection.environments);
const generateInfoSection = () => {
return {
name: collection.name,
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
};
};
const generateCollectionVars = (collection) => {
const pattern = /{{[^{}]+}}/g;
let listOfVars = [];
const findOccurrences = (obj, results) => {
if (typeof obj === 'object') {
if (Array.isArray(obj)) {
obj.forEach((item) => findOccurrences(item, results));
} else {
for (const key in obj) {
findOccurrences(obj[key], results);
}
}
} else if (typeof obj === 'string') {
obj.replace(pattern, (match) => {
results.push(match.replace(/{{|}}/g, ''));
});
}
};
findOccurrences(collection, listOfVars);
const finalArrayOfVars = [...new Set(listOfVars)];
return finalArrayOfVars.map((variable) => ({
key: variable,
value: '',
type: 'default'
}));
};
const generateEventSection = (item) => {
const eventArray = [];
if (item.request.tests.length) {
eventArray.push({
listen: 'test',
script: {
exec: item.request.tests.split('\n')
// type: 'text/javascript'
}
});
}
if (item.request.script.req) {
eventArray.push({
listen: 'prerequest',
script: {
exec: item.request.script.req.split('\n')
// type: 'text/javascript'
}
});
}
return eventArray;
};
const generateHeaders = (headersArray) => {
return map(headersArray, (item) => {
return {
key: item.name,
value: item.value,
disabled: !item.enabled,
type: 'default'
};
});
};
const generateBody = (body) => {
switch (body.mode) {
case 'formUrlEncoded':
return {
mode: 'urlencoded',
urlencoded: map(body.formUrlEncoded, (bodyItem) => {
return {
key: bodyItem.name,
value: bodyItem.value,
disabled: !bodyItem.enabled,
type: 'default'
};
})
};
case 'multipartForm':
return {
mode: 'formdata',
formdata: map(body.multipartForm, (bodyItem) => {
return {
key: bodyItem.name,
value: bodyItem.value,
disabled: !bodyItem.enabled,
type: 'default'
};
})
};
case 'json':
return {
mode: 'raw',
raw: body.json,
options: {
raw: {
language: 'json'
}
}
};
case 'xml':
return {
mode: 'raw',
raw: body.xml,
options: {
raw: {
language: 'xml'
}
}
};
case 'text':
return {
mode: 'raw',
raw: body.text,
options: {
raw: {
language: 'text'
}
}
};
}
};
const generateAuth = (itemAuth) => {
switch (itemAuth) {
case 'bearer':
return {
type: 'bearer',
bearer: {
key: 'token',
value: itemAuth.bearer.token,
type: 'string'
}
};
case 'basic': {
return {
type: 'basic',
basic: [
{
key: 'password',
value: itemAuth.basic.password,
type: 'string'
},
{
key: 'username',
value: itemAuth.basic.username,
type: 'string'
}
]
};
}
}
};
const generateRequestSection = (itemRequest) => {
const requestObject = {
method: itemRequest.method,
header: generateHeaders(itemRequest.headers),
url: itemRequest.url,
auth: generateAuth(itemRequest.auth)
};
if (itemRequest.body.mode != 'none') {
requestObject.body = generateBody(itemRequest.body);
}
return requestObject;
};
const generateItemSection = (itemsArray) => {
return map(itemsArray, (item) => {
if (item.type === 'folder') {
return {
name: item.name,
item: item.items.length ? generateItemSection(item.items) : []
};
} else {
return {
name: item.name,
event: generateEventSection(item),
request: generateRequestSection(item.request)
};
}
});
};
const collectionToExport = {};
collectionToExport.info = generateInfoSection();
collectionToExport.item = generateItemSection(collection.items);
collectionToExport.variable = generateCollectionVars(collection);
const fileName = `${collection.name}.json`;
const fileBlob = new Blob([JSON.stringify(collectionToExport, null, 2)], { type: 'application/json' });
FileSaver.saveAs(fileBlob, fileName);
};
export default exportCollection;

View File

@ -1,3 +1,5 @@
import { safeStringifyJSON } from 'utils/common';
export const sendNetworkRequest = async (item, collection, environment, collectionVariables) => { export const sendNetworkRequest = async (item, collection, environment, collectionVariables) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (['http-request', 'graphql-request'].includes(item.type)) { if (['http-request', 'graphql-request'].includes(item.type)) {
@ -7,7 +9,7 @@ export const sendNetworkRequest = async (item, collection, environment, collecti
state: 'success', state: 'success',
data: response.data, data: response.data,
headers: Object.entries(response.headers), headers: Object.entries(response.headers),
size: response.headers['content-length'] || 0, size: getResponseSize(response),
status: response.status, status: response.status,
statusText: response.statusText, statusText: response.statusText,
duration: response.duration duration: response.duration
@ -23,12 +25,16 @@ const sendHttpRequest = async (item, collection, environment, collectionVariable
const { ipcRenderer } = window; const { ipcRenderer } = window;
ipcRenderer ipcRenderer
.invoke('send-http-request', item, collection.uid, collection.pathname, environment, collectionVariables) .invoke('send-http-request', item, collection, environment, collectionVariables)
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
}); });
}; };
const getResponseSize = (response) => {
return response.headers['content-length'] || Buffer.byteLength(safeStringifyJSON(response.data)) || 0;
};
export const fetchGqlSchema = async (endpoint, environment, request, collection) => { export const fetchGqlSchema = async (endpoint, environment, request, collection) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { ipcRenderer } = window; const { ipcRenderer } = window;

View File

@ -1,6 +1,6 @@
{ {
"name": "@usebruno/cli", "name": "@usebruno/cli",
"version": "0.13.0", "version": "0.14.0",
"license": "MIT", "license": "MIT",
"main": "src/index.js", "main": "src/index.js",
"bin": { "bin": {
@ -25,7 +25,7 @@
], ],
"dependencies": { "dependencies": {
"@usebruno/js": "0.8.0", "@usebruno/js": "0.8.0",
"@usebruno/lang": "0.5.0", "@usebruno/lang": "0.8.0",
"axios": "^1.5.1", "axios": "^1.5.1",
"chai": "^4.3.7", "chai": "^4.3.7",
"chalk": "^3.0.0", "chalk": "^3.0.0",
@ -39,6 +39,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0", "mustache": "^4.2.0",
"qs": "^6.11.0", "qs": "^6.11.0",
"socks-proxy-agent": "^8.0.2",
"yargs": "^17.6.2" "yargs": "^17.6.2"
} }
} }

View File

@ -6,7 +6,7 @@ const { exists, isFile, isDirectory } = require('../utils/filesystem');
const { runSingleRequest } = require('../runner/run-single-request'); const { runSingleRequest } = require('../runner/run-single-request');
const { bruToEnvJson, getEnvVars } = require('../utils/bru'); const { bruToEnvJson, getEnvVars } = require('../utils/bru');
const { rpad } = require('../utils/common'); const { rpad } = require('../utils/common');
const { bruToJson, getOptions } = require('../utils/bru'); const { bruToJson, getOptions, collectionBruToJson } = require('../utils/bru');
const { dotenvToJson } = require('@usebruno/lang'); const { dotenvToJson } = require('@usebruno/lang');
const command = 'run [filename]'; const command = 'run [filename]';
@ -121,6 +121,9 @@ const getBruFilesRecursively = (dir) => {
const currentDirBruJsons = []; const currentDirBruJsons = [];
for (const file of filesInCurrentDir) { for (const file of filesInCurrentDir) {
if (['collection.bru', 'folder.bru'].includes(file)) {
continue;
}
const filePath = path.join(currentPath, file); const filePath = path.join(currentPath, file);
const stats = fs.lstatSync(filePath); const stats = fs.lstatSync(filePath);
@ -151,6 +154,19 @@ const getBruFilesRecursively = (dir) => {
return getFilesInOrder(dir); return getFilesInOrder(dir);
}; };
const getCollectionRoot = (dir) => {
const collectionRootPath = path.join(dir, 'collection.bru');
const exists = fs.existsSync(collectionRootPath);
if (!exists) {
return {};
}
const content = fs.readFileSync(collectionRootPath, 'utf8');
const json = collectionBruToJson(content);
return json;
};
const builder = async (yargs) => { const builder = async (yargs) => {
yargs yargs
.option('r', { .option('r', {
@ -210,6 +226,7 @@ const handler = async function (argv) {
const brunoConfigFile = fs.readFileSync(brunoJsonPath, 'utf8'); const brunoConfigFile = fs.readFileSync(brunoJsonPath, 'utf8');
const brunoConfig = JSON.parse(brunoConfigFile); const brunoConfig = JSON.parse(brunoConfigFile);
const collectionRoot = getCollectionRoot(collectionPath);
if (filename && filename.length) { if (filename && filename.length) {
const pathExists = await exists(filename); const pathExists = await exists(filename);
@ -349,7 +366,8 @@ const handler = async function (argv) {
collectionVariables, collectionVariables,
envVars, envVars,
processEnvVars, processEnvVars,
brunoConfig brunoConfig,
collectionRoot
); );
results.push(result); results.push(result);

View File

@ -1,9 +1,20 @@
const { get, each, filter } = require('lodash'); const { get, each, filter } = require('lodash');
const decomment = require('decomment'); const decomment = require('decomment');
const prepareRequest = (request) => { const prepareRequest = (request, collectionRoot) => {
const headers = {}; const headers = {};
let contentTypeDefined = false; let contentTypeDefined = false;
// collection headers
each(get(collectionRoot, 'request.headers', []), (h) => {
if (h.enabled) {
headers[h.name] = h.value;
if (h.name.toLowerCase() === 'content-type') {
contentTypeDefined = true;
}
}
});
each(request.headers, (h) => { each(request.headers, (h) => {
if (h.enabled) { if (h.enabled) {
headers[h.name] = h.value; headers[h.name] = h.value;
@ -20,6 +31,23 @@ const prepareRequest = (request) => {
}; };
// Authentication // Authentication
// A request can override the collection auth with another auth
// But it cannot override the collection auth with no auth
// We will provide support for disabling the auth via scripting in the future
const collectionAuth = get(collectionRoot, 'request.auth');
if (collectionAuth) {
if (collectionAuth.mode === 'basic') {
axiosRequest.auth = {
username: get(collectionAuth, 'basic.username'),
password: get(collectionAuth, 'basic.password')
};
}
if (collectionAuth.mode === 'bearer') {
axiosRequest.headers['authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`;
}
}
if (request.auth) { if (request.auth) {
if (request.auth.mode === 'basic') { if (request.auth.mode === 'basic') {
axiosRequest.auth = { axiosRequest.auth = {

View File

@ -1,8 +1,9 @@
const os = require('os');
const qs = require('qs'); const qs = require('qs');
const chalk = require('chalk'); const chalk = require('chalk');
const decomment = require('decomment'); const decomment = require('decomment');
const fs = require('fs'); const fs = require('fs');
const { forOwn, each, extend, get } = require('lodash'); const { forOwn, each, extend, get, compact } = require('lodash');
const FormData = require('form-data'); const FormData = require('form-data');
const prepareRequest = require('./prepare-request'); const prepareRequest = require('./prepare-request');
const interpolateVars = require('./interpolate-vars'); const interpolateVars = require('./interpolate-vars');
@ -13,6 +14,7 @@ const { getOptions } = require('../utils/bru');
const https = require('https'); const https = require('https');
const { HttpsProxyAgent } = require('https-proxy-agent'); const { HttpsProxyAgent } = require('https-proxy-agent');
const { HttpProxyAgent } = require('http-proxy-agent'); const { HttpProxyAgent } = require('http-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { makeAxiosInstance } = require('../utils/axios-instance'); const { makeAxiosInstance } = require('../utils/axios-instance');
const runSingleRequest = async function ( const runSingleRequest = async function (
@ -22,12 +24,13 @@ const runSingleRequest = async function (
collectionVariables, collectionVariables,
envVariables, envVariables,
processEnvVars, processEnvVars,
brunoConfig brunoConfig,
collectionRoot
) { ) {
try { try {
let request; let request;
request = prepareRequest(bruJson.request); request = prepareRequest(bruJson.request, collectionRoot);
const scriptingConfig = get(brunoConfig, 'scripts', {}); const scriptingConfig = get(brunoConfig, 'scripts', {});
@ -57,7 +60,10 @@ const runSingleRequest = async function (
} }
// run pre request script // run pre request script
const requestScriptFile = get(bruJson, 'request.script.req'); const requestScriptFile = compact([
get(collectionRoot, 'request.script.req'),
get(bruJson, 'request.script.req')
]).join(os.EOL);
if (requestScriptFile && requestScriptFile.length) { if (requestScriptFile && requestScriptFile.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runRequestScript( await scriptRuntime.runRequestScript(
@ -96,7 +102,7 @@ const runSingleRequest = async function (
// set proxy if enabled // set proxy if enabled
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false); const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) { if (proxyEnabled) {
let proxy; let proxyUri;
const interpolationOptions = { const interpolationOptions = {
envVars: envVariables, envVars: envVariables,
collectionVariables, collectionVariables,
@ -107,6 +113,7 @@ const runSingleRequest = async function (
const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions); const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions);
const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions); const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions);
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false); const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');
interpolateString; interpolateString;
@ -114,17 +121,25 @@ const runSingleRequest = async function (
const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions); const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions); const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions);
proxy = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`; proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`;
} else { } else {
proxy = `${proxyProtocol}://${proxyHostname}:${proxyPort}`; proxyUri = `${proxyProtocol}://${proxyHostname}:${proxyPort}`;
} }
if (socksEnabled) {
const socksProxyAgent = new SocksProxyAgent(proxyUri);
request.httpsAgent = socksProxyAgent;
request.httpAgent = socksProxyAgent;
} else {
request.httpsAgent = new HttpsProxyAgent( request.httpsAgent = new HttpsProxyAgent(
proxy, proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
); );
request.httpAgent = new HttpProxyAgent(proxy); request.httpAgent = new HttpProxyAgent(proxyUri);
}
} else if (Object.keys(httpsAgentRequestFields).length > 0) { } else if (Object.keys(httpsAgentRequestFields).length > 0) {
request.httpsAgent = new https.Agent({ request.httpsAgent = new https.Agent({
...httpsAgentRequestFields ...httpsAgentRequestFields
@ -198,7 +213,10 @@ const runSingleRequest = async function (
} }
// run post response script // run post response script
const responseScriptFile = get(bruJson, 'request.script.res'); const responseScriptFile = compact([
get(collectionRoot, 'request.script.res'),
get(bruJson, 'request.script.res')
]).join(os.EOL);
if (responseScriptFile && responseScriptFile.length) { if (responseScriptFile && responseScriptFile.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runResponseScript( await scriptRuntime.runResponseScript(
@ -240,7 +258,7 @@ const runSingleRequest = async function (
// run tests // run tests
let testResults = []; let testResults = [];
const testFile = get(bruJson, 'request.tests'); const testFile = compact([get(collectionRoot, 'request.tests'), get(bruJson, 'request.tests')]).join(os.EOL);
if (typeof testFile === 'string') { if (typeof testFile === 'string') {
const testRuntime = new TestRuntime(); const testRuntime = new TestRuntime();
const result = await testRuntime.runTests( const result = await testRuntime.runTests(
@ -286,6 +304,7 @@ const runSingleRequest = async function (
testResults testResults
}; };
} catch (err) { } catch (err) {
console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`));
return { return {
request: { request: {
method: null, method: null,

View File

@ -1,12 +1,33 @@
const _ = require('lodash'); const _ = require('lodash');
const Mustache = require('mustache'); const Mustache = require('mustache');
const { bruToEnvJsonV2, bruToJsonV2 } = require('@usebruno/lang'); const { bruToEnvJsonV2, bruToJsonV2, collectionBruToJson: _collectionBruToJson } = require('@usebruno/lang');
// override the default escape function to prevent escaping // override the default escape function to prevent escaping
Mustache.escape = function (value) { Mustache.escape = function (value) {
return value; return value;
}; };
const collectionBruToJson = (bru) => {
try {
const json = _collectionBruToJson(bru);
const transformedJson = {
request: {
params: _.get(json, 'query', []),
headers: _.get(json, 'headers', []),
auth: _.get(json, 'auth', {}),
script: _.get(json, 'script', {}),
vars: _.get(json, 'vars', {}),
tests: _.get(json, 'tests', '')
}
};
return transformedJson;
} catch (error) {
return Promise.reject(error);
}
};
/** /**
* The transformer function for converting a BRU file to JSON. * The transformer function for converting a BRU file to JSON.
* *
@ -91,5 +112,6 @@ module.exports = {
bruToJson, bruToJson,
bruToEnvJson, bruToEnvJson,
getEnvVars, getEnvVars,
getOptions getOptions,
collectionBruToJson
}; };

View File

@ -1,6 +1,7 @@
node_modules node_modules
web web
out out
dist
.env .env
// certs // certs

View File

@ -1,55 +1,44 @@
require('dotenv').config({ path: process.env.DOTENV_PATH }); require('dotenv').config({ path: process.env.DOTENV_PATH });
const config = { const config = {
"appId": "com.usebruno.app", appId: 'com.usebruno.app',
"productName": "Bruno", productName: 'Bruno',
"electronVersion": "21.1.1", electronVersion: '21.1.1',
"directories": { directories: {
"buildResources": "resources", buildResources: 'resources',
"output": "out" output: 'out'
}, },
"files": [ files: ['**/*'],
"**/*" afterSign: 'notarize.js',
], mac: {
"afterSign": "notarize.js", artifactName: '${name}_${version}_${arch}_${os}.${ext}',
"mac": { category: 'public.app-category.developer-tools',
"artifactName": "${name}_${version}_${arch}_${os}.${ext}", target: [
"category": "public.app-category.developer-tools",
"target": [
{ {
"target": "dmg", target: 'dmg',
"arch": [ arch: ['x64', 'arm64']
"x64",
"arm64"
]
}, },
{ {
"target": "zip", target: 'zip',
"arch": [ arch: ['x64', 'arm64']
"x64",
"arm64"
]
} }
], ],
"icon": "resources/icons/mac/icon.icns", icon: 'resources/icons/mac/icon.icns',
"hardenedRuntime": true, hardenedRuntime: true,
"identity": "Anoop MD (W7LPPWA48L)", identity: 'Anoop MD (W7LPPWA48L)',
"entitlements": "resources/entitlements.mac.plist", entitlements: 'resources/entitlements.mac.plist',
"entitlementsInherit": "resources/entitlements.mac.plist" entitlementsInherit: 'resources/entitlements.mac.plist'
}, },
"linux": { linux: {
"artifactName": "${name}_${version}_${arch}_linux.${ext}", artifactName: '${name}_${version}_${arch}_linux.${ext}',
"icon": "resources/icons/png", icon: 'resources/icons/png',
"target": [ target: ['AppImage', 'deb', 'snap', 'rpm']
"AppImage",
"deb"
]
}, },
"win": { win: {
"artifactName": "${name}_${version}_${arch}_win.${ext}", artifactName: '${name}_${version}_${arch}_win.${ext}',
"icon": "resources/icons/png", icon: 'resources/icons/png',
"certificateFile": `${process.env.WIN_CERT_FILEPATH}`, certificateFile: `${process.env.WIN_CERT_FILEPATH}`,
"certificatePassword": `${process.env.WIN_CERT_PASSWORD}`, certificatePassword: `${process.env.WIN_CERT_PASSWORD}`
} }
}; };

View File

@ -1,5 +1,5 @@
{ {
"version": "v0.21.1", "version": "v0.23.0",
"name": "bruno", "name": "bruno",
"description": "Opensource API Client for Exploring and Testing APIs", "description": "Opensource API Client for Exploring and Testing APIs",
"homepage": "https://www.usebruno.com", "homepage": "https://www.usebruno.com",
@ -10,16 +10,20 @@
"clean": "rimraf dist", "clean": "rimraf dist",
"dev": "electron .", "dev": "electron .",
"dist": "electron-builder --mac --config electron-builder-config.js", "dist": "electron-builder --mac --config electron-builder-config.js",
"dist:snap": "electron-builder --linux snap --config electron-builder-config.js",
"pack": "electron-builder --dir", "pack": "electron-builder --dir",
"test": "jest" "test": "jest"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/credential-providers": "^3.425.0",
"@usebruno/js": "0.8.0", "@usebruno/js": "0.8.0",
"@usebruno/lang": "0.5.0", "@usebruno/lang": "0.8.0",
"@usebruno/schema": "0.5.0", "@usebruno/schema": "0.5.0",
"about-window": "^1.15.2", "about-window": "^1.15.2",
"aws4-axios": "^3.3.0",
"axios": "^1.5.1", "axios": "^1.5.1",
"chai": "^4.3.7", "chai": "^4.3.7",
"chai-string": "^1.5.0",
"chokidar": "^3.5.3", "chokidar": "^3.5.3",
"decomment": "^0.9.5", "decomment": "^0.9.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
@ -39,6 +43,7 @@
"nanoid": "3.3.4", "nanoid": "3.3.4",
"node-machine-id": "^1.1.12", "node-machine-id": "^1.1.12",
"qs": "^6.11.0", "qs": "^6.11.0",
"socks-proxy-agent": "^8.0.2",
"uuid": "^9.0.0", "uuid": "^9.0.0",
"vm2": "^3.9.13", "vm2": "^3.9.13",
"yup": "^0.32.11" "yup": "^0.32.11"

View File

@ -3,7 +3,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const { hasBruExtension } = require('../utils/filesystem'); const { hasBruExtension } = require('../utils/filesystem');
const { bruToEnvJson, bruToJson } = require('../bru'); const { bruToEnvJson, bruToJson, collectionBruToJson } = require('../bru');
const { dotenvToJson } = require('@usebruno/lang'); const { dotenvToJson } = require('@usebruno/lang');
const { uuid } = require('../utils/common'); const { uuid } = require('../utils/common');
@ -37,6 +37,13 @@ const isBruEnvironmentConfig = (pathname, collectionPath) => {
return dirname === envDirectory && hasBruExtension(basename); return dirname === envDirectory && hasBruExtension(basename);
}; };
const isCollectionRootBruFile = (pathname, collectionPath) => {
const dirname = path.dirname(pathname);
const basename = path.basename(pathname);
return dirname === collectionPath && basename === 'collection.bru';
};
const hydrateRequestWithUuid = (request, pathname) => { const hydrateRequestWithUuid = (request, pathname) => {
request.uid = getRequestUid(pathname); request.uid = getRequestUid(pathname);
@ -59,6 +66,20 @@ const hydrateRequestWithUuid = (request, pathname) => {
return request; return request;
}; };
const hydrateBruCollectionFileWithUuid = (collectionRoot) => {
const params = _.get(collectionRoot, 'request.params', []);
const headers = _.get(collectionRoot, 'request.headers', []);
const requestVars = _.get(collectionRoot, 'request.vars.req', []);
const responseVars = _.get(collectionRoot, 'request.vars.res', []);
params.forEach((param) => (param.uid = uuid()));
headers.forEach((header) => (header.uid = uuid()));
requestVars.forEach((variable) => (variable.uid = uuid()));
responseVars.forEach((variable) => (variable.uid = uuid()));
return collectionRoot;
};
const envHasSecrets = (environment = {}) => { const envHasSecrets = (environment = {}) => {
const secrets = _.filter(environment.variables, (v) => v.secret); const secrets = _.filter(environment.variables, (v) => v.secret);
@ -195,6 +216,30 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
return addEnvironmentFile(win, pathname, collectionUid, collectionPath); return addEnvironmentFile(win, pathname, collectionUid, collectionPath);
} }
if (isCollectionRootBruFile(pathname, collectionPath)) {
const file = {
meta: {
collectionUid,
pathname,
name: path.basename(pathname),
collectionRoot: true
}
};
try {
let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = collectionBruToJson(bruContent);
hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
return;
} catch (err) {
console.error(err);
return;
}
}
if (hasBruExtension(pathname)) { if (hasBruExtension(pathname)) {
const file = { const file = {
meta: { meta: {
@ -208,6 +253,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
let bruContent = fs.readFileSync(pathname, 'utf8'); let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = bruToJson(bruContent); file.data = bruToJson(bruContent);
hydrateRequestWithUuid(file.data, pathname); hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file); win.webContents.send('main:collection-tree-updated', 'addFile', file);
} catch (err) { } catch (err) {
@ -274,6 +320,30 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
return changeEnvironmentFile(win, pathname, collectionUid, collectionPath); return changeEnvironmentFile(win, pathname, collectionUid, collectionPath);
} }
if (isCollectionRootBruFile(pathname, collectionPath)) {
const file = {
meta: {
collectionUid,
pathname,
name: path.basename(pathname),
collectionRoot: true
}
};
try {
let bruContent = fs.readFileSync(pathname, 'utf8');
file.data = collectionBruToJson(bruContent);
hydrateBruCollectionFileWithUuid(file.data);
win.webContents.send('main:collection-tree-updated', 'change', file);
return;
} catch (err) {
console.error(err);
return;
}
}
if (hasBruExtension(pathname)) { if (hasBruExtension(pathname)) {
try { try {
const file = { const file = {

View File

@ -1,5 +1,56 @@
const _ = require('lodash'); const _ = require('lodash');
const { bruToJsonV2, jsonToBruV2, bruToEnvJsonV2, envJsonToBruV2 } = require('@usebruno/lang'); const {
bruToJsonV2,
jsonToBruV2,
bruToEnvJsonV2,
envJsonToBruV2,
collectionBruToJson: _collectionBruToJson,
jsonToCollectionBru: _jsonToCollectionBru
} = require('@usebruno/lang');
const collectionBruToJson = (bru) => {
try {
const json = _collectionBruToJson(bru);
const transformedJson = {
request: {
params: _.get(json, 'query', []),
headers: _.get(json, 'headers', []),
auth: _.get(json, 'auth', {}),
script: _.get(json, 'script', {}),
vars: _.get(json, 'vars', {}),
tests: _.get(json, 'tests', '')
}
};
return transformedJson;
} catch (error) {
return Promise.reject(error);
}
};
const jsonToCollectionBru = (json) => {
try {
const collectionBruJson = {
query: _.get(json, 'request.params', []),
headers: _.get(json, 'request.headers', []),
auth: _.get(json, 'request.auth', {}),
script: {
req: _.get(json, 'request.script.req', ''),
res: _.get(json, 'request.script.res', '')
},
vars: {
req: _.get(json, 'request.vars.req', []),
res: _.get(json, 'request.vars.req', [])
},
tests: _.get(json, 'request.tests', '')
};
return _jsonToCollectionBru(collectionBruJson);
} catch (error) {
return Promise.reject(error);
}
};
const bruToEnvJson = (bru) => { const bruToEnvJson = (bru) => {
try { try {
@ -65,7 +116,8 @@ const bruToJson = (bru) => {
script: _.get(json, 'script', {}), script: _.get(json, 'script', {}),
vars: _.get(json, 'vars', {}), vars: _.get(json, 'vars', {}),
assertions: _.get(json, 'assertions', []), assertions: _.get(json, 'assertions', []),
tests: _.get(json, 'tests', '') tests: _.get(json, 'tests', ''),
docs: _.get(json, 'docs', '')
} }
}; };
@ -118,7 +170,8 @@ const jsonToBru = (json) => {
res: _.get(json, 'request.vars.res', []) res: _.get(json, 'request.vars.res', [])
}, },
assertions: _.get(json, 'request.assertions', []), assertions: _.get(json, 'request.assertions', []),
tests: _.get(json, 'request.tests', '') tests: _.get(json, 'request.tests', ''),
docs: _.get(json, 'request.docs', '')
}; };
return jsonToBruV2(bruJson); return jsonToBruV2(bruJson);
@ -128,5 +181,7 @@ module.exports = {
bruToJson, bruToJson,
jsonToBru, jsonToBru,
bruToEnvJson, bruToEnvJson,
envJsonToBru envJsonToBru,
collectionBruToJson,
jsonToCollectionBru
}; };

View File

@ -13,12 +13,16 @@ const { loadWindowState, saveWindowState } = require('./utils/window');
const lastOpenedCollections = new LastOpenedCollections(); const lastOpenedCollections = new LastOpenedCollections();
setContentSecurityPolicy(` const contentSecurityPolicy = [
default-src * 'unsafe-inline' 'unsafe-eval'; isDev ? "default-src 'self' 'unsafe-inline' 'unsafe-eval'" : "default-src 'self'",
script-src * 'unsafe-inline' 'unsafe-eval'; "connect-src 'self' https://api.github.com/repos/usebruno/bruno",
connect-src * 'unsafe-inline'; "font-src 'self' https://fonts.gstatic.com",
form-action 'none'; "form-action 'none'",
`); "img-src 'self' blob: data:",
"style-src 'self' https://fonts.googleapis.com"
];
setContentSecurityPolicy(contentSecurityPolicy.join(';'));
const menu = Menu.buildFromTemplate(menuTemplate); const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu);

View File

@ -2,7 +2,7 @@ const _ = require('lodash');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { ipcMain, shell } = require('electron'); const { ipcMain, shell } = require('electron');
const { envJsonToBru, bruToJson, jsonToBru } = require('../bru'); const { envJsonToBru, bruToJson, jsonToBru, jsonToCollectionBru } = require('../bru');
const { const {
isValidPathname, isValidPathname,
@ -101,6 +101,17 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
} }
}); });
ipcMain.handle('renderer:save-collection-root', async (event, collectionPathname, collectionRoot) => {
try {
const collectionBruFilePath = path.join(collectionPathname, 'collection.bru');
const content = jsonToCollectionBru(collectionRoot);
await writeFile(collectionBruFilePath, content);
} catch (error) {
return Promise.reject(error);
}
});
// new request // new request
ipcMain.handle('renderer:new-request', async (event, pathname, request) => { ipcMain.handle('renderer:new-request', async (event, pathname, request) => {
try { try {

View File

@ -0,0 +1,56 @@
const { fromIni } = require('@aws-sdk/credential-providers');
const { aws4Interceptor } = require('aws4-axios');
function isStrPresent(str) {
return str && str !== '' && str !== 'undefined';
}
async function resolveCredentials(request) {
const awsv4 = request.awsv4config;
if (isStrPresent(awsv4.profileName)) {
try {
credentialsProvider = fromIni({
profile: awsv4.profileName
});
credentials = await credentialsProvider();
awsv4.accessKeyId = credentials.accessKeyId;
awsv4.secretAccessKey = credentials.secretAccessKey;
awsv4.sessionToken = credentials.sessionToken;
} catch {
console.error('Failed to fetch credentials from AWS profile.');
}
}
return awsv4;
}
function addAwsV4Interceptor(axiosInstance, request) {
if (!request.awsv4config) {
console.warn('No Auth Config found!');
return;
}
const awsv4 = request.awsv4config;
if (!isStrPresent(awsv4.accessKeyId) || !isStrPresent(awsv4.secretAccessKey)) {
console.warn('Required Auth Fields are not present');
return;
}
const interceptor = aws4Interceptor({
options: {
region: awsv4.region,
service: awsv4.service
},
credentials: {
accessKeyId: awsv4.accessKeyId,
secretAccessKey: awsv4.secretAccessKey,
sessionToken: awsv4.sessionToken
}
});
axiosInstance.interceptors.request.use(interceptor);
}
module.exports = {
addAwsV4Interceptor,
resolveCredentials
};

View File

@ -23,9 +23,9 @@ function makeAxiosInstance() {
return response; return response;
}, },
(error) => { (error) => {
if (error.response) {
const end = Date.now(); const end = Date.now();
const start = error.config.headers['request-start-time']; const start = error.config.headers['request-start-time'];
if (error.response) {
error.response.headers['request-duration'] = end - start; error.response.headers['request-duration'] = end - start;
} }
return Promise.reject(error); return Promise.reject(error);

View File

@ -1,3 +1,4 @@
const os = require('os');
const qs = require('qs'); const qs = require('qs');
const https = require('https'); const https = require('https');
const axios = require('axios'); const axios = require('axios');
@ -5,7 +6,7 @@ const decomment = require('decomment');
const Mustache = require('mustache'); const Mustache = require('mustache');
const FormData = require('form-data'); const FormData = require('form-data');
const { ipcMain } = require('electron'); const { ipcMain } = require('electron');
const { forOwn, extend, each, get } = require('lodash'); const { forOwn, extend, each, get, compact } = require('lodash');
const { VarsRuntime, AssertRuntime, ScriptRuntime, TestRuntime } = require('@usebruno/js'); const { VarsRuntime, AssertRuntime, ScriptRuntime, TestRuntime } = require('@usebruno/js');
const prepareRequest = require('./prepare-request'); const prepareRequest = require('./prepare-request');
const prepareGqlIntrospectionRequest = require('./prepare-gql-introspection-request'); const prepareGqlIntrospectionRequest = require('./prepare-gql-introspection-request');
@ -19,7 +20,9 @@ const { getProcessEnvVars } = require('../../store/process-env');
const { getBrunoConfig } = require('../../store/bruno-config'); const { getBrunoConfig } = require('../../store/bruno-config');
const { HttpsProxyAgent } = require('https-proxy-agent'); const { HttpsProxyAgent } = require('https-proxy-agent');
const { HttpProxyAgent } = require('http-proxy-agent'); const { HttpProxyAgent } = require('http-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { makeAxiosInstance } = require('./axios-instance'); const { makeAxiosInstance } = require('./axios-instance');
const { addAwsV4Interceptor, resolveCredentials } = require('./awsv4auth-helper');
// override the default escape function to prevent escaping // override the default escape function to prevent escaping
Mustache.escape = function (value) { Mustache.escape = function (value) {
@ -73,7 +76,7 @@ const getSize = (data) => {
} }
if (typeof data === 'object') { if (typeof data === 'object') {
return Buffer.byteLength(JSON.stringify(data), 'utf8'); return Buffer.byteLength(safeStringifyJSON(data), 'utf8');
} }
return 0; return 0;
@ -81,9 +84,9 @@ const getSize = (data) => {
const registerNetworkIpc = (mainWindow) => { const registerNetworkIpc = (mainWindow) => {
// handler for sending http request // handler for sending http request
ipcMain.handle( ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => {
'send-http-request', const collectionUid = collection.uid;
async (event, item, collectionUid, collectionPath, environment, collectionVariables) => { const collectionPath = collection.pathname;
const cancelTokenUid = uuid(); const cancelTokenUid = uuid();
const requestUid = uuid(); const requestUid = uuid();
@ -104,8 +107,9 @@ const registerNetworkIpc = (mainWindow) => {
cancelTokenUid cancelTokenUid
}); });
const collectionRoot = get(collection, 'root', {});
const _request = item.draft ? item.draft.request : item.request; const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request); const request = prepareRequest(_request, collectionRoot);
const envVars = getEnvVars(environment); const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid); const processEnvVars = getProcessEnvVars(collectionUid);
const brunoConfig = getBrunoConfig(collectionUid); const brunoConfig = getBrunoConfig(collectionUid);
@ -151,7 +155,9 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run pre-request script // run pre-request script
const requestScript = get(request, 'script.req'); const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(
os.EOL
);
if (requestScript && requestScript.length) { if (requestScript && requestScript.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runRequestScript( const result = await scriptRuntime.runRequestScript(
@ -220,7 +226,7 @@ const registerNetworkIpc = (mainWindow) => {
const brunoConfig = getBrunoConfig(collectionUid); const brunoConfig = getBrunoConfig(collectionUid);
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false); const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) { if (proxyEnabled) {
let proxy; let proxyUri;
const interpolationOptions = { const interpolationOptions = {
envVars, envVars,
@ -232,22 +238,31 @@ const registerNetworkIpc = (mainWindow) => {
const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions); const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions);
const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions); const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions);
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false); const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');
if (proxyAuthEnabled) { if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions); const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions); const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions);
proxy = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`; proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`;
} else { } else {
proxy = `${proxyProtocol}://${proxyHostname}:${proxyPort}`; proxyUri = `${proxyProtocol}://${proxyHostname}:${proxyPort}`;
} }
if (socksEnabled) {
const socksProxyAgent = new SocksProxyAgent(proxyUri);
request.httpsAgent = socksProxyAgent;
request.httpAgent = socksProxyAgent;
} else {
request.httpsAgent = new HttpsProxyAgent( request.httpsAgent = new HttpsProxyAgent(
proxy, proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
); );
request.httpAgent = new HttpProxyAgent(proxy); request.httpAgent = new HttpProxyAgent(proxyUri);
}
} else if (Object.keys(httpsAgentRequestFields).length > 0) { } else if (Object.keys(httpsAgentRequestFields).length > 0) {
request.httpsAgent = new https.Agent({ request.httpsAgent = new https.Agent({
...httpsAgentRequestFields ...httpsAgentRequestFields
@ -256,6 +271,12 @@ const registerNetworkIpc = (mainWindow) => {
const axiosInstance = makeAxiosInstance(); const axiosInstance = makeAxiosInstance();
if (request.awsv4config) {
request.awsv4config = await resolveCredentials(request);
addAwsV4Interceptor(axiosInstance, request);
delete request.awsv4config;
}
/** @type {import('axios').AxiosResponse} */ /** @type {import('axios').AxiosResponse} */
const response = await axiosInstance(request); const response = await axiosInstance(request);
@ -284,7 +305,9 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run post-response script // run post-response script
const responseScript = get(request, 'script.res'); const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join(
os.EOL
);
if (responseScript && responseScript.length) { if (responseScript && responseScript.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runResponseScript( const result = await scriptRuntime.runResponseScript(
@ -330,7 +353,10 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run tests // run tests
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); const testFile = compact([
get(collectionRoot, 'request.tests'),
item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests')
]).join(os.EOL);
if (typeof testFile === 'string') { if (typeof testFile === 'string') {
const testRuntime = new TestRuntime(); const testRuntime = new TestRuntime();
const testResults = await testRuntime.runTests( const testResults = await testRuntime.runTests(
@ -409,7 +435,10 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run tests // run tests
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); const testFile = compact([
get(collectionRoot, 'request.tests'),
item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests')
]).join(os.EOL);
if (typeof testFile === 'string') { if (typeof testFile === 'string') {
const testRuntime = new TestRuntime(); const testRuntime = new TestRuntime();
const testResults = await testRuntime.runTests( const testResults = await testRuntime.runTests(
@ -454,8 +483,7 @@ const registerNetworkIpc = (mainWindow) => {
return Promise.reject(error); return Promise.reject(error);
} }
} });
);
ipcMain.handle('cancel-http-request', async (event, cancelTokenUid) => { ipcMain.handle('cancel-http-request', async (event, cancelTokenUid) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -472,7 +500,8 @@ const registerNetworkIpc = (mainWindow) => {
ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collection) => { ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collection) => {
try { try {
const envVars = getEnvVars(environment); const envVars = getEnvVars(environment);
const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request); const collectionRoot = get(collection, 'root', {});
const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request, collectionRoot);
const preferences = getPreferences(); const preferences = getPreferences();
const sslVerification = get(preferences, 'request.sslVerification', true); const sslVerification = get(preferences, 'request.sslVerification', true);
@ -516,6 +545,7 @@ const registerNetworkIpc = (mainWindow) => {
const folderUid = folder ? folder.uid : null; const folderUid = folder ? folder.uid : null;
const brunoConfig = getBrunoConfig(collectionUid); const brunoConfig = getBrunoConfig(collectionUid);
const scriptingConfig = get(brunoConfig, 'scripts', {}); const scriptingConfig = get(brunoConfig, 'scripts', {});
const collectionRoot = get(collection, 'root', {});
const onConsoleLog = (type, args) => { const onConsoleLog = (type, args) => {
console[type](...args); console[type](...args);
@ -574,7 +604,7 @@ const registerNetworkIpc = (mainWindow) => {
}); });
const _request = item.draft ? item.draft.request : item.request; const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request); const request = prepareRequest(_request, collectionRoot);
const processEnvVars = getProcessEnvVars(collectionUid); const processEnvVars = getProcessEnvVars(collectionUid);
try { try {
@ -611,7 +641,9 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run pre-request script // run pre-request script
const requestScript = get(request, 'script.req'); const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(
os.EOL
);
if (requestScript && requestScript.length) { if (requestScript && requestScript.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runRequestScript( const result = await scriptRuntime.runRequestScript(
@ -656,7 +688,7 @@ const registerNetworkIpc = (mainWindow) => {
const brunoConfig = getBrunoConfig(collectionUid); const brunoConfig = getBrunoConfig(collectionUid);
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false); const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) { if (proxyEnabled) {
let proxy; let proxyUri;
const interpolationOptions = { const interpolationOptions = {
envVars, envVars,
collectionVariables, collectionVariables,
@ -667,6 +699,7 @@ const registerNetworkIpc = (mainWindow) => {
const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions); const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions);
const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions); const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions);
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false); const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');
if (proxyAuthEnabled) { if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString( const proxyAuthUsername = interpolateString(
@ -679,16 +712,23 @@ const registerNetworkIpc = (mainWindow) => {
interpolationOptions interpolationOptions
); );
proxy = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`; proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`;
} else { } else {
proxy = `${proxyProtocol}://${proxyHostname}:${proxyPort}`; proxyUri = `${proxyProtocol}://${proxyHostname}:${proxyPort}`;
} }
request.httpsAgent = new HttpsProxyAgent(proxy, { if (socksEnabled) {
const socksProxyAgent = new SocksProxyAgent(proxyUri);
request.httpsAgent = socksProxyAgent;
request.httpAgent = socksProxyAgent;
} else {
request.httpsAgent = new HttpsProxyAgent(proxyUri, {
rejectUnauthorized: sslVerification rejectUnauthorized: sslVerification
}); });
request.httpAgent = new HttpProxyAgent(proxy); request.httpAgent = new HttpProxyAgent(proxyUri);
}
} else if (!sslVerification) { } else if (!sslVerification) {
request.httpsAgent = new https.Agent({ request.httpsAgent = new https.Agent({
rejectUnauthorized: false rejectUnauthorized: false
@ -724,7 +764,10 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run response script // run response script
const responseScript = get(request, 'script.res'); const responseScript = compact([
get(collectionRoot, 'request.script.res'),
get(request, 'script.res')
]).join(os.EOL);
if (responseScript && responseScript.length) { if (responseScript && responseScript.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runResponseScript( const result = await scriptRuntime.runResponseScript(
@ -768,7 +811,10 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run tests // run tests
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); const testFile = compact([
get(collectionRoot, 'request.tests'),
item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests')
]).join(os.EOL);
if (typeof testFile === 'string') { if (typeof testFile === 'string') {
const testRuntime = new TestRuntime(); const testRuntime = new TestRuntime();
const testResults = await testRuntime.runTests( const testResults = await testRuntime.runTests(
@ -848,7 +894,10 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run tests // run tests
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); const testFile = compact([
get(collectionRoot, 'request.tests'),
item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests')
]).join(os.EOL);
if (typeof testFile === 'string') { if (typeof testFile === 'string') {
const testRuntime = new TestRuntime(); const testRuntime = new TestRuntime();
const testResults = await testRuntime.runTests( const testResults = await testRuntime.runTests(

View File

@ -121,6 +121,16 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
delete request.auth; delete request.auth;
} }
// interpolate vars for aws sigv4 auth
if (request.awsv4config) {
request.awsv4config.accessKeyId = interpolate(request.awsv4config.accessKeyId) || '';
request.awsv4config.secretAccessKey = interpolate(request.awsv4config.secretAccessKey) || '';
request.awsv4config.sessionToken = interpolate(request.awsv4config.sessionToken) || '';
request.awsv4config.service = interpolate(request.awsv4config.service) || '';
request.awsv4config.region = interpolate(request.awsv4config.region) || '';
request.awsv4config.profileName = interpolate(request.awsv4config.profileName) || '';
}
return request; return request;
}; };

View File

@ -1,15 +1,14 @@
const Handlebars = require('handlebars'); const Handlebars = require('handlebars');
const { getIntrospectionQuery } = require('graphql'); const { getIntrospectionQuery } = require('graphql');
const { get } = require('lodash'); const { setAuthHeaders } = require('./prepare-request');
const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => { const prepareGqlIntrospectionRequest = (endpoint, envVars, request, collectionRoot) => {
if (endpoint && endpoint.length) { if (endpoint && endpoint.length) {
endpoint = Handlebars.compile(endpoint, { noEscape: true })(envVars); endpoint = Handlebars.compile(endpoint, { noEscape: true })(envVars);
} }
const introspectionQuery = getIntrospectionQuery();
const queryParams = { const queryParams = {
query: introspectionQuery query: getIntrospectionQuery()
}; };
let axiosRequest = { let axiosRequest = {
@ -23,20 +22,7 @@ const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => {
data: JSON.stringify(queryParams) data: JSON.stringify(queryParams)
}; };
if (request.auth) { return setAuthHeaders(axiosRequest, request, collectionRoot);
if (request.auth.mode === 'basic') {
axiosRequest.auth = {
username: get(request, 'auth.basic.username'),
password: get(request, 'auth.basic.password')
};
}
if (request.auth.mode === 'bearer') {
axiosRequest.headers.authorization = `Bearer ${get(request, 'auth.bearer.token')}`;
}
}
return axiosRequest;
}; };
const mapHeaders = (headers) => { const mapHeaders = (headers) => {

View File

@ -1,9 +1,77 @@
const { get, each, filter } = require('lodash'); const { get, each, filter } = require('lodash');
const decomment = require('decomment'); const decomment = require('decomment');
const prepareRequest = (request) => { // Authentication
// A request can override the collection auth with another auth
// But it cannot override the collection auth with no auth
// We will provide support for disabling the auth via scripting in the future
const setAuthHeaders = (axiosRequest, request, collectionRoot) => {
const collectionAuth = get(collectionRoot, 'request.auth');
if (collectionAuth) {
switch (collectionAuth.mode) {
case 'awsv4':
axiosRequest.awsv4config = {
accessKeyId: get(collectionAuth, 'awsv4.accessKeyId'),
secretAccessKey: get(collectionAuth, 'awsv4.secretAccessKey'),
sessionToken: get(collectionAuth, 'awsv4.sessionToken'),
service: get(collectionAuth, 'awsv4.service'),
region: get(collectionAuth, 'awsv4.region'),
profileName: get(collectionAuth, 'awsv4.profileName')
};
break;
case 'basic':
axiosRequest.auth = {
username: get(collectionAuth, 'basic.username'),
password: get(collectionAuth, 'basic.password')
};
break;
case 'bearer':
axiosRequest.headers['authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`;
break;
}
}
if (request.auth) {
switch (request.auth.mode) {
case 'awsv4':
axiosRequest.awsv4config = {
accessKeyId: get(request, 'auth.awsv4.accessKeyId'),
secretAccessKey: get(request, 'auth.awsv4.secretAccessKey'),
sessionToken: get(request, 'auth.awsv4.sessionToken'),
service: get(request, 'auth.awsv4.service'),
region: get(request, 'auth.awsv4.region'),
profileName: get(request, 'auth.awsv4.profileName')
};
break;
case 'basic':
axiosRequest.auth = {
username: get(request, 'auth.basic.username'),
password: get(request, 'auth.basic.password')
};
break;
case 'bearer':
axiosRequest.headers['authorization'] = `Bearer ${get(request, 'auth.bearer.token')}`;
break;
}
}
return axiosRequest;
};
const prepareRequest = (request, collectionRoot) => {
const headers = {}; const headers = {};
let contentTypeDefined = false; let contentTypeDefined = false;
// collection headers
each(get(collectionRoot, 'request.headers', []), (h) => {
if (h.enabled) {
headers[h.name] = h.value;
if (h.name.toLowerCase() === 'content-type') {
contentTypeDefined = true;
}
}
});
each(request.headers, (h) => { each(request.headers, (h) => {
if (h.enabled) { if (h.enabled) {
headers[h.name] = h.value; headers[h.name] = h.value;
@ -19,19 +87,7 @@ const prepareRequest = (request) => {
headers: headers headers: headers
}; };
// Authentication axiosRequest = setAuthHeaders(axiosRequest, request, collectionRoot);
if (request.auth) {
if (request.auth.mode === 'basic') {
axiosRequest.auth = {
username: get(request, 'auth.basic.username'),
password: get(request, 'auth.basic.password')
};
}
if (request.auth.mode === 'bearer') {
axiosRequest.headers['authorization'] = `Bearer ${get(request, 'auth.bearer.token')}`;
}
}
if (request.body.mode === 'json') { if (request.body.mode === 'json') {
if (!contentTypeDefined) { if (!contentTypeDefined) {
@ -59,6 +115,13 @@ const prepareRequest = (request) => {
axiosRequest.data = request.body.xml; axiosRequest.data = request.body.xml;
} }
if (request.body.mode === 'sparql') {
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/sparql-query';
}
axiosRequest.data = request.body.sparql;
}
if (request.body.mode === 'formUrlEncoded') { if (request.body.mode === 'formUrlEncoded') {
axiosRequest.headers['content-type'] = 'application/x-www-form-urlencoded'; axiosRequest.headers['content-type'] = 'application/x-www-form-urlencoded';
const params = {}; const params = {};
@ -97,3 +160,4 @@ const prepareRequest = (request) => {
}; };
module.exports = prepareRequest; module.exports = prepareRequest;
module.exports.setAuthHeaders = setAuthHeaders;

View File

@ -6,6 +6,7 @@ const BrunoRequest = require('../bruno-request');
const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils'); const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
const { expect } = chai; const { expect } = chai;
chai.use(require('chai-string'));
chai.use(function (chai, utils) { chai.use(function (chai, utils) {
// Custom assertion for checking if a variable is JSON // Custom assertion for checking if a variable is JSON
chai.Assertion.addProperty('json', function () { chai.Assertion.addProperty('json', function () {

View File

@ -15,6 +15,7 @@ const BrunoResponse = require('../bruno-response');
const { cleanJson } = require('../utils'); const { cleanJson } = require('../utils');
// Inbuilt Library Support // Inbuilt Library Support
const ajv = require('ajv');
const atob = require('atob'); const atob = require('atob');
const btoa = require('btoa'); const btoa = require('btoa');
const lodash = require('lodash'); const lodash = require('lodash');
@ -70,6 +71,7 @@ class ScriptRuntime {
}; };
context.console = { context.console = {
log: customLogger('log'), log: customLogger('log'),
debug: customLogger('debug'),
info: customLogger('info'), info: customLogger('info'),
warn: customLogger('warn'), warn: customLogger('warn'),
error: customLogger('error') error: customLogger('error')
@ -93,6 +95,7 @@ class ScriptRuntime {
punycode, punycode,
zlib, zlib,
// 3rd party libs // 3rd party libs
ajv,
atob, atob,
btoa, btoa,
lodash, lodash,
@ -182,6 +185,7 @@ class ScriptRuntime {
punycode, punycode,
zlib, zlib,
// 3rd party libs // 3rd party libs
ajv,
atob, atob,
btoa, btoa,
lodash, lodash,

View File

@ -18,13 +18,15 @@ const TestResults = require('../test-results');
const { cleanJson } = require('../utils'); const { cleanJson } = require('../utils');
// Inbuilt Library Support // Inbuilt Library Support
const ajv = require('ajv');
const atob = require('atob'); const atob = require('atob');
const axios = require('axios');
const btoa = require('btoa'); const btoa = require('btoa');
const lodash = require('lodash'); const lodash = require('lodash');
const moment = require('moment'); const moment = require('moment');
const uuid = require('uuid'); const uuid = require('uuid');
const nanoid = require('nanoid'); const nanoid = require('nanoid');
const axios = require('axios');
const fetch = require('node-fetch');
const CryptoJS = require('crypto-js'); const CryptoJS = require('crypto-js');
class TestRuntime { class TestRuntime {
@ -111,14 +113,16 @@ class TestRuntime {
punycode, punycode,
zlib, zlib,
// 3rd party libs // 3rd party libs
atob, ajv,
axios,
btoa, btoa,
atob,
lodash, lodash,
moment, moment,
uuid, uuid,
nanoid, nanoid,
axios,
chai, chai,
'node-fetch': fetch,
'crypto-js': CryptoJS, 'crypto-js': CryptoJS,
...whitelistedModules, ...whitelistedModules,
fs: allowScriptFilesystemAccess ? fs : undefined fs: allowScriptFilesystemAccess ? fs : undefined

View File

@ -1,6 +1,6 @@
{ {
"name": "@usebruno/lang", "name": "@usebruno/lang",
"version": "0.5.0", "version": "0.8.0",
"license": "MIT", "license": "MIT",
"main": "src/index.js", "main": "src/index.js",
"files": [ "files": [
@ -14,6 +14,7 @@
}, },
"dependencies": { "dependencies": {
"arcsecond": "^5.0.0", "arcsecond": "^5.0.0",
"dotenv": "^16.3.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"ohm-js": "^16.6.0" "ohm-js": "^16.6.0"
} }

View File

@ -1,21 +1,23 @@
const { bruToJson, jsonToBru, bruToEnvJson, envJsonToBru } = require('../v1/src');
const bruToJsonV2 = require('../v2/src/bruToJson'); const bruToJsonV2 = require('../v2/src/bruToJson');
const jsonToBruV2 = require('../v2/src/jsonToBru'); const jsonToBruV2 = require('../v2/src/jsonToBru');
const bruToEnvJsonV2 = require('../v2/src/envToJson'); const bruToEnvJsonV2 = require('../v2/src/envToJson');
const envJsonToBruV2 = require('../v2/src/jsonToEnv'); const envJsonToBruV2 = require('../v2/src/jsonToEnv');
const dotenvToJson = require('../v2/src/dotenvToJson'); const dotenvToJson = require('../v2/src/dotenvToJson');
module.exports = { const collectionBruToJson = require('../v2/src/collectionBruToJson');
bruToJson, const jsonToCollectionBru = require('../v2/src/jsonToCollectionBru');
jsonToBru,
bruToEnvJson,
envJsonToBru,
// Todo: remove V2 suffixes
// Changes will have to be made to the CLI and GUI
module.exports = {
bruToJsonV2, bruToJsonV2,
jsonToBruV2, jsonToBruV2,
bruToEnvJsonV2, bruToEnvJsonV2,
envJsonToBruV2, envJsonToBruV2,
collectionBruToJson,
jsonToCollectionBru,
dotenvToJson dotenvToJson
}; };

View File

@ -23,8 +23,8 @@ const { outdentString } = require('../../v1/src/utils');
*/ */
const grammar = ohm.grammar(`Bru { const grammar = ohm.grammar(`Bru {
BruFile = (meta | http | query | headers | auths | bodies | varsandassert | script | tests | docs)* BruFile = (meta | http | query | headers | auths | bodies | varsandassert | script | tests | docs)*
auths = authbasic | authbearer auths = authawsv4 | authbasic | authbearer
bodies = bodyjson | bodytext | bodyxml | bodygraphql | bodygraphqlvars | bodyforms | body bodies = bodyjson | bodytext | bodyxml | bodysparql | bodygraphql | bodygraphqlvars | bodyforms | body
bodyforms = bodyformurlencoded | bodymultipart bodyforms = bodyformurlencoded | bodymultipart
nl = "\\r"? "\\n" nl = "\\r"? "\\n"
@ -76,6 +76,7 @@ const grammar = ohm.grammar(`Bru {
varsres = "vars:post-response" dictionary varsres = "vars:post-response" dictionary
assert = "assert" assertdictionary assert = "assert" assertdictionary
authawsv4 = "auth:awsv4" dictionary
authbasic = "auth:basic" dictionary authbasic = "auth:basic" dictionary
authbearer = "auth:bearer" dictionary authbearer = "auth:bearer" dictionary
@ -83,6 +84,7 @@ const grammar = ohm.grammar(`Bru {
bodyjson = "body:json" st* "{" nl* textblock tagend bodyjson = "body:json" st* "{" nl* textblock tagend
bodytext = "body:text" st* "{" nl* textblock tagend bodytext = "body:text" st* "{" nl* textblock tagend
bodyxml = "body:xml" st* "{" nl* textblock tagend bodyxml = "body:xml" st* "{" nl* textblock tagend
bodysparql = "body:sparql" st* "{" nl* textblock tagend
bodygraphql = "body:graphql" st* "{" nl* textblock tagend bodygraphql = "body:graphql" st* "{" nl* textblock tagend
bodygraphqlvars = "body:graphql:vars" st* "{" nl* textblock tagend bodygraphqlvars = "body:graphql:vars" st* "{" nl* textblock tagend
@ -294,6 +296,33 @@ const sem = grammar.createSemantics().addAttribute('ast', {
headers: mapPairListToKeyValPairs(dictionary.ast) headers: mapPairListToKeyValPairs(dictionary.ast)
}; };
}, },
authawsv4(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const accessKeyIdKey = _.find(auth, { name: 'accessKeyId' });
const secretAccessKeyKey = _.find(auth, { name: 'secretAccessKey' });
const sessionTokenKey = _.find(auth, { name: 'sessionToken' });
const serviceKey = _.find(auth, { name: 'service' });
const regionKey = _.find(auth, { name: 'region' });
const profileNameKey = _.find(auth, { name: 'profileName' });
const accessKeyId = accessKeyIdKey ? accessKeyIdKey.value : '';
const secretAccessKey = secretAccessKeyKey ? secretAccessKeyKey.value : '';
const sessionToken = sessionTokenKey ? sessionTokenKey.value : '';
const service = serviceKey ? serviceKey.value : '';
const region = regionKey ? regionKey.value : '';
const profileName = profileNameKey ? profileNameKey.value : '';
return {
auth: {
awsv4: {
accessKeyId,
secretAccessKey,
sessionToken,
service,
region,
profileName
}
}
};
},
authbasic(_1, dictionary) { authbasic(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false); const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const usernameKey = _.find(auth, { name: 'username' }); const usernameKey = _.find(auth, { name: 'username' });
@ -366,6 +395,13 @@ const sem = grammar.createSemantics().addAttribute('ast', {
} }
}; };
}, },
bodysparql(_1, _2, _3, _4, textblock, _5) {
return {
body: {
sparql: outdentString(textblock.sourceString)
}
};
},
bodygraphql(_1, _2, _3, _4, textblock, _5) { bodygraphql(_1, _2, _3, _4, textblock, _5) {
return { return {
body: { body: {

View File

@ -4,7 +4,7 @@ const { outdentString } = require('../../v1/src/utils');
const grammar = ohm.grammar(`Bru { const grammar = ohm.grammar(`Bru {
BruFile = (meta | query | headers | auth | auths | vars | script | tests | docs)* BruFile = (meta | query | headers | auth | auths | vars | script | tests | docs)*
auths = authbasic | authbearer auths = authawsv4 | authbasic | authbearer
nl = "\\r"? "\\n" nl = "\\r"? "\\n"
st = " " | "\\t" st = " " | "\\t"
@ -38,6 +38,7 @@ const grammar = ohm.grammar(`Bru {
varsreq = "vars:pre-request" dictionary varsreq = "vars:pre-request" dictionary
varsres = "vars:post-response" dictionary varsres = "vars:post-response" dictionary
authawsv4 = "auth:awsv4" dictionary
authbasic = "auth:basic" dictionary authbasic = "auth:basic" dictionary
authbearer = "auth:bearer" dictionary authbearer = "auth:bearer" dictionary
@ -171,6 +172,33 @@ const sem = grammar.createSemantics().addAttribute('ast', {
headers: mapPairListToKeyValPairs(dictionary.ast) headers: mapPairListToKeyValPairs(dictionary.ast)
}; };
}, },
authawsv4(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const accessKeyIdKey = _.find(auth, { name: 'accessKeyId' });
const secretAccessKeyKey = _.find(auth, { name: 'secretAccessKey' });
const sessionTokenKey = _.find(auth, { name: 'sessionToken' });
const serviceKey = _.find(auth, { name: 'service' });
const regionKey = _.find(auth, { name: 'region' });
const profileNameKey = _.find(auth, { name: 'profileName' });
const accessKeyId = accessKeyIdKey ? accessKeyIdKey.value : '';
const secretAccessKey = secretAccessKeyKey ? secretAccessKeyKey.value : '';
const sessionToken = sessionTokenKey ? sessionTokenKey.value : '';
const service = serviceKey ? serviceKey.value : '';
const region = regionKey ? regionKey.value : '';
const profileName = profileNameKey ? profileNameKey.value : '';
return {
auth: {
awsv4: {
accessKeyId,
secretAccessKey,
sessionToken,
service,
region,
profileName
}
}
};
},
authbasic(_1, dictionary) { authbasic(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false); const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const usernameKey = _.find(auth, { name: 'username' }); const usernameKey = _.find(auth, { name: 'username' });

View File

@ -1,80 +1,9 @@
const ohm = require('ohm-js'); const dotenv = require('dotenv');
const _ = require('lodash');
const grammar = ohm.grammar(`Env {
EnvFile = (entry)*
entry = st* key st* "=" st* value st* nl*
key = keychar*
value = valuechar*
keychar = ~(nl | st | nl | "=") any
valuechar = ~nl any
nl = "\\r"? "\\n"
st = " " | "\\t"
}`);
const concatArrays = (objValue, srcValue) => {
if (_.isArray(objValue) && _.isArray(srcValue)) {
return objValue.concat(srcValue);
}
};
const sem = grammar.createSemantics().addAttribute('ast', {
EnvFile(entries) {
return _.reduce(
entries.ast,
(result, item) => {
return _.mergeWith(result, item, concatArrays);
},
{}
);
},
entry(_1, key, _2, _3, _4, value, _5, _6) {
return { [key.ast.trim()]: value.ast.trim() };
},
key(chars) {
return chars.sourceString;
},
value(chars) {
return chars.sourceString;
},
nl(_1, _2) {
return '';
},
st(_) {
return '';
},
_iter(...elements) {
return elements.map((e) => e.ast);
}
});
const parser = (input) => { const parser = (input) => {
const match = grammar.match(input); const buf = Buffer.from(input);
const parsed = dotenv.parse(buf);
if (match.succeeded()) { return parsed;
const ast = sem(match).ast;
return postProcessEntries(ast);
} else {
throw new Error(match.message);
}
}; };
function postProcessEntries(ast) {
const processed = {};
for (const key in ast) {
const value = ast[key];
if (!isNaN(value)) {
processed[key] = parseFloat(value); // Convert to number if it's a valid number
} else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
processed[key] = value.toLowerCase() === 'true'; // Convert to boolean if it's 'true' or 'false'
} else {
processed[key] = value; // Otherwise, keep it as a string
}
}
return processed;
}
module.exports = parser; module.exports = parser;

View File

@ -87,6 +87,19 @@ const jsonToBru = (json) => {
bru += '\n}\n\n'; bru += '\n}\n\n';
} }
if (auth && auth.awsv4) {
bru += `auth:awsv4 {
${indentString(`accessKeyId: ${auth.awsv4.accessKeyId}`)}
${indentString(`secretAccessKey: ${auth.awsv4.secretAccessKey}`)}
${indentString(`sessionToken: ${auth.awsv4.sessionToken}`)}
${indentString(`service: ${auth.awsv4.service}`)}
${indentString(`region: ${auth.awsv4.region}`)}
${indentString(`profileName: ${auth.awsv4.profileName}`)}
}
`;
}
if (auth && auth.basic) { if (auth && auth.basic) {
bru += `auth:basic { bru += `auth:basic {
${indentString(`username: ${auth.basic.username}`)} ${indentString(`username: ${auth.basic.username}`)}
@ -125,6 +138,14 @@ ${indentString(body.text)}
${indentString(body.xml)} ${indentString(body.xml)}
} }
`;
}
if (body && body.sparql && body.sparql.length) {
bru += `body:sparql {
${indentString(body.sparql)}
}
`; `;
} }

View File

@ -12,7 +12,7 @@ const stripLastLine = (text) => {
return text.replace(/(\r?\n)$/, ''); return text.replace(/(\r?\n)$/, '');
}; };
const jsonToBru = (json) => { const jsonToCollectionBru = (json) => {
const { meta, query, headers, auth, script, tests, vars, docs } = json; const { meta, query, headers, auth, script, tests, vars, docs } = json;
let bru = ''; let bru = '';
@ -72,6 +72,19 @@ const jsonToBru = (json) => {
${indentString(`mode: ${auth.mode}`)} ${indentString(`mode: ${auth.mode}`)}
} }
`;
}
if (auth && auth.awsv4) {
bru += `auth:awsv4 {
${indentString(`accessKeyId: ${auth.awsv4.accessKeyId}`)}
${indentString(`secretAccessKey: ${auth.awsv4.secretAccessKey}`)}
${indentString(`sessionToken: ${auth.awsv4.sessionToken}`)}
${indentString(`service: ${auth.awsv4.service}`)}
${indentString(`region: ${auth.awsv4.region}`)}
${indentString(`profileName: ${auth.awsv4.profileName}`)}
}
`; `;
} }
@ -182,4 +195,4 @@ ${indentString(docs)}
return stripLastLine(bru); return stripLastLine(bru);
}; };
module.exports = jsonToBru; module.exports = jsonToCollectionBru;

View File

@ -26,16 +26,25 @@ BEEP=false
`; `;
const expected = { const expected = {
FOO: 'bar', FOO: 'bar',
BAZ: 2, BAZ: '2',
BEEP: false BEEP: 'false'
}; };
const output = parser(input); const output = parser(input);
expect(output).toEqual(expected); expect(output).toEqual(expected);
}); });
test('it should handle leading and trailing whitespace', () => { test('it should not strip leading and trailing whitespace when using quotes', () => {
const input = ` const input = `
SPACE = value SPACE=" value "
`;
const expected = { SPACE: ' value ' };
const output = parser(input);
expect(output).toEqual(expected);
});
test('it should strip leading and trailing whitespace when NOT using quotes', () => {
const input = `
SPACE= value
`; `;
const expected = { SPACE: 'value' }; const expected = { SPACE: 'value' };
const output = parser(input); const output = parser(input);

View File

@ -22,6 +22,15 @@ headers {
~transaction-id: {{transactionId}} ~transaction-id: {{transactionId}}
} }
auth:awsv4 {
accessKeyId: A12345678
secretAccessKey: thisisasecret
sessionToken: thisisafakesessiontoken
service: execute-api
region: us-east-1
profileName: test_profile
}
auth:basic { auth:basic {
username: john username: john
password: secret password: secret
@ -48,6 +57,13 @@ body:xml {
</xml> </xml>
} }
body:sparql {
SELECT * WHERE {
?subject ?predicate ?object .
}
LIMIT 10
}
body:form-urlencoded { body:form-urlencoded {
apikey: secret apikey: secret
numbers: +91998877665 numbers: +91998877665

View File

@ -45,6 +45,14 @@
} }
], ],
"auth": { "auth": {
"awsv4": {
"accessKeyId": "A12345678",
"secretAccessKey": "thisisasecret",
"sessionToken": "thisisafakesessiontoken",
"service": "execute-api",
"region": "us-east-1",
"profileName": "test_profile"
},
"basic": { "basic": {
"username": "john", "username": "john",
"password": "secret" "password": "secret"
@ -57,6 +65,7 @@
"json": "{\n \"hello\": \"world\"\n}", "json": "{\n \"hello\": \"world\"\n}",
"text": "This is a text body", "text": "This is a text body",
"xml": "<xml>\n <name>John</name>\n <age>30</age>\n</xml>", "xml": "<xml>\n <name>John</name>\n <age>30</age>\n</xml>",
"sparql": "SELECT * WHERE {\n ?subject ?predicate ?object .\n}\nLIMIT 10",
"graphql": { "graphql": {
"query": "{\n launchesPast {\n launch_site {\n site_name\n }\n launch_success\n }\n}", "query": "{\n launchesPast {\n launch_site {\n site_name\n }\n launch_success\n }\n}",
"variables": "{\n \"limit\": 5\n}" "variables": "{\n \"limit\": 5\n}"

View File

@ -57,11 +57,12 @@ const graphqlBodySchema = Yup.object({
const requestBodySchema = Yup.object({ const requestBodySchema = Yup.object({
mode: Yup.string() mode: Yup.string()
.oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql']) .oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql', 'sparql'])
.required('mode is required'), .required('mode is required'),
json: Yup.string().nullable(), json: Yup.string().nullable(),
text: Yup.string().nullable(), text: Yup.string().nullable(),
xml: Yup.string().nullable(), xml: Yup.string().nullable(),
sparql: Yup.string().nullable(),
formUrlEncoded: Yup.array().of(keyValueSchema).nullable(), formUrlEncoded: Yup.array().of(keyValueSchema).nullable(),
multipartForm: Yup.array().of(keyValueSchema).nullable(), multipartForm: Yup.array().of(keyValueSchema).nullable(),
graphql: graphqlBodySchema.nullable() graphql: graphqlBodySchema.nullable()
@ -69,6 +70,17 @@ const requestBodySchema = Yup.object({
.noUnknown(true) .noUnknown(true)
.strict(); .strict();
const authAwsV4Schema = Yup.object({
accessKeyId: Yup.string().nullable(),
secretAccessKey: Yup.string().nullable(),
sessionToken: Yup.string().nullable(),
service: Yup.string().nullable(),
region: Yup.string().nullable(),
profileName: Yup.string().nullable()
})
.noUnknown(true)
.strict();
const authBasicSchema = Yup.object({ const authBasicSchema = Yup.object({
username: Yup.string().nullable(), username: Yup.string().nullable(),
password: Yup.string().nullable() password: Yup.string().nullable()
@ -83,7 +95,8 @@ const authBearerSchema = Yup.object({
.strict(); .strict();
const authSchema = Yup.object({ const authSchema = Yup.object({
mode: Yup.string().oneOf(['none', 'basic', 'bearer']).required('mode is required'), mode: Yup.string().oneOf(['none', 'awsv4', 'basic', 'bearer']).required('mode is required'),
awsv4: authAwsV4Schema.nullable(),
basic: authBasicSchema.nullable(), basic: authBasicSchema.nullable(),
bearer: authBearerSchema.nullable() bearer: authBearerSchema.nullable()
}) })
@ -114,7 +127,8 @@ const requestSchema = Yup.object({
.strict() .strict()
.nullable(), .nullable(),
assertions: Yup.array().of(keyValueSchema).nullable(), assertions: Yup.array().of(keyValueSchema).nullable(),
tests: Yup.string().nullable() tests: Yup.string().nullable(),
docs: Yup.string().nullable()
}) })
.noUnknown(true) .noUnknown(true)
.strict(); .strict();

View File

@ -73,6 +73,7 @@ Even if you are not able to make contributions via code, please don't hesitate t
[Twitter](https://twitter.com/use_bruno) <br /> [Twitter](https://twitter.com/use_bruno) <br />
[Website](https://www.usebruno.com) <br /> [Website](https://www.usebruno.com) <br />
[Discord](https://discord.com/invite/KgcZUncpjq) [Discord](https://discord.com/invite/KgcZUncpjq)
[LinkedIn](https://www.linkedin.com/company/usebruno)
### License 📄 ### License 📄

View File

@ -19,4 +19,9 @@ sed -i'' -e 's@/_next/@_next/@g' packages/bruno-electron/web/**.html
# Remove sourcemaps # Remove sourcemaps
find packages/bruno-electron/web -name '*.map' -type f -delete find packages/bruno-electron/web -name '*.map' -type f -delete
npm run dist --workspace=packages/bruno-electron if [ "$1" == "snap" ]; then
echo "Building snap distribution"
npm run dist:snap --workspace=packages/bruno-electron
else
echo "Please pass a build distribution type"
fi