postman-to-bruno converter package fixes

This commit is contained in:
lohxt1 2025-04-08 19:57:27 +05:30 committed by Anoop M D
parent 44cef9999c
commit 9bb9a914ac
3 changed files with 200 additions and 176 deletions

View File

@ -19,7 +19,7 @@ const importCollection = () => {
fileDialog({ accept: 'application/json' })
.then(readFile)
.then((collection) => postmanToBruno(collection))
.then(({ collection }) => resolve({ collection }))
.then((collection) => resolve({ collection }))
.catch((err) => {
console.log(err);
reject(new BrunoError('Import collection failed'));

View File

@ -91,10 +91,14 @@ const importScriptsFromEvents = (events, requestObject) => {
requestObject.script = {};
}
if (Array.isArray(event.script.exec) && event.script.exec.length > 0) {
requestObject.script.req = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
if (Array.isArray(event.script.exec)) {
if (event.script.exec.length > 0) {
requestObject.script.req = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
} else {
requestObject.script.req = '';
}
} else if (typeof event.script.exec === 'string') {
requestObject.script.req = postmanTranslation(event.script.exec);
} else {
@ -107,12 +111,16 @@ const importScriptsFromEvents = (events, requestObject) => {
requestObject.tests = {};
}
if (Array.isArray(event.script.exec) && event.script.exec.length > 0) {
requestObject.tests = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
if (Array.isArray(event.script.exec)) {
if (event.script.exec.length > 0) {
requestObject.tests = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
} else {
requestObject.tests = '';
}
} else if (typeof event.script.exec === 'string') {
return postmanTranslation(event.script.exec);
requestObject.tests = postmanTranslation(event.script.exec);
} else {
console.warn('Unexpected event.script.exec type', typeof event.script.exec);
}
@ -225,7 +233,7 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth) => {
formUrlEncoded: [],
multipartForm: []
},
docs: i.request.description
docs: i.request.description || ''
}
};
@ -235,10 +243,14 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth) => {
if (!brunoRequestItem.request.script) {
brunoRequestItem.request.script = {};
}
if (Array.isArray(event.script.exec) && event.script.exec.length > 0) {
brunoRequestItem.request.script.req = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
if (Array.isArray(event.script.exec)) {
if (event.script.exec.length > 0) {
brunoRequestItem.request.script.req = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
} else {
brunoRequestItem.request.script.req = '';
}
} else if (typeof event.script.exec === 'string') {
brunoRequestItem.request.script.req = postmanTranslation(event.script.exec);
} else {
@ -249,12 +261,16 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth) => {
if (!brunoRequestItem.request.tests) {
brunoRequestItem.request.tests = {};
}
if (Array.isArray(event.script.exec) && event.script.exec.length > 0) {
brunoRequestItem.request.tests = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
if (Array.isArray(event.script.exec)) {
if (event.script.exec.length > 0) {
brunoRequestItem.request.tests = event.script.exec
.map((line) => postmanTranslation(line))
.join('\n');
} else {
brunoRequestItem.request.tests = '';
}
} else if (typeof event.script.exec === 'string') {
return postmanTranslation(event.script.exec);
brunoRequestItem.request.tests = postmanTranslation(event.script.exec);
} else {
console.warn('Unexpected event.script.exec type', typeof event.script.exec);
}
@ -558,7 +574,7 @@ const postmanToBruno = (postmanCollection) => {
const transformedCollection = transformItemsInCollection(parsedPostmanCollection);
const hydratedCollection = hydrateSeqInCollection(transformedCollection);
const validatedCollection = validateSchema(hydratedCollection);
return ({ collection: validatedCollection });
return validatedCollection;
} catch(err) {
console.log(err);
throw new Error('Import collection failed');

View File

@ -568,159 +568,167 @@ const postmanCollection = {
};
const expectedOutput = {
"collection": {
"name": "CRUD",
"uid": "mockeduuidvalue123456",
"version": "1",
"items": [
{
"uid": "mockeduuidvalue123456",
"name": "GET",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/",
"method": "GET",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "none",
"json": null,
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
}
}
},
{
"uid": "mockeduuidvalue123456",
"name": "POST",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/",
"method": "POST",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "json",
"json": "{\"id\": 1, \"title\": \"first\", \"content\": \"some text\", \"createdAt\": \"some date\", \"updatedAt\": \"some date\"}",
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
},
"script": {
"req": ""
},
"tests": ""
}
},
{
"uid": "mockeduuidvalue123456",
"name": "POST_1",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/",
"method": "POST",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "json",
"json": "{\"id\": 2, \"title\": \"second\", \"content\": \"some text\", \"createdAt\": \"some date\", \"updatedAt\": \"some date\"}",
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
}
}
},
{
"uid": "mockeduuidvalue123456",
"name": "PUT",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/1",
"method": "PUT",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "json",
"json": "{\"id\": 1, \"title\": \"first changed\", \"content\": \"new text\", \"createdAt\": \"some date\", \"updatedAt\": \"some date\"}",
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
}
}
},
{
"uid": "mockeduuidvalue123456",
"name": "DELETE",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/2",
"method": "DELETE",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "none",
"json": null,
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
}
}
}
],
"environments": [],
"root": {
"docs": "",
"meta": {
"name": "CRUD"
},
"request": {
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"script": {},
"tests": "",
"vars": {}
}
}
}
"name": "CRUD",
"uid": "mockeduuidvalue123456",
"version": "1",
"items": [
{
"uid": "mockeduuidvalue123456",
"name": "GET",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/",
"method": "GET",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "none",
"json": null,
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
},
"docs": ""
},
"seq": 1
},
{
"uid": "mockeduuidvalue123456",
"name": "POST",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/",
"method": "POST",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "json",
"json": "{\"id\": 1, \"title\": \"first\", \"content\": \"some text\", \"createdAt\": \"some date\", \"updatedAt\": \"some date\"}",
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
},
"docs": "",
"script": {
"req": ""
},
"tests": ""
},
"seq": 2
},
{
"uid": "mockeduuidvalue123456",
"name": "POST_1",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/",
"method": "POST",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "json",
"json": "{\"id\": 2, \"title\": \"second\", \"content\": \"some text\", \"createdAt\": \"some date\", \"updatedAt\": \"some date\"}",
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
},
"docs": ""
},
"seq": 3
},
{
"uid": "mockeduuidvalue123456",
"name": "PUT",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/1",
"method": "PUT",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "json",
"json": "{\"id\": 1, \"title\": \"first changed\", \"content\": \"new text\", \"createdAt\": \"some date\", \"updatedAt\": \"some date\"}",
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
},
"docs": ""
},
"seq": 4
},
{
"uid": "mockeduuidvalue123456",
"name": "DELETE",
"type": "http-request",
"request": {
"url": "https://node-task2.herokuapp.com/api/notes/2",
"method": "DELETE",
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"params": [],
"body": {
"mode": "none",
"json": null,
"text": null,
"xml": null,
"formUrlEncoded": [],
"multipartForm": []
},
"docs": ""
},
"seq": 5
}
],
"environments": [],
"root": {
"docs": "",
"meta": {
"name": "CRUD"
},
"request": {
"auth": {
"mode": "none",
"basic": null,
"bearer": null,
"awsv4": null
},
"headers": [],
"script": {},
"tests": "",
"vars": {}
}
}
};