Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INS-1697] Create WebSocket Request #5041

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/insomnia/src/common/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export async function getRenderContextAncestors(base?: Request | GrpcRequest | W
return await db.withAncestors<RenderContextAncestor>(base || null, [
models.request.type,
models.grpcRequest.type,
models.websocketRequest.type,
models.requestGroup.type,
models.workspace.type,
models.project.type,
Expand Down
3 changes: 3 additions & 0 deletions packages/insomnia/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as _stats from './stats';
import * as _unitTest from './unit-test';
import * as _unitTestResult from './unit-test-result';
import * as _unitTestSuite from './unit-test-suite';
import * as _websocketRequest from './websocket-request';
import * as _workspace from './workspace';
import * as _workspaceMeta from './workspace-meta';

Expand Down Expand Up @@ -75,6 +76,7 @@ export const protoFile = _protoFile;
export const protoDirectory = _protoDirectory;
export const grpcRequest = _grpcRequest;
export const grpcRequestMeta = _grpcRequestMeta;
export const websocketRequest = _websocketRequest;
export const workspace = _workspace;
export const workspaceMeta = _workspaceMeta;

Expand Down Expand Up @@ -108,6 +110,7 @@ export function all() {
protoDirectory,
grpcRequest,
grpcRequestMeta,
websocketRequest,
] as const;
}

Expand Down
46 changes: 46 additions & 0 deletions packages/insomnia/src/models/websocket-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { database } from '../common/database';
import type { BaseModel } from '.';

export const name = 'WebSocket Request';

export const type = 'WebSocketRequest';

export const prefix = 'ws-req';

export const canDuplicate = true;

export const canSync = false;

export interface BaseWebSocketRequest {
name: string;
}

export type WebSocketRequest = BaseWebSocketRequest & BaseModel;

export const isWebSocketRequest = (model: Pick<BaseModel, 'type'>): model is WebSocketRequest => (
model.type === type
);

export const init = (): BaseWebSocketRequest => ({
name: 'New WebSocket Request',
});

export const migrate = (doc: WebSocketRequest) => doc;

export const create = (patch: Partial<WebSocketRequest> = {}) => {
if (!patch.parentId) {
throw new Error('TODO');
}
return database.docCreate<WebSocketRequest>(type, patch);
};

export const remove = (obj: WebSocketRequest) => database.remove(obj);

export const update = (
obj: WebSocketRequest,
patch: Partial<WebSocketRequest> = {}
) => database.docUpdate(obj, patch);

export const getById = (_id: string) => database.getWhere<WebSocketRequest>(type, { _id });

export const all = () => database.all<WebSocketRequest>(type);
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export const RequestGroupActionsDropdown = forwardRef<RequestGroupActionsDropdow

const create = useCallback((requestType: CreateRequestType) => {
if (activeWorkspaceId) {
createRequest({ parentId: requestGroup._id, requestType, workspaceId: activeWorkspaceId });
createRequest({
parentId: requestGroup._id,
requestType, workspaceId: activeWorkspaceId,
});
}
}, [activeWorkspaceId, requestGroup._id]);

Expand Down Expand Up @@ -143,6 +146,10 @@ export const RequestGroupActionsDropdown = forwardRef<RequestGroupActionsDropdow
<i className="fa fa-plus-circle" />New gRPC Request
</DropdownItem>

<DropdownItem value="WebSocket" onClick={create}>
<i className="fa fa-plus-circle" />WebSocket Request
</DropdownItem>

<DropdownItem onClick={createGroup}>
<i className="fa fa-folder" /> New Folder
<DropdownHint keyBindings={hotKeyRegistry[hotKeyRefs.REQUEST_SHOW_CREATE_FOLDER.id]} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export const SidebarCreateDropdown: FC<Props> = ({ right }) => {
<i className="fa fa-plus-circle" />gRPC Request
</DropdownItem>

<DropdownItem value="WebSocket" onClick={create}>
<i className="fa fa-plus-circle" />WebSocket Request
</DropdownItem>

<DropdownItem onClick={createGroup}>
<i className="fa fa-folder" />New Folder
<DropdownHint keyBindings={hotKeyRegistry[hotKeyRefs.REQUEST_SHOW_CREATE_FOLDER.id]} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React, { FC } from 'react';

export const WebSocketRequestPane: FC = () => {
return <div>WebSocket Request</div>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React, { FC } from 'react';

export const WebSocketResponsePane: FC = () => {
return <div>WebSocket Response</div>;
};
81 changes: 49 additions & 32 deletions packages/insomnia/src/ui/components/wrapper-debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isGrpcRequest } from '../../models/grpc-request';
import { isRemoteProject } from '../../models/project';
import { Request, RequestHeader } from '../../models/request';
import type { Response } from '../../models/response';
import { isWebSocketRequest } from '../../models/websocket-request';
import { isCollection, isDesign } from '../../models/workspace';
import { VCS } from '../../sync/vcs/vcs';
import {
Expand All @@ -27,6 +28,8 @@ import { RequestPane } from './panes/request-pane';
import { ResponsePane } from './panes/response-pane';
import { SidebarChildren } from './sidebar/sidebar-children';
import { SidebarFilter } from './sidebar/sidebar-filter';
import { WebSocketRequestPane } from './websocket-request-pane';
import { WebSocketResponsePane } from './websocket-response-pane';
import { WorkspacePageHeader } from './workspace-page-header';
import type { HandleActivityChange } from './wrapper';

Expand Down Expand Up @@ -114,42 +117,56 @@ export const WrapperDebug: FC<Props> = ({
: null}
renderPaneOne={activeWorkspace ?
<ErrorBoundary showAlert>
{activeRequest && isGrpcRequest(activeRequest) ?
<GrpcRequestPane
activeRequest={activeRequest}
environmentId={activeEnvironment ? activeEnvironment._id : ''}
workspaceId={activeWorkspace._id}
forceRefreshKey={forceRefreshKey}
settings={settings}
/>
:
<RequestPane
environmentId={activeEnvironment ? activeEnvironment._id : ''}
forceRefreshCounter={forceRefreshKey}
forceUpdateRequest={handleForceUpdateRequest}
forceUpdateRequestHeaders={handleForceUpdateRequestHeaders}
handleImport={handleImport}
headerEditorKey={headerEditorKey}
request={activeRequest}
settings={settings}
updateRequestMimeType={handleUpdateRequestMimeType}
workspace={activeWorkspace}
/>}
{activeRequest && (
isGrpcRequest(activeRequest) ? (
<GrpcRequestPane
activeRequest={activeRequest}
environmentId={activeEnvironment ? activeEnvironment._id : ''}
workspaceId={activeWorkspace._id}
forceRefreshKey={forceRefreshKey}
settings={settings}
/>
) : (
isWebSocketRequest(activeRequest) ? (
<WebSocketRequestPane />
) : (
<RequestPane
environmentId={activeEnvironment ? activeEnvironment._id : ''}
forceRefreshCounter={forceRefreshKey}
forceUpdateRequest={handleForceUpdateRequest}
forceUpdateRequestHeaders={handleForceUpdateRequestHeaders}
handleImport={handleImport}
headerEditorKey={headerEditorKey}
request={activeRequest}
settings={settings}
updateRequestMimeType={handleUpdateRequestMimeType}
workspace={activeWorkspace}
/>
)
)
)}
</ErrorBoundary>
: null}
renderPaneTwo={
<ErrorBoundary showAlert>
{activeRequest && isGrpcRequest(activeRequest) ?
<GrpcResponsePane
activeRequest={activeRequest}
forceRefreshKey={forceRefreshKey}
/>
:
<ResponsePane
handleSetFilter={handleSetResponseFilter}
request={activeRequest}
handleSetActiveResponse={handleSetActiveResponse}
/>}
{activeRequest && (
isGrpcRequest(activeRequest) ? (
<GrpcResponsePane
activeRequest={activeRequest}
forceRefreshKey={forceRefreshKey}
/>
) : (
isWebSocketRequest(activeRequest) ? (
<WebSocketResponsePane />
) : (
<ResponsePane
handleSetFilter={handleSetResponseFilter}
request={activeRequest}
handleSetActiveResponse={handleSetActiveResponse}
/>
)
)
)}
</ErrorBoundary>}
/>
);
Expand Down
12 changes: 11 additions & 1 deletion packages/insomnia/src/ui/hooks/create-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const setActiveRequest = async (
});
};

export type CreateRequestType = 'HTTP' | 'gRPC' | 'GraphQL';
export type CreateRequestType = 'HTTP' | 'gRPC' | 'GraphQL' | 'WebSocket';
type RequestCreator = (input: {
parentId: string;
requestType: CreateRequestType;
Expand Down Expand Up @@ -110,6 +110,16 @@ export const createRequest: RequestCreator = async ({
break;
}

case 'WebSocket': {
const request = await models.websocketRequest.create({
parentId,
name: 'New WebSocket Request',
});
models.stats.incrementCreatedRequests();
setActiveRequest(request._id, workspaceId);
break;
}

default:
unreachableCase(
requestType,
Expand Down
7 changes: 7 additions & 0 deletions packages/insomnia/src/ui/redux/modules/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Stats } from '../../../models/stats';
import { UnitTest } from '../../../models/unit-test';
import { UnitTestResult } from '../../../models/unit-test-result';
import { UnitTestSuite } from '../../../models/unit-test-suite';
import { WebSocketRequest } from '../../../models/websocket-request';
import { Workspace } from '../../../models/workspace';
import { WorkspaceMeta } from '../../../models/workspace-meta';

Expand Down Expand Up @@ -70,6 +71,7 @@ export interface EntitiesState {
protoDirectories: EntityRecord<ProtoDirectory>;
grpcRequests: EntityRecord<GrpcRequest>;
grpcRequestMetas: EntityRecord<GrpcRequestMeta>;
websocketRequests: EntityRecord<WebSocketRequest>;
}

export const initialEntitiesState: EntitiesState = {
Expand Down Expand Up @@ -98,6 +100,7 @@ export const initialEntitiesState: EntitiesState = {
protoDirectories: {},
grpcRequests: {},
grpcRequestMetas: {},
websocketRequests: {},
};

export function reducer(state = initialEntitiesState, action: any) {
Expand All @@ -108,6 +111,9 @@ export function reducer(state = initialEntitiesState, action: any) {

for (const doc of docs) {
const referenceName = getReducerName(doc.type);
if (!(freshState as any)[referenceName]) {
(freshState as any)[referenceName] = {};
}
(freshState as any)[referenceName][doc._id] = doc;
}

Expand Down Expand Up @@ -194,5 +200,6 @@ export async function allDocs() {
...(await models.protoDirectory.all()),
...(await models.grpcRequest.all()),
...(await models.grpcRequestMeta.all()),
...(await models.websocketRequest.all()),
];
}
15 changes: 11 additions & 4 deletions packages/insomnia/src/ui/redux/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export const selectActiveWorkspaceEntities = createSelector(

export const selectPinnedRequests = createSelector(selectEntitiesLists, entities => {
const pinned: Record<string, boolean> = {};
const requests = [...entities.requests, ...entities.grpcRequests];
const requests = [...entities.requests, ...entities.grpcRequests, ...entities.websocketRequests];
const requestMetas = [...entities.requestMetas, ...entities.grpcRequestMetas];

// Default all to unpinned
Expand Down Expand Up @@ -341,13 +341,20 @@ export const selectActiveRequest = createSelector(
selectActiveWorkspaceMeta,
(entities, workspaceMeta) => {
const id = workspaceMeta?.activeRequestId || 'n/a';

if (id in entities.requests) {
return entities.requests[id];
} else if (id in entities.grpcRequests) {
}

if (id in entities.grpcRequests) {
return entities.grpcRequests[id];
} else {
return null;
}

if (id in entities.websocketRequests) {
return entities.websocketRequests[id];
}

return null;
},
);

Expand Down