Skip to content
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
38 changes: 37 additions & 1 deletion packages/insomnia/src/utils/importers/convert.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from '@jest/globals';
import { fail } from 'assert';

import { convert } from './convert';
import { convert, dotInKeyNameInvariant } from './convert';

describe('Import errors', () => {
it('fail to find importer', async () => {
Expand All @@ -13,3 +13,39 @@ describe('Import errors', () => {
}
});
});

describe('test dotInKeyNameInvariant', () => {
[
{
input: {
'.hehe': 'haha',
},
noError: false,
},
{
input: {
'.': '',
'arr': [''],
},
noError: false,
},
{
input: [
'',
1,
],
noError: true,
},
].forEach(testCase => {
it(`check: ${testCase.input}`, () => {
let e: Error | undefined = undefined;

try {
dotInKeyNameInvariant(testCase.input);
} catch (ex) {
e = ex;
}
expect(e === undefined).toBe(testCase.noError);
});
});
});
12 changes: 12 additions & 0 deletions packages/insomnia/src/utils/importers/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const convert = async (rawData: string) => {
if (!resources) {
continue;
}
dotInKeyNameInvariant(resources);

if (resources.length > 0 && resources[0].variable) {
resources[0].environment = resources[0].variable;
Expand All @@ -51,3 +52,14 @@ export const convert = async (rawData: string) => {

throw new Error('No importers found for file');
};

// this checks invalid keys ahead, or nedb would return an error in importing.
export function dotInKeyNameInvariant(entity: object) {
JSON.stringify(entity, (key, value) => {
if (key.includes('.')) {
throw new Error(`Detected invalid key "${key}", which contains '.'. Please update it in the original tool and re-import it.`);
}

return value;
});
}