insomnia/packages/insomnia-app/app/models/api-spec.ts

73 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-04-26 20:33:39 +00:00
import type { BaseModel } from './index';
import { database as db } from '../common/database';
import { strings } from '../common/strings';
2020-04-26 20:33:39 +00:00
export const name = 'ApiSpec';
2020-04-26 20:33:39 +00:00
export const type = 'ApiSpec';
2020-04-26 20:33:39 +00:00
export const prefix = 'spc';
2020-04-26 20:33:39 +00:00
export const canDuplicate = true;
export const canSync = false;
2020-04-26 20:33:39 +00:00
interface BaseApiSpec {
fileName: string;
contentType: 'json' | 'yaml';
contents: string;
}
2020-04-26 20:33:39 +00:00
export type ApiSpec = BaseModel & BaseApiSpec;
export function init(): BaseApiSpec {
return {
fileName: `New ${strings.document}`,
2020-04-26 20:33:39 +00:00
contents: '',
contentType: 'yaml',
};
}
export async function migrate(doc: ApiSpec) {
return doc;
2020-04-26 20:33:39 +00:00
}
export function getByParentId(workspaceId: string) {
return db.getWhere<ApiSpec>(type, { parentId: workspaceId });
2020-04-26 20:33:39 +00:00
}
export async function getOrCreateForParentId(
workspaceId: string,
patch: Partial<ApiSpec> = {},
) {
const spec = await db.getWhere<ApiSpec>(type, {
parentId: workspaceId,
});
2020-04-26 20:33:39 +00:00
if (!spec) {
return db.docCreate<ApiSpec>(type, { ...patch, parentId: workspaceId });
2020-04-26 20:33:39 +00:00
}
return spec;
}
export async function updateOrCreateForParentId(
workspaceId: string,
patch: Partial<ApiSpec> = {},
) {
2020-04-26 20:33:39 +00:00
const spec = await getOrCreateForParentId(workspaceId);
return db.docUpdate(spec, patch);
}
export async function all() {
return db.all<ApiSpec>(type);
2020-04-26 20:33:39 +00:00
}
export function update(apiSpec: ApiSpec, patch: Partial<ApiSpec> = {}) {
2020-04-26 20:33:39 +00:00
return db.docUpdate(apiSpec, patch);
}
export function removeWhere(parentId: string) {
return db.removeWhere(type, { parentId });
}