Convert Insomnia variables syntax in headers, parameters and form data (#291)

Co-authored-by: Gregory Schier <gschier1990@gmail.com>
This commit is contained in:
Jeroen Van den Berghe
2025-11-11 02:24:30 +01:00
committed by GitHub
parent 0d725b59bd
commit 2e9f21f838
5 changed files with 53 additions and 37 deletions

View File

@@ -1,9 +1,3 @@
export function convertSyntax(variable: string): string {
if (!isJSString(variable)) return variable;
return variable.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}');
}
export function isJSObject(obj: unknown) { export function isJSObject(obj: unknown) {
return Object.prototype.toString.call(obj) === '[object Object]'; return Object.prototype.toString.call(obj) === '[object Object]';
} }
@@ -32,3 +26,18 @@ export function deleteUndefinedAttrs<T>(obj: T): T {
return obj; return obj;
} }
} }
/** Recursively render all nested object properties */
export function convertTemplateSyntax<T>(obj: T): T {
if (typeof obj === 'string') {
return obj.replaceAll(/{{\s*(_\.)?([^}]+)\s*}}/g, '${[$2]}') as T;
} else if (Array.isArray(obj) && obj != null) {
return obj.map(convertTemplateSyntax) as T;
} else if (typeof obj === 'object' && obj != null) {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, convertTemplateSyntax(v)]),
) as T;
} else {
return obj;
}
}

View File

