From d1e1da53e508624179f58abb26c851e700d9ce11 Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Thu, 29 Oct 2020 11:23:07 +1300 Subject: [PATCH] Save a GrpcRequest after protofile selection (#2785) --- .../models/__tests__/grpc-request-meta.test.js | 10 +++++++++- .../app/models/__tests__/request-meta.test.js | 18 ++++++++++++++++++ .../app/models/grpc-request-meta.js | 18 ++++++++++++++++++ .../insomnia-app/app/models/request-meta.js | 16 ++++++++++++++++ .../components/modals/request-create-modal.js | 12 +++++++----- .../components/proto-file/proto-file-list.js | 2 +- packages/insomnia-app/app/ui/containers/app.js | 10 +++++----- 7 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 packages/insomnia-app/app/models/__tests__/request-meta.test.js diff --git a/packages/insomnia-app/app/models/__tests__/grpc-request-meta.test.js b/packages/insomnia-app/app/models/__tests__/grpc-request-meta.test.js index 890601554..b1edac2b6 100644 --- a/packages/insomnia-app/app/models/__tests__/grpc-request-meta.test.js +++ b/packages/insomnia-app/app/models/__tests__/grpc-request-meta.test.js @@ -6,6 +6,7 @@ describe('init()', () => { it('contains all required fields', async () => { expect(models.grpcRequestMeta.init()).toEqual({ pinned: false, + lastActive: 0, }); }); }); @@ -26,6 +27,7 @@ describe('create()', () => { parentId: 'greq_124', pinned: true, type: 'GrpcRequestMeta', + lastActive: 0, }; expect(request).toEqual(expected); @@ -43,15 +45,21 @@ describe('create()', () => { parentId: 'greq_124', pinned: false, type: 'GrpcRequestMeta', + lastActive: 0, }; expect(request).toEqual(expected); }); it('fails when missing parentId', async () => { - Date.now = jest.fn().mockReturnValue(1478795580200); expect(() => models.grpcRequestMeta.create({ pinned: true })).toThrow( 'New GrpcRequestMeta missing `parentId`', ); }); + + it('fails when parentId prefix is not that of a GrpcRequest', async () => { + expect(() => models.grpcRequestMeta.create({ parentId: 'req_123' })).toThrow( + 'Expected the parent of GrpcRequestMeta to be a GrpcRequest', + ); + }); }); diff --git a/packages/insomnia-app/app/models/__tests__/request-meta.test.js b/packages/insomnia-app/app/models/__tests__/request-meta.test.js new file mode 100644 index 000000000..e37895891 --- /dev/null +++ b/packages/insomnia-app/app/models/__tests__/request-meta.test.js @@ -0,0 +1,18 @@ +import * as models from '../index'; +import { globalBeforeEach } from '../../__jest__/before-each'; + +describe('create()', () => { + beforeEach(globalBeforeEach); + + it('fails when missing parentId', async () => { + expect(() => models.requestMeta.create({ pinned: true })).toThrow( + 'New RequestMeta missing `parentId`', + ); + }); + + it('fails when parentId prefix is not that of a Request', async () => { + expect(() => models.requestMeta.create({ parentId: 'greq_123' })).toThrow( + 'Expected the parent of RequestMeta to be a Request', + ); + }); +}); diff --git a/packages/insomnia-app/app/models/grpc-request-meta.js b/packages/insomnia-app/app/models/grpc-request-meta.js index 91e66ace9..581afd009 100644 --- a/packages/insomnia-app/app/models/grpc-request-meta.js +++ b/packages/insomnia-app/app/models/grpc-request-meta.js @@ -1,6 +1,7 @@ // @flow import * as db from '../common/database'; import type { BaseModel } from './index'; +import { prefix as grpcRequestPrefix } from './grpc-request'; export const name = 'gRPC Request Meta'; export const type = 'GrpcRequestMeta'; @@ -10,6 +11,7 @@ export const canSync = false; type BaseGrpcRequestMeta = { pinned: boolean, + lastActive: number, }; export type GrpcRequestMeta = BaseModel & BaseGrpcRequestMeta; @@ -17,6 +19,7 @@ export type GrpcRequestMeta = BaseModel & BaseGrpcRequestMeta; export function init() { return { pinned: false, + lastActive: 0, }; } @@ -29,6 +32,10 @@ export function create(patch: $Shape = {}): Promise) { + const requestMeta = await getByParentId(parentId); + + if (requestMeta) { + return update(requestMeta, patch); + } else { + const newPatch = Object.assign({ parentId }, patch); + return create(newPatch); + } +} + export function all(): Promise> { return db.all(type); } diff --git a/packages/insomnia-app/app/models/request-meta.js b/packages/insomnia-app/app/models/request-meta.js index 40ad0017a..0ea9ce779 100644 --- a/packages/insomnia-app/app/models/request-meta.js +++ b/packages/insomnia-app/app/models/request-meta.js @@ -2,6 +2,7 @@ import * as db from '../common/database'; import { PREVIEW_MODE_FRIENDLY } from '../common/constants'; import type { BaseModel } from './index'; +import { prefix as requestPrefix } from './request'; export const name = 'Request Meta'; export const type = 'RequestMeta'; @@ -46,6 +47,10 @@ export function create(patch: $Shape = {}) { throw new Error('New RequestMeta missing `parentId` ' + JSON.stringify(patch)); } + if (!patch.parentId.startsWith(`${requestPrefix}_`)) { + throw new Error('Expected the parent of RequestMeta to be a Request'); + } + return db.docCreate(type, patch); } @@ -67,6 +72,17 @@ export async function getOrCreateByParentId(parentId: string) { return create({ parentId }); } +export async function updateOrCreateByParentId(parentId: string, patch: $Shape) { + const requestMeta = await getByParentId(parentId); + + if (requestMeta) { + return update(requestMeta, patch); + } else { + const newPatch = Object.assign({ parentId }, patch); + return create(newPatch); + } +} + export function all(): Promise> { return db.all(type); } diff --git a/packages/insomnia-app/app/ui/components/modals/request-create-modal.js b/packages/insomnia-app/app/ui/components/modals/request-create-modal.js index e8bd8ccf2..a24b11a46 100644 --- a/packages/insomnia-app/app/ui/components/modals/request-create-modal.js +++ b/packages/insomnia-app/app/ui/components/modals/request-create-modal.js @@ -56,12 +56,14 @@ class RequestCreateModal extends PureComponent { const requestName = this._input.value; if (selectedMethod === METHOD_GRPC) { showModal(ProtoFilesModal, { - onSave: async protofileId => { - // TODO: Create new grpc request with the name and selected proto file - INS-198 - console.log(`Create request name: [${requestName}], and protofileId: [${protofileId}]`); + onSave: async (protoFileId: string) => { + const createdRequest = await models.grpcRequest.create({ + parentId, + name: requestName, + protoFileId, + }); - const createdRequestId = 'gr_123'; - this._onComplete(createdRequestId); + this._onComplete(createdRequest._id); }, }); } else { diff --git a/packages/insomnia-app/app/ui/components/proto-file/proto-file-list.js b/packages/insomnia-app/app/ui/components/proto-file/proto-file-list.js index 507eb7151..707524c87 100644 --- a/packages/insomnia-app/app/ui/components/proto-file/proto-file-list.js +++ b/packages/insomnia-app/app/ui/components/proto-file/proto-file-list.js @@ -27,7 +27,7 @@ const ProtoFileList = ({ {!protoFiles.length && No proto files exist for this workspace} {protoFiles.map(p => (