Skip to content

Commit

Permalink
feat: specific import adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
gduliscouet-ubitransport committed Sep 5, 2024
1 parent 450b1d3 commit 10a6763
Showing 1 changed file with 53 additions and 25 deletions.
78 changes: 53 additions & 25 deletions packages/bruno-app/src/utils/importers/openapi-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { uuid } from 'utils/common';
import { BrunoError } from 'utils/common/error';
import { validateSchema, transformItemsInCollection, hydrateSeqInCollection } from './common';

const readFile = (files) => {
const readFile = (file) => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = (e) => {
Expand All @@ -26,7 +26,7 @@ const readFile = (files) => {
}
};
fileReader.onerror = (err) => reject(err);
fileReader.readAsText(files[0]);
fileReader.readAsText(file);
});
};

Expand All @@ -53,10 +53,10 @@ const buildEmptyJsonBody = (bodySchema) => {
const transformOpenapiRequestItem = (request) => {
let _operationObject = request.operationObject;

let operationName = _operationObject.summary || _operationObject.operationId || _operationObject.description;
if (!operationName) {
operationName = `${request.method} ${request.path}`;
}
// let operationName = _operationObject.summary || _operationObject.operationId || _operationObject.description;
// if (!operationName) {
const operationName = `${request.method} ${request.path}`;
// }

// replace OpenAPI links in path by Bruno variables
let path = request.path.replace(/{([a-zA-Z]+)}/g, `{{${_operationObject.operationId}_$1}}`);
Expand All @@ -69,7 +69,7 @@ const transformOpenapiRequestItem = (request) => {
url: ensureUrl(request.global.server + path),
method: request.method.toUpperCase(),
auth: {
mode: 'none',
mode: 'inherit',
basic: null,
bearer: null,
digest: null
Expand Down Expand Up @@ -142,20 +142,20 @@ const transformOpenapiRequestItem = (request) => {
token: '{{token}}'
};
} else if (auth.type === 'apiKey' && auth.in === 'header') {
brunoRequestItem.request.headers.push({
uid: uuid(),
name: auth.name,
value: '{{apiKey}}',
description: 'Authentication header',
enabled: true
});
// brunoRequestItem.request.headers.push({
// uid: uuid(),
// name: auth.name,
// value: '{{apiKey}}',
// description: 'Authentication header',
// enabled: true
// });
}
}

// TODO: handle allOf/anyOf/oneOf
if (_operationObject.requestBody) {
let content = get(_operationObject, 'requestBody.content', {});
let mimeType = Object.keys(content)[0];
let mimeType = Object.keys(content).find((key) => key !== 'application/ld+json');
let body = content[mimeType] || {};
let bodySchema = body.schema;
if (mimeType === 'application/json') {
Expand Down Expand Up @@ -297,8 +297,10 @@ const groupRequestsByTags = (requests) => {
return [groups, ungrouped];
};

const getDefaultUrl = (serverObject) => {
const getDefaultUrl = (serverObject, title) => {
let url = serverObject.url;
const name = title.toLowerCase().replace(/\s/g, '-');
if (url === '/') return `{{baseUrl-${name}}}`;
if (serverObject.variables) {
each(serverObject.variables, (variable, variableName) => {
let sub = variable.default || (variable.enum ? variable.enum[0] : `{{${variableName}}}`);
Expand Down Expand Up @@ -369,9 +371,11 @@ const parseOpenApiCollection = (data) => {
}

// TODO what if info.title not defined?
brunoCollection.name = collectionData.info.title;
brunoCollection.name = collectionData.info.title ?? 'No name';
let servers = collectionData.servers || [];
let baseUrl = servers[0] ? getDefaultUrl(servers[0]) : '';
let baseUrl = servers[0]
? getDefaultUrl(servers[0], brunoCollection.name)
: getDefaultUrl({ url: '/' }, brunoCollection.name);
let securityConfig = getSecurity(collectionData);

let allRequests = Object.entries(collectionData.paths)
Expand Down Expand Up @@ -417,20 +421,44 @@ const parseOpenApiCollection = (data) => {
});
};

const importCollection = () => {
return new Promise((resolve, reject) => {
fileDialog({ accept: '.json, .yaml, .yml, application/json, application/yaml, application/x-yaml' })
.then(readFile)
const importCollection = (file) => {
return readFile(file)
.then(parseOpenApiCollection)
.then(transformItemsInCollection)
.then(hydrateSeqInCollection)
.then(validateSchema)
.then((collection) => resolve({ collection }))
.catch((err) => {
console.error(err);
reject(new BrunoError('Import collection failed: ' + err.message));
throw new BrunoError('Import collection failed: ' + err.message);
});
};

const importCollections = async () => {
const files = await fileDialog({
accept: '.json, .yaml, .yml, application/json, application/yaml, application/x-yaml',
multiple: true
});
const collections = await Promise.all(Array.from(files).map(importCollection));
const brunoCollection = {
name: '2Cloud-services',
uid: uuid(),
version: '1',
items: [],
environments: []
};

collections.forEach(({ uid, name, items }, index) => {
brunoCollection.items.push({
uid,
type: 'folder',
seq: index,
name,
items,
filename: null,
pathname: null
});
});
return { collection: brunoCollection };
};

export default importCollection;
export default importCollections;

0 comments on commit 10a6763

Please sign in to comment.