Skip to content
Draft
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
8 changes: 5 additions & 3 deletions packages/insomnia/src/common/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import type { CaCertificate } from '~/models/ca-certificate';
import type { ClientCertificate } from '~/models/client-certificate';
import type { CloudProviderCredential } from '~/models/cloud-credential';
import type { WorkspaceMeta } from '~/models/workspace-meta';
import { invariant } from '~/utils/invariant';

import { mustGetModel } from '../models';
import { getModel } from '../models';
import type { CookieJar } from '../models/cookie-jar';
import { type Environment } from '../models/environment';
import type { GitRepository } from '../models/git-repository';
Expand Down Expand Up @@ -116,7 +117,8 @@ export const database = {
const flushId = await database.bufferChanges();

async function next<T extends BaseModel>(docToCopy: T, patch: Partial<T>) {
const model = mustGetModel(docToCopy.type);
const model = getModel(docToCopy.type);
invariant(model, `Invalid model type ${docToCopy.type}`);
const overrides = {
_id: generateId(model.prefix),
modified: Date.now(),
Expand All @@ -131,7 +133,7 @@ export const database = {
// 2. Get all the children
for (const type of Object.keys(nedbBucket) as AllTypes[]) {
// Note: We never want to duplicate a response
if (!models.canDuplicate(type)) {
if (!model.canDuplicate) {
continue;
}

Expand Down
28 changes: 0 additions & 28 deletions packages/insomnia/src/models/__tests__/index.test.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/insomnia/src/models/api-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export function init(): BaseApiSpec {
};
}

export function migrate(doc: ApiSpec) {
return doc;
}

export function getByParentId(workspaceId: string) {
return db.findOne<ApiSpec>(type, { parentId: workspaceId });
}
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/ca-certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export function init(): BaseCaCertificate {

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

export function migrate(doc: CaCertificate) {
return doc;
}

export function create(patch: Partial<CaCertificate> = {}) {
if (!patch.parentId) {
throw new Error('New CaCertificate missing `parentId`: ' + JSON.stringify(patch));
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/client-certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ export function init(): BaseClientCertificate {

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

export function migrate(doc: ClientCertificate) {
return doc;
}

export function create(patch: Partial<ClientCertificate> = {}) {
if (!patch.parentId) {
throw new Error('New ClientCertificate missing `parentId`: ' + JSON.stringify(patch));
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/cloud-credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ export function init(): Partial<CloudProviderCredential> {
};
}

export function migrate(doc: CloudProviderCredential) {
return doc;
}

export function create(patch: Partial<CloudProviderCredential> = {}) {
return db.docCreate<CloudProviderCredential>(type, patch);
}
Expand Down
21 changes: 0 additions & 21 deletions packages/insomnia/src/models/cookie-jar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ export function init() {
};
}

export function migrate(doc: CookieJar) {
try {
doc = migrateCookieId(doc);
return doc;
} catch (e) {
console.log('[db] Error during cookie jar migration', e);
throw e;
}
}

export async function create(patch: Partial<CookieJar>) {
if (!patch.parentId) {
throw new Error(`New CookieJar missing \`parentId\`: ${JSON.stringify(patch)}`);
Expand Down Expand Up @@ -90,14 +80,3 @@ export async function getById(id: string): Promise<CookieJar | undefined> {
export async function update(cookieJar: CookieJar, patch: Partial<CookieJar> = {}) {
return db.docUpdate(cookieJar, patch);
}

/** Ensure every cookie has an ID property */
function migrateCookieId(cookieJar: CookieJar) {
for (const cookie of cookieJar.cookies) {
if (!cookie.id) {
cookie.id = uuidv4();
}
}

return cookieJar;
}
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ export function init() {
};
}

export function migrate(doc: Environment) {
return doc;
}

export function create(patch: Partial<Environment> = {}) {
if (!patch.parentId) {
throw new Error(`New Environment missing \`parentId\`: ${JSON.stringify(patch)}`);
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/git-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ interface BaseGitCredentials {
};
}

export function migrate(doc: GitCredentials) {
return doc;
}

export function create(patch: Partial<GitCredentials> = {}) {
return db.docCreate<GitCredentials>(type, patch);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/git-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ export interface BaseGitRepository {

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

export function migrate(doc: GitRepository) {
return doc;
}

export function create(patch: Partial<GitRepository> = {}) {
return db.docCreate<GitRepository>(type, {
uriNeedsMigration: false,
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/grpc-request-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export function init() {
};
}

export function migrate(doc: GrpcRequestMeta) {
return doc;
}

export function create(patch: Partial<GrpcRequestMeta> = {}) {
if (!patch.parentId) {
throw new Error('New GrpcRequestMeta missing `parentId`');
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/grpc-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export function init(): BaseGrpcRequest {
};
}

export function migrate(doc: GrpcRequest) {
return doc;
}

export function create(patch: Partial<GrpcRequest> = {}) {
if (!patch.parentId) {
throw new Error('New GrpcRequest missing `parentId`');
Expand Down
76 changes: 53 additions & 23 deletions packages/insomnia/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { v4 } from 'uuid';

import { invariant } from '~/utils/invariant';

import { generateId } from '../common/misc';
import { typedKeys } from '../utils';
import * as _apiSpec from './api-spec';
import * as _caCertificate from './ca-certificate';
import * as _clientCertificate from './client-certificate';
import * as _cloudCredential from './cloud-credential';
import type { CookieJar } from './cookie-jar';
import * as _cookieJar from './cookie-jar';
import * as _environment from './environment';
import * as _gitCredentials from './git-credentials';
Expand All @@ -18,13 +23,15 @@ import * as _project from './project';
import * as _protoDirectory from './proto-directory';
import * as _protoFile from './proto-file';
import * as _request from './request';
import { migrateBody } from './request';
import * as _requestGroup from './request-group';
import * as _requestGroupMeta from './request-group-meta';
import * as _requestMeta from './request-meta';
import * as _requestVersion from './request-version';
import * as _response from './response';
import * as _runnerTestResult from './runner-test-result';
import * as _settings from './settings';
import { migrateEnsureHotKeys } from './settings';
import * as _socketIOPayload from './socket-io-payload';
import * as _socketIORequest from './socket-io-request';
import * as _socketIoResponse from './socket-io-response';
Expand All @@ -37,6 +44,7 @@ import * as _webSocketPayload from './websocket-payload';
import * as _webSocketRequest from './websocket-request';
import * as _webSocketResponse from './websocket-response';
import * as _workspace from './workspace';
import { _migrateExtractClientCertificates, _migrateScope } from './workspace';
import * as _workspaceMeta from './workspace-meta';

export interface BaseModel {
Expand Down Expand Up @@ -202,30 +210,10 @@ export function getModel(type: string) {
return all().find(m => m.type === type) || null;
}

export function mustGetModel(type: string) {
const model = getModel(type);

if (!model) {
throw new Error(`The model type ${type} must exist but could not be found.`);
}

return model;
}

export function canDuplicate(type: string) {
const model = getModel(type);
return model ? model.canDuplicate : false;
}

export async function initModel<T extends BaseModel>(type: string, ...sources: Record<string, any>[]): Promise<T> {
const model = getModel(type);

if (!model) {
const choices = all()
.map(m => m.type)
.join(', ');
throw new Error(`Tried to init invalid model "${type}". Choices are ${choices}`);
}
invariant(isValidType(type), `Invalid model type ${type}`);
invariant(model, `Model type ${type} not found`);

// Define global default fields
const objectDefaults = Object.assign(
Expand All @@ -248,7 +236,7 @@ export async function initModel<T extends BaseModel>(type: string, ...sources: R

// Migrate the model
// NOTE: Do migration before pruning because we might need to look at those fields
const migratedDoc = model.migrate(fullObject);
const migratedDoc = migrationFunction(fullObject);
// optional keys do not generated in init method but should allow update.
// If we put those keys in init method, all related models will show as modified in git sync.
const modelOptionalKeys: string[] = 'optionalKeys' in model ? model.optionalKeys || [] : [];
Expand All @@ -261,7 +249,49 @@ export async function initModel<T extends BaseModel>(type: string, ...sources: R

return migratedDoc as T;
}
/** Ensure every cookie has an ID property */
function migrateCookieId(cookieJar: CookieJar) {
for (const cookie of cookieJar.cookies) {
if (!cookie.id) {
cookie.id = v4();
}
}

return cookieJar;
}

const migrationFunction = (doc: BaseModel) => {
// Perform any necessary migrations on the document
if (doc.type === cookieJar.type) {
doc = migrateCookieId(doc);
}
if (doc.type === response.type) {
if (doc.bodyCompression === '__NEEDS_MIGRATION__') {
doc.bodyCompression = 'zip';
}
}
if (doc.type === settings.type) {
doc = migrateEnsureHotKeys(doc);
}
if (doc.type === workspace.type) {
doc = _migrateExtractClientCertificates(doc);
if (typeof doc.name !== 'string') {
doc.name = 'My Workspace';
}
doc = _migrateScope(doc);
}
if (doc.type === request.type) {
doc = migrateBody(doc);
if (typeof doc.url !== 'string') {
doc.url = '';
}
const isAuthSet = doc?.authentication && 'username' in doc.authentication && doc.authentication.username;
if (isAuthSet && !doc.authentication.type) {
doc.authentication.type = 'basic';
}
}
return doc;
};
// Use function instead of object to avoid issues with circular dependencies
export const getAllDescendantMap = (): Partial<Record<AllTypes, AllTypes[]>> => {
return {
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/mock-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ export function init(): BaseMockRoute {

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

export function migrate(doc: MockRoute) {
return doc;
}

export function create(patch: Partial<MockRoute> = {}) {
if (!patch.parentId) {
throw new Error('New MockRoute missing `parentId`: ' + JSON.stringify(patch));
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/mock-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export function init(): BaseMockServer {

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

export function migrate(doc: MockServer) {
return doc;
}

export function create(patch: Partial<MockServer> = {}) {
if (!patch.parentId) {
throw new Error('New MockServer missing `parentId`: ' + JSON.stringify(patch));
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/o-auth-2-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export function init(): BaseOAuth2Token {
};
}

export function migrate(doc: OAuth2Token) {
return doc;
}

export function create(patch: Partial<OAuth2Token> = {}) {
if (!patch.parentId) {
throw new Error(`New OAuth2Token missing \`parentId\` ${JSON.stringify(patch)}`);
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/plugin-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export function init(): BasePluginData {
};
}

export function migrate(doc: PluginData) {
return doc;
}

export function create(patch: Partial<PluginData> = {}) {
return db.docCreate<PluginData>(type, patch);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export function init(): Partial<Project> {
};
}

export function migrate(project: Project) {
return project;
}

export function createId() {
return generateId(prefix);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/proto-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export function init(): BaseProtoDirectory {
};
}

export function migrate(doc: ProtoDirectory) {
return doc;
}

export function createId() {
return generateId(prefix);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/insomnia/src/models/proto-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export function init(): BaseProtoFile {
};
}

export function migrate(doc: ProtoFile) {
return doc;
}

export function create(patch: Partial<ProtoFile> = {}) {
if (!patch.parentId) {
throw new Error('New ProtoFile missing `parentId`');
Expand Down
Loading
Loading