From 6b7f12a5765c9098d34938aa76d1abd7d2f36c89 Mon Sep 17 00:00:00 2001 From: jackkav Date: Fri, 21 Jun 2024 12:11:23 +0200 Subject: [PATCH] normalise json path --- .../src/utils/importers/importers/postman-env.ts | 4 +--- .../insomnia/src/utils/importers/importers/postman.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/insomnia/src/utils/importers/importers/postman-env.ts b/packages/insomnia/src/utils/importers/importers/postman-env.ts index 208026fa0bfe..5bcfab417afc 100644 --- a/packages/insomnia/src/utils/importers/importers/postman-env.ts +++ b/packages/insomnia/src/utils/importers/importers/postman-env.ts @@ -39,11 +39,9 @@ export const convert: Converter = rawData => { if (!enabled) { return accumulator; } - // hyphenated keys are not allowed in nunjucks eg. {{ foo-bar }} -> {{ foo_bar }} - const transformedString = key.replace(/-/g, '_'); return { ...accumulator, - [transformedString]: value, + [key]: value, }; }, {}), }, diff --git a/packages/insomnia/src/utils/importers/importers/postman.ts b/packages/insomnia/src/utils/importers/importers/postman.ts index ab94a45a80a3..222a62be05d5 100644 --- a/packages/insomnia/src/utils/importers/importers/postman.ts +++ b/packages/insomnia/src/utils/importers/importers/postman.ts @@ -1,4 +1,5 @@ import { AuthTypeOAuth2 } from '../../../models/request'; +import { forceBracketNotation } from '../../../templating/utils'; import { fakerFunctions } from '../../../ui/components/templating/faker-functions'; import { Converter, ImportRequest, Parameter } from '../entities'; import { @@ -64,20 +65,22 @@ export const transformPostmanToNunjucksString = (inputString?: string | null) => if (typeof inputString !== 'string') { return inputString; } - const sanitizedString = replaceHyphens(inputString); + const sanitizedString = normaliseJsonPath(inputString); return postmanTagRegexs.reduce((transformedString, { tag, regex }) => { return transformedString.replace(regex, postmanToNunjucksLookup[tag]); }, sanitizedString); }; -export const replaceHyphens = (input?: string) => { +// old: {{ arr-name-with-dash }} +// new: {{ _['arr-name-with-dash'] }} +export const normaliseJsonPath = (input?: string) => { if (!input) { return ''; } // Use a regular expression to find and replace the pattern return input.replace(/\{\{([^\}]+)\}\}/g, (_, match) => { // Replace hyphens with underscores within the match - const replaced = match.replace(/-/g, '_'); + const replaced = forceBracketNotation('_', match); // Return the replaced pattern within the curly braces return `{{${replaced}}}`; });