Skip to content

Commit

Permalink
normalise json path
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Jun 21, 2024
1 parent 6c35b16 commit 6b7f12a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ export const convert: Converter<Data> = 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,
};
}, {}),
},
Expand Down
9 changes: 6 additions & 3 deletions packages/insomnia/src/utils/importers/importers/postman.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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}}}`;
});
Expand Down

0 comments on commit 6b7f12a

Please sign in to comment.