Skip to content

Commit

Permalink
create model
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Sep 7, 2022
1 parent a5b1850 commit 2732a43
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/insomnia/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 _webSocketPayload from './websocket-payload';
import * as _webSocketRequest from './websocket-request';
import * as _workspace from './workspace';
import * as _workspaceMeta from './workspace-meta';
Expand Down Expand Up @@ -77,6 +78,7 @@ export const protoFile = _protoFile;
export const protoDirectory = _protoDirectory;
export const grpcRequest = _grpcRequest;
export const grpcRequestMeta = _grpcRequestMeta;
export const webSocketPayload = _webSocketPayload;
export const webSocketRequest = _webSocketRequest;
export const workspace = _workspace;
export const workspaceMeta = _workspaceMeta;
Expand Down Expand Up @@ -111,6 +113,7 @@ export function all() {
protoDirectory,
grpcRequest,
grpcRequestMeta,
webSocketPayload,
webSocketRequest,
] as const;
}
Expand Down
66 changes: 66 additions & 0 deletions packages/insomnia/src/models/websocket-payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { database } from '../common/database';
import type { BaseModel } from '.';

export const name = 'WebSocket Payload';

export const type = 'WebSocketPayload';

export const prefix = 'ws-msg';

export const canDuplicate = true;

// @TODO: enable this at some point
export const canSync = false;

export interface BaseWebSocketPayload {
value: string;
mode: 'application/json' | 'text/plain';
}

export type WebSocketPayload = BaseModel & BaseWebSocketPayload & { type: typeof type };

export const isWebSocketPayloadId = (id: string | null) => (
id?.startsWith(`${prefix}_`)
);

export const init = (): BaseWebSocketPayload => ({
value: '',
mode: 'application/json',
});

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

export const create = (patch: Partial<WebSocketPayload> = {}) => {
if (!patch.parentId) {
throw new Error(`New WebSocketPayload missing \`parentId\`: ${JSON.stringify(patch)}`);
}

return database.docCreate<WebSocketPayload>(type, patch);
};

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

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

export async function duplicate(request: WebSocketPayload, patch: Partial<WebSocketPayload> = {}) {
// Only set name and "(Copy)" if the patch does
// not define it and the request itself has a name.
// Otherwise leave it blank so the request URL can
// fill it in automatically.
if (!patch.name && request.name) {
patch.name = `${request.name} (Copy)`;
}

return database.duplicate<WebSocketPayload>(request, {
name,
...patch,
});
}

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

export const all = () => database.all<WebSocketPayload>(type);
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const WebSocketRequestForm: FC<FormProps> = ({
environmentId,
}) => {
const editorRef = useRef<UnconnectedCodeEditor>(null);
const onChange = (value: string) => {
// @TODO: update payload model
// const payload = await models.websocketPayload.create({
// parentId,
// value: '',
// });
console.log(value);
};
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const message = editorRef.current?.getValue() || '';
Expand Down Expand Up @@ -105,6 +113,7 @@ const WebSocketRequestForm: FC<FormProps> = ({
mode={payloadType}
ref={editorRef}
defaultValue=''
onChange={onChange}
enableNunjucks
/>
</EditorWrapper>
Expand Down
4 changes: 4 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 { WebSocketPayload } from '../../../models/websocket-payload';
import { WebSocketRequest } from '../../../models/websocket-request';
import { Workspace } from '../../../models/workspace';
import { WorkspaceMeta } from '../../../models/workspace-meta';
Expand Down Expand Up @@ -71,6 +72,7 @@ export interface EntitiesState {
protoDirectories: EntityRecord<ProtoDirectory>;
grpcRequests: EntityRecord<GrpcRequest>;
grpcRequestMetas: EntityRecord<GrpcRequestMeta>;
webSocketPayloads: EntityRecord<WebSocketPayload>;
webSocketRequests: EntityRecord<WebSocketRequest>;
}

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

Expand Down Expand Up @@ -200,6 +203,7 @@ export async function allDocs() {
...(await models.protoDirectory.all()),
...(await models.grpcRequest.all()),
...(await models.grpcRequestMeta.all()),
...(await models.webSocketPayload.all()),
...(await models.webSocketRequest.all()),
];
}

0 comments on commit 2732a43

Please sign in to comment.