Skip to content

Commit

Permalink
feat: Add 'executeScript' switch for generate code modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-treason committed Oct 31, 2024
1 parent c806f53 commit d3ebd94
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ type CodeGeneratorProps = {
requestUid: string;
targetId: string;
clientId: string;
executeScript: 'true' | 'false';
};

export const CodeGenerator: React.FC<CodeGeneratorProps> = ({ clientId, collectionUid, requestUid, targetId }) => {
export const CodeGenerator: React.FC<CodeGeneratorProps> = ({
clientId,
collectionUid,
requestUid,
targetId,
executeScript
}) => {
// Debounce values here, to prevent flickering if invalid values are in state
const [debounced] = useDebouncedValue({ clientId, targetId }, 50);
const generateCodeResult = useGenerateCode(collectionUid, requestUid, debounced.targetId, debounced.clientId);
const generateCodeResult = useGenerateCode(
collectionUid,
requestUid,
debounced.targetId,
debounced.clientId,
executeScript
);

if (generateCodeResult.error) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
import { LanguageClientSelector } from './LanguageClientSelector';
import { useLanguageClient } from '../hooks/useLangugeClient';
import { CodeGenerator } from './CodeGenerator';
import { useLocalStorage } from '@mantine/hooks';

type CodeGeneratorModalProps = {
requestUid?: string;
Expand All @@ -22,6 +23,10 @@ export const CodeGeneratorModal: React.FC<CodeGeneratorModalProps> = ({
onClose
}) => {
const { clientId, setClientId, setTargetId, targetId } = useLanguageClient();
const [executeScript, setExecuteScript] = useLocalStorage<'true' | 'false'>({
key: 'generate-code-execute-script',
defaultValue: 'false'
});

return (
<Modal size={'xl'} title={'Generate code'} opened={opened} onClose={onClose}>
Expand All @@ -30,10 +35,18 @@ export const CodeGeneratorModal: React.FC<CodeGeneratorModalProps> = ({
setClientId={setClientId}
targetId={targetId}
setTargetId={setTargetId}
executeScript={executeScript}
setExecuteScript={setExecuteScript}
/>

{requestUid !== undefined && collectionUid !== undefined ? (
<CodeGenerator clientId={clientId} targetId={targetId} collectionUid={collectionUid} requestUid={requestUid} />
<CodeGenerator
clientId={clientId}
targetId={targetId}
collectionUid={collectionUid}
requestUid={requestUid}
executeScript={executeScript}
/>
) : null}
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of bruno-app.
* For license information, see the file LICENSE_GPL3 at the root directory of this distribution.
*/
import { Group, Select, Stack, Tabs, Text } from '@mantine/core';
import { Group, Select, Stack, Switch, Tabs, Text } from '@mantine/core';
import { useEffect, useMemo } from 'react';

const options = {
Expand Down Expand Up @@ -81,13 +81,17 @@ type LanguageClientSelectorProps = {
setTargetId: (newTargetId: string) => void;
clientId: string;
setClientId: (newClientId: string) => void;
executeScript: 'true' | 'false';
setExecuteScript: (newClientId: 'true' | 'false') => void;
};

export const LanguageClientSelector: React.FC<LanguageClientSelectorProps> = ({
clientId,
setClientId,
setTargetId,
targetId
targetId,
executeScript,
setExecuteScript
}) => {
const selectOptions = useMemo(() => {
return Object.keys(options).map((targetId) => ({
Expand Down Expand Up @@ -137,6 +141,13 @@ export const LanguageClientSelector: React.FC<LanguageClientSelectorProps> = ({
</Tabs.List>
</Tabs>
</Stack>

<Switch
checked={executeScript === 'true'}
onClick={() => setExecuteScript(executeScript === 'true' ? 'false' : 'true')}
label="Execute pre-request script"
mt={'lg'}
/>
</Group>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import { globalEnvironmentStore } from 'src/store/globalEnvironmentStore';

type ReduxStore = { collections: { collections: CollectionSchema[] } };

export function useGenerateCode(collectionId: string, requestId: string, targetId: string, clientId: string) {
export function useGenerateCode(
collectionId: string,
requestId: string,
targetId: string,
clientId: string,
executeScript: 'true' | 'false'
) {
const collection: CollectionSchema = useSelector((store: ReduxStore) =>
findCollectionByUid(store.collections.collections, collectionId)
);

return useQuery<string>({
queryKey: [collectionId, requestId, targetId, clientId],
queryKey: [collectionId, requestId, targetId, clientId, executeScript],
retry: 0,
queryFn: async () => {
const item = findItemInCollection(collection, requestId);
Expand All @@ -36,7 +42,8 @@ export function useGenerateCode(collectionId: string, requestId: string, targetI

const options = {
targetId,
clientId
clientId,
executeScript: executeScript === 'true'
};

return await window.ipcRenderer.invoke(
Expand Down
5 changes: 4 additions & 1 deletion packages/bruno-core/src/codeGenerator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createHar } from './createHar';
type GenerateCodeOptions = {
targetId: string;
clientId: string;
executeScript: boolean;
};

export async function generateCode(
Expand Down Expand Up @@ -86,7 +87,9 @@ async function doGenerateCode(context: RequestContext, options: GenerateCodeOpti

// Folder Headers are also applied here
applyCollectionSettings(context, folderData);
await preRequestScript(context, folderData);
if (options.executeScript) {
await preRequestScript(context, folderData);
}
interpolateRequest(context);

const harRequest = await createHar(context);
Expand Down

0 comments on commit d3ebd94

Please sign in to comment.