diff --git a/app/common/constants.js b/app/common/constants.js index f7e82d9d93..463b2ffc6b 100644 --- a/app/common/constants.js +++ b/app/common/constants.js @@ -137,7 +137,7 @@ export function getContentTypeName (contentType) { if (typeof contentType !== 'string') { return 'No Body'; } else { - return contentTypesMap[contentType] || ''; + return contentTypesMap[contentType] || 'Other'; } } diff --git a/app/common/database.js b/app/common/database.js index 2ad4f64d8a..5540284a21 100644 --- a/app/common/database.js +++ b/app/common/database.js @@ -189,13 +189,13 @@ export async function upsert (doc, fromSync = false) { export function insert (doc, fromSync = false) { return new Promise((resolve, reject) => { - db[doc.type].insert(doc, (err, newDoc) => { + const docWithDefaults = initModel(doc.type, doc); + db[doc.type].insert(docWithDefaults, (err, newDoc) => { if (err) { return reject(err); } - notifyOfChange(CHANGE_INSERT, doc, fromSync); - + notifyOfChange(CHANGE_INSERT, newDoc, fromSync); resolve(newDoc); }); }); @@ -203,14 +203,15 @@ export function insert (doc, fromSync = false) { export function update (doc, fromSync = false) { return new Promise((resolve, reject) => { - db[doc.type].update({_id: doc._id}, doc, err => { + const docWithDefaults = initModel(doc.type, doc); + db[doc.type].update({_id: docWithDefaults._id}, docWithDefaults, err => { if (err) { return reject(err); } - notifyOfChange(CHANGE_UPDATE, doc, fromSync); + notifyOfChange(CHANGE_UPDATE, docWithDefaults, fromSync); - resolve(doc); + resolve(docWithDefaults); }); }); } diff --git a/app/common/import.js b/app/common/import.js index 9cb67a234a..b187d85922 100644 --- a/app/common/import.js +++ b/app/common/import.js @@ -28,12 +28,19 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) try { results = convert(rawContent); } catch (e) { - console.error('Failed to import data', e); - return; + console.warn('Failed to import data', e); + return { + source: 'not found', + error: 'No importers found for file', + summary: {} + }; } const {data} = results; + // Fetch the base environment in case we need it + const baseEnvironment = await models.environment.getOrCreateForWorkspace(workspace); + // Generate all the ids we may need const generatedIds = {}; for (const r of data.resources) { @@ -42,8 +49,9 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) } } - // Also always replace __WORKSPACE_ID__ with the current workspace if we see it + // Always replace these "constants" generatedIds['__WORKSPACE_ID__'] = workspace._id; + generatedIds['__BASE_ENVIRONMENT_ID__'] = baseEnvironment._id; // Import everything backwards so they get inserted in the correct order data.resources.reverse(); @@ -90,7 +98,8 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) return { source: results.type.id, - summary: importedDocs + summary: importedDocs, + error: null }; } diff --git a/app/package.json b/app/package.json index 896718591b..44e7f69b1f 100644 --- a/app/package.json +++ b/app/package.json @@ -16,7 +16,7 @@ "electron-squirrel-startup": "^1.0.0", "hkdf": "0.0.2", "httpsnippet": "git@github.com:getinsomnia/httpsnippet.git#a3a2c0a0167fa844bf92df52a1442fa1d68a9053", - "insomnia-importers": "^1.0.0", + "insomnia-importers": "^1.1.2", "json-lint": "^0.1.0", "jsonpath-plus": "^0.15.0", "mime-types": "^2.1.12", diff --git a/app/ui/components/RequestPane.js b/app/ui/components/RequestPane.js index 602c5cb5f0..21455af8fe 100644 --- a/app/ui/components/RequestPane.js +++ b/app/ui/components/RequestPane.js @@ -70,7 +70,6 @@ class RequestPane extends PureComponent { editorFontSize, editorLineWrapping, handleSend, - workspace, forceRefreshCounter, useBulkHeaderEditor, handleGenerateCode, diff --git a/app/ui/components/Wrapper.js b/app/ui/components/Wrapper.js index cdba681c21..24d0e684fc 100644 --- a/app/ui/components/Wrapper.js +++ b/app/ui/components/Wrapper.js @@ -51,7 +51,8 @@ class Wrapper extends Component { const {activeRequest} = this.props; try { - const {resources} = importers.import(url); + const {data} = importers.convert(url); + const {resources} = data; const r = resources[0]; if (r && r._type === 'request') { @@ -82,7 +83,10 @@ class Wrapper extends Component { _handleUpdateSettingsUseBulkHeaderEditor = useBulkHeaderEditor => sUpdate(this.props.settings, {useBulkHeaderEditor}); // Other Helpers - _handleImportFile = () => this.props.handleImportFileToWorkspace(this.props.activeWorkspace._id); + _handleImportFile = () => { + console.log('IMPORT', this.props); + this.props.handleImportFileToWorkspace(this.props.activeWorkspace._id); + } _handleExportWorkspaceToFile = () => this.props.handleExportFile(this.props.activeWorkspace._id); _handleSetActiveResponse = responseId => this.props.handleSetActiveResponse(this.props.activeRequest._id, responseId); _handleShowEnvironmentsModal = () => showModal(WorkspaceEnvironmentsEditModal, this.props.activeWorkspace); diff --git a/app/ui/components/modals/LoginModal.js b/app/ui/components/modals/LoginModal.js index 32553e24de..d9262ad775 100644 --- a/app/ui/components/modals/LoginModal.js +++ b/app/ui/components/modals/LoginModal.js @@ -19,7 +19,7 @@ class LoginModal extends Component { message: '', }; - async _handleLogin (e) { + _handleLogin = async e => { e.preventDefault(); this.setState({error: '', loading: true}); @@ -39,15 +39,15 @@ class LoginModal extends Component { } catch (e) { this.setState({error: e.message, loading: false}); } - } + }; - _handleSignup (e) { + _handleSignup = e => { e.preventDefault(); this.modal.hide(); showModal(SignupModal); trackEvent('Login', 'Switch to Signup'); - } + }; show (options = {}) { const {title, message} = options; @@ -66,7 +66,7 @@ class LoginModal extends Component { {message ? (

{message}

) : null} -
+
diff --git a/app/ui/containers/App.js b/app/ui/containers/App.js index c1c84bdce5..a1baf14f1b 100644 --- a/app/ui/containers/App.js +++ b/app/ui/containers/App.js @@ -125,9 +125,7 @@ class App extends Component { _requestCreate = async (parentId) => { const request = await showModal(RequestCreateModal, {parentId}); - const {activeWorkspace, handleSetActiveRequest} = this.props; - - handleSetActiveRequest(activeWorkspace._id, request._id); + this._handleSetActiveRequest(request._id) }; _requestGroupDuplicate = async requestGroup => { @@ -135,14 +133,12 @@ class App extends Component { }; _requestDuplicate = async request => { - const {activeWorkspace, handleSetActiveRequest} = this.props; - if (!request) { return; } const newRequest = await models.request.duplicate(request); - handleSetActiveRequest(activeWorkspace._id, newRequest._id); + this._handleSetActiveRequest(newRequest._id) }; _handleGenerateCode = () => { @@ -154,7 +150,8 @@ class App extends Component { if (requestGroupMeta) { await models.requestGroupMeta.update(requestGroupMeta, patch); } else { - await models.requestGroupMeta.create({parentId: requestGroupId}, patch); + const newPatch = Object.assign({parentId: requestGroupId}, patch); + await models.requestGroupMeta.create(newPatch); } }; @@ -164,7 +161,8 @@ class App extends Component { if (requestMeta) { await models.workspaceMeta.update(requestMeta, patch); } else { - await models.workspaceMeta.create({parentId: workspaceId}, patch); + const newPatch = Object.assign({parentId: workspaceId}, patch); + await models.workspaceMeta.create(newPatch); } }; @@ -173,7 +171,8 @@ class App extends Component { if (requestMeta) { await models.requestMeta.update(requestMeta, patch); } else { - await models.requestMeta.create({parentId: requestId}, patch); + const newPatch = Object.assign({parentId: requestId}, patch); + await models.requestMeta.create(newPatch); } }; diff --git a/app/ui/redux/modules/global.js b/app/ui/redux/modules/global.js index c143357a7c..af4b032abc 100644 --- a/app/ui/redux/modules/global.js +++ b/app/ui/redux/modules/global.js @@ -117,7 +117,6 @@ export function importFile (workspaceId) { dispatch(loadStart()); const workspace = await models.workspace.getById(workspaceId); - const options = { title: 'Import Insomnia Data', buttonLabel: 'Import', @@ -144,12 +143,18 @@ export function importFile (workspaceId) { const data = fs.readFileSync(path, 'utf8'); dispatch(loadStop()); - const {summary, source} = await importRaw(workspace, data); + const result = await importRaw(workspace, data); + const {summary, source, error} = result; + + if (error) { + showModal(AlertModal, {title: 'Import Failed', message: error}); + return; + } let statements = Object.keys(summary).map(type => { const count = summary[type].length; const name = models.getModelName(type, count); - return count === 0 ? null :`${count} ${name}`; + return count === 0 ? null : `${count} ${name}`; }).filter(s => s !== null); let message; @@ -159,10 +164,10 @@ export function importFile (workspaceId) { message = `You imported ${statements.join(', ')}!`; } showModal(AlertModal, {title: 'Import Succeeded', message}); - trackEvent('Import', source, 'Success'); + trackEvent('Import', 'Success', source); } catch (e) { showModal(AlertModal, {title: 'Import Failed', message: e + ''}); - trackEvent('Import', source, 'Failure'); + trackEvent('Import', 'Failure'); } } }); diff --git a/package.json b/package.json index 251c705677..95cf569cb9 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "electron-squirrel-startup": "^1.0.0", "hkdf": "0.0.2", "httpsnippet": "git@github.com:getinsomnia/httpsnippet.git#a3a2c0a0167fa844bf92df52a1442fa1d68a9053", - "insomnia-importers": "^1.0.0", + "insomnia-importers": "^1.1.2", "json-lint": "^0.1.0", "jsonpath-plus": "^0.15.0", "mime-types": "^2.1.12",