Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): Navbar > Export as Insomnia collection #226

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
toTree,
flattenTree,
convertCollectionsFromRestfoxToPostman,
exportAsPostmanCollection,
convertCollectionsFromRestfoxToInsomnia,
exportCollection,
} from '@/helpers'
import { getCollectionForWorkspace } from '@/db'
import constants from '../constants'
Expand All @@ -143,7 +144,7 @@
LogsModal
},
props: {
nav: String,

Check warning on line 147 in packages/ui/src/components/NavBar.vue

View workflow job for this annotation

GitHub Actions / test

Prop 'nav' requires default value to be set
},
data() {
return {
Expand Down Expand Up @@ -278,7 +279,11 @@
}

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 @@
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
}
Loading