Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform hyphens to underscores on import #7564

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5840,14 +5840,21 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = `
{
"_id": "__GRP_1__",
"_type": "request_group",
"afterResponseScript": "",
"afterResponseScript": "console.log('after')",
"authentication": {},
"description": "",
"environment": {},
"environment": {
"bob": "something",
"dssx": "{{env_var_in_global}}",
},
"metaSortKey": -1622117984000,
"name": "New Collection",
"parentId": "__WORKSPACE_ID__",
"preRequestScript": "",
"preRequestScript": "console.log('pre')",
"variable": {
"bob": "something",
"dssx": "{{env_var_in_global}}",
},
},
{
"_id": "__REQ_1__",
Expand Down Expand Up @@ -5900,7 +5907,7 @@ exports[`Fixtures Import postman-env no-name-input.json 1`] = `
"_id": "__ENV_1__",
"_type": "environment",
"data": {
"foo": "production",
"foo_and_bar": "production-env",
},
"name": "Postman Environment",
"parentId": "__BASE_ENVIRONMENT_ID__",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"id": "fd5ae82a-d4c3-fa38-b53f-cddafb084535",
"values": [
{
"key": "foo",
"value": "production",
"key": "foo-and-bar",
"value": "production-env",
"type": "text",
"enabled": true
},
{
"key": "bar",
"value": "hahah",
"key": "disabled-key",
"value": "hahah-haha",
"type": "text",
"enabled": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,37 @@
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
"console.log('pre')"
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
"console.log('after')"
]
}
}
],
"variable": [
{
"key": "dssx",
"value": "{{env-var-in-global}}"
},
{
"key": "bob",
"value": "something"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ 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,
[key]: value,
[transformedString]: value,
};
}, {}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ describe('postman', () => {
})) as HttpsSchemaGetpostmanComJsonCollectionV210;

describe('transformPostmanToNunjucksString', () => {
it('should transform a postman request string to an insomnia request string', () => {
it('should transform to nunjucks syntax', () => {
const input = '{{$guid}}abc{{$randomStreetAddress}}def{{$guid}}';
const output = '{% faker \'guid\' %}abc{% faker \'randomStreetAddress\' %}def{% faker \'guid\' %}';
expect(transformPostmanToNunjucksString(input)).toEqual(output);
expect(transformPostmanToNunjucksString()).toEqual('');
});
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';
expect(transformPostmanToNunjucksString(input)).toEqual(output);
expect(transformPostmanToNunjucksString()).toEqual('');
});
});

describe('headers', () => {
Expand Down
33 changes: 23 additions & 10 deletions packages/insomnia/src/utils/importers/importers/postman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Request1 as V200Request1,
Url,
UrlEncodedParameter as V200UrlEncodedParameter,
Variable2 as V200Variable2,
} from './postman-2.0.types';
import {
Auth as V210Auth,
Expand All @@ -26,7 +25,6 @@ import {
QueryParam,
Request1 as V210Request1,
UrlEncodedParameter as V210UrlEncodedParameter,
Variable2 as V210Variable2,
} from './postman-2.1.types';

export const id = 'postman';
Expand All @@ -35,7 +33,6 @@ export const description = 'Importer for Postman collections';

type PostmanCollection = V200Schema | V210Schema;
type EventList = V200EventList | V210EventList;
type Variable = V200Variable2 | V210Variable2;

type Authetication = V200Auth | V210Auth;

Expand Down Expand Up @@ -64,10 +61,26 @@ export const transformPostmanToNunjucksString = (inputString?: string | null) =>
if (!inputString) {
return '';
}

if (typeof inputString !== 'string') {
return inputString;
}
const sanitizedString = replaceHyphens(inputString);
return postmanTagRegexs.reduce((transformedString, { tag, regex }) => {
return transformedString.replace(regex, postmanToNunjucksLookup[tag]);
}, inputString);
}, sanitizedString);
};

export const replaceHyphens = (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, '_');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: replaceAll might improve the performance a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Return the replaced pattern within the curly braces
return `{{${replaced}}}`;
});
};

const POSTMAN_SCHEMA_V2_0 =
Expand Down Expand Up @@ -112,18 +125,18 @@ export class ImportPostman {
this.collection = collection;
}

importVariable = (variables: Variable[]) => {
importVariable = (variables: { [key: string]: string }[]) => {
if (variables?.length === 0) {
return null;
}

const variable: { [key: string]: Variable['value'] } = {};
const variable: { [key: string]: string } = {};
for (let i = 0; i < variables.length; i++) {
const key = variables[i].key;
const { key, value } = variables[i];
if (key === undefined) {
continue;
}
variable[key] = variables[i].value;
variable[key] = transformPostmanToNunjucksString(value);
}
return variable;
};
Expand Down Expand Up @@ -269,7 +282,7 @@ export class ImportPostman {
event,
} = this.collection;

const postmanVariable = this.importVariable(variable || []);
const postmanVariable = this.importVariable((variable as { [key: string]: string }[]) || []);
const { authentication } = this.importAuthentication(auth);
const preRequestScript = this.importPreRequestScript(event);
const afterResponseScript = this.importAfterResponseScript(event);
Expand Down
Loading