-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
216 lines (186 loc) · 5.47 KB
/
client.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
import { message } from 'antd';
import _ from 'underscore';
const jsf = require('common/json-schema-mockjs');
function improtData(importDataModule) {
var SwaggerData;
function handlePath(path) {
if (path.charAt(0) != "/") {
path = "/" + path;
}
if (path.charAt(path.length - 1) === "/") {
path = path.substr(0, path.length - 1);
}
return path;
}
function run(res) {
try {
const interfaceData = { apis: [], cats: [] };
res = res.replace(/components\/schemas/g, 'definitions');
res = JSON.parse(res);
res.definitions = res.components.schemas;
delete res.components;
SwaggerData = res;
if (res.tags && Array.isArray(res.tags)) {
res.tags.forEach(tag => {
interfaceData.cats.push({
name: tag.name,
desc: tag.description
})
})
}
_.each(res.paths, (apis, path) => {
_.each(apis, (api, method) => {
api.path = path;
api.method = method;
let data = null;
try {
data = handleSwagger(api)
} catch (err) {
data = null;
}
if (data) {
interfaceData.apis.push(data);
}
})
})
return interfaceData;
} catch (e) {
console.error(e);
message.error("数据格式有误");
}
}
function handleSwagger(data) {
let api = {};
//处理基本信息
api.method = data.method.toUpperCase();
api.title = data.summary || data.path;
api.desc = data.description;
api.catname = data.tags && Array.isArray(data.tags) ? data.tags[0] : null;
api.path = handlePath(data.path);
api.req_params = [];
api.req_body_form = [];
api.req_headers = [];
api.req_query = [];
api.req_body_type = 'raw';
api.res_body_type = 'raw';
if (data.produces && data.produces.indexOf('application/json') > -1) {
api.res_body_type = 'json';
}
if (data.consumes && Array.isArray(data.consumes)) {
if (data.consumes.indexOf('application/x-www-form-urlencoded') > -1 || data.consumes.indexOf('multipart/form-data') > -1) {
api.req_body_type = 'form';
} else if (data.consumes.indexOf('application/json') > -1) {
api.req_body_type = 'json';
}
}
//处理response
api.res_body = handleResponse(data.responses);
try {
JSON.parse(api.res_body);
api.res_body_type = 'json';
} catch (e) {
api.res_body_type = 'raw';
}
//处理参数
function simpleJsonPathParse(key, json) {
if (!key || typeof key !== 'string' || key.indexOf('#/') !== 0 || key.length <= 2) {
return null;
}
let keys = key.substr(2).split("/");
keys = keys.filter(item => {
return item;
})
for (let i = 0, l = keys.length; i < l; i++) {
try {
json = json[keys[i]];
} catch (e) {
json = '';
break;
}
}
return json;
}
if (data.parameters && Array.isArray(data.parameters)) {
data.parameters.forEach(param => {
if (param && typeof param === 'object' && param.$ref) {
param = simpleJsonPathParse(param.$ref, { parameters: SwaggerData.parameters })
}
let defaultParam = {
name: param.name,
desc: param.description,
required: param.required ? "1" : "0"
}
switch (param.in) {
case 'path': api.req_params.push(defaultParam); break;
case 'query': api.req_query.push(defaultParam); break;
case 'formData': defaultParam.type = param.type === 'file' ? 'file' : 'text'; api.req_body_form.push(defaultParam); break;
case 'header': api.req_headers.push(defaultParam); break;
}
})
}
if (data.requestBody) {
handleBodyPamras(data.requestBody.content['application/json'].schema, api);
}
return api;
}
function isJson(json) {
try {
return JSON.parse(json);
} catch (e) {
return false;
}
}
function handleBodyPamras(data, api) {
api.req_body_other = handleSchema(data);
if (isJson(api.req_body_other)) {
api.req_body_type = 'json';
}
}
function handleResponse(api) {
let res_body = '';
if (!api || typeof api !== 'object') {
return res_body;
}
_.each(api, (res, code) => {
if (code.startsWith('2')) {
if (res && typeof res === 'object') {
if (res.content && res.content['application/json'] && res.content['application/json'].schema) {
res_body = handleSchema(res.content['application/json'].schema);
} else if (res.description) {
res_body = res.description;
}
} else if (typeof res === 'string') {
res_body = res;
} else {
res_body = '';
}
}
})
return res_body;
}
function handleSchema(data) {
if (!data) return data;
if (typeof data !== 'object') {
return data;
}
try {
data.definitions = SwaggerData.definitions;
const jsfData = JSON.stringify(jsf(data), null, 2);
return jsfData;
} catch (e) {
return '';
}
}
if (!importDataModule || typeof importDataModule !== 'object') {
console.error('importDataModule 参数Must be Object Type');
return null;
}
importDataModule.openapi = {
name: 'OpenAPI',
run: run,
desc: 'OpenAPI数据导入( 支持 v3.0+ )'
}
}
module.exports = function () {
this.bindHook('import_data', improtData)
}