Skip to content

Commit

Permalink
feat(ui): Navbar > Export as Insomnia collection (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Aug 16, 2024
1 parent 6cfc1c3 commit 9683d03
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
15 changes: 13 additions & 2 deletions packages/ui/src/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ import {
toTree,
flattenTree,
convertCollectionsFromRestfoxToPostman,
exportAsPostmanCollection,
convertCollectionsFromRestfoxToInsomnia,
exportCollection,
} from '@/helpers'
import { getCollectionForWorkspace } from '@/db'
import constants from '../constants'
Expand Down Expand Up @@ -278,7 +279,11 @@ export default {
}
if (value === 'Postman') {
exportAsPostmanCollection(await convertCollectionsFromRestfoxToPostman(collection))
exportCollection(await convertCollectionsFromRestfoxToPostman(collection), value)
}
if (value === 'Insomnia') {
exportCollection(await convertCollectionsFromRestfoxToInsomnia(collection), value)
}
},
setActiveWorkspace(workspace) {
Expand Down Expand Up @@ -397,6 +402,12 @@ export default {
value: 'Postman',
class: 'context-menu-item-with-left-padding'
},
{
type: 'option',
label: 'Insomnia collection',
value: 'Insomnia',
class: 'context-menu-item-with-left-padding'
},
]
},
selectEnv(value) {
Expand Down
97 changes: 94 additions & 3 deletions packages/ui/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1408,8 +1408,8 @@ export function exportRestfoxCollection(collection: CollectionItem[], environmen
})
}

export function exportAsPostmanCollection(collection: any) {
downloadObjectAsJSON(`Postman_${todayISODate()}.json`, collection)
export function exportCollection(collection: any, appName: 'Postman' | 'Insomnia') {
downloadObjectAsJSON(`${appName}_${todayISODate()}.json`, collection)
}

// From: https://github.com/Kong/insomnia/blob/fac2627d695a10865d0f7f9ea7b2c04a77d92194/packages/insomnia/src/common/misc.ts#L169-L192
Expand Down Expand Up @@ -1792,7 +1792,7 @@ export function toggleDropdown(event: any, dropdownState: any) {
* @param {string} scriptType - The type of script being converted.
* @returns {string} - The converted script.
*/
export function scriptConversion(scriptToConvert: string, scriptType: 'postmanToRestfox' | 'restfoxToPostman') {
export function scriptConversion(scriptToConvert: string, scriptType: 'postmanToRestfox' | 'restfoxToPostman' | 'restfoxToInsomnia') {
const mappings = {
postmanToRestfox: {
'pm.environment.set': 'rf.setEnvVar',
Expand All @@ -1804,6 +1804,11 @@ export function scriptConversion(scriptToConvert: string, scriptType: 'postmanTo
'rf.getEnvVar': 'pm.environment.get',
'rf.response.getBodyJSON()': 'pm.response.json()'
},
restfoxToInsomnia: {
'rf.setEnvVar': 'insomnia.setEnvironmentVariable',
'rf.getEnvVar': 'insomnia.getEnvironmentVariable',
'rf.response.getBodyJSON()': 'insomnia.response.json()'
},
}

const selectedMapping = mappings[scriptType]
Expand Down Expand Up @@ -1908,3 +1913,89 @@ export async function convertCollectionsFromRestfoxToPostman(restfoxCollections:

return postmanCollection
}

export async function convertCollectionsFromRestfoxToInsomnia(restfoxCollections: any) {
const insomniaCollection: any = {
_type: 'export',
__export_format: 4,
__export_date: new Date().toISOString(),
__export_source: 'restfox-to-insomnia-converter',
resources: []
}

const workspaceId = restfoxCollections[0].workspaceId || 'root_workspace'

const workspace = {
_id: workspaceId,
_type: 'workspace',
name: 'Imported from Restfox',
description: '',
scope: 'collection',
}

insomniaCollection.resources.push(workspace)

restfoxCollections.forEach((restfoxRequest: any) => {
if (restfoxRequest._type !== 'request') {
return
}

const insomniaRequest: any = {
_id: restfoxRequest._id,
_type: 'request',
parentId: workspaceId,
name: restfoxRequest.name || restfoxRequest.url,
method: restfoxRequest.method,
url: restfoxRequest.url,
body: {
mimeType: restfoxRequest.body?.mimeType || constants.MIME_TYPE.JSON,
text: restfoxRequest.body?.text || ''
},
headers: restfoxRequest.headers.map((header: any) => ({
name: header.name,
value: header.value
})),
authentication: convertRestfoxAuthToInsomniaAuth(restfoxRequest.authentication),
parameters: restfoxRequest.parameters?.map((param: any) => ({
name: param.name,
value: param.value
})),
}

const scripts = restfoxRequest.plugins?.find((plugin: any) => plugin.type === 'script')
if (scripts) {
insomniaRequest.hook = {
preRequest: scriptConversion(scripts.code.pre_request, 'restfoxToInsomnia').trim() || '',
postRequest: scriptConversion(scripts.code.post_request, 'restfoxToInsomnia').trim() || ''
}
}

insomniaCollection.resources.push(insomniaRequest)
})

return insomniaCollection
}

function convertRestfoxAuthToInsomniaAuth(auth: any) {
const insomniaAuth: any = {}

switch (auth?.type) {
case 'No Auth':
insomniaAuth.type = 'none'
break
case 'Basic Auth':
insomniaAuth.type = 'basic'
insomniaAuth.username = auth?.username || ''
insomniaAuth.password = auth?.password || ''
break
case 'Bearer Token':
insomniaAuth.type = 'bearer'
insomniaAuth.token = auth?.token || ''
break
default:
insomniaAuth.type = 'none'
break
}

return insomniaAuth
}

0 comments on commit 9683d03

Please sign in to comment.