mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
parent
3fec8698d1
commit
c7385f00be
@ -5845,7 +5845,7 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = `
|
||||
"description": "",
|
||||
"environment": {
|
||||
"bob": "something",
|
||||
"dssx": "{{env_var_in_global}}",
|
||||
"dssx": "{{_['env-var-in-global']}}",
|
||||
},
|
||||
"metaSortKey": -1622117984000,
|
||||
"name": "New Collection",
|
||||
@ -5853,7 +5853,7 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = `
|
||||
"preRequestScript": "console.log('pre')",
|
||||
"variable": {
|
||||
"bob": "something",
|
||||
"dssx": "{{env_var_in_global}}",
|
||||
"dssx": "{{_['env-var-in-global']}}",
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -5907,7 +5907,7 @@ exports[`Fixtures Import postman-env no-name-input.json 1`] = `
|
||||
"_id": "__ENV_1__",
|
||||
"_type": "environment",
|
||||
"data": {
|
||||
"foo_and_bar": "production-env",
|
||||
"foo-and-bar": "production-env",
|
||||
},
|
||||
"name": "Postman Environment",
|
||||
"parentId": "__BASE_ENVIRONMENT_ID__",
|
||||
|
@ -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,
|
||||
};
|
||||
}, {}),
|
||||
},
|
||||
|
@ -41,7 +41,7 @@ describe('postman', () => {
|
||||
});
|
||||
it('should transform hyphens to underscores', () => {
|
||||
const input = 'abc{{my-env-var}}def{{here-and-here}}ghi';
|
||||
const output = 'abc{{my_env_var}}def{{here_and_here}}ghi';
|
||||
const output = "abc{{_['my-env-var']}}def{{_['here-and-here']}}ghi";
|
||||
expect(transformPostmanToNunjucksString(input)).toEqual(output);
|
||||
expect(transformPostmanToNunjucksString()).toEqual('');
|
||||
});
|
||||
|
@ -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,25 @@ export const transformPostmanToNunjucksString = (inputString?: string | null) =>
|
||||
if (typeof inputString !== 'string') {
|
||||
return inputString;
|
||||
}
|
||||
const sanitizedString = replaceHyphens(inputString);
|
||||
return postmanTagRegexs.reduce((transformedString, { tag, regex }) => {
|
||||
const replaceFaker = postmanTagRegexs.reduce((transformedString, { tag, regex }) => {
|
||||
return transformedString.replace(regex, postmanToNunjucksLookup[tag]);
|
||||
}, sanitizedString);
|
||||
}, inputString);
|
||||
return normaliseJsonPath(replaceFaker);
|
||||
};
|
||||
|
||||
export const replaceHyphens = (input?: string) => {
|
||||
// old: {{ arr-name-with-dash }}
|
||||
// new: {{ _['arr-name-with-dash'] }}
|
||||
export const normaliseJsonPath = (input?: string) => {
|
||||
if (!input) {
|
||||
return '';
|
||||
}
|
||||
if (!input.includes('-')) {
|
||||
return input;
|
||||
}
|
||||
// Use a regular expression to find and replace the pattern
|
||||
return input.replace(/\{\{([^\}]+)\}\}/g, (_, match) => {
|
||||
return input.replace(/{{\s*([^ }]+)\s*[^}]*\s*}}/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}}}`;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user