Skip to content

Commit 74577cd

Browse files
JeroenVdbgschier
andauthored
Convert Insomnia variables syntax in headers, parameters and form data (#291)
Co-authored-by: Gregory Schier <gschier1990@gmail.com>
1 parent 4f79352 commit 74577cd

File tree

5 files changed

+53
-37
lines changed

5 files changed

+53
-37
lines changed

plugins/importer-insomnia/src/common.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
2-
export function convertSyntax(variable: string): string {
3-
if (!isJSString(variable)) return variable;
4-
return variable.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}');
5-
}
6-
71
export function isJSObject(obj: unknown) {
82
return Object.prototype.toString.call(obj) === '[object Object]';
93
}
@@ -32,3 +26,18 @@ export function deleteUndefinedAttrs<T>(obj: T): T {
3226
return obj;
3327
}
3428
}
29+
30+
/** Recursively render all nested object properties */
31+
export function convertTemplateSyntax<T>(obj: T): T {
32+
if (typeof obj === 'string') {
33+
return obj.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}') as T;
34+
} else if (Array.isArray(obj) && obj != null) {
35+
return obj.map(convertTemplateSyntax) as T;
36+
} else if (typeof obj === 'object' && obj != null) {
37+
return Object.fromEntries(
38+
Object.entries(obj).map(([k, v]) => [k, convertTemplateSyntax(v)]),
39+
) as T;
40+
} else {
41+
return obj;
42+
}
43+
}

plugins/importer-insomnia/src/v4.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import type { PartialImportResources } from '@yaakapp/api';
3-
import { convertId, convertSyntax, isJSObject } from './common';
3+
import { convertId, convertTemplateSyntax, isJSObject } from './common';
44

