Created
April 7, 2025 11:58
-
-
Save hermanschutte/cb121d8df7dfca89dd1d22509319131a to your computer and use it in GitHub Desktop.
Import Insomnia 5 Yaml to Yaak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import YAML from 'yaml'; | |
import { PluginDefinition } from '@yaakapp/api'; | |
interface HttpRequest { | |
id: string; | |
createdAt?: string; | |
updatedAt?: string; | |
workspaceId: string; | |
model: string; | |
sortPriority: number; | |
name: string; | |
description?: string; | |
url: string; | |
method: string; | |
headers: Array<{enabled: boolean; name: string; value: string}>; | |
body?: { | |
text?: string; | |
mimeType?: string; | |
params?: any; | |
}; | |
} | |
interface Resources { | |
workspaces: Array<any>; | |
environments: Array<any>; | |
httpRequests: Array<HttpRequest>; | |
grpcRequests: Array<any>; | |
folders: Array<any>; | |
} | |
export const plugin: PluginDefinition = { | |
importer: { | |
name: 'Insomnia', | |
description: 'Import Insomnia workspaces', | |
onImport(ctx, args: { text: string }) { | |
return convertInsomnia(args.text) as any; | |
}, | |
}, | |
}; | |
function convertInsomnia(contents: string) { | |
let parsed: any; | |
try { | |
parsed = JSON.parse(contents); | |
} catch (e) { | |
try { | |
parsed = YAML.parse(contents); | |
} catch (e) { | |
return; | |
} | |
} | |
if (!isJSObject(parsed)) return; | |
// Handle v5 format | |
if (parsed.type?.startsWith('collection.insomnia.rest/5.0')) { | |
const resources: Resources = { | |
workspaces: [{ | |
id: convertId(parsed.meta.id), | |
createdAt: parsed.meta.created ? new Date(parsed.meta.created).toISOString().replace('Z', '') : undefined, | |
updatedAt: parsed.meta.modified ? new Date(parsed.meta.modified).toISOString().replace('Z', '') : undefined, | |
model: 'workspace', | |
name: parsed.name, | |
}], | |
environments: [], | |
httpRequests: [], | |
grpcRequests: [], | |
folders: [], | |
}; | |
// Import requests from collection | |
if (Array.isArray(parsed.collection)) { | |
let sortPriority = 0; | |
for (const item of parsed.collection) { | |
if (isHttpRequest(item)) { | |
resources.httpRequests.push( | |
importHttpRequestV5(item, parsed.meta.id, sortPriority++) | |
); | |
} | |
} | |
} | |
// Import environments | |
if (Array.isArray(parsed.environments)) { | |
for (const env of parsed.environments) { | |
resources.environments.push( | |
importEnvironmentV5(env, parsed.meta.id) | |
); | |
} | |
} | |
return { resources: deleteUndefinedAttrs(resources) }; | |
} | |
// Fallback to v4 format handling | |
// ... existing v4 conversion code ... | |
} | |
function importHttpRequestV5(r: any, workspaceId: string, sortPriority = 0) { | |
return { | |
id: convertId(r.meta.id), | |
createdAt: r.meta.created ? new Date(r.meta.created).toISOString().replace('Z', '') : undefined, | |
updatedAt: r.meta.modified ? new Date(r.meta.modified).toISOString().replace('Z', '') : undefined, | |
workspaceId: convertId(workspaceId), | |
model: 'http_request', | |
sortPriority, | |
name: r.name, | |
description: r.description, | |
url: r.url, | |
method: r.method, | |
headers: (r.headers ?? []) | |
.map((h: any) => ({ | |
enabled: !h.disabled, | |
name: h.name ?? '', | |
value: h.value ?? '', | |
})) | |
.filter(({name, value}: any) => name !== '' || value !== ''), | |
body: r.body ? { | |
text: r.body.text ?? '', | |
mimeType: r.body.mimeType, | |
params: r.body.params, | |
} : undefined, | |
}; | |
} | |
function importEnvironmentV5(e: any, workspaceId: string) { | |
return { | |
id: convertId(e.meta.id), | |
createdAt: e.meta.created ? new Date(e.meta.created).toISOString().replace('Z', '') : undefined, | |
updatedAt: e.meta.modified ? new Date(e.meta.modified).toISOString().replace('Z', '') : undefined, | |
workspaceId: convertId(workspaceId), | |
model: 'environment', | |
name: e.name, | |
variables: Object.entries(e.data || {}).map(([name, value]) => ({ | |
enabled: true, | |
name, | |
value: `${value}`, | |
})), | |
}; | |
} | |
function isJSObject(obj: any): obj is Record<string, any> { | |
return obj !== null && typeof obj === 'object' && !Array.isArray(obj); | |
} | |
function convertId(id: string): string { | |
return id.replace(/^(req|wrk|env|fld)_/, ''); | |
} | |
function isHttpRequest(item: any): boolean { | |
return item.method && item.url && item.meta?.id; | |
} | |
function deleteUndefinedAttrs(obj: any): any { | |
if (Array.isArray(obj)) { | |
return obj.map(deleteUndefinedAttrs); | |
} | |
if (obj !== null && typeof obj === 'object') { | |
return Object.fromEntries( | |
Object.entries(obj) | |
.filter(([_, v]) => v !== undefined) | |
.map(([k, v]) => [k, deleteUndefinedAttrs(v)]) | |
); | |
} | |
return obj; | |
} |
Hi. You need to follow the tutorial here: https://feedback.yaak.app/help/articles/6911763-quick-start and then just use the code above in the index.ts for your plugin.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. I tried to use your code, but maybe I'm doing something wrong. Can you tell me the steps to use this importer?