Created
July 6, 2023 19:04
-
-
Save doesdev/5e1f1d477333619b6b21d057ffdd94ef to your computer and use it in GitHub Desktop.
Convert Talend API Tester JSON to Postman Collection(s) 2.1.0
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
const fs = require('fs') | |
const path = require('path') | |
const schema = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json' | |
const remappers = { | |
'somesecurethinglikeapasswordoranauthtoken': '{{VARIABLE}}' | |
} | |
const processHeaders = (headers = []) => { | |
const authHeader = headers.find((h) => h.name.toLowerCase() === 'authorization') | |
const remaining = headers.filter((h) => h !== authHeader) | |
const auth = authHeader && authHeader.enabled && { | |
type: 'bearer', | |
bearer: [{ | |
key: 'token', | |
value: authHeader.value.replace(/^bearer /i, ''), | |
type: 'string' | |
}] | |
} | |
const headersOut = remaining.map(({ enabled, name: key, value }) => { | |
return { key, value, disabled: !enabled } | |
}) | |
return { auth: auth || null, headers: headersOut } | |
} | |
const toPostmanRequest = (request) => { | |
const { headers: header, auth } = processHeaders(request.headers) | |
return { | |
name: request.name, | |
request: { | |
method: request.method.name, | |
header, | |
auth, | |
body: { | |
mode: request.body.bodyType === 'Text' ? 'raw' : 'formdata', | |
raw: request.body.textBody | |
}, | |
url: `${request.uri.scheme.name}://${request.uri.host}${request.uri.path}` | |
} | |
} | |
} | |
const processEntity = ({ entity, children = [] }) => { | |
if (entity.type === 'Request') return toPostmanRequest(entity) | |
if (!children.length) return null | |
const item = children.map((child) => processEntity(child)) || [] | |
return { name: entity.name, item } | |
} | |
const convertToPostman = (inputFilePath) => { | |
const { entities } = JSON.parse(fs.readFileSync(inputFilePath)) | |
const projects = entities.filter(({ entity }) => entity.type === 'Project') | |
for (const { entity: { name }, children = [] } of projects) { | |
const outputData = { | |
info: { name, schema }, | |
item: children.map((entity) => processEntity(entity)).filter((el) => el) | |
} | |
const jsonOutRaw = JSON.stringify(outputData, null, 2) | |
const jsonOut = Object.entries(remappers).reduce((out, [init, replace]) => { | |
return out.replaceAll(init, replace) | |
}, jsonOutRaw) | |
const outDir = path.join(path.dirname(inputFilePath), 'output') | |
const outFile = path.join(outDir, `postman-${name}.json`) | |
fs.writeFileSync(outFile, jsonOut) | |
} | |
} | |
const inputFilePath = process.argv[2] | |
convertToPostman(inputFilePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment