From 2aa7d26a897d76c1e4dc97c8267f331c01be0fd4 Mon Sep 17 00:00:00 2001 From: lohit Date: Thu, 4 Jul 2024 13:18:40 +0530 Subject: [PATCH] add collection and folder data to exported bruno collection json (#2560) * add collection and folder data to exported bruno collection json * folder root data order --- .../bruno-app/src/utils/collections/index.js | 20 +++++++++ packages/bruno-cli/src/utils/bru.js | 1 - packages/bruno-electron/src/bru/index.js | 14 +++---- packages/bruno-electron/src/ipc/collection.js | 8 ++++ .../bruno-schema/src/collections/index.js | 41 ++++++++++++++++++- 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/packages/bruno-app/src/utils/collections/index.js b/packages/bruno-app/src/utils/collections/index.js index 6145ee200..a30f901cb 100644 --- a/packages/bruno-app/src/utils/collections/index.js +++ b/packages/bruno-app/src/utils/collections/index.js @@ -380,6 +380,25 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {} } } + if (si?.root) { + di.root = { + request: { + headers: si?.root?.request?.headers, + script: si?.root?.request?.script, + vars: si?.root?.request?.vars, + tests: si?.root?.request?.tests + }, + docs: si?.root?.request?.docs, + meta: { + name: si?.root?.meta?.name + } + }; + + di.root.request.auth = { + mode: get(si.root.request, 'auth.mode', 'none') + }; + } + if (si.type === 'js') { di.fileContent = si.raw; } @@ -402,6 +421,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {} collectionToSave.items = []; collectionToSave.activeEnvironmentUid = collection.activeEnvironmentUid; collectionToSave.environments = collection.environments || []; + collectionToSave.root = collection.root || {}; collectionToSave.brunoConfig = cloneDeep(collection?.brunoConfig); diff --git a/packages/bruno-cli/src/utils/bru.js b/packages/bruno-cli/src/utils/bru.js index 262cca650..713ef928a 100644 --- a/packages/bruno-cli/src/utils/bru.js +++ b/packages/bruno-cli/src/utils/bru.js @@ -13,7 +13,6 @@ const collectionBruToJson = (bru) => { const transformedJson = { request: { - params: _.get(json, 'params', []), headers: _.get(json, 'headers', []), auth: _.get(json, 'auth', {}), script: _.get(json, 'script', {}), diff --git a/packages/bruno-electron/src/bru/index.js b/packages/bruno-electron/src/bru/index.js index 07041b93b..3159b5167 100644 --- a/packages/bruno-electron/src/bru/index.js +++ b/packages/bruno-electron/src/bru/index.js @@ -14,7 +14,6 @@ const collectionBruToJson = (bru) => { const transformedJson = { request: { - params: _.get(json, 'params', []), headers: _.get(json, 'headers', []), auth: _.get(json, 'auth', {}), script: _.get(json, 'script', {}), @@ -29,8 +28,7 @@ const collectionBruToJson = (bru) => { // in the future, all of this will be replaced by standard bru lang if (json.meta) { transformedJson.meta = { - name: json.meta.name, - seq: json.meta.seq + name: json.meta.name }; } @@ -43,7 +41,6 @@ const collectionBruToJson = (bru) => { const jsonToCollectionBru = (json) => { try { const collectionBruJson = { - params: _.get(json, 'request.params', []), headers: _.get(json, 'request.headers', []), auth: _.get(json, 'request.auth', {}), script: { @@ -61,10 +58,9 @@ const jsonToCollectionBru = (json) => { // add meta if it exists // this is only for folder bru file // in the future, all of this will be replaced by standard bru lang - if (json.meta) { + if (json?.meta) { collectionBruJson.meta = { - name: json.meta.name, - seq: json.meta.seq + name: json.meta.name }; } @@ -123,7 +119,6 @@ const bruToJson = (bru) => { } const sequence = _.get(json, 'meta.seq'); - const transformedJson = { type: requestType, name: _.get(json, 'meta.name'), @@ -170,11 +165,12 @@ const jsonToBru = (json) => { type = 'http'; } + const sequence = _.get(json, 'meta.seq'); const bruJson = { meta: { name: _.get(json, 'name'), type: type, - seq: _.get(json, 'seq') + seq: !isNaN(sequence) ? Number(sequence) : 1 }, http: { method: _.lowerCase(_.get(json, 'request.method')), diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 029129308..957ecc1d9 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -439,6 +439,11 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection const folderPath = path.join(currentPath, item.name); fs.mkdirSync(folderPath); + const folderBruFilePath = path.join(folderPath, 'folder.bru'); + const folderContent = jsonToCollectionBru(item.root); + console.log('folder COntent', item.root, folderContent); + fs.writeFileSync(folderBruFilePath, folderContent); + if (item.items && item.items.length) { parseCollectionItems(item.items, folderPath); } @@ -488,6 +493,9 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection // Write the Bruno configuration to a file await writeFile(path.join(collectionPath, 'bruno.json'), stringifiedBrunoConfig); + const collectionContent = jsonToCollectionBru(collection.root); + await writeFile(path.join(collectionPath, 'collection.bru'), collectionContent); + mainWindow.webContents.send('main:collection-opened', collectionPath, uid, brunoConfig); ipcMain.emit('main:collection-opened', mainWindow, collectionPath, uid, brunoConfig); diff --git a/packages/bruno-schema/src/collections/index.js b/packages/bruno-schema/src/collections/index.js index cc93ed671..64a2b98fb 100644 --- a/packages/bruno-schema/src/collections/index.js +++ b/packages/bruno-schema/src/collections/index.js @@ -231,6 +231,39 @@ const requestSchema = Yup.object({ .noUnknown(true) .strict(); +const folderRootSchema = Yup.object({ + request: Yup.object({ + headers: Yup.array().of(keyValueSchema).required('headers are required'), + auth: authSchema, + script: Yup.object({ + req: Yup.string().nullable(), + res: Yup.string().nullable() + }) + .noUnknown(true) + .strict(), + vars: Yup.object({ + req: Yup.array().of(varsSchema).nullable(), + res: Yup.array().of(varsSchema).nullable() + }) + .noUnknown(true) + .strict() + .nullable(), + tests: Yup.string().nullable() + }) + .noUnknown(true) + .strict() + .nullable(), + docs: Yup.string().nullable(), + meta: Yup.object({ + name: Yup.string().nullable() + }) + .noUnknown(true) + .strict() + .nullable() +}) + .noUnknown(true) + .nullable(); + const itemSchema = Yup.object({ uid: uidSchema, type: Yup.string().oneOf(['http-request', 'graphql-request', 'folder', 'js']).required('type is required'), @@ -248,6 +281,11 @@ const itemSchema = Yup.object({ // For all other types, the fileContent field is not required and can be null. otherwise: Yup.string().nullable() }), + root: Yup.mixed().when('type', { + is: 'folder', + then: folderRootSchema, + otherwise: Yup.mixed().nullable().notRequired() + }), items: Yup.lazy(() => Yup.array().of(itemSchema)), filename: Yup.string().nullable(), pathname: Yup.string().nullable() @@ -270,7 +308,8 @@ const collectionSchema = Yup.object({ items: Yup.array() }), collectionVariables: Yup.object(), - brunoConfig: Yup.object() + brunoConfig: Yup.object(), + root: folderRootSchema }) .noUnknown(true) .strict();