2017-11-26 13:42:43 +00:00
|
|
|
// @flow
|
2018-06-25 17:42:50 +00:00
|
|
|
import { convert } from 'insomnia-importers';
|
2019-04-18 18:13:12 +00:00
|
|
|
import clone from 'clone';
|
2017-11-22 14:00:12 +00:00
|
|
|
import * as db from './database';
|
|
|
|
import * as har from './har';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from '../models/index';
|
2017-11-18 22:47:54 +00:00
|
|
|
import * as models from '../models/index';
|
2020-04-26 20:33:39 +00:00
|
|
|
import { CONTENT_TYPE_GRAPHQL, getAppVersion } from './constants';
|
2019-08-12 20:50:41 +00:00
|
|
|
import { showError, showModal } from '../ui/components/modals/index';
|
2017-11-22 14:00:12 +00:00
|
|
|
import AlertModal from '../ui/components/modals/alert-modal';
|
2017-05-03 17:48:23 +00:00
|
|
|
import fs from 'fs';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { fnOrString, generateId } from './misc';
|
2019-04-18 03:50:23 +00:00
|
|
|
import YAML from 'yaml';
|
2020-04-26 20:33:39 +00:00
|
|
|
import { trackEvent } from './analytics';
|
2016-11-19 03:21:15 +00:00
|
|
|
|
2019-08-12 20:50:41 +00:00
|
|
|
const WORKSPACE_ID_KEY = '__WORKSPACE_ID__';
|
|
|
|
const BASE_ENVIRONMENT_ID_KEY = '__BASE_ENVIRONMENT_ID__';
|
|
|
|
|
2019-04-18 03:50:23 +00:00
|
|
|
const EXPORT_FORMAT = 4;
|
2016-11-23 03:07:35 +00:00
|
|
|
|
2016-11-19 03:21:15 +00:00
|
|
|
const EXPORT_TYPE_REQUEST = 'request';
|
|
|
|
const EXPORT_TYPE_REQUEST_GROUP = 'request_group';
|
|
|
|
const EXPORT_TYPE_WORKSPACE = 'workspace';
|
|
|
|
const EXPORT_TYPE_COOKIE_JAR = 'cookie_jar';
|
|
|
|
const EXPORT_TYPE_ENVIRONMENT = 'environment';
|
2020-04-26 20:33:39 +00:00
|
|
|
const EXPORT_TYPE_API_SPEC = 'api_spec';
|
2016-11-19 03:21:15 +00:00
|
|
|
|
|
|
|
// If we come across an ID of this form, we will replace it with a new one
|
2019-08-15 19:56:32 +00:00
|
|
|
const REPLACE_ID_REGEX = /__\w+_\d+__/g;
|
2016-11-19 03:21:15 +00:00
|
|
|
|
|
|
|
const MODELS = {
|
|
|
|
[EXPORT_TYPE_REQUEST]: models.request,
|
|
|
|
[EXPORT_TYPE_REQUEST_GROUP]: models.requestGroup,
|
|
|
|
[EXPORT_TYPE_WORKSPACE]: models.workspace,
|
|
|
|
[EXPORT_TYPE_COOKIE_JAR]: models.cookieJar,
|
2018-12-12 17:36:11 +00:00
|
|
|
[EXPORT_TYPE_ENVIRONMENT]: models.environment,
|
2020-04-26 20:33:39 +00:00
|
|
|
[EXPORT_TYPE_API_SPEC]: models.apiSpec,
|
2016-11-19 03:21:15 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 02:11:15 +00:00
|
|
|
export type ImportResult = {
|
|
|
|
source: string,
|
|
|
|
error: Error | null,
|
|
|
|
summary: { [string]: Array<BaseModel> },
|
|
|
|
};
|
|
|
|
|
2019-08-12 20:50:41 +00:00
|
|
|
export async function importUri(
|
|
|
|
getWorkspaceId: () => Promise<string | null>,
|
|
|
|
uri: string,
|
2020-05-14 02:11:15 +00:00
|
|
|
): Promise<ImportResult> {
|
2017-05-03 17:48:23 +00:00
|
|
|
let rawText;
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
// If GH preview, force raw
|
|
|
|
const url = new URL(uri);
|
|
|
|
if (url.origin === 'https://github.com') {
|
|
|
|
uri = uri
|
|
|
|
.replace('https://github.com', 'https://raw.githubusercontent.com')
|
|
|
|
.replace('blob/', '');
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:48:23 +00:00
|
|
|
if (uri.match(/^(http|https):\/\//)) {
|
2019-04-18 00:50:03 +00:00
|
|
|
const response = await window.fetch(uri);
|
2017-05-03 17:48:23 +00:00
|
|
|
rawText = await response.text();
|
|
|
|
} else if (uri.match(/^(file):\/\//)) {
|
|
|
|
const path = uri.replace(/^(file):\/\//, '');
|
|
|
|
rawText = fs.readFileSync(path, 'utf8');
|
|
|
|
} else {
|
2020-02-12 19:01:51 +00:00
|
|
|
// Treat everything else as raw text
|
|
|
|
rawText = decodeURIComponent(uri);
|
2017-05-03 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 20:50:41 +00:00
|
|
|
const result = await importRaw(getWorkspaceId, rawText);
|
2018-06-25 17:42:50 +00:00
|
|
|
const { summary, error } = result;
|
2017-05-03 17:48:23 +00:00
|
|
|
|
|
|
|
if (error) {
|
2019-04-26 04:27:01 +00:00
|
|
|
showError({
|
|
|
|
title: 'Failed to import',
|
|
|
|
error: error.message,
|
|
|
|
message: 'Import failed',
|
|
|
|
});
|
2019-08-12 21:15:44 +00:00
|
|
|
return result;
|
2017-05-03 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:32:19 +00:00
|
|
|
const statements = Object.keys(summary)
|
2018-06-25 17:42:50 +00:00
|
|
|
.map(type => {
|
|
|
|
const count = summary[type].length;
|
|
|
|
const name = models.getModelName(type, count);
|
|
|
|
return count === 0 ? null : `${count} ${name}`;
|
|
|
|
})
|
|
|
|
.filter(s => s !== null);
|
2017-05-03 17:48:23 +00:00
|
|
|
|
|
|
|
let message;
|
|
|
|
if (statements.length === 0) {
|
|
|
|
message = 'Nothing was found to import.';
|
|
|
|
} else {
|
|
|
|
message = `You imported ${statements.join(', ')}!`;
|
|
|
|
}
|
2018-06-25 17:42:50 +00:00
|
|
|
showModal(AlertModal, { title: 'Import Succeeded', message });
|
2019-08-12 21:15:44 +00:00
|
|
|
|
|
|
|
return result;
|
2017-05-03 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function importRaw(
|
2019-08-12 20:50:41 +00:00
|
|
|
getWorkspaceId: () => Promise<string | null>,
|
2017-11-26 13:42:43 +00:00
|
|
|
rawContent: string,
|
2020-05-14 02:11:15 +00:00
|
|
|
): Promise<ImportResult> {
|
2016-12-01 00:02:35 +00:00
|
|
|
let results;
|
2016-11-19 03:21:15 +00:00
|
|
|
try {
|
2018-01-16 05:51:28 +00:00
|
|
|
results = await convert(rawContent);
|
2019-04-26 04:27:01 +00:00
|
|
|
} catch (err) {
|
2016-12-01 03:54:26 +00:00
|
|
|
return {
|
|
|
|
source: 'not found',
|
2019-04-26 04:27:01 +00:00
|
|
|
error: err,
|
2018-12-12 17:36:11 +00:00
|
|
|
summary: {},
|
2016-12-01 03:54:26 +00:00
|
|
|
};
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const { data } = results;
|
2016-12-01 00:02:35 +00:00
|
|
|
|
2016-11-19 03:21:15 +00:00
|
|
|
// Generate all the ids we may need
|
2020-05-14 02:11:15 +00:00
|
|
|
const generatedIds: { [string]: string | Function } = {};
|
2016-11-19 03:21:15 +00:00
|
|
|
for (const r of data.resources) {
|
2019-08-15 19:56:32 +00:00
|
|
|
for (const key of r._id.match(REPLACE_ID_REGEX) || []) {
|
|
|
|
generatedIds[key] = generateId(MODELS[r._type].prefix);
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 20:50:41 +00:00
|
|
|
// Contains the ID of the workspace to be used with the import
|
|
|
|
generatedIds[WORKSPACE_ID_KEY] = async () => {
|
|
|
|
const workspaceId = await getWorkspaceId();
|
|
|
|
|
|
|
|
// First try getting the workspace to overwrite
|
|
|
|
let workspace = await models.workspace.getById(workspaceId || 'n/a');
|
|
|
|
|
|
|
|
// If none provided, create a new workspace
|
|
|
|
if (workspace === null) {
|
2018-06-25 17:42:50 +00:00
|
|
|
workspace = await models.workspace.create({ name: 'Imported Workspace' });
|
2017-11-26 13:42:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 20:50:41 +00:00
|
|
|
// Update this fn so it doesn't run again
|
|
|
|
generatedIds[WORKSPACE_ID_KEY] = workspace._id;
|
|
|
|
|
2017-11-26 13:42:43 +00:00
|
|
|
return workspace._id;
|
|
|
|
};
|
|
|
|
|
2019-08-12 20:50:41 +00:00
|
|
|
// Contains the ID of the base environment to be used with the import
|
|
|
|
generatedIds[BASE_ENVIRONMENT_ID_KEY] = async () => {
|
|
|
|
const parentId = await fnOrString(generatedIds[WORKSPACE_ID_KEY]);
|
|
|
|
const baseEnvironment = await models.environment.getOrCreateForWorkspaceId(parentId);
|
|
|
|
|
|
|
|
// Update this fn so it doesn't run again
|
|
|
|
generatedIds[BASE_ENVIRONMENT_ID_KEY] = baseEnvironment._id;
|
|
|
|
|
2017-11-26 13:42:43 +00:00
|
|
|
return baseEnvironment._id;
|
|
|
|
};
|
2016-11-19 03:21:15 +00:00
|
|
|
|
2016-11-20 07:43:22 +00:00
|
|
|
// Import everything backwards so they get inserted in the correct order
|
|
|
|
data.resources.reverse();
|
|
|
|
|
|
|
|
const importedDocs = {};
|
|
|
|
for (const model of models.all()) {
|
|
|
|
importedDocs[model.type] = [];
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:21:15 +00:00
|
|
|
for (const resource of data.resources) {
|
|
|
|
// Buffer DB changes
|
|
|
|
// NOTE: Doing it inside here so it's more "scalable"
|
2017-11-22 14:00:12 +00:00
|
|
|
await db.bufferChanges(100);
|
2016-11-19 03:21:15 +00:00
|
|
|
|
|
|
|
// Replace null parentIds with current workspace
|
2017-11-26 13:42:43 +00:00
|
|
|
if (!resource.parentId && resource._type !== EXPORT_TYPE_WORKSPACE) {
|
2019-08-12 20:50:41 +00:00
|
|
|
resource.parentId = WORKSPACE_ID_KEY;
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 19:56:32 +00:00
|
|
|
// Replace ID placeholders (eg. __WORKSPACE_ID__) with generated values
|
|
|
|
for (const key of Object.keys(generatedIds)) {
|
|
|
|
const { parentId, _id } = resource;
|
2019-08-15 21:06:55 +00:00
|
|
|
|
|
|
|
if (parentId && parentId.includes(key)) {
|
|
|
|
resource.parentId = parentId.replace(key, await fnOrString(generatedIds[key]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_id && _id.includes(key)) {
|
|
|
|
resource._id = _id.replace(key, await fnOrString(generatedIds[key]));
|
|
|
|
}
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 13:42:43 +00:00
|
|
|
const model: Object = MODELS[resource._type];
|
2016-11-19 03:21:15 +00:00
|
|
|
if (!model) {
|
2016-12-03 05:15:38 +00:00
|
|
|
console.warn('Unknown doc type for import', resource._type);
|
|
|
|
continue;
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
// Hack to switch to GraphQL based on finding `graphql` in the URL path
|
|
|
|
// TODO: Support this in a better way
|
|
|
|
if (
|
|
|
|
model.type === models.request.type &&
|
|
|
|
resource.body &&
|
|
|
|
typeof resource.body.text === 'string' &&
|
|
|
|
typeof resource.url === 'string' &&
|
|
|
|
resource.body.text.includes('"query"') &&
|
|
|
|
resource.url.includes('graphql')
|
|
|
|
) {
|
|
|
|
resource.body.mimeType = CONTENT_TYPE_GRAPHQL;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:58:12 +00:00
|
|
|
// Try adding Content-Type JSON if no Content-Type exists
|
|
|
|
if (
|
|
|
|
model.type === models.request.type &&
|
|
|
|
resource.body &&
|
|
|
|
typeof resource.body.text === 'string' &&
|
|
|
|
Array.isArray(resource.headers) &&
|
|
|
|
!resource.headers.find(h => h.name.toLowerCase() === 'content-type')
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
JSON.parse(resource.body.text);
|
|
|
|
resource.headers.push({ name: 'Content-Type', value: 'application/json' });
|
|
|
|
} catch (err) {
|
|
|
|
// Not JSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingDoc = await db.get(model.type, resource._id);
|
2017-11-26 13:42:43 +00:00
|
|
|
let newDoc: BaseModel;
|
2017-07-27 22:59:07 +00:00
|
|
|
if (existingDoc) {
|
2017-11-26 13:42:43 +00:00
|
|
|
newDoc = await db.docUpdate(existingDoc, resource);
|
2017-07-27 22:59:07 +00:00
|
|
|
} else {
|
2017-11-26 13:42:43 +00:00
|
|
|
newDoc = await db.docCreate(model.type, resource);
|
2017-07-27 22:59:07 +00:00
|
|
|
|
|
|
|
// Mark as not seen if we created a new workspace from sync
|
|
|
|
if (newDoc.type === models.workspace.type) {
|
2018-10-17 16:42:33 +00:00
|
|
|
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(newDoc._id);
|
2018-06-25 17:42:50 +00:00
|
|
|
await models.workspaceMeta.update(workspaceMeta, { hasSeen: false });
|
2017-07-27 22:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-19 03:21:15 +00:00
|
|
|
|
2016-11-20 07:43:22 +00:00
|
|
|
importedDocs[newDoc.type].push(newDoc);
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
// Store spec under workspace if it's OpenAPI
|
|
|
|
for (const workspace of importedDocs[models.workspace.type]) {
|
|
|
|
if (isApiSpec(results.type.id)) {
|
|
|
|
const spec = await models.apiSpec.updateOrCreateForParentId(workspace._id, {
|
|
|
|
contents: rawContent,
|
|
|
|
contentType: 'yaml',
|
|
|
|
});
|
|
|
|
|
|
|
|
importedDocs[spec.type].push(spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default environment if there is one
|
|
|
|
const meta = await models.workspaceMeta.getOrCreateByParentId(workspace._id);
|
|
|
|
const envs = importedDocs[models.environment.type];
|
|
|
|
meta.activeEnvironmentId = envs.length > 0 ? envs[0]._id : null;
|
|
|
|
await models.workspaceMeta.update(meta);
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:00:12 +00:00
|
|
|
await db.flushChanges();
|
2016-11-20 07:43:22 +00:00
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
trackEvent('Data', 'Import', results.type.id);
|
|
|
|
|
2016-12-01 00:02:35 +00:00
|
|
|
return {
|
2018-10-17 16:42:33 +00:00
|
|
|
source: results.type && typeof results.type.id === 'string' ? results.type.id : 'unknown',
|
2016-12-01 03:54:26 +00:00
|
|
|
summary: importedDocs,
|
2018-12-12 17:36:11 +00:00
|
|
|
error: null,
|
2016-12-01 00:02:35 +00:00
|
|
|
};
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export function isApiSpec(content: string): boolean {
|
|
|
|
return content === 'openapi3' || content === 'swagger2';
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:53:45 +00:00
|
|
|
export async function exportWorkspacesHAR(
|
2017-11-26 13:42:43 +00:00
|
|
|
parentDoc: BaseModel | null = null,
|
2018-12-12 17:36:11 +00:00
|
|
|
includePrivateDocs: boolean = false,
|
2017-11-26 13:42:43 +00:00
|
|
|
): Promise<string> {
|
2019-03-12 16:37:05 +00:00
|
|
|
const docs: Array<BaseModel> = await getDocWithDescendants(parentDoc, includePrivateDocs);
|
|
|
|
const requests: Array<BaseModel> = docs.filter(doc => doc.type === models.request.type);
|
|
|
|
return exportRequestsHAR(requests, includePrivateDocs);
|
2017-11-01 11:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 06:53:45 +00:00
|
|
|
export async function exportRequestsHAR(
|
|
|
|
requests: Array<BaseModel>,
|
|
|
|
includePrivateDocs: boolean = false,
|
|
|
|
): Promise<string> {
|
|
|
|
const workspaces: Array<BaseModel> = [];
|
|
|
|
const mapRequestIdToWorkspace: Object = {};
|
|
|
|
const workspaceLookup: Object = {};
|
|
|
|
for (const request of requests) {
|
|
|
|
const ancestors: Array<BaseModel> = await db.withAncestors(request, [
|
|
|
|
models.workspace.type,
|
|
|
|
models.requestGroup.type,
|
|
|
|
]);
|
|
|
|
const workspace = ancestors.find(ancestor => ancestor.type === models.workspace.type);
|
|
|
|
mapRequestIdToWorkspace[request._id] = workspace;
|
|
|
|
if (workspace == null || workspaceLookup.hasOwnProperty(workspace._id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
workspaceLookup[workspace._id] = true;
|
|
|
|
workspaces.push(workspace);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapWorkspaceIdToEnvironmentId: Object = {};
|
|
|
|
for (const workspace of workspaces) {
|
|
|
|
const workspaceMeta = await models.workspaceMeta.getByParentId(workspace._id);
|
|
|
|
let environmentId = workspaceMeta ? workspaceMeta.activeEnvironmentId : null;
|
|
|
|
const environment = await models.environment.getById(environmentId || 'n/a');
|
|
|
|
if (!environment || (environment.isPrivate && !includePrivateDocs)) {
|
|
|
|
environmentId = 'n/a';
|
|
|
|
}
|
|
|
|
mapWorkspaceIdToEnvironmentId[workspace._id] = environmentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
requests = requests.sort((a: Object, b: Object) => (a.metaSortKey < b.metaSortKey ? -1 : 1));
|
|
|
|
const harRequests: Array<Object> = [];
|
|
|
|
for (const request of requests) {
|
|
|
|
const workspace = mapRequestIdToWorkspace[request._id];
|
|
|
|
if (workspace == null) {
|
|
|
|
// Workspace not found for request, so don't export it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const environmentId = mapWorkspaceIdToEnvironmentId[workspace._id];
|
|
|
|
harRequests.push({
|
|
|
|
requestId: request._id,
|
|
|
|
environmentId: environmentId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await har.exportHar(harRequests);
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
trackEvent('Data', 'Export', 'HAR');
|
|
|
|
|
2019-03-05 06:53:45 +00:00
|
|
|
return JSON.stringify(data, null, '\t');
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:23:15 +00:00
|
|
|
export async function exportWorkspacesData(
|
2019-04-18 04:21:29 +00:00
|
|
|
parentDoc: BaseModel | null,
|
|
|
|
includePrivateDocs: boolean,
|
|
|
|
format: 'json' | 'yaml',
|
2017-11-26 13:42:43 +00:00
|
|
|
): Promise<string> {
|
2018-10-17 16:42:33 +00:00
|
|
|
const docs: Array<BaseModel> = await getDocWithDescendants(parentDoc, includePrivateDocs);
|
2019-03-12 16:37:05 +00:00
|
|
|
const requests: Array<BaseModel> = docs.filter(doc => doc.type === models.request.type);
|
2019-04-18 17:23:15 +00:00
|
|
|
return exportRequestsData(requests, includePrivateDocs, format);
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
2017-11-01 11:23:22 +00:00
|
|
|
|
2019-04-18 17:23:15 +00:00
|
|
|
export async function exportRequestsData(
|
2019-03-05 06:53:45 +00:00
|
|
|
requests: Array<BaseModel>,
|
2019-04-18 17:23:15 +00:00
|
|
|
includePrivateDocs: boolean,
|
|
|
|
format: 'json' | 'yaml',
|
2019-03-05 06:53:45 +00:00
|
|
|
): Promise<string> {
|
|
|
|
const data = {
|
|
|
|
_type: 'export',
|
|
|
|
__export_format: EXPORT_FORMAT,
|
|
|
|
__export_date: new Date(),
|
|
|
|
__export_source: `insomnia.desktop.app:v${getAppVersion()}`,
|
|
|
|
resources: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const docs: Array<BaseModel> = [];
|
|
|
|
const workspaces: Array<BaseModel> = [];
|
|
|
|
const mapTypeAndIdToDoc: Object = {};
|
|
|
|
for (const req of requests) {
|
2019-04-18 18:13:12 +00:00
|
|
|
const ancestors: Array<BaseModel> = clone(await db.withAncestors(req));
|
2019-03-05 06:53:45 +00:00
|
|
|
for (const ancestor of ancestors) {
|
|
|
|
const key = ancestor.type + '___' + ancestor._id;
|
|
|
|
if (mapTypeAndIdToDoc.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mapTypeAndIdToDoc[key] = ancestor;
|
|
|
|
docs.push(ancestor);
|
|
|
|
if (ancestor.type === models.workspace.type) {
|
|
|
|
workspaces.push(ancestor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const workspace of workspaces) {
|
|
|
|
const descendants: Array<BaseModel> = (await db.withDescendants(workspace)).filter(d => {
|
|
|
|
// Only interested in these additional model types.
|
2020-04-26 20:33:39 +00:00
|
|
|
return (
|
|
|
|
d.type === models.cookieJar.type ||
|
|
|
|
d.type === models.environment.type ||
|
|
|
|
d.type === models.apiSpec.type
|
|
|
|
);
|
2019-03-05 06:53:45 +00:00
|
|
|
});
|
|
|
|
docs.push(...descendants);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.resources = docs
|
|
|
|
.filter(d => {
|
|
|
|
// Only export these model types.
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
d.type === models.request.type ||
|
|
|
|
d.type === models.requestGroup.type ||
|
|
|
|
d.type === models.workspace.type ||
|
|
|
|
d.type === models.cookieJar.type ||
|
2020-04-26 20:33:39 +00:00
|
|
|
d.type === models.environment.type ||
|
|
|
|
d.type === models.apiSpec.type
|
2019-03-05 06:53:45 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// BaseModel doesn't have isPrivate, so cast it first.
|
|
|
|
return !(d: Object).isPrivate || includePrivateDocs;
|
|
|
|
})
|
|
|
|
.map((d: Object) => {
|
|
|
|
if (d.type === models.workspace.type) {
|
|
|
|
d._type = EXPORT_TYPE_WORKSPACE;
|
|
|
|
} else if (d.type === models.cookieJar.type) {
|
|
|
|
d._type = EXPORT_TYPE_COOKIE_JAR;
|
|
|
|
} else if (d.type === models.environment.type) {
|
|
|
|
d._type = EXPORT_TYPE_ENVIRONMENT;
|
|
|
|
} else if (d.type === models.requestGroup.type) {
|
|
|
|
d._type = EXPORT_TYPE_REQUEST_GROUP;
|
|
|
|
} else if (d.type === models.request.type) {
|
|
|
|
d._type = EXPORT_TYPE_REQUEST;
|
2020-04-26 20:33:39 +00:00
|
|
|
} else if (d.type === models.apiSpec.type) {
|
|
|
|
d._type = EXPORT_TYPE_API_SPEC;
|
2019-03-05 06:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the things we don't want to export
|
|
|
|
delete d.type;
|
|
|
|
return d;
|
|
|
|
});
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
trackEvent('Data', 'Export', `Insomnia ${format}`);
|
|
|
|
|
2019-04-18 04:21:29 +00:00
|
|
|
if (format.toLowerCase() === 'yaml') {
|
|
|
|
return YAML.stringify(data);
|
|
|
|
} else if (format.toLowerCase() === 'json') {
|
|
|
|
return JSON.stringify(data);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Invalid export format ${format}. Must be "json" or "yaml"`);
|
|
|
|
}
|
2019-03-05 06:53:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function getDocWithDescendants(
|
2017-11-26 13:42:43 +00:00
|
|
|
parentDoc: BaseModel | null = null,
|
2018-12-12 17:36:11 +00:00
|
|
|
includePrivateDocs: boolean = false,
|
2017-11-26 13:42:43 +00:00
|
|
|
): Promise<Array<BaseModel>> {
|
2017-11-01 11:23:22 +00:00
|
|
|
const docs = await db.withDescendants(parentDoc);
|
2018-06-25 17:42:50 +00:00
|
|
|
return docs.filter(
|
|
|
|
d =>
|
|
|
|
// Don't include if private, except if we want to
|
2018-12-12 17:36:11 +00:00
|
|
|
!(d: any).isPrivate || includePrivateDocs,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-11-01 11:23:22 +00:00
|
|
|
}
|