diff --git a/packages/bruno-app/src/utils/importers/insomnia-collection.js b/packages/bruno-app/src/utils/importers/insomnia-collection.js index 8b7b7fb6a..8d8113b49 100644 --- a/packages/bruno-app/src/utils/importers/insomnia-collection.js +++ b/packages/bruno-app/src/utils/importers/insomnia-collection.js @@ -1,3 +1,4 @@ +import jsyaml from 'js-yaml'; import each from 'lodash/each'; import get from 'lodash/get'; import fileDialog from 'file-dialog'; @@ -8,7 +9,22 @@ import { validateSchema, transformItemsInCollection, hydrateSeqInCollection } fr const readFile = (files) => { return new Promise((resolve, reject) => { const fileReader = new FileReader(); - fileReader.onload = (e) => resolve(e.target.result); + fileReader.onload = (e) => { + try { + // try to load JSON + const parsedData = JSON.parse(e.target.result); + resolve(parsedData); + } catch (jsonError) { + // not a valid JSOn, try yaml + try { + const parsedData = jsyaml.load(e.target.result); + resolve(parsedData); + } catch (yamlError) { + console.error('Error parsing the file :', jsonError, yamlError); + reject(new BrunoError('Import collection failed')); + } + } + }; fileReader.onerror = (err) => reject(err); fileReader.readAsText(files[0]); }); @@ -167,7 +183,7 @@ const parseInsomniaCollection = (data) => { return new Promise((resolve, reject) => { try { - const insomniaExport = JSON.parse(data); + const insomniaExport = data; const insomniaResources = get(insomniaExport, 'resources', []); const insomniaCollection = insomniaResources.find((resource) => resource._type === 'workspace'); @@ -213,7 +229,7 @@ const parseInsomniaCollection = (data) => { const importCollection = () => { return new Promise((resolve, reject) => { - fileDialog({ accept: 'application/json' }) + fileDialog({ accept: '.json, .yaml, .yml, application/json, application/yaml, application/x-yaml' }) .then(readFile) .then(parseInsomniaCollection) .then(transformItemsInCollection) @@ -221,8 +237,8 @@ const importCollection = () => { .then(validateSchema) .then((collection) => resolve(collection)) .catch((err) => { - console.log(err); - reject(new BrunoError('Import collection failed')); + console.error(err); + reject(new BrunoError('Import collection failed: ' + err.message)); }); }); };