insomnia/packages/insomnia-app/app/plugins/context/data.ts
2021-07-23 11:04:56 +12:00

57 lines
1.6 KiB
TypeScript

import { exportWorkspacesData, exportWorkspacesHAR } from '../../common/export';
import type { ImportRawConfig } from '../../common/import';
import { importRaw, importUri } from '../../common/import';
import type { Workspace, WorkspaceScope } from '../../models/workspace';
interface PluginImportOptions {
workspaceId?: string;
scope?: WorkspaceScope;
}
interface InsomniaExport {
workspace?: Workspace;
includePrivate?: boolean;
format?: 'json' | 'yaml';
}
type HarExport = Omit<InsomniaExport, 'format'>;
const buildImportRawConfig = (options: PluginImportOptions): ImportRawConfig => ({
getWorkspaceId: () => Promise.resolve(options.workspaceId || null),
getWorkspaceScope: options.scope && (() => (
Promise.resolve<WorkspaceScope>(options.scope as WorkspaceScope))
),
});
export const init = () => ({
data: {
import: {
uri: async (uri: string, options: PluginImportOptions = {}) => {
await importUri(uri, buildImportRawConfig(options));
},
raw: async (text: string, options: PluginImportOptions = {}) => {
await importRaw(text, buildImportRawConfig(options));
},
},
export: {
insomnia: async ({
workspace,
includePrivate,
format,
}: InsomniaExport = {}) => exportWorkspacesData(
workspace ? [workspace] : [],
Boolean(includePrivate),
format || 'json',
),
har: async ({
workspace,
includePrivate,
}: HarExport = {}) => exportWorkspacesHAR(
workspace ? [workspace] : [],
Boolean(includePrivate),
),
},
},
});