insomnia/packages/insomnia-app/app/models/api-spec.js
Opender Singh 049964bb9b
Prompt for import as collection or document (#3130)
* feat: add prompt and update import logic

* feat: update usages and plugin api

* chore: WIP

* chore: convert to options

* chore: don't create workspace model until needed

* chore: add OR condition

* chore: add workspace name and don't change scope

* feat: prompt with appropriate name

* chore: rename

* chore: rename type

* chore: update signature

* chore: properly type the import functions

* feat: don't activate the workspace after importing

* chore: show loading on homepage

* fix: typo

* chore: fix tests and rename

* Update packages/insomnia-app/app/common/__tests__/import.test.js

Co-authored-by: David Marby <david@dmarby.se>

Co-authored-by: David Marby <david@dmarby.se>
2021-03-03 10:16:48 +13:00

68 lines
1.6 KiB
JavaScript

// @flow
import type { BaseModel } from './index';
import * as db from '../common/database';
import { strings } from '../common/strings';
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: `New ${strings.document}`,
contents: '',
contentType: 'yaml',
};
}
export async function migrate(doc: ApiSpec): Promise<ApiSpec> {
return doc;
}
export function getByParentId(workspaceId: string): Promise<ApiSpec | null> {
return db.getWhere(type, { parentId: workspaceId });
}
export async function getOrCreateForParentId(
workspaceId: string,
patch: $Shape<ApiSpec> = {},
): Promise<ApiSpec> {
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<ApiSpec> = {},
): Promise<ApiSpec> {
const spec = await getOrCreateForParentId(workspaceId);
return db.docUpdate(spec, patch);
}
export async function all(): Promise<Array<ApiSpec>> {
return db.all(type);
}
export function update(apiSpec: ApiSpec, patch: $Shape<ApiSpec> = {}): Promise<ApiSpec> {
return db.docUpdate(apiSpec, patch);
}
export function removeWhere(parentId: string): Promise<void> {
return db.removeWhere(type, { parentId });
}