Merge pull request #754 from survivant/feature/add-yaml-support-insomnia

[feature] Can import Insomnia Yaml file
This commit is contained in:
Anoop M D 2023-10-24 03:51:46 +05:30 committed by GitHub
commit d90cb91a19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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));
});
});
};