mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
5f4c19da35
Co-authored-by: Opender Singh <opender.singh@konghq.com>
119 lines
2.9 KiB
TypeScript
119 lines
2.9 KiB
TypeScript
import * as crypto from 'crypto';
|
|
import { database as db } from '../common/database';
|
|
import type { BaseModel } from './index';
|
|
import type { Workspace } from './workspace';
|
|
|
|
export const name = 'Environment';
|
|
|
|
export const type = 'Environment';
|
|
|
|
export const prefix = 'env';
|
|
|
|
export const canDuplicate = true;
|
|
|
|
export const canSync = true;
|
|
|
|
interface BaseEnvironment {
|
|
name: string;
|
|
data: Record<string, any>;
|
|
dataPropertyOrder: Record<string, any> | null;
|
|
color: string | null;
|
|
metaSortKey: number;
|
|
// For sync control
|
|
isPrivate: boolean;
|
|
}
|
|
|
|
export type Environment = BaseModel & BaseEnvironment;
|
|
|
|
export function init() {
|
|
return {
|
|
name: 'New Environment',
|
|
data: {},
|
|
dataPropertyOrder: null,
|
|
color: null,
|
|
isPrivate: false,
|
|
metaSortKey: Date.now(),
|
|
};
|
|
}
|
|
|
|
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)}`);
|
|
}
|
|
|
|
return db.docCreate<Environment>(type, patch);
|
|
}
|
|
|
|
export function update(environment: Environment, patch: Partial<Environment>) {
|
|
return db.docUpdate(environment, patch);
|
|
}
|
|
|
|
export function findByParentId(parentId: string) {
|
|
return db.find<Environment>(
|
|
type,
|
|
{
|
|
parentId,
|
|
},
|
|
{
|
|
metaSortKey: 1,
|
|
},
|
|
);
|
|
}
|
|
|
|
export async function getOrCreateForWorkspaceId(workspaceId: string) {
|
|
const environments = await db.find<Environment>(type, {
|
|
parentId: workspaceId,
|
|
});
|
|
|
|
if (!environments.length) {
|
|
return create({
|
|
parentId: workspaceId,
|
|
name: 'Base Environment',
|
|
// Deterministic base env ID. It helps reduce sync complexity since we won't have to
|
|
// de-duplicate environments.
|
|
_id: `${prefix}_${crypto.createHash('sha1').update(workspaceId).digest('hex')}`,
|
|
});
|
|
}
|
|
|
|
return environments[environments.length - 1];
|
|
}
|
|
|
|
export async function getOrCreateForWorkspace(workspace: Workspace) {
|
|
return getOrCreateForWorkspaceId(workspace._id);
|
|
}
|
|
|
|
export function getById(id: string): Promise<Environment | null> {
|
|
return db.get(type, id);
|
|
}
|
|
|
|
export async function duplicate(environment: Environment) {
|
|
const name = `${environment.name} (Copy)`;
|
|
// Get sort key of next environment
|
|
const q = {
|
|
metaSortKey: {
|
|
$gt: environment.metaSortKey,
|
|
},
|
|
};
|
|
// @ts-expect-error -- TSCONVERSION appears to be a genuine error
|
|
const [nextEnvironment] = await db.find<Environment>(type, q, { metaSortKey: 1 });
|
|
const nextSortKey = nextEnvironment ? nextEnvironment.metaSortKey : environment.metaSortKey + 100;
|
|
// Calculate new sort key
|
|
const metaSortKey = (environment.metaSortKey + nextSortKey) / 2;
|
|
return db.duplicate(environment, {
|
|
name,
|
|
metaSortKey,
|
|
});
|
|
}
|
|
|
|
export function remove(environment: Environment) {
|
|
return db.remove(environment);
|
|
}
|
|
|
|
export function all() {
|
|
return db.all<Environment>(type);
|
|
}
|