@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import type { PartialImportResources } from '@yaakapp/api'; import type { PartialImportResources } from '@yaakapp/api';
import { convertId, convertSyntax, isJSObject } from './common'; import { convertId, convertTemplateSyntax, isJSObject } from './common';
export function convertInsomniaV4(parsed: any) { export function convertInsomniaV4(parsed: any) {
if (!Array.isArray(parsed.resources)) return null; if (!Array.isArray(parsed.resources)) return null;
@@ -60,7 +60,7 @@ export function convertInsomniaV4(parsed: any) {
resources.environments = resources.environments.filter(Boolean); resources.environments = resources.environments.filter(Boolean);
resources.workspaces = resources.workspaces.filter(Boolean); resources.workspaces = resources.workspaces.filter(Boolean);
return { resources }; return { resources: convertTemplateSyntax(resources) };
} }
function importHttpRequest(r: any, workspaceId: string): PartialImportResources['httpRequests'][0] { function importHttpRequest(r: any, workspaceId: string): PartialImportResources['httpRequests'][0] {
@@ -90,10 +90,10 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[
}; };
} else if (r.body?.mimeType === 'application/graphql') { } else if (r.body?.mimeType === 'application/graphql') {
bodyType = 'graphql'; bodyType = 'graphql';
body = { text: convertSyntax(r.body.text ?? '') }; body = { text: r.body.text ?? '' };
} else if (r.body?.mimeType === 'application/json') { } else if (r.body?.mimeType === 'application/json') {
bodyType = 'application/json'; bodyType = 'application/json';
body = { text: convertSyntax(r.body.text ?? '') }; body = { text: r.body.text ?? '' };
} }
let authenticationType: string | null = null; let authenticationType: string | null = null;
@@ -101,13 +101,13 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[
if (r.authentication.type === 'bearer') { if (r.authentication.type === 'bearer') {
authenticationType = 'bearer'; authenticationType = 'bearer';
authentication = { authentication = {
token: convertSyntax(r.authentication.token), token: r.authentication.token,
}; };
} else if (r.authentication.type === 'basic') { } else if (r.authentication.type === 'basic') {
authenticationType = 'basic'; authenticationType = 'basic';
authentication = { authentication = {
username: convertSyntax(r.authentication.username), username: r.authentication.username,
password: convertSyntax(r.authentication.password), password: r.authentication.password,
}; };
} }
@@ -121,13 +121,12 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[
sortPriority: r.metaSortKey, sortPriority: r.metaSortKey,
name: r.name, name: r.name,
description: r.description || undefined, description: r.description || undefined,
url: convertSyntax(r.url), url: r.url,
urlParameters: (r.parameters ?? []) urlParameters: (r.parameters ?? []).map((p: any) => ({
.map((p: any) => ({ enabled: !p.disabled,
enabled: !p.disabled, name: p.name ?? '',
name: p.name ?? '', value: p.value ?? '',
value: p.value ?? '', })),
})),
body, body,
bodyType, bodyType,
authentication, authentication,
@@ -158,7 +157,7 @@ function importGrpcRequest(r: any, workspaceId: string): PartialImportResources[
sortPriority: r.metaSortKey, sortPriority: r.metaSortKey,
name: r.name, name: r.name,
description: r.description || undefined, description: r.description || undefined,
url: convertSyntax(r.url), url: r.url,
service, service,
method, method,
message: r.body?.text ?? '', message: r.body?.text ?? '',

View File

@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import type { PartialImportResources } from '@yaakapp/api'; import type { PartialImportResources } from '@yaakapp/api';
import { convertId, convertSyntax, isJSObject } from './common'; import { convertId, convertTemplateSyntax, isJSObject } from './common';
export function convertInsomniaV5(parsed: any) { export function convertInsomniaV5(parsed: any) {
// Assert parsed is object // Assert parsed is object
@@ -69,7 +69,7 @@ export function convertInsomniaV5(parsed: any) {
resources.environments = resources.environments.filter(Boolean); resources.environments = resources.environments.filter(Boolean);
resources.workspaces = resources.workspaces.filter(Boolean); resources.workspaces = resources.workspaces.filter(Boolean);
return { resources }; return { resources: convertTemplateSyntax(resources) };
} }
function importHttpRequest( function importHttpRequest(
@@ -108,10 +108,10 @@ function importHttpRequest(
}; };
} else if (r.body?.mimeType === 'application/graphql') { } else if (r.body?.mimeType === 'application/graphql') {
bodyType = 'graphql'; bodyType = 'graphql';
body = { text: convertSyntax(r.body.text ?? '') }; body = { text: r.body.text ?? '' };
} else if (r.body?.mimeType === 'application/json') { } else if (r.body?.mimeType === 'application/json') {
bodyType = 'application/json'; bodyType = 'application/json';
body = { text: convertSyntax(r.body.text ?? '') }; body = { text: r.body.text ?? '' };
} }
return { return {
@@ -124,13 +124,12 @@ function importHttpRequest(
model: 'http_request', model: 'http_request',
name: r.name, name: r.name,
description: r.meta?.description || undefined, description: r.meta?.description || undefined,
url: convertSyntax(r.url), url: r.url,
urlParameters: (r.parameters ?? []) urlParameters: (r.parameters ?? []).map((p: any) => ({
.map((p: any) => ({ enabled: !p.disabled,
enabled: !p.disabled, name: p.name ?? '',
name: p.name ?? '', value: p.value ?? '',
value: p.value ?? '', })),
})),
body, body,
bodyType, bodyType,
method: r.method, method: r.method,
@@ -163,7 +162,7 @@ function importGrpcRequest(
sortPriority: sortKey, sortPriority: sortKey,
name: r.name, name: r.name,
description: r.description || undefined, description: r.description || undefined,
url: convertSyntax(r.url), url: r.url,
service, service,
method, method,
message: r.body?.text ?? '', message: r.body?.text ?? '',
@@ -197,7 +196,7 @@ function importWebsocketRequest(
sortPriority: sortKey, sortPriority: sortKey,
name: r.name, name: r.name,
description: r.description || undefined, description: r.description || undefined,
url: convertSyntax(r.url), url: r.url,
message: r.body?.text ?? '', message: r.body?.text ?? '',
...importHeaders(r), ...importHeaders(r),
...importAuthentication(r), ...importAuthentication(r),
@@ -221,13 +220,13 @@ function importAuthentication(obj: any) {
if (obj.authentication?.type === 'bearer') { if (obj.authentication?.type === 'bearer') {
authenticationType = 'bearer'; authenticationType = 'bearer';
authentication = { authentication = {
token: convertSyntax(obj.authentication.token), token: obj.authentication.token,
}; };
} else if (obj.authentication?.type === 'basic') { } else if (obj.authentication?.type === 'basic') {
authenticationType = 'basic'; authenticationType = 'basic';
authentication = { authentication = {
username: convertSyntax(obj.authentication.username), username: obj.authentication.username,
password: convertSyntax(obj.authentication.password), password: obj.authentication.password,
}; };
} }

View File

@@ -46,6 +46,10 @@ collection:
name: X-Header name: X-Header
value: xxxx value: xxxx
disabled: false disabled: false
- id: pair_ab4b870278e943cba6babf5a73e213e3
name: "{{ _.ApiHeaderName }}"
value: "{{ _.ApiKey }}"
disabled: false
authentication: authentication:
type: basic type: basic
useISO88591: false useISO88591: false

View File

@@ -127,6 +127,11 @@
"enabled": true, "enabled": true,
"name": "X-Header", "name": "X-Header",
"value": "xxxx" "value": "xxxx"
},
{
"enabled": true,
"name": "${[ApiHeaderName ]}",
"value": "${[ApiKey ]}"
} }
], ],
"id": "GENERATE_ID::req_d72fff2a6b104b91a2ebe9de9edd2785", "id": "GENERATE_ID::req_d72fff2a6b104b91a2ebe9de9edd2785",