1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83import type { ImportEntry, ImportRequest } from './entities';
import { setDefaults } from './utils';
export interface InsomniaImporter {
id: string;
name: string;
description: string;
}
export interface ConvertResult {
type: InsomniaImporter;
data: {
_type: 'export';
__export_format: 4;
__export_date: string;
__export_source: `insomnia.importers:v${string}`;
resources: ImportRequest[];
};
}
export const convert = async (
importEntry: ImportEntry,
{
importerId,
}: {
importerId?: string;
} = {},
) => {
let importers = (await import('./importers')).importers;
const errMsgList: string[] = [];
if (importerId) {
importers = importers.filter(i => i.id === importerId);
}
for (const importer of importers) {
const resources = await (importer.acceptFilePath === true
? importer.convert(importEntry)
: importer.convert(importEntry.contentStr));
if (!resources) {
continue;
}
if ('convertErrorMessage' in resources) {
// ConvertErrorResult
errMsgList.push(`Error in importer ${importer.name}: ${resources.convertErrorMessage}`);
continue;
}
dotInKeyNameInvariant(resources);
const convertedResult = {
type: {
id: importer.id,
name: importer.name,
description: importer.description,
},
data: {
_type: 'export',
__export_format: 4,
__export_date: new Date().toISOString(),
__export_source: 'insomnia.importers:v0.1.0',
resources: resources.map(setDefaults) as ImportRequest[],
},
};
return convertedResult;
}
throw new Error(errMsgList.length > 0 ? errMsgList.join('\n') : '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;
});
}