This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (198 loc) · 7.21 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const fs = require('fs'),
convert = require('./convert.js'),
_ = require('lodash'),
extend = require('extend'),
request = require('request-promise');
function handleConversion(collection, config) {
var swaggerFileName = collection.swagger_file_name;
var postmanFileName = collection.postman_file_name;
var collectionName = collection.collection_name;
var environment_file_name = collection.environment_file_name;
var environment_name = collection.environment_name;
var upload = config.upload;
var swaggerObject = JSON.parse(
fs.readFileSync(swaggerFileName, 'utf8')
);
var conversionResult = convert(swaggerObject);
downloadEnvironment(environment_file_name, environment_name, config.key);
var getOptions = {
method: 'GET',
url: 'https://api.getpostman.com/collections/',
qs: {
format: '2.1.0'
},
headers: {
'Cache-Control': 'no-cache',
'X-Api-Key': config.key,
'Content-Type': 'application/json'
},
json: true
};
request(getOptions)
.then(function (postmanJson) {
var collections = postmanJson.collections;
if (!collections) throw new Error("collections not found");
collections = collections.filter(function(collection) {
return collection.name == collectionName;
});
collections = collections.sort(function(a,b) {
return a.id > b.id;
});
var collectionUid = collections[0].uid;
if(collectionUid) {
downloadCollection(conversionResult, postmanFileName, collectionUid, config);
} else {
fs.writeFileSync(postmanFileName, JSON.stringify(conversionResult.collection, null, 2));
if(upload){
updateCollection(postmanFileName, collectionUid, config.key);
}
}
}).catch(function (error) {
throw new Error("collections not found");
});
}
function downloadEnvironment(environmentFileName, environmentName, key) {
var getOptions = {
method: 'GET',
url: 'https://api.getpostman.com/environments/',
qs: {
format: '2.1.0'
},
headers: {
'Cache-Control': 'no-cache',
'X-Api-Key': key,
'Content-Type': 'application/json'
},
json: true
};
request(getOptions)
.then(function (environmentJson) {
var environments = environmentJson.environments;
if (!environments) throw new Error("environments not found");
environments = environments.filter(function(environment) {
return environment.name == environmentName;
});
environments = environments.sort(function(a,b) {
return a.id > b.id;
});
var environmentUid = environments[0].uid;
var getOptions = {
method: 'GET',
url: 'https://api.getpostman.com/environments/' + environmentUid,
qs: {
format: '2.1.0'
},
headers: {
'Cache-Control': 'no-cache',
'X-Api-Key': key,
'Content-Type': 'application/json'
},
json: true
};
request(getOptions)
.then(function (environmentJson) {
var json = JSON.stringify(environmentJson.environment, null, 2);
fs.writeFileSync(environmentFileName, json);
}).catch(function (error) {
throw new Error("environment not found");
});
}).catch(function (error) {
throw new Error("environments not found");
});
}
// postmanのcollectionデータをダウンロードしてmergeして保存
function downloadCollection(swaggerJson, postmanFileName, collection_uid, config) {
var getOptions = {
method: 'GET',
url: 'https://api.getpostman.com/collections/' + collection_uid,
qs: {
format: '2.1.0'
},
headers: {
'Cache-Control': 'no-cache',
'X-Api-Key': config.key,
'Content-Type': 'application/json'
},
json: true
};
request(getOptions).then(function (postmanJson) {
swaggerJson.collection.event = extend(true, [], swaggerJson.collection.event, postmanJson.collection.event);
delete swaggerJson.collection.variables;
swaggerJson.collection.variable = postmanJson.collection.variable.filter(function(value) {
return value.key && !value.disabled;
});
var json = JSON.stringify(swaggerJson.collection, null, 2);
fs.writeFileSync(postmanFileName, json);
if(config.upload){
updateCollection(postmanFileName, collection_uid, config.key);
}
}).catch(function (error) {
throw new Error("collection download failed");
});
}
function updateLocalCollection(postmanFileName, newFile) {
fs.writeFileSync('./' + postmanFileName, JSON.stringify(newFile, null, 2));
console.log('writing to ' + postmanFileName);
}
function updatePostman(newFileName, collection_uid, key) {
var data = fs.readFileSync('./' + newFileName, 'utf8');
var putOptions = {
method: 'PUT',
url: 'https://api.getpostman.com/collections/' + collection_uid,
qs: {
format: '2.1.0'
},
headers: {
'Cache-Control': 'no-cache',
'X-Api-Key': key,
'Content-Type': 'application/json'
},
body: JSON.parse(data),
json: true
};
request(putOptions).then(function (body) {
console.log(body);
}).catch(function (error) {
throw new Error("update failed");
});
}
function updateCollection(newFileName, collection_uid, key) {
var data = fs.readFileSync('./' + newFileName, 'utf8');
var file = JSON.parse(data);
file.info.id = collection_uid;
file.info._postman_id = file.info.id;
var newFile = {};
newFile.collection = file;
// ローカルのファイルをcollectionオブジェクトでラップしたものを保存。
updateLocalCollection(newFileName, newFile);
// postmanアプリにアップロードする。
updatePostman(newFileName, collection_uid, key);
}
function convert_upload(config) {
if(config && typeof config === 'object'){
config.collections.forEach( function (collection) {
handleConversion(collection, config);
}.bind(config));
} else {
console.log("configファイルを設定してください。");
}
}
swagger2postman = {
convert_upload: convert_upload
};
module.exports = swagger2postman;
// 開発時に利用
// convert_upload({
// "collections": [
// {
// //campfireApi
// "swagger_file_name": "***.json", //swagger読み込み用ファイル.json
// "postman_file_name": "***.json", //postman書き出しファイル名
// "environment_file_name": "***.json", //"environment書き出しファイル名
// "environment_name": "****", //読み込むenvironment名
// "collection_name": "***", //読み込むcollection名"
// },
// ],
// "key": "***********************", //postmanAPI_KEY
// "upload": false // "postmanへのuploadフラグ"
// });