refactor: rename binaryFile to file and update related references

This commit is contained in:
Sanjai Kumar
2025-02-04 17:41:24 +05:30
committed by Anoop M D
parent 2b9960adc7
commit c9b2aa7760
29 changed files with 226 additions and 121 deletions

View File

@@ -3,22 +3,22 @@ import { get, cloneDeep, isArray } from 'lodash';
import { IconTrash } from '@tabler/icons';
import { useDispatch } from 'react-redux';
import { useTheme } from 'providers/Theme';
import { addBinaryFile, updateBinaryFile, deleteBinaryFile } from 'providers/ReduxStore/slices/collections/index';
import { addFile as _addFile, updateFile, deleteFile } from 'providers/ReduxStore/slices/collections/index';
import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
import FilePickerEditor from 'components/FilePickerEditor/index';
import SingleLineEditor from 'components/SingleLineEditor/index';
const Binary = ({ item, collection }) => {
const FileBody = ({ item, collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();
const params = item.draft ? get(item, 'draft.request.body.binaryFile') : get(item, 'request.body.binaryFile');
const params = item.draft ? get(item, 'draft.request.body.file') : get(item, 'request.body.file');
const [enabledFileUid, setEnableFileUid] = useState(params && params.length ? params[0].uid : '');
const addFile = () => {
dispatch(
addBinaryFile({
_addFile({
itemUid: item.uid,
collectionUid: collection.uid,
})
@@ -47,7 +47,7 @@ const Binary = ({ item, collection }) => {
}
}
dispatch(
updateBinaryFile({
updateFile({
param: param,
itemUid: item.uid,
collectionUid: collection.uid
@@ -57,7 +57,7 @@ const Binary = ({ item, collection }) => {
const handleRemoveParams = (param) => {
dispatch(
deleteBinaryFile({
deleteFile({
paramUid: param.uid,
itemUid: item.uid,
collectionUid: collection.uid
@@ -161,4 +161,4 @@ const Binary = ({ item, collection }) => {
</StyledWrapper>
);
};
export default Binary;
export default FileBody;

View File

@@ -132,7 +132,7 @@ const RequestBodyMode = ({ item, collection }) => {
className="dropdown-item"
onClick={() => {
dropdownTippyRef.current.hide();
onModeChange('binaryFile');
onModeChange('file');
}}
>
File / Binary

View File

@@ -8,7 +8,7 @@ import { useTheme } from 'providers/Theme';
import { updateRequestBody } from 'providers/ReduxStore/slices/collections';
import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';
import Binary from '../Binary/index';
import FileBody from '../FileBody/index';
const RequestBody = ({ item, collection }) => {
const dispatch = useDispatch();
@@ -63,8 +63,8 @@ const RequestBody = ({ item, collection }) => {
);
}
if (bodyMode === 'binaryFile') {
return <Binary item={item} collection={collection}/>
if (bodyMode === 'file') {
return <FileBody item={item} collection={collection}/>
}
if (bodyMode === 'formUrlEncoded') {
@@ -77,4 +77,4 @@ const RequestBody = ({ item, collection }) => {
return <StyledWrapper className="w-full">No Body</StyledWrapper>;
};
export default RequestBody;
export default RequestBody;

View File

@@ -759,7 +759,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
sparql: null,
multipartForm: null,
formUrlEncoded: null,
binaryFile: null
file: null
},
auth: auth ?? {
mode: 'none'

View File

@@ -897,7 +897,7 @@ export const collectionsSlice = createSlice({
}
}
},
addBinaryFile: (state, action) => {
addFile: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
@@ -907,9 +907,9 @@ export const collectionsSlice = createSlice({
if (!item.draft) {
item.draft = cloneDeep(item);
}
item.draft.request.body.binaryFile = item.draft.request.body.binaryFile || [];
item.draft.request.body.file = item.draft.request.body.file || [];
item.draft.request.body.binaryFile.push({
item.draft.request.body.file.push({
uid: uuid(),
filePath: '',
contentType: '',
@@ -918,7 +918,7 @@ export const collectionsSlice = createSlice({
}
}
},
updateBinaryFile: (state, action) => {
updateFile: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
@@ -929,7 +929,7 @@ export const collectionsSlice = createSlice({
item.draft = cloneDeep(item);
}
const param = find(item.draft.request.body.binaryFile, (p) => p.uid === action.payload.param.uid);
const param = find(item.draft.request.body.file, (p) => p.uid === action.payload.param.uid);
if (param) {
const contentType = mime.contentType(path.extname(action.payload.param.filePath));
@@ -937,7 +937,7 @@ export const collectionsSlice = createSlice({
param.contentType = action.payload.param.contentType || contentType || '';
param.selected = action.payload.param.selected;
item.draft.request.body.binaryFile = item.draft.request.body.binaryFile.map((p) => {
item.draft.request.body.file = item.draft.request.body.file.map((p) => {
p.selected = p.uid === param.uid;
return p;
});
@@ -945,7 +945,7 @@ export const collectionsSlice = createSlice({
}
}
},
deleteBinaryFile: (state, action) => {
deleteFile: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if (collection) {
@@ -956,13 +956,13 @@ export const collectionsSlice = createSlice({
item.draft = cloneDeep(item);
}
item.draft.request.body.binaryFile = filter(
item.draft.request.body.binaryFile,
item.draft.request.body.file = filter(
item.draft.request.body.file,
(p) => p.uid !== action.payload.paramUid
);
if (item.draft.request.body.binaryFile.length > 0) {
item.draft.request.body.binaryFile[0].selected = true;
if (item.draft.request.body.file.length > 0) {
item.draft.request.body.file[0].selected = true;
}
}
}
@@ -1023,8 +1023,8 @@ export const collectionsSlice = createSlice({
item.draft.request.body.sparql = action.payload.content;
break;
}
case 'binaryFile': {
item.draft.request.body.binaryFile = action.payload.content;
case 'file': {
item.draft.request.body.file = action.payload.content;
break;
}
case 'formUrlEncoded': {
@@ -2028,9 +2028,9 @@ export const {
addMultipartFormParam,
updateMultipartFormParam,
deleteMultipartFormParam,
addBinaryFile,
updateBinaryFile,
deleteBinaryFile,
addFile,
updateFile,
deleteFile,
moveMultipartFormParam,
updateRequestAuthMode,
updateRequestBodyMode,

View File

@@ -14,7 +14,7 @@ const createContentType = (mode) => {
return 'application/json';
case 'multipartForm':
return 'multipart/form-data';
case 'binaryFile':
case 'file':
return 'application/octet-stream';
default:
return '';
@@ -93,8 +93,8 @@ const createPostData = (body, type) => {
...(param.type === 'file' && { fileName: param.value })
}))
};
case 'binaryFile':
const binary = {
case 'file':
return {
mimeType: body[body.mode].filter((param) => param.enabled)[0].contentType,
params: body[body.mode]
.filter((param) => param.selected)
@@ -102,7 +102,6 @@ const createPostData = (body, type) => {
value: param.filePath,
}))
};
return binary;
default:
return {
mimeType: contentType,

View File

@@ -14,7 +14,7 @@ export const deleteUidsInItems = (items) => {
each(get(item, 'request.vars.assertions'), (a) => delete a.uid);
each(get(item, 'request.body.multipartForm'), (param) => delete param.uid);
each(get(item, 'request.body.formUrlEncoded'), (param) => delete param.uid);
each(get(item, 'request.body.binaryFile'), (param) => delete param.uid);
each(get(item, 'request.body.file'), (param) => delete param.uid);
}
if (item.items && item.items.length) {

View File

@@ -281,7 +281,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
});
};
const copyBinaryFileParams = (params = []) => {
const copyFileParams = (params = []) => {
return map(params, (param) => {
return {
uid: param.uid,
@@ -320,7 +320,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
sparql: si.request.body.sparql,
formUrlEncoded: copyFormUrlEncodedParams(si.request.body.formUrlEncoded),
multipartForm: copyMultipartFormParams(si.request.body.multipartForm),
binaryFile: copyBinaryFileParams(si.request.body.binaryFile)
file: copyFileParams(si.request.body.file)
},
script: si.request.script,
vars: si.request.vars,
@@ -673,7 +673,7 @@ export const humanizeRequestBodyMode = (mode) => {
label = 'SPARQL';
break;
}
case 'binaryFile': {
case 'file': {
label = 'File / Binary';
break;
}
@@ -777,7 +777,7 @@ export const refreshUidsInItem = (item) => {
each(get(item, 'request.params'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.multipartForm'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.formUrlEncoded'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.binaryFile'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.file'), (param) => (param.uid = uuid()));
return item;
};
@@ -788,13 +788,13 @@ export const deleteUidsInItem = (item) => {
const headers = get(item, 'request.headers', []);
const bodyFormUrlEncoded = get(item, 'request.body.formUrlEncoded', []);
const bodyMultipartForm = get(item, 'request.body.multipartForm', []);
const binaryFile = get(item, 'request.body.binaryFile', []);
const file = get(item, 'request.body.file', []);
params.forEach((param) => delete param.uid);
headers.forEach((header) => delete header.uid);
bodyFormUrlEncoded.forEach((param) => delete param.uid);
bodyMultipartForm.forEach((param) => delete param.uid);
binaryFile.forEach((param) => delete param.uid);
file.forEach((param) => delete param.uid);
return item;
};

View File

@@ -117,7 +117,7 @@ describe('curlToJson', () => {
value: ['/path/to/file'],
enabled: true,
contentType: 'application/json;charset=utf-8',
type: 'binaryFile'
type: 'file'
}
]
});

View File

@@ -51,7 +51,7 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque
multipartForm: null,
formUrlEncoded: null,
graphql: null,
binaryFile: null
file: null
};
if (parsedBody && contentType && typeof contentType === 'string') {
@@ -59,8 +59,8 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque
body.mode = 'graphql';
body.graphql = parseGraphQL(parsedBody);
} else if (requestType === 'http-request' && request.isDataBinary) {
body.mode = 'binaryFile';
body.binaryFile = parsedBody;
body.mode = 'file';
body.file = parsedBody;
}else if (contentType.includes('application/json')) {
body.mode = 'json';
body.json = convertToCodeMirrorJson(parsedBody);

View File

@@ -35,7 +35,7 @@ export const updateUidsInCollection = (_collection) => {
each(get(item, 'request.assertions'), (a) => (a.uid = uuid()));
each(get(item, 'request.body.multipartForm'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.formUrlEncoded'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.binaryFile'), (param) => (param.uid = uuid()));
each(get(item, 'request.body.file'), (param) => (param.uid = uuid()));
if (item.items && item.items.length) {
updateItemUids(item.items);

View File

@@ -28,7 +28,7 @@ const { makeAxiosInstance } = require('./axios-instance');
const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper');
const { addDigestInterceptor } = require('./digestauth-helper');
const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../../utils/proxy-util');
const { chooseFileToSave, writeBinaryFile, writeFile } = require('../../utils/filesystem');
const { chooseFileToSave, writeFile } = require('../../utils/filesystem');
const { getCookieStringForUrl, addCookieToJar, getDomainsWithCookies } = require('../../utils/cookies');
const {
resolveOAuth2AuthorizationCodeAccessToken,
@@ -614,7 +614,7 @@ const registerNetworkIpc = (mainWindow) => {
url: request.url,
method: request.method,
headers: request.headers,
data: request.mode == 'binaryFile'? "<request body redacted>": safeParseJSON(safeStringifyJSON(request.data)) ,
data: request.mode == 'file'? "<request body redacted>": safeParseJSON(safeStringifyJSON(request.data)) ,
timestamp: Date.now()
},
collectionUid,
@@ -1371,7 +1371,7 @@ const registerNetworkIpc = (mainWindow) => {
if (encoding === 'utf-8') {
await writeFile(filePath, data);
} else {
await writeBinaryFile(filePath, data);
await writeFile(filePath, data, true);
}
}
} catch (error) {

View File

@@ -253,14 +253,14 @@ const prepareRequest = async (item, collection, abortController) => {
axiosRequest.data = request.body.sparql;
}
if (request.body.mode === 'binaryFile') {
if (request.body.mode === 'file') {
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/octet-stream'; // Default headers for binary file uploads
}
const binaryFile = find(request.body.binaryFile, (param) => param.selected);
if (binaryFile) {
let { filePath, contentType } = binaryFile;
const bodyFile = find(request.body.file, (param) => param.selected);
if (bodyFile) {
let { filePath, contentType } = bodyFile;
axiosRequest.headers['content-type'] = contentType;
if (filePath) {

View File

@@ -240,7 +240,7 @@ const hydrateRequestWithUuid = (request, pathname) => {
const assertions = get(request, 'request.assertions', []);
const bodyFormUrlEncoded = get(request, 'request.body.formUrlEncoded', []);
const bodyMultipartForm = get(request, 'request.body.multipartForm', []);
const binaryFile = get(request, 'request.body.binaryFile', []);
const file = get(request, 'request.body.file', []);
params.forEach((param) => (param.uid = uuid()));
headers.forEach((header) => (header.uid = uuid()));
@@ -249,7 +249,7 @@ const hydrateRequestWithUuid = (request, pathname) => {
assertions.forEach((assertion) => (assertion.uid = uuid()));
bodyFormUrlEncoded.forEach((param) => (param.uid = uuid()));
bodyMultipartForm.forEach((param) => (param.uid = uuid()));
binaryFile.forEach((param) => (param.uid = uuid()));
file.forEach((param) => (param.uid = uuid()));
return request;
};

View File

@@ -68,20 +68,13 @@ function normalizeWslPath(pathname) {
return pathname.replace(/^\/wsl.localhost/, '\\\\wsl.localhost').replace(/\//g, '\\');
}
const writeFile = async (pathname, content) => {
const writeFile = async (pathname, content, isBinary = false) => {
try {
fs.writeFileSync(pathname, content, {
encoding: 'utf8'
await fs.writeFile(pathname, content, {
encoding: !isBinary ? "utf-8" : null
});
} catch (err) {
return Promise.reject(err);
}
};
const writeBinaryFile = async (pathname, content) => {
try {
fs.writeFileSync(pathname, content);
} catch (err) {
console.error(`Error writing file at ${pathname}:`, err);
return Promise.reject(err);
}
};
@@ -265,7 +258,6 @@ module.exports = {
isWSLPath,
normalizeWslPath,
writeFile,
writeBinaryFile,
hasJsonExtension,
hasBruExtension,
createDirectory,

View File

@@ -25,7 +25,7 @@ const grammar = ohm.grammar(`Bru {
BruFile = (meta | http | query | params | headers | auths | bodies | varsandassert | script | tests | docs)*
auths = authawsv4 | authbasic | authbearer | authdigest | authNTLM | authOAuth2 | authwsse | authapikey
bodies = bodyjson | bodytext | bodyxml | bodysparql | bodygraphql | bodygraphqlvars | bodyforms | body
bodyforms = bodyformurlencoded | bodymultipart | bodybinaryfile
bodyforms = bodyformurlencoded | bodymultipart | bodyfile
params = paramspath | paramsquery
nl = "\\r"? "\\n"
@@ -102,7 +102,7 @@ const grammar = ohm.grammar(`Bru {
bodyformurlencoded = "body:form-urlencoded" dictionary
bodymultipart = "body:multipart-form" dictionary
bodybinaryfile = "body:binary-file" dictionary
bodyfile = "body:file" dictionary
script = scriptreq | scriptres
scriptreq = "script:pre-request" st* "{" nl* textblock tagend
@@ -174,7 +174,7 @@ const multipartExtractContentType = (pair) => {
}
};
const binaryFileExtractContentType = (pair) => {
const fileExtractContentType = (pair) => {
if (_.isString(pair.value)) {
const match = pair.value.match(/^(.*?)\s*@contentType\((.*?)\)\s*$/);
if (match && match.length > 2) {
@@ -204,10 +204,10 @@ const mapPairListToKeyValPairsMultipart = (pairList = [], parseEnabled = true) =
});
};
const mapPairListToKeyValPairsBinaryFile = (pairList = [], parseEnabled = true) => {
const mapPairListToKeyValPairsFile = (pairList = [], parseEnabled = true) => {
const pairs = mapPairListToKeyValPairs(pairList, parseEnabled);
return pairs.map((pair) => {
binaryFileExtractContentType(pair);
fileExtractContentType(pair);
if (pair.value.startsWith('@file(') && pair.value.endsWith(')')) {
let filePath = pair.value.replace(/^@file\(/, '').replace(/\)$/, '');
@@ -609,10 +609,10 @@ const sem = grammar.createSemantics().addAttribute('ast', {
}
};
},
bodybinaryfile(_1, dictionary) {
bodyfile(_1, dictionary) {
return {
body: {
binaryFile: mapPairListToKeyValPairsBinaryFile(dictionary.ast)
file: mapPairListToKeyValPairsFile(dictionary.ast)
}
};
},

View File

@@ -314,13 +314,13 @@ ${indentString(body.sparql)}
}
if (body && body.binaryFile && body.binaryFile.length) {
bru += `body:binary-file {`;
const binaryFiles = enabled(body.binaryFile, "selected").concat(disabled(body.binaryFile, "selected"));
if (body && body.file && body.file.length) {
bru += `body:file {`;
const files = enabled(body.file, "selected").concat(disabled(body.file, "selected"));
if (binaryFiles.length) {
if (files.length) {
bru += `\n${indentString(
binaryFiles
files
.map((item) => {
const selected = item.selected ? '' : '~';
const contentType =

View File

@@ -102,7 +102,7 @@ body:multipart-form {
~message: hello
}
body:binary-file {
body:file {
file: @file(path/to/file.json) @contentType(application/json)
file: @file(path/to/file.json) @contentType(application/json)
~file: @file(path/to/file2.json) @contentType(application/json)

View File

@@ -138,7 +138,7 @@
"type": "text"
}
],
"binaryFile" : [
"file" : [
{
"filePath": "path/to/file.json",
"contentType": "application/json",

View File

@@ -75,7 +75,7 @@ const multipartFormSchema = Yup.object({
.strict();
const binaryFileSchema = Yup.object({
const fileSchema = Yup.object({
uid: uidSchema,
filePath: Yup.string().nullable(),
contentType: Yup.string().nullable(),
@@ -86,7 +86,7 @@ const binaryFileSchema = Yup.object({
const requestBodySchema = Yup.object({
mode: Yup.string()
.oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql', 'sparql', 'binaryFile'])
.oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql', 'sparql', 'file'])
.required('mode is required'),
json: Yup.string().nullable(),
text: Yup.string().nullable(),
@@ -95,7 +95,7 @@ const requestBodySchema = Yup.object({
formUrlEncoded: Yup.array().of(keyValueSchema).nullable(),
multipartForm: Yup.array().of(multipartFormSchema).nullable(),
graphql: graphqlBodySchema.nullable(),
binaryFile: Yup.array().of(binaryFileSchema).nullable()
file: Yup.array().of(fileSchema).nullable()
})
.noUnknown(true)
.strict();

View File

@@ -1,27 +0,0 @@
meta {
name: binary-files-types
type: http
seq: 1
}
post {
url: {{host}}/api/binaryFile/binary-file-types
body: binaryFile
auth: none
}
body:binary-file {
file1: @file() @contentType()
file2: @file(binaryFile/binary-file.json) @contentType()
file3: @file(binaryFile/binary-file.json) @contentType(application/json)
}
assert {
res.status: eq 200
res.body.find(p=>p.name === 'file1').value[0]: isUndefined
res.body.find(p=>p.name === 'file1').contentType: isUndefined
res.body.find(p=>p.name === 'file2').value[0]: eq binaryFile/binary-file.json
res.body.find(p=>p.name === 'file2').contentType: eq isUndefined
res.body.find(p=>p.name === 'file3').value[0]: eq binaryFile/binary-file.json
res.body.find(p=>p.name === 'file3').contentType: eq application/json
}

View File

@@ -1,9 +0,0 @@
{
"version": "1",
"name": "bruno-testing",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,40 @@
meta {
name: echo file body
type: http
seq: 4
}
post {
url: {{echo-host}}
body: file
auth: none
}
body:file {
file: @file(ping.bru) @contentType(bru)
}
tests {
test("should return bru file body contents", function() {
const data = res.getBody();
const expectedData = `meta {
name: ping
type: http
seq: 1
}
get {
url: {{host}}/ping
body: none
auth: none
}
`;
expect(res.getBody()).to.eql(expectedData);
});
test("should return proper header", function() {
const contentType = res.getHeader('content-type');
expect(contentType).to.eql('bru');
});
}

View File

@@ -0,0 +1,22 @@
meta {
name: echo image body
type: http
seq: 1
}
post {
url: {{echo-host}}
body: file
auth: none
}
body:file {
file: @file(bruno.png) @contentType(image/png)
}
tests {
test("should return proper header", function() {
const contentType = res.getHeader('content-type');
expect(contentType).to.eql('image/png');
});
}

View File

@@ -0,0 +1,55 @@
meta {
name: echo json file body
type: http
seq: 2
}
post {
url: {{echo-host}}
body: file
auth: none
}
body:file {
file: @file(file.json) @contentType(application/json; charset=utf-8)
}
tests {
test("should return json file body contents buffer", function() {
const data = res.getBody();
expect(res.getBody()).to.eql({
"type": "Buffer",
"data": [
123,
10,
32,
32,
34,
104,
101,
108,
108,
111,
34,
58,
32,
34,
98,
114,
117,
110,
111,
34,
10,
125,
10
]
});
});
test("should return proper header", function() {
const contentType = res.getHeader('content-type');
expect(contentType).to.eql('application/json; charset=utf-8');
});
}

View File

@@ -0,0 +1,30 @@
meta {
name: echo text file body
type: http
seq: 3
}
post {
url: {{echo-host}}
body: file
auth: none
}
body:file {
file: @file(file.txt) @contentType(text/plain; charset=utf-8)
}
tests {
test("should return json file body contents", function() {
const data = res.getBody();
const expectedData = `file.txt
hello, bruno`;
expect(res.getBody()).to.eql(expectedData);
});
test("should return proper header", function() {
const contentType = res.getHeader('content-type');
expect(contentType).to.eql('text/plain; charset=utf-8');
});
}

View File

@@ -6,10 +6,10 @@ meta {
post {
url: {{echo-host}}
body: binaryFile
body: file
auth: none
}
body:binary-file {
body:file {
file: @file(bruno.png) @contentType(image/png)
}

View File

@@ -0,0 +1,3 @@
file.txt
hello, bruno