Skip to content

Commit f73229f

Browse files
committed
Refactor and enhance mock data and variable interpolation
feat: add mock data generation functions using faker.js refactor: remove interpolateRandom function and integrate random value interpolation into interpolateVars feat: add randomIPv4 function and fix randomCreditCardMask implementation refactor: simplify variable interpolation by integrating mock variable handling refactor: interpolation logic. feat: add mock variable suggestions fixed formatting refactored code to reduce duplications Refactor mock variable handling by replacing mockVarsNames with mockDataFunctions and removing unused files Remove @faker-js/faker dependency from bruno-electron/package.json
1 parent e129753 commit f73229f

File tree

6 files changed

+84
-203
lines changed

6 files changed

+84
-203
lines changed

packages/bruno-app/src/components/CodeEditor/index.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import { JSHINT } from 'jshint';
1414
import stripJsonComments from 'strip-json-comments';
1515
import { getAllVariables } from 'utils/collections';
1616

17+
import { mockDataFunctions } from '@usebruno/common';
18+
1719
let CodeMirror;
1820
const SERVER_RENDERED = typeof window === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
1921
const TAB_SIZE = 2;
22+
const MOCK_FUNCTION_SUGGESTIONS = Object.keys(mockDataFunctions).map(key => `$${key}`);
2023

