-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
124 lines (99 loc) · 3.78 KB
/
Copy pathparse.js
File metadata and controls
124 lines (99 loc) · 3.78 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const POSTMAN_FILE_EXTENSION = '.postman_collection.json';
function createVariableObject(varArray) {
let obj = {};
for (let o in varArray) {
const { key, value } = varArray[o];
obj[key] = value;
}
return obj;
}
function replaceVariables(inputString, data) {
const regex = /\{\{(\w+)\}\}/g;
return inputString.replace(regex, (_, variableName) => {
return data[variableName] || ""; // Replace with an empty string if the variable is not found
});
}
function processItem(collectionName, items, varObj) {
/*
* Items are a nested structure
* See https://schema.postman.com/collection/json/v2.1.0/draft-07/collection.json
* Item is an array of `definitions/item` or `definitions/item-group`
* If it's an actual item it has a `request` otherwise it might be a group.
*/
let endpointArray = [];
for (let i in items) {
const item = items[i];
if (!item?.request) {
// This is an item-group, iterate
console.log('Item Group!', item.item);
endpointArray = endpointArray.concat(processItem(collectionName, item.item, varObj));
}
else {
console.log('Item!', item);
const endpointId = crypto.randomUUID();
const endpointName = item.name;
const endpointMethod = item.request.method;
let endpointURL = item.request.url.raw;
// Variables are denoted like so, {{var}}
if (endpointURL.includes('{{'))
{
console.log(`Variable seen in endpoint, ${endpointName}`);
endpointURL = replaceVariables(endpointURL, varObj);
}
const endpoint =
{
'endpointId': endpointId,
'endpointName': endpointName,
'connectorName': collectionName,
'endpointUri': endpointURL,
'httpRequestType': endpointMethod,
};
endpointArray.push(endpoint);
}
}
console.log('Completed Iteration!', endpointArray);
return endpointArray;
}
function readPostmanFile(file) {
const reader = new FileReader();
reader.addEventListener('load', (event) => {
if (!file.type?.startsWith('application/json')) {
console.log('File is not a JSON file!', file.type, file);
return;
}
if (!file.name?.endsWith(POSTMAN_FILE_EXTENSION)) {
console.log('Does not appear to be a Postman Collection!', file.name, file);
return;
}
const data = JSON.parse(event.target.result);
const collectionName = data.info.name;
const varObj = createVariableObject(data.variable);
console.log(`Collection: ${collectionName}`);
console.log('Data:', data);
console.log('varObj:', varObj);
const exportData =
{
'connectorName': collectionName,
'endpoints': [],
'version': '0.0.1'
};
console.log('Data.item:', data.item);
exportData.endpoints = processItem(collectionName, data.item, varObj);
if (exportData.endpoints.length === 0) {
console.log('No endpoints detected. Is your collection v2.1.0?');
return;
}
const outputFile = `${collectionName}.json`;
console.log(`Created ${outputFile}`);
createDownloadLink({ filename: outputFile, data: exportData });
});
reader.readAsText(file);
}
function createDownloadLink(file) {
const url = URL.createObjectURL(new Blob([JSON.stringify(file.data, null, 2)], { type: 'application/json' }));
const a = document.createElement('a');
a.href = url;
a.download = file.filename;
a.innerHTML = file.filename;
document.getElementById('download').replaceChildren(a);
}