Skip to content

Commit

Permalink
feat(ui): Include plugins attached to a request or a request folder i…
Browse files Browse the repository at this point in the history
…n export & import
  • Loading branch information
flawiddsouza committed Mar 3, 2024
1 parent 1e4133e commit b18e991
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
20 changes: 17 additions & 3 deletions packages/ui/src/components/ImportModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default {
let json = null
let collectionTree = []
let plugins = []
if(this.importFrom === 'Postman URL') {
const response = await fetch(this.urlToImport)
Expand Down Expand Up @@ -165,7 +166,10 @@ export default {
}
if(this.importFrom === 'Restfox') {
collectionTree = collectionTree.concat(convertRestfoxExportToRestfoxCollection(json, this.activeWorkspace._id))
const { newCollectionTree, newPlugins } = convertRestfoxExportToRestfoxCollection(json, this.activeWorkspace._id)
collectionTree = collectionTree.concat(newCollectionTree)
if(json.environments) {
this.activeWorkspace.environments = mergeArraysByProperty(this.activeWorkspace.environments ?? [], json.environments, 'name')
this.$store.commit('updateWorkspaceEnvironments', {
Expand All @@ -178,6 +182,10 @@ export default {
environment: this.activeWorkspace.environment,
})
}
if(newPlugins.length > 0) {
plugins = plugins.concat(newPlugins)
}
}
if(this.importFrom === 'OpenAPI') {
Expand All @@ -193,9 +201,15 @@ export default {
})
}
generateNewIdsForTree(collectionTree)
const oldIdNewIdMapping = generateNewIdsForTree(collectionTree)
plugins.forEach(plugin => {
if(plugin.collectionId) {
plugin.collectionId = oldIdNewIdMapping[plugin.collectionId]
}
})
this.$store.dispatch('setCollectionTree', { collectionTree, parentId: this.selectedRequestGroupId })
this.$store.dispatch('setCollectionTree', { collectionTree, parentId: this.selectedRequestGroupId, plugins })
const importedFileCount = this.filesToImport.length
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export default {
methods: {
async exportCollection() {
const collection = await getCollectionForWorkspace(this.activeWorkspace._id)
for(const item of collection) {
item.plugins = this.$store.state.plugins.filter(plugin => plugin.collectionId === item._id)
}
exportRestfoxCollection(collection, this.activeWorkspace.environments)
},
async clearCollection() {
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/src/components/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ export default {
if(clickedSidebarItem === 'Export') {
const collectionItemToExport = JSON.parse(JSON.stringify(this.activeSidebarItemForContextMenu))
collectionItemToExport.parentId = null
exportRestfoxCollection(flattenTree([collectionItemToExport]))
const collection = flattenTree([collectionItemToExport])
for(const item of collection) {
item.plugins = this.$store.state.plugins.filter(plugin => plugin.collectionId === item._id)
}
exportRestfoxCollection(collection)
}
if(clickedSidebarItem === 'Copy as Curl') {
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ export async function deletePluginsByWorkspace(workspaceId) {
export async function deletePluginsByCollectionIds(collectionIds) {
await db.plugins.where('collectionId').anyOf(collectionIds).delete()
}

// used for import
export async function createPlugins(plugin) {
await db.plugins.bulkPut(plugin)
}
22 changes: 19 additions & 3 deletions packages/ui/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ function importPostmanV2(collections, workspaceId) {

function importRestfoxV1(collections, workspaceId) {
const collection = []
const plugins = []

collections.forEach(item => {
if(item._type === 'request_group') {
Expand Down Expand Up @@ -935,12 +936,19 @@ function importRestfoxV1(collections, workspaceId) {
})
}
}

if(item.plugins) {
plugins.push(...item.plugins)
}
})

const collectionTree = toTree(collection)
sortTree(collectionTree)

return collectionTree
return {
newCollectionTree: collectionTree,
newPlugins: plugins
}
}

export function convertRestfoxExportToRestfoxCollection(json, workspaceId) {
Expand Down Expand Up @@ -1098,18 +1106,26 @@ export function generateNewIdsForTreeItemChildren(treeItem) {
}

export function generateNewIdsForTree(array) {
const oldIdNewIdMapping = {}

array.forEach(treeItem => {
treeItem._id = nanoid()
const newId = nanoid()
oldIdNewIdMapping[treeItem._id] = newId
treeItem._id = newId
if('children' in treeItem) {
treeItem.children.forEach(item => {
item._id = nanoid()
const newId = nanoid()
oldIdNewIdMapping[item._id] = newId
item._id = newId
item.parentId = treeItem._id
if('children' in item) {
generateNewIdsForTreeItemChildren(item)
}
})
}
})

return oldIdNewIdMapping
}

// From: https://stackoverflow.com/a/6470794/4932305
Expand Down
11 changes: 10 additions & 1 deletion packages/ui/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
createPlugin,
updatePlugin,
deletePlugin,
createPlugins,
} from './db'
import { nextTick } from 'vue'
import constants from './constants'
Expand Down Expand Up @@ -788,16 +789,24 @@ const store = createStore({
generateNewIdsForTree(collectionTree)
await createCollections(flattenTree(collectionTree))
},
async setCollectionTree(context, { collectionTree, parentId = null }) {
async setCollectionTree(context, { collectionTree, parentId = null, plugins = [] }) {
if(parentId) {
const parentCollection = findItemInTreeById(context.state.collectionTree, parentId)
collectionTree = parentCollection.children.concat(collectionTree)
} else {
collectionTree = context.state.collectionTree.concat(collectionTree)
}

addSortOrderToTree(collectionTree)

const flattenedCollectionTree = JSON.parse(JSON.stringify(flattenTree(collectionTree)))
await createCollections(flattenedCollectionTree)

if (plugins.length > 0) {
await createPlugins(plugins)
context.state.plugins.push(...plugins)
}

context.commit('setCollection', await getCollectionForWorkspace(context.state.activeWorkspace._id))
},
async updateActiveTabEnvironmentResolved(context) {
Expand Down

0 comments on commit b18e991

Please sign in to comment.