// @flow import type { BaseModel } from './index'; import * as db from '../common/database'; export const name = 'ApiSpec'; export const type = 'ApiSpec'; export const prefix = 'spc'; export const canDuplicate = true; export const canSync = false; type BaseApiSpec = { fileName: string, contentType: 'json' | 'yaml', contents: string, }; export type ApiSpec = BaseModel & BaseApiSpec; export function init(): BaseApiSpec { return { fileName: 'Insomnia Designer', contents: '', contentType: 'yaml', }; } export async function migrate(doc: ApiSpec): Promise { return doc; } export function getByParentId(workspaceId: string): Promise { return db.getWhere(type, { parentId: workspaceId }); } export async function getOrCreateForParentId( workspaceId: string, patch: $Shape = {}, ): Promise { const spec = await db.getWhere(type, { parentId: workspaceId }); if (!spec) { return db.docCreate(type, { ...patch, parentId: workspaceId }); } return spec; } export async function updateOrCreateForParentId( workspaceId: string, patch: $Shape = {}, ): Promise { const spec = await getOrCreateForParentId(workspaceId); return db.docUpdate(spec, patch); } export async function all(): Promise> { return db.all(type); } export function update(apiSpec: ApiSpec, patch: $Shape = {}): Promise { return db.docUpdate(apiSpec, patch); } export function removeWhere(parentId: string): Promise { return db.removeWhere(type, { parentId }); }