Skip to content

Instantly share code, notes, and snippets.

@hermanschutte
Created April 7, 2025 11:58
Show Gist options
  • Save hermanschutte/cb121d8df7dfca89dd1d22509319131a to your computer and use it in GitHub Desktop.
Save hermanschutte/cb121d8df7dfca89dd1d22509319131a to your computer and use it in GitHub Desktop.
Import Insomnia 5 Yaml to Yaak
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;
}
@franciscoarruda-certiv
Copy link

Hello. I tried to use your code, but maybe I'm doing something wrong. Can you tell me the steps to use this importer?

@hermanschutte
Copy link
Author

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.

@franciscoarruda-certiv
Copy link

error_

Maybe i'm having just bad luck 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment