2021-07-22 23:04:56 +00:00
|
|
|
import fs from 'fs';
|
2021-06-14 15:18:35 +00:00
|
|
|
import { convert } from 'insomnia-importers';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
|
|
|
import type { ApiSpec } from '../models/api-spec';
|
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';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { isRequest } from '../models/request';
|
2021-06-16 19:19:00 +00:00
|
|
|
import { isWorkspace, Workspace } from '../models/workspace';
|
2021-09-27 13:47:22 +00:00
|
|
|
import { AlertModal } from '../ui/components/modals/alert-modal';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { showError, showModal } from '../ui/components/modals/index';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { ImportToWorkspacePrompt, SetWorkspaceScopePrompt } from '../ui/redux/modules/helpers';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { trackEvent } from './analytics';
|
2021-06-14 15:18:35 +00:00
|
|
|
import {
|
|
|
|
BASE_ENVIRONMENT_ID_KEY,
|
|
|
|
CONTENT_TYPE_GRAPHQL,
|
|
|
|
EXPORT_TYPE_WORKSPACE,
|
|
|
|
WORKSPACE_ID_KEY,
|
|
|
|
} from './constants';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { database as db } from './database';
|
|
|
|
import { diffPatchObj, fnOrString, generateId } from './misc';
|
2016-11-19 03:21:15 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface ImportResult {
|
|
|
|
source: string;
|
|
|
|
error: Error | null;
|
2021-05-18 20:32:18 +00:00
|
|
|
summary: Record<string, BaseModel[]>;
|
2021-05-12 06:35:00 +00:00
|
|
|
}
|
2020-05-14 02:11:15 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface ConvertResultType {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
}
|
2021-03-02 21:16:48 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface ConvertResult {
|
|
|
|
type: ConvertResultType;
|
2021-03-02 21:16:48 +00:00
|
|
|
data: {
|
2021-05-18 20:32:18 +00:00
|
|
|
resources: Record<string, any>[];
|
2021-05-12 06:35:00 +00:00
|
|
|
};
|
|
|
|
}
|
2021-03-02 21:16:48 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface ImportRawConfig {
|
|
|
|
getWorkspaceId: ImportToWorkspacePrompt;
|
2021-08-20 15:12:36 +00:00
|
|
|
getProjectId?: () => Promise<string>;
|
2021-05-12 06:35:00 +00:00
|
|
|
getWorkspaceScope?: SetWorkspaceScopePrompt;
|
|
|
|
enableDiffBasedPatching?: boolean;
|
|
|
|
enableDiffDeep?: boolean;
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
bypassDiffProps?: {
|
2021-05-12 06:35:00 +00:00
|
|
|
url: boolean;
|
|
|
|
};
|
|
|
|
}
|
2021-03-02 21:16:48 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function importUri(uri: string, importConfig: ImportRawConfig) {
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-02 21:16:48 +00:00
|
|
|
const result = await importRaw(rawText, importConfig);
|
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',
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION appears to be a genuine error
|
2019-04-26 04:27:01 +00:00
|
|
|
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;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-05-03 17:48:23 +00:00
|
|
|
if (statements.length === 0) {
|
|
|
|
message = 'Nothing was found to import.';
|
|
|
|
} else {
|
|
|
|
message = `You imported ${statements.join(', ')}!`;
|
|
|
|
}
|
2019-08-12 21:15:44 +00:00
|
|
|
|
2021-05-12 06:35:00 +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
|
|
|
}
|
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
// If we come across an ID of this form, we will replace it with a new one
|
|
|
|
const REPLACE_ID_REGEX = /__\w+_\d+__/g;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function importRaw(
|
2017-11-26 13:42:43 +00:00
|
|
|
rawContent: string,
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
{
|
|
|
|
getWorkspaceId,
|
|
|
|
getWorkspaceScope,
|
2021-08-20 15:12:36 +00:00
|
|
|
getProjectId,
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
enableDiffBasedPatching,
|
|
|
|
enableDiffDeep,
|
|
|
|
bypassDiffProps,
|
|
|
|
}: ImportRawConfig,
|
2021-05-12 06:35:00 +00:00
|
|
|
) {
|
2021-03-02 21:16:48 +00:00
|
|
|
let results: ConvertResult;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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) {
|
2021-05-12 06:35:00 +00:00
|
|
|
const importResult: ImportResult = {
|
2016-12-01 03:54:26 +00:00
|
|
|
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
|
|
|
};
|
2021-05-12 06:35:00 +00:00
|
|
|
return importResult;
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:16:48 +00:00
|
|
|
const { data, type: resultsType } = results;
|
2016-11-19 03:21:15 +00:00
|
|
|
// Generate all the ids we may need
|
2021-05-18 20:32:18 +00:00
|
|
|
const generatedIds: Record<string, string | ((...args: any[]) => any)> = {};
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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) || []) {
|
2021-06-14 15:18:35 +00:00
|
|
|
generatedIds[key] = generateId(models.MODELS_BY_EXPORT_TYPE[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
|
2021-03-02 21:16:48 +00:00
|
|
|
const workspace = await models.workspace.getById(workspaceId || 'n/a');
|
2019-08-12 20:50:41 +00:00
|
|
|
// Update this fn so it doesn't run again
|
2021-03-02 21:16:48 +00:00
|
|
|
const idToUse = workspace?._id || generateId(models.workspace.prefix);
|
|
|
|
generatedIds[WORKSPACE_ID_KEY] = idToUse;
|
|
|
|
return idToUse;
|
2017-11-26 13:42:43 +00:00
|
|
|
};
|
|
|
|
|
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]);
|
2021-06-22 20:45:30 +00:00
|
|
|
const baseEnvironment = await models.environment.getOrCreateForParentId(parentId);
|
2019-08-12 20:50:41 +00:00
|
|
|
// 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 = {};
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-11-20 07:43:22 +00:00
|
|
|
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
|
|
|
|
2021-07-30 03:32:08 +00:00
|
|
|
if (parentId?.includes(key)) {
|
2019-08-15 21:06:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
const model = models.MODELS_BY_EXPORT_TYPE[resource._type];
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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 (
|
2020-11-13 23:31:18 +00:00
|
|
|
isRequest(model) &&
|
2020-04-26 20:33:39 +00:00
|
|
|
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 (
|
2020-11-13 23:31:18 +00:00
|
|
|
isRequest(model) &&
|
2019-12-11 16:58:12 +00:00
|
|
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
resource.headers.push({
|
|
|
|
name: 'Content-Type',
|
|
|
|
value: 'application/json',
|
|
|
|
});
|
2019-12-11 16:58:12 +00:00
|
|
|
} catch (err) {
|
|
|
|
// Not JSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingDoc = await db.get(model.type, resource._id);
|
2017-11-26 13:42:43 +00:00
|
|
|
let newDoc: BaseModel;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
if (existingDoc) {
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
let updateDoc = resource;
|
|
|
|
|
|
|
|
// Do differential patching when enabled
|
|
|
|
if (enableDiffBasedPatching) {
|
|
|
|
updateDoc = diffPatchObj(resource, existingDoc, enableDiffDeep);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass differential update for urls when enabled
|
|
|
|
if (bypassDiffProps?.url && updateDoc.url) {
|
|
|
|
updateDoc.url = resource.url;
|
|
|
|
}
|
|
|
|
|
2021-07-14 00:10:49 +00:00
|
|
|
// If workspace preserve the scope and parentId of the existing workspace while importing
|
2021-03-02 21:16:48 +00:00
|
|
|
if (isWorkspace(model)) {
|
2021-05-12 06:35:00 +00:00
|
|
|
(updateDoc as Workspace).scope = (existingDoc as Workspace).scope;
|
2021-07-14 00:10:49 +00:00
|
|
|
(updateDoc as Workspace).parentId = (existingDoc as Workspace).parentId;
|
2021-03-02 21:16:48 +00:00
|
|
|
}
|
Differential (Additive/Removal) patching when switching between design / debug (#3124)
* fix(import) do differential patching for design activity imports
This patch adds differential patching for imports that occur during the switching between
design and debug tabs inside of Designer. As reported through #2971, and others, this patch
favors existing data over imported data, values that exist already on the document will remain
unchanged, only new values (including array based values) will be added / removed.
This also includes a bypass feature for urls, currently these options are not exposed through
the interface but could be. This feature is an object to allow for future properties for
preference-based patching.
- Adds `options.enableDiffBasedPatching` and `options.enableDiffDeep` to `importRaw` for backwards compat.
- Adds `options.bypassDiffProps.url` for url bypassing and use an object for future items.
- Adds `diffPatchObj` based on differential patching for objects (works with arrays as well).
Future ideas:
- `hasBeenModifiedByUser` property map object to allow changing properties that haven't been touched by the user with options.
fixes: #2971, #2882, #3038, #2442
* adds some basic tests
- also adds jest (which was (mistakenly) not there before)
- does not call `.hasOwnProperty` directly, per https://eslint.org/docs/rules/no-prototype-builtins (which, we will add more formally at a later date)
* don't special-case workspaces
this ensures the behavior of the initial PR is more preserved
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
2021-03-03 17:57:09 +00:00
|
|
|
|
|
|
|
newDoc = await db.docUpdate(existingDoc, updateDoc);
|
2017-07-27 22:59:07 +00:00
|
|
|
} else {
|
2021-07-14 00:10:49 +00:00
|
|
|
// If workspace, check and set the scope and parentId while importing a new workspace
|
2021-03-02 21:16:48 +00:00
|
|
|
if (isWorkspace(model)) {
|
2021-05-12 06:35:00 +00:00
|
|
|
await updateWorkspaceScope(resource as Workspace, resultsType, getWorkspaceScope);
|
2021-08-20 15:12:36 +00:00
|
|
|
await createWorkspaceInProject(resource as Workspace, getProjectId);
|
2021-03-02 21:16:48 +00:00
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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
|
2021-01-25 03:27:36 +00:00
|
|
|
if (isWorkspace(newDoc)) {
|
2018-10-17 16:42:33 +00:00
|
|
|
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(newDoc._id);
|
2021-05-12 06:35:00 +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]) {
|
2021-03-02 21:16:48 +00:00
|
|
|
if (isApiSpecImport(resultsType)) {
|
2020-04-26 20:33:39 +00:00
|
|
|
const spec = await models.apiSpec.updateOrCreateForParentId(workspace._id, {
|
|
|
|
contents: rawContent,
|
|
|
|
contentType: 'yaml',
|
|
|
|
});
|
|
|
|
importedDocs[spec.type].push(spec);
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:22:12 +00:00
|
|
|
// Set active environment when none is currently selected and one exists
|
2020-04-26 20:33:39 +00:00
|
|
|
const meta = await models.workspaceMeta.getOrCreateByParentId(workspace._id);
|
|
|
|
const envs = importedDocs[models.environment.type];
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-03-03 18:22:12 +00:00
|
|
|
if (!meta.activeEnvironmentId && envs.length > 0) {
|
|
|
|
meta.activeEnvironmentId = envs[0]._id;
|
|
|
|
await models.workspaceMeta.update(meta);
|
|
|
|
}
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 14:00:12 +00:00
|
|
|
await db.flushChanges();
|
2021-03-02 21:16:48 +00:00
|
|
|
trackEvent('Data', 'Import', resultsType.id);
|
2021-05-12 06:35:00 +00:00
|
|
|
const importRequest: ImportResult = {
|
2021-03-02 21:16:48 +00:00
|
|
|
source: resultsType && typeof resultsType.id === 'string' ? resultsType.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
|
|
|
};
|
2021-05-12 06:35:00 +00:00
|
|
|
return importRequest;
|
2016-11-19 03:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:16:48 +00:00
|
|
|
async function updateWorkspaceScope(
|
|
|
|
resource: Workspace,
|
|
|
|
resultType: ConvertResultType,
|
2021-05-12 06:35:00 +00:00
|
|
|
getWorkspaceScope?: SetWorkspaceScopePrompt,
|
2021-03-02 21:16:48 +00:00
|
|
|
) {
|
2021-07-14 00:10:49 +00:00
|
|
|
// Set the workspace scope if creating a new workspace during import
|
2021-03-02 21:16:48 +00:00
|
|
|
// IF is creating a new workspace
|
|
|
|
// AND imported resource has no preset scope property OR scope is null
|
|
|
|
// AND we have a function to get scope
|
|
|
|
if ((!resource.hasOwnProperty('scope') || resource.scope === null) && getWorkspaceScope) {
|
|
|
|
const workspaceName = resource.name;
|
|
|
|
let specName;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-03-02 21:16:48 +00:00
|
|
|
// If is from insomnia v4 and the spec has contents, add to the name when prompting
|
|
|
|
if (isInsomniaV4Import(resultType)) {
|
|
|
|
const spec: ApiSpec | null = await models.apiSpec.getByParentId(resource._id);
|
|
|
|
|
|
|
|
if (spec && spec.contents.trim()) {
|
|
|
|
specName = spec.fileName;
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-03-02 21:16:48 +00:00
|
|
|
const nameToPrompt = specName ? `${specName} / ${workspaceName}` : workspaceName;
|
2021-06-14 15:18:35 +00:00
|
|
|
resource.scope = await getWorkspaceScope(nameToPrompt);
|
2021-03-02 21:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:12:36 +00:00
|
|
|
async function createWorkspaceInProject(
|
2021-07-14 00:10:49 +00:00
|
|
|
resource: Workspace,
|
2021-08-20 15:12:36 +00:00
|
|
|
getProjectId?: () => Promise<string>,
|
2021-07-14 00:10:49 +00:00
|
|
|
) {
|
2021-08-20 15:12:36 +00:00
|
|
|
if (getProjectId) {
|
2021-07-14 00:10:49 +00:00
|
|
|
// Set the workspace parent if creating a new workspace during import
|
2021-08-20 15:12:36 +00:00
|
|
|
resource.parentId = await getProjectId();
|
2021-07-14 00:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isApiSpecImport = ({ id }: Pick<ConvertResultType, 'id'>) => (
|
|
|
|
id === 'openapi3' || id === 'swagger2'
|
|
|
|
);
|
2021-03-02 21:16:48 +00:00
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isInsomniaV4Import = ({ id }: Pick<ConvertResultType, 'id'>) => (
|
|
|
|
id === 'insomnia-4'
|
|
|
|
);
|