-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (89 loc) · 3.37 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const json2ts = require('json2ts');
var argv = require('minimist')(process.argv.slice(2));
console.log('argv',argv);
async function fetchData(url, method, data = null) {
try {
const response = await axios({ method, url, data });
return response.data;
} catch (error) {
console.error(`Error fetching data from ${url}: ${error.message}`);
return null;
}
}
async function fetchAllData(requests) {
const promises = requests.map(({ url, method, data }) =>
fetchData(url, method, data)
);
try {
const allData = await Promise.all(promises);
return allData;
} catch (error) {
console.error('Error fetching data:', error.message);
return [];
}
}
function convertToPascalCase(inputString) {
return inputString.replace(/(^|-)([a-z])/g, (_, __, letter) => letter.toUpperCase());
}
async function generateAndSaveTypes(data, typeName, filePath) {
const RootObjectName = `I${convertToPascalCase(typeName)}`;
const types = json2ts.convert(data, typeName).replace("RootObject", RootObjectName);
saveTypesToFile(types, filePath);
}
function saveTypesToFile(types, filePath) {
const normalizedPath = path.normalize(filePath);
const directoryPath = path.dirname(normalizedPath);
// Create directories recursively if they don't exist
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
// Create a .ts file for types
fs.writeFileSync(normalizedPath, types, 'utf8');
console.log(`Generated types saved to ${normalizedPath}`);
}
async function generateTypes(postman){
try {
const requests = JSON.parse(postman).item.flatMap((pm) => {
if (pm.request.url.raw) {
return {
name: pm.name,
url: pm.request.url.raw,
method: pm.request.method,
data: pm?.request?.body?.raw ? JSON.parse(pm.request.body.raw) : undefined
};
}
return [];
});
const responseData = await fetchAllData(requests);
responseData.forEach(async (result, index) => {
const fileName = (requests[index].name).replace(/\s+/g, '-');
const requestTypeName = `${fileName}Request`;
const responseTypeName = `${fileName}Response`;
const requestFilePath = `types/request/${fileName}.ts`;
const responseFilePath = `types/response/${fileName}.ts`;
await generateAndSaveTypes(JSON.stringify(requests[index]), requestTypeName, requestFilePath);
await generateAndSaveTypes(JSON.stringify(result), responseTypeName, responseFilePath);
});
} catch (parseError) {
console.error(`Error parsing JSON data: ${parseError}`);
}
}
if (argv['c']) {
fetchData(argv['c'], "GET").then((response) => {
console.log('Generating types for collection url', response.collection)
let collection = JSON.stringify(response.collection);
generateTypes(collection);
})
return;
}
fs.readFile('data.json', 'utf8', async (error, collection) => {
//collection should be JSON/ JSON.stringify(obj)
if (error) {
console.error(`Error reading JSON file: ${error}`);
return;
}
generateTypes(collection);
});