Skip to content

Import Insomnia Collection with folder level variables and also Import collection level environments #4903

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
111 changes: 95 additions & 16 deletions packages/bruno-converters/src/insomnia/insomnia-to-bruno.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const transformInsomniaRequestItem = (request, index, allRequests) => {
name,
type: 'http-request',
request: {
url: request.url,
url: normalizeVariables(request.url),
method: request.method,
auth: {
mode: 'none',
Expand All @@ -73,8 +73,8 @@ const transformInsomniaRequestItem = (request, index, allRequests) => {
each(request.headers, (header) => {
brunoRequestItem.request.headers.push({
uid: uuid(),
name: header.name,
value: header.value,
name: normalizeVariables(header.name),
value: normalizeVariables(header.value),
description: header.description,
enabled: !header.disabled
});
Expand All @@ -83,8 +83,8 @@ const transformInsomniaRequestItem = (request, index, allRequests) => {
each(request.parameters, (param) => {
brunoRequestItem.request.params.push({
uid: uuid(),
name: param.name,
value: param.value,
name: normalizeVariables(param.name),
value: normalizeVariables(param.value),
description: param.description,
type: 'query',
enabled: !param.disabled
Expand All @@ -94,8 +94,8 @@ const transformInsomniaRequestItem = (request, index, allRequests) => {
each(request.pathParameters, (param) => {
brunoRequestItem.request.params.push({
uid: uuid(),
name: param.name,
value: param.value,
name: normalizeVariables(param.name),
value: normalizeVariables(param.value),
description: '',
type: 'path',
enabled: true
Expand All @@ -121,14 +121,14 @@ const transformInsomniaRequestItem = (request, index, allRequests) => {

if (mimeType === 'application/json') {
brunoRequestItem.request.body.mode = 'json';
brunoRequestItem.request.body.json = request.body.text;
brunoRequestItem.request.body.json = normalizeVariables(request.body.text);
} else if (mimeType === 'application/x-www-form-urlencoded') {
brunoRequestItem.request.body.mode = 'formUrlEncoded';
each(request.body.params, (param) => {
brunoRequestItem.request.body.formUrlEncoded.push({
uid: uuid(),
name: param.name,
value: param.value,
name: normalizeVariables(param.name),
value: normalizeVariables(param.value),
description: param.description,
enabled: !param.disabled
});
Expand All @@ -139,22 +139,22 @@ const transformInsomniaRequestItem = (request, index, allRequests) => {
brunoRequestItem.request.body.multipartForm.push({
uid: uuid(),
type: 'text',
name: param.name,
value: param.value,
name: normalizeVariables(param.name),
value: normalizeVariables(param.value),
description: param.description,
enabled: !param.disabled
});
});
} else if (mimeType === 'text/plain') {
brunoRequestItem.request.body.mode = 'text';
brunoRequestItem.request.body.text = request.body.text;
brunoRequestItem.request.body.text = normalizeVariables(request.body.text);
} else if (mimeType === 'text/xml' || mimeType === 'application/xml') {
brunoRequestItem.request.body.mode = 'xml';
brunoRequestItem.request.body.xml = request.body.text;
brunoRequestItem.request.body.xml = normalizeVariables(request.body.text);
} else if (mimeType === 'application/graphql') {
brunoRequestItem.type = 'graphql-request';
brunoRequestItem.request.body.mode = 'graphql';
brunoRequestItem.request.body.graphql = parseGraphQL(request.body.text);
brunoRequestItem.request.body.graphql = parseGraphQL(normalizeVariables(request.body.text));
}

return brunoRequestItem;
Expand Down Expand Up @@ -204,12 +204,39 @@ const parseInsomniaV5Collection = (data) => {
};
return transformInsomniaRequestItem(request, index, allItems);
} else if (item.children && Array.isArray(item.children)) {

let root = null;

if (item.environment && typeof item.environment === 'object') {
let requestVariables = Object.entries(item.environment).map(([name, value]) => ({
uid: uuid(),
name: normalizeVariables(name),
value: normalizeVariables(value.toString() ?? ''),
enabled: true
}));

if(requestVariables.length > 0) {
root = {
meta: {
name: item.name || 'Untitled Folder',
seq: 1
},
request: {
vars: {
req: requestVariables,
}
}
};
}
}

// Process folder
return {
uid: uuid(),
name: item.name || 'Untitled Folder',
type: 'folder',
items: parseCollectionItems(item.children, item.children)
items: parseCollectionItems(item.children, item.children),
root: root,
};
}
return null;
Expand All @@ -222,6 +249,58 @@ const parseInsomniaV5Collection = (data) => {

// Parse environments if available
if (data.environments) {
if(data.environments.data && typeof data.environments.data === 'object' && Object.keys(data.environments.data).length > 0) {
let baseEnvironment = {
uid: uuid(),
name: 'Base Environment',
variables: Object.entries(data.environments.data).map(([name, value]) => ({
uid: uuid(),
name: normalizeVariables(name),
value: normalizeVariables(value.toString() ?? ''),
type: 'text',
enabled: true
}))
};

brunoCollection.environments.push(baseEnvironment);
}

if (data.environments.subEnvironments && Array.isArray(data.environments.subEnvironments)) {
data.environments.subEnvironments.forEach((subEnv) => {
if (subEnv.data && typeof subEnv.data === 'object' && Object.keys(subEnv.data).length > 0) {
let environment = {
uid: uuid(),
name: subEnv.name || 'Untitled Environment',
variables: Object.entries(subEnv.data).map(([name, value]) => ({
uid: uuid(),
name: normalizeVariables(name),
value: normalizeVariables(value.toString() ?? ''),
type: 'text',
enabled: true
})),
};

// apply base environment variables if not exists in sub environment
if(data.environments.data && typeof data.environments.data === 'object') {
Object.keys(data.environments.data).forEach((name) => {
if (subEnv.data.hasOwnProperty(name)) {
return;
}
environment.variables.push({
uid: uuid(),
name: normalizeVariables(name),
value: normalizeVariables(data.environments.data[name].toString() ?? ''),
type: 'text',
enabled: true
});
});
}

brunoCollection.environments.push(environment);
}
});
}

// Handle environments implementation if needed
}

Expand Down