-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (67 loc) · 2.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require("fs");
function isThunderClient(jsonFile) {
if (!(jsonFile?.client === "Thunder Client")) {
console.log("Not a thunder client file");
process.exit();
}
return true;
}
function toPostmanRequest(request) {
const url = new URL(request.url);
const path = url.pathname.split("/").slice(1);
const req = {
name: request.name,
request: {
method: request.method,
header: request.headers,
url: { raw: url.href, protocol: url.protocol.replace(":", ""), host: [url.hostname], port: url.port, path },
},
response: [],
};
// yes ik this is messy
if (request.body) {
if (Object.keys(request.body).includes("raw")) {
req.request.body = { mode: "raw", raw: request.body.raw, options: { raw: { language: request.body.type } } };
} else {
req.request.body = {};
req.request.body["mode"] = request.body.type;
// lmao
Object.keys(request.body).forEach((property) => {
const check = request.body[property];
// the length check is to make sure that we dont get other objects that are just empty. the assumption is that the object with more than one item in it is the actual data we want
if (typeof check === "object" && check.length > 0) {
req.request.body[request.body.type] = request.body[property];
}
});
}
}
// if the thunder client request had form data do this. Thunder client has they key prop called "name" and in postman it is called "key"
req.request.body?.formdata?.forEach((item, i) => {
req.request.body.formdata[i].key = item.name;
});
return req;
}
function main() {
const [input, output = "out.json"] = process.argv.slice(2);
// make sure file name is passed in
if (!input) {
console.log("Please pass in a file");
process.exit();
}
const data = fs.readFileSync(input);
const jsonData = JSON.parse(data);
isThunderClient(jsonData);
const postmanCollection = {
info: { name: jsonData.collectionName, schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" },
item: [],
};
jsonData["requests"].forEach((request) => {
const thunderRequest = toPostmanRequest(request);
postmanCollection.item.push(thunderRequest);
});
// ship it!
const postmanStringifyObject = JSON.stringify(postmanCollection);
fs.writeFileSync(output, postmanStringifyObject);
console.log(`Done :) - Written to ${output}`);
}
main();