-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Import Insomnia environments #5716
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
Merged
bijin-bruno
merged 3 commits into
usebruno:main
from
sanjaikumar-bruno:feat/import-insomnia-envs
Oct 29, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { uuid } from '../common'; | ||
| import { flattenObject } from '../utils/flatten'; | ||
|
|
||
| /** | ||
| * Converts an Insomnia environment node into a Bruno environment using JSON-path-like keys. | ||
| * - Flattens env.data to dot-notation keys; values are converted to strings. | ||
| */ | ||
| export const toBrunoEnv = (env, index = 0) => { | ||
| const variables = []; | ||
| const flatEnvData = flattenObject(env?.data || {}); | ||
| Object.entries(flatEnvData).forEach(([name, value]) => { | ||
| variables.push({ | ||
| uid: uuid(), | ||
| name, | ||
| value: String(value), | ||
| type: 'text', | ||
| enabled: true, | ||
| secret: false | ||
| }); | ||
| }); | ||
|
|
||
| return { | ||
| uid: uuid(), | ||
| name: (env?.name && String(env.name).trim()) || `Environment ${index + 1}`, | ||
| variables | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Shallowly merges two flattened env data objects. | ||
| * - Keys in override replace keys in base. | ||
| * - No recursive merging. | ||
| */ | ||
| const shallowMergeFlat = (baseFlat = {}, overrideFlat = {}) => ({ ...baseFlat, ...overrideFlat }); | ||
|
|
||
| /** | ||
| * Builds Bruno environments from Insomnia v5 environments. | ||
| * - Expects a single object (base env) with optional subEnvironments. | ||
| * - Creates one env for base and one env per sub using flattened, shallow-merged keys. | ||
| */ | ||
| export const buildV5Environments = (baseEnv) => { | ||
| if (!baseEnv || typeof baseEnv !== 'object') return []; | ||
|
|
||
| const result = []; | ||
|
|
||
| // include base as standalone | ||
| result.push(toBrunoEnv(baseEnv)); | ||
|
|
||
| const subs = Array.isArray(baseEnv.subEnvironments) ? baseEnv.subEnvironments : []; | ||
| const baseFlat = flattenObject(baseEnv?.data || {}); | ||
| subs.forEach((sub, i) => { | ||
| const subFlat = flattenObject(sub?.data || {}); | ||
| const mergedFlat = shallowMergeFlat(baseFlat, subFlat); | ||
| result.push(toBrunoEnv({ name: sub?.name, data: mergedFlat }, i + 1)); | ||
| }); | ||
| return result; | ||
| }; | ||
|
|
||
| /** | ||
| * Builds Bruno environments from Insomnia v4 resources. | ||
| * - Base env: parentId equals workspaceId; included as-is (flattened). | ||
| * - Sub envs: merge base (flattened) with sub (flattened) and import. | ||
| * | ||
| * Note: Insomnia supports only ONE base environment per workspace. | ||
| */ | ||
| export const buildV4Environments = (resources, workspaceId) => { | ||
| const allEnvResources = resources.filter((r) => r._type === 'environment') || []; | ||
| const envById = {}; | ||
| allEnvResources.forEach((e) => (envById[e._id] = e)); | ||
|
|
||
| const isBaseEnv = (env) => env.parentId === workspaceId; | ||
|
|
||
| const result = []; | ||
|
|
||
| const baseEnv = allEnvResources.find(isBaseEnv); | ||
| if (baseEnv) { | ||
| result.push(toBrunoEnv(baseEnv)); | ||
| } | ||
|
|
||
| // sub envs - all inherit from the single base environment | ||
| const subEnvs = allEnvResources.filter((e) => !isBaseEnv(e)); | ||
| const baseFlat = flattenObject(baseEnv?.data || {}); | ||
| subEnvs.forEach((sub, idx) => { | ||
| const subFlat = flattenObject(sub.data || {}); | ||
| const mergedFlat = shallowMergeFlat(baseFlat, subFlat); | ||
| result.push(toBrunoEnv({ name: sub.name, data: mergedFlat }, idx + 1)); | ||
| }); | ||
|
|
||
| return result; | ||
| }; | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Adapted from flat library by Hugh Kennedy (https://github.com/hughsk/flat) | ||
| // MIT License | ||
|
|
||
| /** | ||
| * Recursively flattens a nested object or array into a flat object with JavaScript-style keys. | ||
| * Arrays use square bracket notation (e.g., items[0].id). | ||
| * Only primitives and null are included as values. | ||
| * | ||
| * @param {object|array} obj - The object or array to flatten. | ||
| * @param {string} [prefix] - Used internally for recursion to build the path. | ||
| * @returns {object} A flat object with JavaScript-style keys. | ||
| */ | ||
| function flattenObject(obj, prefix = '') { | ||
| // Store the final flat result | ||
| const result = {}; | ||
|
|
||
| /** | ||
| * Internal recursive function to process each value. | ||
| * @param {*} value - The current value (can be object, array, primitive, or null) | ||
| * @param {string} path - The JavaScript-style key up to this point | ||
| */ | ||
| function step(value, path) { | ||
| // If value is a primitive (string, number, boolean) or null, add it to the result | ||
| if (value === null || typeof value !== 'object') { | ||
| result[path] = value; | ||
| return; | ||
| } | ||
|
|
||
| // If value is an array, iterate over each item by index | ||
| if (Array.isArray(value)) { | ||
| value.forEach((item, idx) => { | ||
| // Build the next path with array index using square brackets (e.g. "items[0]") | ||
| step(item, path ? `${path}[${idx}]` : `[${idx}]`); | ||
| }); | ||
| } else { | ||
| // If value is an object, iterate over its keys | ||
| Object.entries(value).forEach(([key, val]) => { | ||
| // Build the next path with object key (e.g. "user.name") | ||
| step(val, path ? `${path}.${key}` : key); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Start recursive flattening from the root object | ||
| step(obj, prefix); | ||
|
|
||
| // Return the flat result object | ||
| return result; | ||
| } | ||
|
|
||
| export { flattenObject }; |
100 changes: 100 additions & 0 deletions
100
packages/bruno-converters/tests/insomnia/env-utils.spec.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { describe, it, expect } from '@jest/globals'; | ||
| import { buildV5Environments, buildV4Environments } from '../../src/insomnia/env-utils'; | ||
|
|
||
| const getVar = (env, name) => { | ||
| return env.variables.find((v) => v.name === name); | ||
| }; | ||
|
|
||
| describe('env-utils', () => { | ||
| describe('buildV5Environments', () => { | ||
| it('creates base and sub environments with flattened keys and shallow overrides', () => { | ||
| const environmentsNode = { | ||
| name: 'Base', | ||
| data: { | ||
| baseurl: 'https://api.example.com', | ||
| nested: { name: 'alice', roles: ['admin'] }, | ||
| numbers: [1, 2] | ||
| }, | ||
| subEnvironments: [ | ||
| { | ||
| name: 'Staging', | ||
| data: { | ||
| baseurl: 'https://staging.example.com', | ||
| nested: { name: 'bob' } | ||
| } | ||
| }, | ||
| { name: 'Dev', data: {} } | ||
| ] | ||
| }; | ||
|
|
||
| const envs = buildV5Environments(environmentsNode); | ||
| expect(envs.length).toBe(3); | ||
|
|
||
| const base = envs[0]; | ||
| const staging = envs[1]; | ||
| const dev = envs[2]; | ||
|
|
||
| expect(base.name).toBe('Base'); | ||
| expect(getVar(base, 'baseurl')?.value).toBe('https://api.example.com'); | ||
| expect(getVar(base, 'nested.name')?.value).toBe('alice'); | ||
| expect(getVar(base, 'nested.roles[0]')?.value).toBe('admin'); | ||
| expect(getVar(base, 'numbers[1]')?.value).toBe('2'); | ||
|
|
||
| expect(staging.name).toBe('Staging'); | ||
| // baseurl overridden in sub | ||
| expect(getVar(staging, 'baseurl')?.value).toBe('https://staging.example.com'); | ||
| // nested.name overridden, nested array preserved from base | ||
| expect(getVar(staging, 'nested.name')?.value).toBe('bob'); | ||
| expect(getVar(staging, 'nested.roles[0]')?.value).toBe('admin'); | ||
|
|
||
| expect(dev.name).toBe('Dev'); | ||
| // no sub data => inherits base | ||
| expect(getVar(dev, 'baseurl')?.value).toBe('https://api.example.com'); | ||
| expect(getVar(dev, 'nested.name')?.value).toBe('alice'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildV4Environments', () => { | ||
| it('merges nearest base and sub env data (flattened) into standalone Bruno envs', () => { | ||
| const workspaceId = 'wrk_1'; | ||
| const resources = [ | ||
| { _id: workspaceId, _type: 'workspace', name: 'WS' }, | ||
| { | ||
| _id: 'env_base', | ||
| _type: 'environment', | ||
| parentId: workspaceId, | ||
| name: 'Base', | ||
| data: { | ||
| baseurl: 'https://api.example.com', | ||
| user: { name: 'alice' }, | ||
| arr: [{ id: 1 }] | ||
| } | ||
| }, | ||
| { | ||
| _id: 'env_sub', | ||
| _type: 'environment', | ||
| parentId: 'env_base', | ||
| name: 'Sub', | ||
| data: { | ||
| user: { name: 'bob' } | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const envs = buildV4Environments(resources, workspaceId); | ||
| expect(envs.length).toBe(2); | ||
|
|
||
| const base = envs.find((e) => e.name === 'Base'); | ||
| const sub = envs.find((e) => e.name === 'Sub'); | ||
|
|
||
| expect(getVar(base, 'baseurl')?.value).toBe('https://api.example.com'); | ||
| expect(getVar(base, 'user.name')?.value).toBe('alice'); | ||
| expect(getVar(base, 'arr[0].id')?.value).toBe('1'); | ||
|
|
||
| // sub should inherit base, override user.name | ||
| expect(getVar(sub, 'baseurl')?.value).toBe('https://api.example.com'); | ||
| expect(getVar(sub, 'user.name')?.value).toBe('bob'); | ||
| expect(getVar(sub, 'arr[0].id')?.value).toBe('1'); | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.