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) {
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;
}
}