-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpostmanToAPI.js
More file actions
177 lines (158 loc) · 5.55 KB
/
Copy pathpostmanToAPI.js
File metadata and controls
177 lines (158 loc) · 5.55 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const fs = require('fs');
const collection = require('./Paystack.postman_collection');
const baseUrl = "https://api.paystack.co";
let parsedAPIs = {}
collection.item.forEach((APIObject) => {
let command_name = APIObject.name.toLowerCase();
command_name = command_name.replace(/ /g, '');
if (command_name.includes('integration/')) {
command_name = command_name.replace('integration/', '')
}
parsedAPIs[command_name] = [];
if (!isAPIObject(APIObject)) {
APIObject.item.forEach((subObject) => {
let subcommand_name = subObject.name.toLowerCase();
subcommand_name = subcommand_name.replace(/ /g, '');
if (!isAPIObject(subObject)) {
subObject.item.forEach((_subObject) => {
subcommand_name = _subObject.name.toLowerCase();
subcommand_name = subcommand_name.replace(/ /g, '');
if (isAPIObject(_subObject)) {
let formattedAPI = createAPIObject(_subObject, command_name);
parsedAPIs[command_name].push(formattedAPI);
} else {
// console.log('Naah', _subObject)
}
})
} else {
//add to API collection
let formattedAPI = createAPIObject(subObject, command_name);
parsedAPIs[command_name].push(formattedAPI);
// console.log(Object.keys(subObject))
}
})
}
else {
//add to API collection
let formattedAPI = createAPIObject(APIObject, command_name);
parsedAPIs[command_name].push(formattedAPI);
// console.log(formattedAPI);
// console.log(Object.keys(APIObject))
}
})
fs.writeFile(`./APIs.json`, JSON.stringify(parsedAPIs), () => {
console.log("done")
})
function isAPIObject(obj) {
if (obj.item) {
return false;
}
if (obj.request && obj.response) {
return true;
}
}
function createAPIObject(schema, parent) {
// console.log(schema);
let API = {
baseUrl: "https://api.paystack.co",
path: "/transaction/initialize",
method: "POST",
params: [{
parameter: "email",
required: true,
type: "String",
description: "Lorem ipsum"
}],
api: "initialize",
parent: "transaction"
}
try {
API.baseUrl = "https://api.paystack.co/";
API.path = schema.request.url.path.join('/');
API.method = schema.request.method;
API.parent = parent;
API.api = '';
//Format command name
let name = schema.name.toLowerCase();
if (name.includes(parent + 's')) {
API.api = name.replace(parent + 's', '');
} else if (name.includes(parent)) {
API.api = name.replace(parent, '');
} else {
if (noOfWords(name) === 2) {
name = name.replace(' ', '_');
API.api = name;
} else {
let words = name.split(' ');
let lastTwo = words[words.length - 2] + words[words.length - 1];
if (lastTwo == parent) {
API.api = words[0];
} else if (lastTwo == parent + 's') {
API.api = words[0];
}
else {
API.api = "****" + name
}
}
}
API.api = API.api.replace(' ', ' ');
API.api = API.api.trim();
if (noOfWords(API.api) > 1) {
API.api = API.api.replace(/ /g, '_');
API.api = "****" + API.api
}
//Get parameters
API.params = [];
if (API.method == "GET") {
let query = schema.request.url.query;
if (query) {
query.forEach((param) => {
let _p = {
parameter: param.key,
description: param.description,
required: false
}
API.params.push(_p)
})
} else {
console.log(API.parent + ' - ' + API.name, "Shoout!")
}
} else if (API.method == "POST" || API.method == "PUT") {
// console.log(Object.keys(schema.request))
if (schema.request.body) {
let body = schema.request.body.urlencoded;
if (body) {
body.forEach((param) => {
let _p = {
parameter: param.key,
description: param.description,
required: false
}
if (_p.description.toLowerCase().includes('required')) {
_p.required = true;
}
API.params.push(_p)
})
} else {
console.log(API.parent + ' - ' + API.name, "Shoout!!")
}
} else {
console.log(API.parent + ' - ' + API.name, "why not");
}
}
//GET variables
if(schema.request.url.variable){
API.variables = schema.request.url.variable
}
} catch (err) {
console.error(err)
}
return API;
}
function noOfWords(str) {
return str.split(' ').length;
}
/*
Highlight - New phone, new merch and stuff, Tomilola's birthday weekend. We have BPOs now!
Lowlight - Fuel, I'm tired, but I don't feel like I did plenty work
*/