Cam import Yaml OpenApi file

This commit is contained in:
Sebastien Dionne 2023-10-22 18:28:28 -04:00
parent 9d9e4b5065
commit d06d28b7fc
3 changed files with 54 additions and 6 deletions

33
package-lock.json generated
View File

@ -16717,7 +16717,7 @@
}, },
"packages/bruno-electron": { "packages/bruno-electron": {
"name": "bruno", "name": "bruno",
"version": "v0.25.0", "version": "v0.27.0",
"dependencies": { "dependencies": {
"@aws-sdk/credential-providers": "^3.425.0", "@aws-sdk/credential-providers": "^3.425.0",
"@usebruno/js": "0.8.0", "@usebruno/js": "0.8.0",
@ -16742,6 +16742,7 @@
"http-proxy-agent": "^7.0.0", "http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2", "https-proxy-agent": "^7.0.2",
"is-valid-path": "^0.1.1", "is-valid-path": "^0.1.1",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0", "mustache": "^4.2.0",
"nanoid": "3.3.4", "nanoid": "3.3.4",
@ -16776,6 +16777,11 @@
"node": ">= 14" "node": ">= 14"
} }
}, },
"packages/bruno-electron/node_modules/argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"packages/bruno-electron/node_modules/aws4-axios": { "packages/bruno-electron/node_modules/aws4-axios": {
"version": "3.3.0", "version": "3.3.0",
"resolved": "https://registry.npmjs.org/aws4-axios/-/aws4-axios-3.3.0.tgz", "resolved": "https://registry.npmjs.org/aws4-axios/-/aws4-axios-3.3.0.tgz",
@ -16892,6 +16898,17 @@
"node": ">= 14" "node": ">= 14"
} }
}, },
"packages/bruno-electron/node_modules/js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"dependencies": {
"argparse": "^2.0.1"
},
"bin": {
"js-yaml": "bin/js-yaml.js"
}
},
"packages/bruno-electron/node_modules/uuid": { "packages/bruno-electron/node_modules/uuid": {
"version": "9.0.0", "version": "9.0.0",
"license": "MIT", "license": "MIT",
@ -21633,6 +21650,7 @@
"http-proxy-agent": "^7.0.0", "http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2", "https-proxy-agent": "^7.0.2",
"is-valid-path": "^0.1.1", "is-valid-path": "^0.1.1",
"js-yaml": "*",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0", "mustache": "^4.2.0",
"nanoid": "3.3.4", "nanoid": "3.3.4",
@ -21654,6 +21672,11 @@
"debug": "^4.3.4" "debug": "^4.3.4"
} }
}, },
"argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"aws4-axios": { "aws4-axios": {
"version": "3.3.0", "version": "3.3.0",
"resolved": "https://registry.npmjs.org/aws4-axios/-/aws4-axios-3.3.0.tgz", "resolved": "https://registry.npmjs.org/aws4-axios/-/aws4-axios-3.3.0.tgz",
@ -21724,6 +21747,14 @@
"debug": "4" "debug": "4"
} }
}, },
"js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"requires": {
"argparse": "^2.0.1"
}
},
"uuid": { "uuid": {
"version": "9.0.0" "version": "9.0.0"
} }

View File

@ -1,3 +1,4 @@
import jsyaml from 'js-yaml';
import each from 'lodash/each'; import each from 'lodash/each';
import get from 'lodash/get'; import get from 'lodash/get';
import fileDialog from 'file-dialog'; import fileDialog from 'file-dialog';
@ -8,7 +9,22 @@ import { validateSchema, transformItemsInCollection, hydrateSeqInCollection } fr
const readFile = (files) => { const readFile = (files) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const fileReader = new FileReader(); 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('Erreur de parsing du fichier :', jsonError, yamlError);
reject(new BrunoError('Import collection failed'));
}
}
};
fileReader.onerror = (err) => reject(err); fileReader.onerror = (err) => reject(err);
fileReader.readAsText(files[0]); fileReader.readAsText(files[0]);
}); });
@ -270,7 +286,7 @@ const getSecurity = (apiSpec) => {
}; };
}; };
const parseOpenapiCollection = (data) => { const parseOpenApiCollection = (data) => {
const brunoCollection = { const brunoCollection = {
name: '', name: '',
uid: uuid(), uid: uuid(),
@ -281,7 +297,7 @@ const parseOpenapiCollection = (data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
const collectionData = resolveRefs(JSON.parse(data)); const collectionData = resolveRefs(data);
if (!collectionData) { if (!collectionData) {
reject(new BrunoError('Invalid OpenAPI collection. Failed to resolve refs.')); reject(new BrunoError('Invalid OpenAPI collection. Failed to resolve refs.'));
return; return;
@ -341,9 +357,9 @@ const parseOpenapiCollection = (data) => {
const importCollection = () => { const importCollection = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fileDialog({ accept: 'application/json' }) fileDialog({ accept: '.json, .yaml, .yml, application/json, application/yaml, application/x-yaml' })
.then(readFile) .then(readFile)
.then(parseOpenapiCollection) .then(parseOpenApiCollection)
.then(transformItemsInCollection) .then(transformItemsInCollection)
.then(hydrateSeqInCollection) .then(hydrateSeqInCollection)
.then(validateSchema) .then(validateSchema)

View File

@ -42,6 +42,7 @@
"http-proxy-agent": "^7.0.0", "http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2", "https-proxy-agent": "^7.0.2",
"is-valid-path": "^0.1.1", "is-valid-path": "^0.1.1",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0", "mustache": "^4.2.0",
"nanoid": "3.3.4", "nanoid": "3.3.4",