mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-11 22:40:26 +01:00
Convert Insomnia variables syntax in headers, parameters and form data (#291)
Co-authored-by: Gregory Schier <gschier1990@gmail.com>
This commit is contained in:
committed by
GitHub
parent
0d725b59bd
commit
2e9f21f838
@@ -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<T>(obj: T): T {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ?? '',
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -127,6 +127,11 @@
|
||||
"enabled": true,
|
||||
"name": "X-Header",
|
||||
"value": "xxxx"
|
||||
},
|
||||
{
|
||||
"enabled": true,
|
||||
"name": "${[ApiHeaderName ]}",
|
||||
"value": "${[ApiKey ]}"
|
||||
}
|
||||
],
|
||||
"id": "GENERATE_ID::req_d72fff2a6b104b91a2ebe9de9edd2785",
|
||||
|
||||
Reference in New Issue
Block a user