Forked from wesleyegberto/convert-postman-to-insomnia.js
Last active
October 16, 2023 15:44
-
-
Save paulkirow/80baababe1ab856de6be4324fbe8d42e to your computer and use it in GitHub Desktop.
Script to convert a Postman backupt to Insomnia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Script to parse a Postman backupt to Insomnia keeping the same structure. | |
| * | |
| * It parses: | |
| * - Folders | |
| * - Requests | |
| * - Environments | |
| * | |
| * Notes: Insomnia doesn't accept vars with dots, if you are using you must replace yours URLs manually (see ENVIRONMENTS_EXPORTS). | |
| */ | |
| 'use strict'; | |
| const fs = require('fs'); | |
| const postmanDump = require('./Backup.postman_dump.json'); | |
| if (!postmanDump) { | |
| throw new Error('Invalid JSON'); | |
| } | |
| if (postmanDump.version != 1) { | |
| throw new Error('Version not supported, try 1!'); | |
| } | |
| function generateId(length) { | |
| var result = ''; | |
| var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
| var charactersLength = characters.length; | |
| for (var i = 0; i < length; i++) { | |
| result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
| } | |
| return result; | |
| } | |
| String.prototype.toId = function toId() { | |
| return this.replace(/-/g, ''); | |
| }; | |
| String.prototype.toUrl = function toUrl() { | |
| return this.replace(/{{(.*?)}}/g, (match, p1) => { | |
| return '{{ _.' + p1.replace(/[-.]/g, '_') + ' }}'; | |
| }); | |
| } | |
| // always a new workspace to avoid problems | |
| const WORKDIR = 'wrk_' + generateId(20); | |
| const ENVBASE = 'env_' + generateId(20); | |
| const resources = [ | |
| { | |
| _id: WORKDIR, | |
| _type: 'workspace', | |
| name: 'Postman Dump ' + (new Date()).toISOString(), | |
| parentId: null, | |
| scope: null, | |
| }, | |
| { | |
| _id: 'spc_' + generateId(20), | |
| _type: 'api_spec', | |
| parentId: WORKDIR, | |
| fileName: 'Insomnia', | |
| contents: '', | |
| contentType: 'yaml', | |
| }, | |
| { | |
| _id: ENVBASE, | |
| _type: 'environment', | |
| parentId: WORKDIR, | |
| name: 'Base Environment', | |
| data: {}, | |
| dataPropertyOrder: {}, | |
| color: null, | |
| isPrivate: false, | |
| metaSortKey: 1597080078957, | |
| }, | |
| ]; | |
| function mapRequest(item) { | |
| const request = item.request; | |
| const parentId = item.parentId; | |
| const mapped = { | |
| _id: 'req_' + generateId(10), | |
| _type: 'request', | |
| parentId: parentId.toId(), | |
| name: item.name, | |
| description: item.description || '', | |
| url: request.url.raw.toUrl() || '', | |
| method: request.method, | |
| }; | |
| if (request.header && request.header.length) { | |
| mapped.headers = request.header.map(header => ({ | |
| id: 'pair_' + generateId(10), | |
| name: header.key, | |
| value: header.value, | |
| })); | |
| } | |
| if (request.body && request.body.mode == 'urlencoded' && request.body.urlencoded && request.body.urlencoded.length) { | |
| mapped.body = { | |
| mimeType: 'application/x-www-form-urlencoded', | |
| params: request.body.urlencoded.map(param => ({ | |
| id: 'pair_' + generateId(10), | |
| name: param.key, | |
| value: param.value | |
| })) | |
| }; | |
| } | |
| if (request.body && request.body.mode == 'raw') { | |
| mapped.body = { | |
| mimeType: 'application/json', | |
| text: request.body.raw, | |
| }; | |
| } | |
| if (request.headerData && request.headerData.length) { | |
| mapped.headers = request.headerData.map(header => ({ | |
| id: 'pair_' + generateId(10), | |
| name: header.key, | |
| value: header.value, | |
| })); | |
| } | |
| if (request.queryParams && request.queryParams.length) { | |
| mapped.parameters = request.queryParams.map(param => ({ | |
| id: 'pair_' + generateId(10), | |
| name: param.key, | |
| value: param.value, | |
| disabled: !param.enabled, | |
| })); | |
| } | |
| if (request.auth) { | |
| mapped.authentication = { | |
| type: request.auth.type | |
| }; | |
| if (request.auth.bearer && request.auth.bearer.length > 0) { | |
| mapped.authentication.token = request.auth.bearer[0].value | |
| } | |
| } | |
| if (request.dataMode == 'urlencoded' && request.data && request.data.length) { | |
| mapped.body = { | |
| mimeType: 'application/x-www-form-urlencoded', | |
| params: request.data.map(param => ({ | |
| id: 'pair_' + generateId(10), | |
| name: param.key, | |
| value: param.value | |
| })) | |
| }; | |
| } | |
| if (request.dataMode == 'raw') { | |
| mapped.body = { | |
| mimeType: 'application/json', | |
| text: request.rawModeData, | |
| }; | |
| } | |
| return mapped; | |
| } | |
| function parseFolder(collection, folders) { | |
| const id = generateId(20); | |
| const parent = { | |
| _id: 'fld_' + id, | |
| _type: 'request_group', | |
| name: collection.name, | |
| description: collection.description, | |
| parentId: collection.parentId, // Set in parseCollection/parseItem | |
| }; | |
| resources.push(parent); | |
| console.log(collection.name, '- Verifying folders'); | |
| if (folders && folders.length) { | |
| folders.forEach(folder => { | |
| console.log('Parent: ', collection.name, ':', parent._id, ' -> ', folder.name); | |
| folder.parentId = parent._id; | |
| if (folder.item) { | |
| parseFolder(folder, folder.item); | |
| } else if (folder.request) { | |
| resources.push(mapRequest(folder)); | |
| } | |
| }); | |
| } | |
| } | |
| function parseCollection(collection) { | |
| collection.parentId = WORKDIR; | |
| console.log('Collection', collection.name, '- Folders:', collection.folders.length, '- Requests:', collection.requests.length); | |
| parseFolder(collection, collection.folders); | |
| if (collection.requests && collection.requests.length) { | |
| collection.requests.forEach(request => { | |
| resources.push(mapRequest(request)); | |
| }); | |
| } | |
| } | |
| function parseItem(item) { | |
| item.parentId = WORKDIR; | |
| console.log('Item', item.name); | |
| if (item.item && item.item.length) { | |
| console.log('Item', item.name, '- Child Items:', item.item.length); | |
| parseFolder(item, item.item); | |
| } | |
| if (item.requests && item.requests.length) { | |
| item.requests.forEach(request => { | |
| resources.push(mapRequest(request)); | |
| }); | |
| } | |
| if (item.request) { | |
| resources.push(mapRequest(item)); | |
| } | |
| } | |
| function parseVariable(variable) { | |
| // Find the resource with name = "Base Environment" | |
| const resource = resources.find(resource => resource.name === "Base Environment"); | |
| resource.data[variable.key] = variable.value; | |
| if (resource.dataPropertyOrder["&"] === undefined) { | |
| resource.dataPropertyOrder["&"] = []; | |
| } | |
| resource.dataPropertyOrder["&"].push(variable.key); | |
| } | |
| console.log('Starting parsing'); | |
| if (postmanDump && postmanDump.collections) { | |
| console.log('Parsing collections'); | |
| postmanDump.collections.forEach(collection => parseCollection(collection)); | |
| } | |
| if (postmanDump && postmanDump.item) { | |
| console.log('Parsing items'); | |
| postmanDump.item.forEach(item => parseItem(item)); | |
| } | |
| if (postmanDump && postmanDump.variable) { | |
| console.log('Parsing variables'); | |
| postmanDump.variable.forEach(variable => parseVariable(variable)); | |
| } | |
| // ENVIRONMENTS_EXPORTS | |
| if (postmanDump.environments && postmanDump.environments.length) { | |
| console.log('Parsing environments'); | |
| postmanDump.environments.forEach(env => { | |
| console.log('Adding environment:', env.name); | |
| const mapped = { | |
| _id: 'env_' + env.id.toId(), | |
| _type: 'environment', | |
| parentId: ENVBASE, | |
| name: env.name, | |
| data: {} | |
| }; | |
| if (env.values && env.values.length) { | |
| env.values.forEach(item => { | |
| const key = item.key.replace(/[-.]/g, '_'); | |
| mapped.data[key] = item.value; | |
| }); | |
| } | |
| resources.push(mapped); | |
| }); | |
| } | |
| console.log('Finished parsing, exporting JSON'); | |
| const data = JSON.stringify({ | |
| _type: 'export', | |
| __export_format: 4, | |
| resources: resources, | |
| }); | |
| fs.writeFileSync('insomnia-converted-from-postman.json', data); | |
| console.log('Exported finished'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment