fix(Export): Allow exporting workspaces without requests (#7643)

* allow exporting without requests

* seaparate export for envs

* add export to workspace dropdown
This commit is contained in:
James Gatz 2024-07-03 11:24:51 +02:00 committed by GitHub
parent 53421aa72e
commit 9ac3926afc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 12 deletions

View File

@ -455,6 +455,7 @@ const exportMockServer = async (workspace: Workspace, selectedFormat: 'json' | '
}
return JSON.stringify(data);
};
export const exportMockServerToFile = async (workspace: Workspace) => {
const options = [{ name: 'Insomnia v4 (JSON)', value: VALUE_JSON }, { name: 'Insomnia v4 (YAML)', value: VALUE_YAML }];
const lastFormat = window.localStorage.getItem('insomnia.lastExportFormat');
@ -490,8 +491,67 @@ export const exportMockServerToFile = async (workspace: Workspace) => {
}
},
});
};
const exportGlobalEnvironment = async (workspace: Workspace, selectedFormat: 'json' | 'yaml') => {
const data: Insomnia4Data = {
_type: 'export',
__export_format: EXPORT_FORMAT,
__export_date: new Date(),
__export_source: `insomnia.desktop.app:v${getAppVersion()}`,
resources: [],
};
const baseEnvironment = await models.environment.getOrCreateForParentId(workspace._id);
const subEnvironments = await models.environment.findByParentId(baseEnvironment._id);
data.resources.push({ ...workspace, _type: 'workspace' });
data.resources.push({ ...baseEnvironment, _type: 'environment' });
subEnvironments.map(environment => data.resources.push({ ...environment, _type: 'environment' }));
if (selectedFormat === 'yaml') {
return YAML.stringify(data);
}
return JSON.stringify(data);
};
export const exportGlobalEnvironmentToFile = async (workspace: Workspace) => {
const options = [{ name: 'Insomnia v4 (JSON)', value: VALUE_JSON }, { name: 'Insomnia v4 (YAML)', value: VALUE_YAML }];
const lastFormat = window.localStorage.getItem('insomnia.lastExportFormat');
const defaultValue = options.find(({ value }) => value === lastFormat) ? lastFormat : VALUE_JSON;
showModal(SelectModal, {
title: 'Select Export Type',
value: defaultValue,
options,
message: 'Which format would you like to export as?',
onDone: async selectedFormat => {
invariant(selectedFormat, 'expected selected format to be defined');
invariant(selectedFormat === 'json' || selectedFormat === 'yaml', 'unexpected selected format');
window.localStorage.setItem('insomnia.lastExportFormat', selectedFormat);
const fileName = await showSaveExportedFileDialog({
exportedFileNamePrefix: workspace.name,
selectedFormat,
});
if (!fileName) {
return;
}
try {
const stringifiedExport = await exportGlobalEnvironment(workspace, selectedFormat);
writeExportedFileToFileSystem(fileName, stringifiedExport, err => err && console.warn('Export failed', err));
window.main.trackSegmentEvent({ event: SegmentEvent.dataExport, properties: { type: selectedFormat, scope: 'environment' } });
} catch (err) {
showError({
title: 'Export Failed',
error: err,
message: 'Export failed due to an unexpected error',
});
return;
}
},
});
};
export const exportRequestsToFile = (workspaceId: string, requestIds: string[]) => {
showSelectExportTypeModal({
onDone: async selectedFormat => {

View File

@ -4,7 +4,7 @@ import { useFetcher, useParams } from 'react-router-dom';
import { parseApiSpec } from '../../../common/api-specs';
import { getProductName } from '../../../common/constants';
import { exportMockServerToFile } from '../../../common/export';
import { exportGlobalEnvironmentToFile, exportMockServerToFile } from '../../../common/export';
import { getWorkspaceLabel } from '../../../common/get-workspace-label';
import { RENDER_PURPOSE_NO_RENDER } from '../../../common/render';
import type { ApiSpec } from '../../../models/api-spec';
@ -153,9 +153,15 @@ export const WorkspaceCardDropdown: FC<Props> = props => {
<ItemContent
label="Export"
icon="file-export"
onClick={() => workspace.scope !== 'mock-server'
? setIsExportModalOpen(true)
: exportMockServerToFile(workspace)}
onClick={() => {
if (workspace.scope === 'mock-server') {
return exportMockServerToFile(workspace);
}
if (workspace.scope === 'environment') {
return exportGlobalEnvironmentToFile(workspace);
}
return setIsExportModalOpen(true);
}}
/>
</DropdownItem>
<DropdownItem aria-label='Settings'>

View File

@ -5,7 +5,7 @@ import { useFetcher, useParams, useRouteLoaderData } from 'react-router-dom';
import { getProductName } from '../../../common/constants';
import { database as db } from '../../../common/database';
import { exportMockServerToFile } from '../../../common/export';
import { exportGlobalEnvironmentToFile, exportMockServerToFile } from '../../../common/export';
import { getWorkspaceLabel } from '../../../common/get-workspace-label';
import { RENDER_PURPOSE_NO_RENDER } from '../../../common/render';
import { PlatformKeyCombinations } from '../../../common/settings';
@ -122,9 +122,17 @@ export const WorkspaceDropdown: FC = () => {
id: 'Export',
name: 'Export',
icon: <Icon icon='file-export' />,
action: () => activeWorkspace.scope !== 'mock-server'
? setIsExportModalOpen(true)
: exportMockServerToFile(activeWorkspace),
action: () => {
if (activeWorkspace.scope === 'mock-server') {
return exportMockServerToFile(activeWorkspace);
}
if (activeWorkspace.scope === 'environment') {
return exportGlobalEnvironmentToFile(activeWorkspace);
}
return setIsExportModalOpen(true);
},
}],
}];
@ -187,9 +195,17 @@ export const WorkspaceDropdown: FC = () => {
id: 'export',
name: 'Export',
icon: <Icon icon='file-export' />,
action: () => activeWorkspace.scope !== 'mock-server'
? setIsExportModalOpen(true)
: exportMockServerToFile(activeWorkspace),
action: () => {
if (activeWorkspace.scope === 'mock-server') {
return exportMockServerToFile(activeWorkspace);
}
if (activeWorkspace.scope === 'environment') {
return exportGlobalEnvironmentToFile(activeWorkspace);
}
return setIsExportModalOpen(true);
},
},
{
id: 'settings',