import { database as db } from '../common/database'; import { strings } from '../common/strings'; import type { BaseModel } from './index'; export const name = 'ApiSpec'; export const type = 'ApiSpec'; export const prefix = 'spc'; export const canDuplicate = true; export const canSync = false; interface BaseApiSpec { fileName: string; contentType: 'json' | 'yaml'; contents: string; } export type ApiSpec = BaseModel & BaseApiSpec; export const isApiSpec = (model: Pick): model is ApiSpec => ( model.type === type ); export function init(): BaseApiSpec { return { fileName: `New ${strings.document.singular}`, contents: '', contentType: 'yaml', }; } export async function migrate(doc: ApiSpec) { return doc; } export function getByParentId(workspaceId: string) { return db.getWhere(type, { parentId: workspaceId }); } export async function getOrCreateForParentId( workspaceId: string, patch: Partial = {}, ) { 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: Partial = {}, ) { const spec = await getOrCreateForParentId(workspaceId); return db.docUpdate(spec, patch); } export async function all() { return db.all(type); } export function update(apiSpec: ApiSpec, patch: Partial = {}) { return db.docUpdate(apiSpec, patch); } export function removeWhere(parentId: string) { return db.removeWhere(type, { parentId }); }