Skip to content

Commit

Permalink
throw error on naming collision
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Mar 26, 2024
1 parent b49ba5e commit d331dd9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
26 changes: 24 additions & 2 deletions packages/insomnia/src/ui/routes/mock-route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Har from 'har-format';
import React from 'react';
import { LoaderFunction, useFetcher, useParams, useRouteLoaderData } from 'react-router-dom';
import { LoaderFunction, useFetcher, useLoaderData, useParams, useRouteLoaderData } from 'react-router-dom';

Check failure on line 3 in packages/insomnia/src/ui/routes/mock-route.tsx

View workflow job for this annotation

GitHub Actions / Test

'useLoaderData' is defined but never used

import { CONTENT_TYPE_JSON, CONTENT_TYPE_PLAINTEXT, CONTENT_TYPE_XML, CONTENT_TYPE_YAML, contentTypesMap, getMockServiceURL, RESPONSE_CODE_REASONS } from '../../common/constants';
import { database as db } from '../../common/database';
Expand All @@ -17,10 +17,12 @@ import { CodeEditor } from '../components/codemirror/code-editor';
import { MockResponseHeadersEditor } from '../components/editors/mock-response-headers-editor';
import { MockResponsePane } from '../components/mocks/mock-response-pane';
import { MockUrlBar } from '../components/mocks/mock-url-bar';
import { showAlert } from '../components/modals';
import { showAlert, showModal } from '../components/modals';
import { AlertModal } from '../components/modals/alert-modal';
import { EmptyStatePane } from '../components/panes/empty-state-pane';
import { Pane, PaneBody, PaneHeader } from '../components/panes/pane';
import { SvgIcon } from '../components/svg-icon';
import { MockServerLoaderData } from './mock-server';
import { useRootLoaderData } from './root';

export interface MockRouteLoaderData {
Expand Down Expand Up @@ -91,6 +93,8 @@ export const useMockRoutePatcher = () => {

export const MockRouteRoute = () => {
const { mockServer, mockRoute } = useRouteLoaderData(':mockRouteId') as MockRouteLoaderData;
const { mockRoutes } = useRouteLoaderData('mock-server') as MockServerLoaderData;

const { userSession } = useRootLoaderData();
const patchMockRoute = useMockRoutePatcher();
const mockbinUrl = mockServer.useInsomniaCloud ? getMockServiceURL() : mockServer.url;
Expand Down Expand Up @@ -142,6 +146,15 @@ export const MockRouteRoute = () => {
});

const upsertMockbinHar = async (pathInput?: string) => {
const hasRouteInServer = mockRoutes.find(m => m.name === pathInput);
if (hasRouteInServer) {
showModal(AlertModal, {
title: 'Error',
message: 'Route name must be unique. Please enter a different name.',
});

return;
};
const compoundId = mockRoute.parentId + pathInput;
const error = await upsertBinOnRemoteFromResponse(compoundId);
if (error) {
Expand All @@ -163,6 +176,15 @@ export const MockRouteRoute = () => {
});
};
const onSend = async (pathInput: string) => {
const hasRouteInServer = mockRoutes.find(m => m.name === pathInput);
if (hasRouteInServer) {
showModal(AlertModal, {
title: 'Error',
message: 'Route name must be unique. Please enter a different name.',
});

return;
};
await upsertMockbinHar(pathInput);
const compoundId = mockRoute.parentId + pathInput;
createandSendPrivateRequest({
Expand Down
23 changes: 20 additions & 3 deletions packages/insomnia/src/ui/routes/mock-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import { WorkspaceSyncDropdown } from '../components/dropdowns/workspace-sync-dr
import { EditableInput } from '../components/editable-input';
import { Icon } from '../components/icon';
import { showModal, showPrompt } from '../components/modals';
import { AlertModal } from '../components/modals/alert-modal';
import { AskModal } from '../components/modals/ask-modal';
import { EmptyStatePane } from '../components/panes/empty-state-pane';
import { SidebarLayout } from '../components/sidebar-layout';
import { SvgIcon } from '../components/svg-icon';
import { formatMethodName } from '../components/tags/method-tag';
import { MockRouteResponse, MockRouteRoute, useMockRoutePatcher } from './mock-route';
interface LoaderData {
export interface MockServerLoaderData {
mockServerId: string;
mockRoutes: MockRoute[];
}
export const loader: LoaderFunction = async ({ params }): Promise<LoaderData> => {
export const loader: LoaderFunction = async ({ params }): Promise<MockServerLoaderData> => {
const { organizationId, projectId, workspaceId } = params;
invariant(organizationId, 'Organization ID is required');
invariant(projectId, 'Project ID is required');
Expand All @@ -46,7 +47,7 @@ const MockServerRoute = () => {
workspaceId: string;
mockRouteId: string;
};
const { mockServerId, mockRoutes } = useLoaderData() as LoaderData;
const { mockServerId, mockRoutes } = useLoaderData() as MockServerLoaderData;
const fetcher = useFetcher();
const navigate = useNavigate();
const patchMockRoute = useMockRoutePatcher();
Expand All @@ -66,6 +67,14 @@ const MockServerRoute = () => {
defaultValue: mockRoutes.find(s => s._id === id)?.name,
submitName: 'Rename',
onComplete: name => {
const hasRouteInServer = mockRoutes.find(s => s.name === name);
if (hasRouteInServer) {
showModal(AlertModal, {
title: 'Error',
message: 'Route name must be unique. Please enter a different name.',
});
return;
};
name && patchMockRoute(id, { name });
},
});
Expand Down Expand Up @@ -195,6 +204,14 @@ const MockServerRoute = () => {
});
}}
onSubmit={name => {
const hasRouteInServer = mockRoutes.find(s => s.name === name);
if (hasRouteInServer) {
showModal(AlertModal, {
title: 'Error',
message: 'Route name must be unique. Please enter a different name.',
});
return;
};
name && fetcher.submit(
{ name },
{
Expand Down

0 comments on commit d331dd9

Please sign in to comment.