diff --git a/plugins/importer-insomnia/src/common.ts b/plugins/importer-insomnia/src/common.ts index 262a1d64..c82198dc 100644 --- a/plugins/importer-insomnia/src/common.ts +++ b/plugins/importer-insomnia/src/common.ts @@ -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) { return Object.prototype.toString.call(obj) === '[object Object]'; } @@ -32,3 +26,18 @@ export function deleteUndefinedAttrs(obj: T): T { return obj; } } + +/** Recursively render all nested object properties */ +export function convertTemplateSyntax(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; + } +} diff --git a/plugins/importer-insomnia/src/v4.ts b/plugins/importer-insomnia/src/v4.ts index 0062efb2..b3189b44 100644 --- a/plugins/importer-insomnia/src/v4.ts +++ b/plugins/importer-insomnia/src/v4.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import type { PartialImportResources } from '@yaakapp/api'; -import { convertId, convertSyntax, isJSObject } from './common'; +import { convertId, convertTemplateSyntax, isJSObject } from './common'; export function convertInsomniaV4(parsed: any) { if (!Array.isArray(parsed.resources)) return null; @@ -60,7 +60,7 @@ export function convertInsomniaV4(parsed: any) { resources.environments = resources.environments.filter(Boolean); resources.workspaces = resources.workspaces.filter(Boolean); - return { resources }; + return { resources: convertTemplateSyntax(resources) }; } 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') { bodyType = 'graphql'; - body = { text: convertSyntax(r.body.text ?? '') }; + body = { text: r.body.text ?? '' }; } else if (r.body?.mimeType === 'application/json') { bodyType = 'application/json'; - body = { text: convertSyntax(r.body.text ?? '') }; + body = { text: r.body.text ?? '' }; } let authenticationType: string | null = null; @@ -101,13 +101,13 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[ if (r.authentication.type === 'bearer') { authenticationType = 'bearer'; authentication = { - token: convertSyntax(r.authentication.token), + token: r.authentication.token, }; } else if (r.authentication.type === 'basic') { authenticationType = 'basic'; authentication = { - username: convertSyntax(r.authentication.username), - password: convertSyntax(r.authentication.password), + username: r.authentication.username, + password: r.authentication.password, }; } @@ -121,13 +121,12 @@ function importHttpRequest(r: any, workspaceId: string): PartialImportResources[ sortPriority: r.metaSortKey, name: r.name, description: r.description || undefined, - url: convertSyntax(r.url), - urlParameters: (r.parameters ?? []) - .map((p: any) => ({ - enabled: !p.disabled, - name: p.name ?? '', - value: p.value ?? '', - })), + url: r.url, + urlParameters: (r.parameters ?? []).map((p: any) => ({ + enabled: !p.disabled, + name: p.name ?? '', + value: p.value ?? '', + })), body, bodyType, authentication, @@ -158,7 +157,7 @@ function importGrpcRequest(r: any, workspaceId: string): PartialImportResources[ sortPriority: r.metaSortKey, name: r.name, description: r.description || undefined, - url: convertSyntax(r.url), + url: r.url, service, method, message: r.body?.text ?? '', diff --git a/plugins/importer-insomnia/src/v5.ts b/plugins/importer-insomnia/src/v5.ts index 2209351f..6ac31621 100644 --- a/plugins/importer-insomnia/src/v5.ts +++ b/plugins/importer-insomnia/src/v5.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import type { PartialImportResources } from '@yaakapp/api'; -import { convertId, convertSyntax, isJSObject } from './common'; +import { convertId, convertTemplateSyntax, isJSObject } from './common'; export function convertInsomniaV5(parsed: any) { // Assert parsed is object @@ -69,7 +69,7 @@ export function convertInsomniaV5(parsed: any) { resources.environments = resources.environments.filter(Boolean); resources.workspaces = resources.workspaces.filter(Boolean); - return { resources }; + return { resources: convertTemplateSyntax(resources) }; } function importHttpRequest( @@ -108,10 +108,10 @@ function importHttpRequest( }; } else if (r.body?.mimeType === 'application/graphql') { bodyType = 'graphql'; - body = { text: convertSyntax(r.body.text ?? '') }; + body = { text: r.body.text ?? '' }; } else if (r.body?.mimeType === 'application/json') { bodyType = 'application/json'; - body = { text: convertSyntax(r.body.text ?? '') }; + body = { text: r.body.text ?? '' }; } return { @@ -124,13 +124,12 @@ function importHttpRequest( model: 'http_request', name: r.name, description: r.meta?.description || undefined, - url: convertSyntax(r.url), - urlParameters: (r.parameters ?? []) - .map((p: any) => ({ - enabled: !p.disabled, - name: p.name ?? '', - value: p.value ?? '', - })), + url: r.url, + urlParameters: (r.parameters ?? []).map((p: any) => ({ + enabled: !p.disabled, + name: p.name ?? '', + value: p.value ?? '', + })), body, bodyType, method: r.method, @@ -163,7 +162,7 @@ function importGrpcRequest( sortPriority: sortKey, name: r.name, description: r.description || undefined, - url: convertSyntax(r.url), + url: r.url, service, method, message: r.body?.text ?? '', @@ -197,7 +196,7 @@ function importWebsocketRequest( sortPriority: sortKey, name: r.name, description: r.description || undefined, - url: convertSyntax(r.url), + url: r.url, message: r.body?.text ?? '', ...importHeaders(r), ...importAuthentication(r), @@ -221,13 +220,13 @@ function importAuthentication(obj: any) { if (obj.authentication?.type === 'bearer') { authenticationType = 'bearer'; authentication = { - token: convertSyntax(obj.authentication.token), + token: obj.authentication.token, }; } else if (obj.authentication?.type === 'basic') { authenticationType = 'basic'; authentication = { - username: convertSyntax(obj.authentication.username), - password: convertSyntax(obj.authentication.password), + username: obj.authentication.username, + password: obj.authentication.password, }; } diff --git a/plugins/importer-insomnia/tests/fixtures/version-5.input.yaml b/plugins/importer-insomnia/tests/fixtures/version-5.input.yaml index 6bbd81f4..a55abd67 100644 --- a/plugins/importer-insomnia/tests/fixtures/version-5.input.yaml +++ b/plugins/importer-insomnia/tests/fixtures/version-5.input.yaml @@ -46,6 +46,10 @@ collection: name: X-Header value: xxxx disabled: false + - id: pair_ab4b870278e943cba6babf5a73e213e3 + name: "{{ _.ApiHeaderName }}" + value: "{{ _.ApiKey }}" + disabled: false authentication: type: basic useISO88591: false diff --git a/plugins/importer-insomnia/tests/fixtures/version-5.output.json b/plugins/importer-insomnia/tests/fixtures/version-5.output.json index 0c577ca4..e61f3424 100644 --- a/plugins/importer-insomnia/tests/fixtures/version-5.output.json +++ b/plugins/importer-insomnia/tests/fixtures/version-5.output.json @@ -127,6 +127,11 @@ "enabled": true, "name": "X-Header", "value": "xxxx" + }, + { + "enabled": true, + "name": "${[ApiHeaderName ]}", + "value": "${[ApiKey ]}" } ], "id": "GENERATE_ID::req_d72fff2a6b104b91a2ebe9de9edd2785",