2124
if (!SERVER_RENDERED) {
2225
CodeMirror = require('codemirror');
@@ -87,7 +90,8 @@ if (!SERVER_RENDERED) {
8790
'bru.runner.skipRequest()',
8891
'bru.runner.stopExecution()'
8992
];
90-
CodeMirror.registerHelper('hint', 'brunoJS', (editor, options) => {
93+
94+
CodeMirror.registerHelper('hint', 'brunoJS', (editor, _options) => {
9195
const cursor = editor.getCursor();
9296
const currentLine = editor.getLine(cursor.line);
9397
let startBru = cursor.ch;
@@ -114,6 +118,7 @@ if (!SERVER_RENDERED) {
114118
}
115119
return result;
116120
});
121+
117122
CodeMirror.commands.autocomplete = (cm, hint, options) => {
118123
cm.showHint({ hint, ...options });
119124
};
@@ -280,6 +285,7 @@ export default class CodeEditor extends React.Component {
280285
editor.on('change', this._onEdit);
281286
this.addOverlay();
282287
}
288+
283289
if (this.props.mode == 'javascript') {
284290
editor.on('keyup', function (cm, event) {
285291
const cursor = editor.getCursor();
@@ -300,6 +306,40 @@ export default class CodeEditor extends React.Component {
300306
}
301307
});
302308
}
309+
310+
const getHints = (cm) => {
311+
const cursor = cm.getCursor();
312+
const currentString = cm.getRange({ line: cursor.line, ch: 0 }, cursor);
313+
314+
const match = currentString.match(/\{\{\$(\w*)$/);
315+
if (!match) return null;
316+
317+
const wordMatch = match[1];
318+
if (!wordMatch) return null;
319+
320+
const suggestions = MOCK_FUNCTION_SUGGESTIONS.filter(name => name.startsWith(`$${wordMatch}`));
321+
if (!suggestions.length) return null;
322+
323+
const startPos = { line: cursor.line, ch: currentString.lastIndexOf('{{$') + 2 }; // +2 accounts for `{{
324+
325+
return {
326+
list: suggestions,
327+
from: startPos,
328+
to: cm.getCursor(),
329+
};
330+
};
331+
332+
editor.on('inputRead', function (cm, event) {
333+
const hints = getHints(cm);
334+
if (!hints) {
335+
return;
336+
}
337+
338+
cm.showHint({
339+
hint: () => hints,
340+
completeSingle: false,
341+
});
342+
});
303343
}
304344

305345
componentDidUpdate(prevProps) {

packages/bruno-app/src/components/SingleLineEditor/index.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { defineCodeMirrorBrunoVariablesMode, MaskedEditor } from 'utils/common/c
55
import StyledWrapper from './StyledWrapper';
66
import { IconEye, IconEyeOff } from '@tabler/icons';
77

8+
import { mockDataFunctions } from '@usebruno/common';
9+
810
let CodeMirror;
911
const SERVER_RENDERED = typeof window === 'undefined' || global['PREVENT_CODEMIRROR_RENDER'] === true;
12+
const MOCK_FUNCTION_SUGGESTIONS = Object.keys(mockDataFunctions).map(key => `$${key}`);
1013

1114
if (!SERVER_RENDERED) {
1215
CodeMirror = require('codemirror');
@@ -26,6 +29,7 @@ class SingleLineEditor extends Component {
2629
maskInput: props.isSecret || false // Always mask the input by default (if it's a secret)
2730
};
2831
}
32+
2933
componentDidMount() {
3034
// Initialize CodeMirror as a single line editor
3135
/** @type {import("codemirror").Editor} */
@@ -75,6 +79,7 @@ class SingleLineEditor extends Component {
7579
'Shift-Tab': false
7680
}
7781
});
82+
7883
if (this.props.autocomplete) {
7984
this.editor.on('keyup', (cm, event) => {
8085
if (!cm.state.completionActive /*Enables keyboard navigation in autocomplete list*/ && event.key !== 'Enter') {
@@ -83,6 +88,41 @@ class SingleLineEditor extends Component {
8388
}
8489
});
8590
}
91+
92+
const getHints = (cm) => {
93+
const cursor = cm.getCursor();
94+
const currentString = cm.getRange({ line: cursor.line, ch: 0 }, cursor);
95+
96+
const match = currentString.match(/\{\{\$(\w*)$/);
97+
if (!match) return null;
98+
99+
const wordMatch = match[1];
100+
if (!wordMatch) return null;
101+
102+
const suggestions = MOCK_FUNCTION_SUGGESTIONS.filter(name => name.startsWith(`$${wordMatch}`));
103+
if (!suggestions.length) return null;
104+
105+
const startPos = { line: cursor.line, ch: currentString.lastIndexOf('{{$') + 2 }; // +2 accounts for `{{`
106+
107+
return {
108+
list: suggestions,
109+
from: startPos,
110+
to: cm.getCursor(),
111+
};
112+
};
113+
114+
this.editor.on('inputRead', function (cm, event) {
115+
const hints = getHints(cm);
116+
if (!hints) {
117+
return;
118+
}
119+
120+
cm.showHint({
121+
hint: () => hints,
122+
completeSingle: false,
123+
});
124+
});
125+
86126
this.editor.setValue(String(this.props.value) || '');
87127
this.editor.on('change', this._onEdit);
88128
this.addOverlay(variables);
@@ -94,7 +134,6 @@ class SingleLineEditor extends Component {
94134
_enableMaskedEditor = (enabled) => {
95135
if (typeof enabled !== 'boolean') return;
96136

97-
console.log('Enabling masked editor: ' + enabled);
98137
if (enabled == true) {
99138
if (!this.maskedEditor) this.maskedEditor = new MaskedEditor(this.editor, '*');
100139
this.maskedEditor.enable();

packages/bruno-common/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
import { mockDataFunctions } from './utils/faker-functions';
2+
3+
export { mockDataFunctions };
14
export { default as interpolate } from './interpolate';

packages/bruno-electron/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
},
2727
"dependencies": {
2828
"@aws-sdk/credential-providers": "3.750.0",
29-
"@faker-js/faker": "^9.5.1",
3029
"@usebruno/common": "0.1.0",
3130
"@usebruno/converters": "^0.1.0",
3231
"@usebruno/js": "0.12.0",
@@ -63,7 +62,6 @@
6362
"socks-proxy-agent": "^8.0.2",
6463
"tough-cookie": "^4.1.3",
6564
"uuid": "^9.0.0",
66-
"@faker-js/faker": "^9.5.0",
6765
"yup": "^0.32.11"
6866
},
6967
"optionalDependencies": {

packages/bruno-electron/src/ipc/network/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,6 @@ const registerNetworkIpc = (mainWindow) => {
370370
collection.globalEnvironmentVariables = scriptResult.globalEnvironmentVariables;
371371
}
372372

373-
// interpolate random/dynamic values inside request
374-
interpolateRandom(request);
375-
376373
// interpolate variables inside request
377374
interpolateVars(request, envVars, runtimeVariables, processEnvVars);
378375

packages/bruno-electron/src/ipc/network/interpolate-random.js

Lines changed: 0 additions & 196 deletions
This file was deleted.

0 commit comments

Comments
 (0)