55
export function convertInsomniaV4(parsed: any) {
66
if (!Array.isArray(parsed.resources)) return null;
@@ -60,7 +60,7 @@ export function convertInsomniaV4(parsed: any) {
6060
resources.environments = resources.environments.filter(Boolean);
6161
resources.workspaces = resources.workspaces.filter(Boolean);
6262

63-
return { resources };
63+
return { resources: convertTemplateSyntax(resources) };
6464
}
6565

6666
function importHttpRequest(r: any, workspaceId: string): PartialImportResources['httpRequests'][0] {
@@ -90,24 +90,24 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[
9090
};
9191
} else if (r.body?.mimeType === 'application/graphql') {
9292
bodyType = 'graphql';
93-
body = { text: convertSyntax(r.body.text ?? '') };
93+
body = { text: r.body.text ?? '' };
9494
} else if (r.body?.mimeType === 'application/json') {
9595
bodyType = 'application/json';
96-
body = { text: convertSyntax(r.body.text ?? '') };
96+
body = { text: r.body.text ?? '' };
9797
}
9898

9999
let authenticationType: string | null = null;
100100
let authentication = {};
101101
if (r.authentication.type === 'bearer') {
102102
authenticationType = 'bearer';
103103
authentication = {
104-
token: convertSyntax(r.authentication.token),
104+
token: r.authentication.token,
105105
};
106106
} else if (r.authentication.type === 'basic') {
107107
authenticationType = 'basic';
108108
authentication = {
109-
username: convertSyntax(r.authentication.username),
110-
password: convertSyntax(r.authentication.password),
109+
username: r.authentication.username,
110+
password: r.authentication.password,
111111
};
112112
}
113113

@@ -121,13 +121,12 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[
121121
sortPriority: r.metaSortKey,
122122
name: r.name,
123123
description: r.description || undefined,
124-
url: convertSyntax(r.url),
125-
urlParameters: (r.parameters ?? [])
126-
.map((p: any) => ({
127-
enabled: !p.disabled,
128-
name: p.name ?? '',
129-
value: p.value ?? '',
130-
})),
124+
url: r.url,
125+
urlParameters: (r.parameters ?? []).map((p: any) => ({
126+
enabled: !p.disabled,
127+
name: p.name ?? '',
128+
value: p.value ?? '',
129+
})),
131130
body,
132131
bodyType,
133132
authentication,
@@ -158,7 +157,7 @@ function importGrpcRequest(r: any, workspaceId: string): PartialImportResources[
158157
sortPriority: r.metaSortKey,
159158
name: r.name,
160159
description: r.description || undefined,
161-
url: convertSyntax(r.url),
160+
url: r.url,
162161
service,
163162
method,
164163
message: r.body?.text ?? '',

plugins/importer-insomnia/src/v5.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import type { PartialImportResources } from '@yaakapp/api';
3-
import { convertId, convertSyntax, isJSObject } from './common';
3+
import { convertId, convertTemplateSyntax, isJSObject } from './common';
44

55
export function convertInsomniaV5(parsed: any) {
66
// Assert parsed is object
@@ -69,7 +69,7 @@ export function convertInsomniaV5(parsed: any) {
6969
resources.environments = resources.environments.filter(Boolean);
7070
resources.workspaces = resources.workspaces.filter(Boolean);
7171

72-
return { resources };
72+
return { resources: convertTemplateSyntax(resources) };
7373
}
7474

7575
function importHttpRequest(
@@ -108,10 +108,10 @@ function importHttpRequest(
108108
};
109109
} else if (r.body?.mimeType === 'application/graphql') {
110110
bodyType = 'graphql';
111-
body = { text: convertSyntax(r.body.text ?? '') };
111+
body = { text: r.body.text ?? '' };
112112
} else if (r.body?.mimeType === 'application/json') {
113113
bodyType = 'application/json';
114-
body = { text: convertSyntax(r.body.text ?? '') };
114+
body = { text: r.body.text ?? '' };
115115
}
116116

117117
return {
@@ -124,13 +124,12 @@ function importHttpRequest(
124124
model: 'http_request',
125125
name: r.name,
126126
description: r.meta?.description || undefined,
127-
url: convertSyntax(r.url),
128-
urlParameters: (r.parameters ?? [])
129-
.map((p: any) => ({
130-
enabled: !p.disabled,
131-
name: p.name ?? '',
132-
value: p.value ?? '',
133-
})),
127+
url: r.url,
128+
urlParameters: (r.parameters ?? []).map((p: any) => ({
129+
enabled: !p.disabled,
130+
name: p.name ?? '',
131+
value: p.value ?? '',
132+
})),
134133
body,
135134
bodyType,
136135
method: r.method,
@@ -163,7 +162,7 @@ function importGrpcRequest(
163162
sortPriority: sortKey,
164163
name: r.name,
165164
description: r.description || undefined,
166-
url: convertSyntax(r.url),
165+
url: r.url,
167166
service,
168167
method,
169168
message: r.body?.text ?? '',
@@ -197,7 +196,7 @@ function importWebsocketRequest(
197196
sortPriority: sortKey,
198197
name: r.name,
199198
description: r.description || undefined,
200-
url: convertSyntax(r.url),
199+
url: r.url,
201200
message: r.body?.text ?? '',
202201
...importHeaders(r),
203202
...importAuthentication(r),
@@ -221,13 +220,13 @@ function importAuthentication(obj: any) {
221220
if (obj.authentication?.type === 'bearer') {
222221
authenticationType = 'bearer';
223222
authentication = {
224-
token: convertSyntax(obj.authentication.token),
223+
token: obj.authentication.token,
225224
};
226225
} else if (obj.authentication?.type === 'basic') {
227226
authenticationType = 'basic';
228227
authentication = {
229-
username: convertSyntax(obj.authentication.username),
230-
password: convertSyntax(obj.authentication.password),
228+
username: obj.authentication.username,
229+
password: obj.authentication.password,
231230
};
232231
}
233232

plugins/importer-insomnia/tests/fixtures/version-5.input.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ collection:
4646
name: X-Header
4747
value: xxxx
4848
disabled: false
49+
- id: pair_ab4b870278e943cba6babf5a73e213e3
50+
name: "{{ _.ApiHeaderName }}"
51+
value: "{{ _.ApiKey }}"
52+
disabled: false
4953
authentication:
5054
type: basic
5155
useISO88591: false

plugins/importer-insomnia/tests/fixtures/version-5.output.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127
"enabled": true,
128128
"name": "X-Header",
129129
"value": "xxxx"
130+
},
131+
{
132+
"enabled": true,
133+
"name": "${[ApiHeaderName ]}",
134+
"value": "${[ApiKey ]}"
130135
}
131136
],
132137
"id": "GENERATE_ID::req_d72fff2a6b104b91a2ebe9de9edd2785",

0 commit comments

Comments
 (0)