yapi-to-typescript2 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/Generator.d.ts +49 -0
- package/lib/cjs/Generator.js +729 -1121
- package/lib/cjs/SwaggerToYApiServer.d.ts +20 -0
- package/lib/cjs/SwaggerToYApiServer.js +222 -330
- package/lib/cjs/cli.d.ts +4 -0
- package/lib/cjs/cli.js +247 -278
- package/lib/cjs/helpers.d.ts +49 -0
- package/lib/cjs/helpers.js +179 -203
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +17 -18
- package/lib/cjs/swaggerJsonToYApiData.d.ts +6 -0
- package/lib/cjs/swaggerJsonToYApiData.js +408 -465
- package/lib/cjs/types.d.ts +693 -0
- package/lib/cjs/types.js +75 -82
- package/lib/cjs/utils.d.ts +92 -0
- package/lib/cjs/utils.js +499 -654
- package/lib/esm/Generator.d.ts +49 -0
- package/lib/esm/Generator.js +696 -1093
- package/lib/esm/SwaggerToYApiServer.d.ts +20 -0
- package/lib/esm/SwaggerToYApiServer.js +212 -307
- package/lib/esm/cli.d.ts +4 -0
- package/lib/esm/cli.js +209 -242
- package/lib/esm/helpers.d.ts +49 -0
- package/lib/esm/helpers.js +174 -190
- package/lib/esm/index.d.ts +2 -845
- package/lib/esm/index.js +1 -1
- package/lib/esm/swaggerJsonToYApiData.d.ts +6 -0
- package/lib/esm/swaggerJsonToYApiData.js +399 -450
- package/lib/esm/types.d.ts +693 -0
- package/lib/esm/types.js +66 -55
- package/lib/esm/utils.d.ts +92 -0
- package/lib/esm/utils.js +462 -584
- package/package.json +9 -4
|
@@ -1,483 +1,432 @@
|
|
|
1
|
-
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
-
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
-
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
1
|
// @ts-nocheck
|
|
5
2
|
// ref: https://github.com/YMFE/yapi/blob/master/exts/yapi-plugin-import-swagger/run.js
|
|
3
|
+
var __assign = (this && this.__assign) || function () {
|
|
4
|
+
__assign = Object.assign || function(t) {
|
|
5
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
6
|
+
s = arguments[i];
|
|
7
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
8
|
+
t[p] = s[p];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
return __assign.apply(this, arguments);
|
|
13
|
+
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
24
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
25
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
26
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
27
|
+
function step(op) {
|
|
28
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
29
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
30
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
31
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
32
|
+
switch (op[0]) {
|
|
33
|
+
case 0: case 1: t = op; break;
|
|
34
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
35
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
36
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
37
|
+
default:
|
|
38
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
39
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
40
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
41
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
42
|
+
if (t[2]) _.ops.pop();
|
|
43
|
+
_.trys.pop(); continue;
|
|
44
|
+
}
|
|
45
|
+
op = body.call(thisArg, _);
|
|
46
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
47
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
48
|
+
}
|
|
49
|
+
};
|
|
6
50
|
import dayjs from 'dayjs';
|
|
7
51
|
import swagger from 'swagger-client';
|
|
8
52
|
import { each, find } from 'vtils';
|
|
9
53
|
var SwaggerData, isOAS3;
|
|
10
|
-
|
|
11
54
|
function handlePath(path) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return path;
|
|
55
|
+
if (path === '/')
|
|
56
|
+
return path;
|
|
57
|
+
if (path.charAt(0) !== '/') {
|
|
58
|
+
path = "/".concat(path);
|
|
59
|
+
}
|
|
60
|
+
if (path.charAt(path.length - 1) === '/') {
|
|
61
|
+
path = path.substr(0, path.length - 1);
|
|
62
|
+
}
|
|
63
|
+
return path;
|
|
23
64
|
}
|
|
24
|
-
|
|
25
65
|
function openapi2swagger(data) {
|
|
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
|
-
|
|
66
|
+
data.swagger = '2.0';
|
|
67
|
+
each(data.paths, function (apis) {
|
|
68
|
+
each(apis, function (api) {
|
|
69
|
+
each(api.responses, function (res) {
|
|
70
|
+
if (res.content &&
|
|
71
|
+
res.content['application/json'] &&
|
|
72
|
+
typeof res.content['application/json'] === 'object') {
|
|
73
|
+
Object.assign(res, res.content['application/json']);
|
|
74
|
+
delete res.content;
|
|
75
|
+
}
|
|
76
|
+
if (res.content &&
|
|
77
|
+
res.content['application/hal+json'] &&
|
|
78
|
+
typeof res.content['application/hal+json'] === 'object') {
|
|
79
|
+
Object.assign(res, res.content['application/hal+json']);
|
|
80
|
+
delete res.content;
|
|
81
|
+
}
|
|
82
|
+
if (res.content &&
|
|
83
|
+
res.content['*/*'] &&
|
|
84
|
+
typeof res.content['*/*'] === 'object') {
|
|
85
|
+
Object.assign(res, res.content['*/*']);
|
|
86
|
+
delete res.content;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
if (api.requestBody) {
|
|
90
|
+
if (!api.parameters)
|
|
91
|
+
api.parameters = [];
|
|
92
|
+
var body = {
|
|
93
|
+
type: 'object',
|
|
94
|
+
name: 'body',
|
|
95
|
+
in: 'body',
|
|
96
|
+
};
|
|
97
|
+
try {
|
|
98
|
+
body.schema = api.requestBody.content['application/json'].schema;
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
body.schema = {};
|
|
102
|
+
}
|
|
103
|
+
api.parameters.push(body);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
62
106
|
});
|
|
63
|
-
|
|
64
|
-
return data;
|
|
107
|
+
return data;
|
|
65
108
|
}
|
|
66
|
-
|
|
67
|
-
function
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
spec: res
|
|
80
|
-
});
|
|
81
|
-
data.then(function (res) {
|
|
82
|
-
resolve(res.spec);
|
|
83
|
-
});
|
|
84
|
-
}));
|
|
85
|
-
|
|
86
|
-
case 1:
|
|
87
|
-
case "end":
|
|
88
|
-
return _context.stop();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}, _callee);
|
|
92
|
-
}));
|
|
93
|
-
return _handleSwaggerData.apply(this, arguments);
|
|
109
|
+
function handleSwaggerData(res) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
111
|
+
return __generator(this, function (_a) {
|
|
112
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
113
|
+
var data = swagger({
|
|
114
|
+
spec: res,
|
|
115
|
+
});
|
|
116
|
+
data.then(function (res) {
|
|
117
|
+
resolve(res.spec);
|
|
118
|
+
});
|
|
119
|
+
})];
|
|
120
|
+
});
|
|
121
|
+
});
|
|
94
122
|
}
|
|
95
|
-
|
|
96
|
-
function
|
|
97
|
-
|
|
123
|
+
function run(res) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
125
|
+
var interfaceData;
|
|
126
|
+
return __generator(this, function (_a) {
|
|
127
|
+
switch (_a.label) {
|
|
128
|
+
case 0:
|
|
129
|
+
interfaceData = { apis: [], cats: [], basePath: '', swaggerData: {} };
|
|
130
|
+
if (typeof res === 'string' && res) {
|
|
131
|
+
try {
|
|
132
|
+
res = JSON.parse(res);
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
console.error('json 解析出错', e.message);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
isOAS3 = res.openapi && String(res.openapi).startsWith('3.');
|
|
139
|
+
if (isOAS3) {
|
|
140
|
+
res = openapi2swagger(res);
|
|
141
|
+
}
|
|
142
|
+
return [4 /*yield*/, handleSwaggerData(res)];
|
|
143
|
+
case 1:
|
|
144
|
+
res = _a.sent();
|
|
145
|
+
SwaggerData = res;
|
|
146
|
+
interfaceData.swaggerData = SwaggerData;
|
|
147
|
+
interfaceData.basePath = res.basePath || '';
|
|
148
|
+
if (res.tags && Array.isArray(res.tags)) {
|
|
149
|
+
res.tags.forEach(function (tag) {
|
|
150
|
+
interfaceData.cats.push({
|
|
151
|
+
name: tag.name,
|
|
152
|
+
desc: tag.description,
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
res.tags = [];
|
|
158
|
+
}
|
|
159
|
+
each(res.paths, function (apis, path) {
|
|
160
|
+
// parameters is common parameters, not a method
|
|
161
|
+
delete apis.parameters;
|
|
162
|
+
each(apis, function (api, method) {
|
|
163
|
+
api.path = path;
|
|
164
|
+
api.method = method;
|
|
165
|
+
var data = null;
|
|
166
|
+
try {
|
|
167
|
+
data = handleSwagger(api, res.tags);
|
|
168
|
+
if (data.catname) {
|
|
169
|
+
if (!find(interfaceData.cats, function (item) { return item.name === data.catname; })) {
|
|
170
|
+
if (res.tags.length === 0) {
|
|
171
|
+
interfaceData.cats.push({
|
|
172
|
+
name: data.catname,
|
|
173
|
+
desc: data.catname,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
data = null;
|
|
181
|
+
}
|
|
182
|
+
if (data) {
|
|
183
|
+
interfaceData.apis.push(data);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
interfaceData.cats = interfaceData.cats.filter(function (catData) {
|
|
188
|
+
var catName = catData.name;
|
|
189
|
+
return find(interfaceData.apis, function (apiData) {
|
|
190
|
+
return apiData.catname === catName;
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
return [2 /*return*/, interfaceData];
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
98
197
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
if (typeof res === 'string' && res) {
|
|
115
|
-
try {
|
|
116
|
-
res = JSON.parse(res);
|
|
117
|
-
} catch (e) {
|
|
118
|
-
console.error('json 解析出错', e.message);
|
|
119
|
-
}
|
|
198
|
+
function handleSwagger(data, originTags) {
|
|
199
|
+
if (originTags === void 0) { originTags = []; }
|
|
200
|
+
var api = {};
|
|
201
|
+
//处理基本信息
|
|
202
|
+
api.method = data.method.toUpperCase();
|
|
203
|
+
api.title = data.summary || data.path;
|
|
204
|
+
api.desc = data.description;
|
|
205
|
+
api.catname = null;
|
|
206
|
+
if (data.tags && Array.isArray(data.tags)) {
|
|
207
|
+
api.tag = data.tags;
|
|
208
|
+
var _loop_1 = function (i) {
|
|
209
|
+
if (/v[0-9.]+/.test(data.tags[i])) {
|
|
210
|
+
return "continue";
|
|
120
211
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
212
|
+
// 如果根路径有 tags,使用根路径 tags,不使用每个接口定义的 tag 做完分类
|
|
213
|
+
if (originTags.length > 0 &&
|
|
214
|
+
find(originTags, function (item) {
|
|
215
|
+
return item.name === data.tags[i];
|
|
216
|
+
})) {
|
|
217
|
+
api.catname = data.tags[i];
|
|
218
|
+
return "break";
|
|
126
219
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
case 6:
|
|
132
|
-
res = _context2.sent;
|
|
133
|
-
SwaggerData = res;
|
|
134
|
-
interfaceData.swaggerData = SwaggerData;
|
|
135
|
-
interfaceData.basePath = res.basePath || '';
|
|
136
|
-
|
|
137
|
-
if (res.tags && Array.isArray(res.tags)) {
|
|
138
|
-
res.tags.forEach(function (tag) {
|
|
139
|
-
interfaceData.cats.push({
|
|
140
|
-
name: tag.name,
|
|
141
|
-
desc: tag.description
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
} else {
|
|
145
|
-
res.tags = [];
|
|
220
|
+
if (originTags.length === 0) {
|
|
221
|
+
api.catname = data.tags[i];
|
|
222
|
+
return "break";
|
|
146
223
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
api.path = path;
|
|
153
|
-
api.method = method;
|
|
154
|
-
var data = null;
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
data = handleSwagger(api, res.tags);
|
|
158
|
-
|
|
159
|
-
if (data.catname) {
|
|
160
|
-
if (!find(interfaceData.cats, function (item) {
|
|
161
|
-
return item.name === data.catname;
|
|
162
|
-
})) {
|
|
163
|
-
if (res.tags.length === 0) {
|
|
164
|
-
interfaceData.cats.push({
|
|
165
|
-
name: data.catname,
|
|
166
|
-
desc: data.catname
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
} catch (err) {
|
|
172
|
-
data = null;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (data) {
|
|
176
|
-
interfaceData.apis.push(data);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
interfaceData.cats = interfaceData.cats.filter(function (catData) {
|
|
181
|
-
var catName = catData.name;
|
|
182
|
-
return find(interfaceData.apis, function (apiData) {
|
|
183
|
-
return apiData.catname === catName;
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
return _context2.abrupt("return", interfaceData);
|
|
187
|
-
|
|
188
|
-
case 14:
|
|
189
|
-
case "end":
|
|
190
|
-
return _context2.stop();
|
|
224
|
+
};
|
|
225
|
+
for (var i = 0; i < data.tags.length; i++) {
|
|
226
|
+
var state_1 = _loop_1(i);
|
|
227
|
+
if (state_1 === "break")
|
|
228
|
+
break;
|
|
191
229
|
}
|
|
192
|
-
}
|
|
193
|
-
}, _callee2);
|
|
194
|
-
}));
|
|
195
|
-
return _run.apply(this, arguments);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function handleSwagger(data, originTags) {
|
|
199
|
-
if (originTags === void 0) {
|
|
200
|
-
originTags = [];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
var api = {}; //处理基本信息
|
|
204
|
-
|
|
205
|
-
api.method = data.method.toUpperCase();
|
|
206
|
-
api.title = data.summary || data.path;
|
|
207
|
-
api.desc = data.description;
|
|
208
|
-
api.catname = null;
|
|
209
|
-
|
|
210
|
-
if (data.tags && Array.isArray(data.tags)) {
|
|
211
|
-
api.tag = data.tags;
|
|
212
|
-
|
|
213
|
-
var _loop = function _loop(i) {
|
|
214
|
-
if (/v[0-9.]+/.test(data.tags[i])) {
|
|
215
|
-
return "continue";
|
|
216
|
-
} // 如果根路径有 tags,使用根路径 tags,不使用每个接口定义的 tag 做完分类
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (originTags.length > 0 && find(originTags, function (item) {
|
|
220
|
-
return item.name === data.tags[i];
|
|
221
|
-
})) {
|
|
222
|
-
api.catname = data.tags[i];
|
|
223
|
-
return "break";
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (originTags.length === 0) {
|
|
227
|
-
api.catname = data.tags[i];
|
|
228
|
-
return "break";
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
for (var i = 0; i < data.tags.length; i++) {
|
|
233
|
-
var _ret = _loop(i);
|
|
234
|
-
|
|
235
|
-
if (_ret === "continue") continue;
|
|
236
|
-
if (_ret === "break") break;
|
|
237
230
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
api.req_query = [];
|
|
245
|
-
api.req_body_type = 'raw';
|
|
246
|
-
api.res_body_type = 'raw';
|
|
247
|
-
|
|
248
|
-
if (data.produces && data.produces.indexOf('application/json') > -1) {
|
|
249
|
-
api.res_body_type = 'json';
|
|
250
|
-
api.res_body_is_json_schema = true;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (data.consumes && Array.isArray(data.consumes)) {
|
|
254
|
-
if (data.consumes.indexOf('application/x-www-form-urlencoded') > -1 || data.consumes.indexOf('multipart/form-data') > -1) {
|
|
255
|
-
api.req_body_type = 'form';
|
|
256
|
-
} else if (data.consumes.indexOf('application/json') > -1) {
|
|
257
|
-
api.req_body_type = 'json';
|
|
258
|
-
api.req_body_is_json_schema = true;
|
|
259
|
-
}
|
|
260
|
-
} //处理response
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
api.res_body = handleResponse(data.responses);
|
|
264
|
-
|
|
265
|
-
try {
|
|
266
|
-
JSON.parse(api.res_body);
|
|
267
|
-
api.res_body_type = 'json';
|
|
268
|
-
api.res_body_is_json_schema = true;
|
|
269
|
-
} catch (e) {
|
|
231
|
+
api.path = handlePath(data.path);
|
|
232
|
+
api.req_params = [];
|
|
233
|
+
api.req_body_form = [];
|
|
234
|
+
api.req_headers = [];
|
|
235
|
+
api.req_query = [];
|
|
236
|
+
api.req_body_type = 'raw';
|
|
270
237
|
api.res_body_type = 'raw';
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
function simpleJsonPathParse(key, json) {
|
|
275
|
-
if (!key || typeof key !== 'string' || key.indexOf('#/') !== 0 || key.length <= 2) {
|
|
276
|
-
return null;
|
|
238
|
+
if (data.produces && data.produces.indexOf('application/json') > -1) {
|
|
239
|
+
api.res_body_type = 'json';
|
|
240
|
+
api.res_body_is_json_schema = true;
|
|
277
241
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
242
|
+
if (data.consumes && Array.isArray(data.consumes)) {
|
|
243
|
+
if (data.consumes.indexOf('application/x-www-form-urlencoded') > -1 ||
|
|
244
|
+
data.consumes.indexOf('multipart/form-data') > -1) {
|
|
245
|
+
api.req_body_type = 'form';
|
|
246
|
+
}
|
|
247
|
+
else if (data.consumes.indexOf('application/json') > -1) {
|
|
248
|
+
api.req_body_type = 'json';
|
|
249
|
+
api.req_body_is_json_schema = true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
//处理response
|
|
253
|
+
api.res_body = handleResponse(data.responses);
|
|
254
|
+
try {
|
|
255
|
+
JSON.parse(api.res_body);
|
|
256
|
+
api.res_body_type = 'json';
|
|
257
|
+
api.res_body_is_json_schema = true;
|
|
258
|
+
}
|
|
259
|
+
catch (e) {
|
|
260
|
+
api.res_body_type = 'raw';
|
|
291
261
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
262
|
+
//处理参数
|
|
263
|
+
function simpleJsonPathParse(key, json) {
|
|
264
|
+
if (!key ||
|
|
265
|
+
typeof key !== 'string' ||
|
|
266
|
+
key.indexOf('#/') !== 0 ||
|
|
267
|
+
key.length <= 2) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
var keys = key.substr(2).split('/');
|
|
271
|
+
keys = keys.filter(function (item) {
|
|
272
|
+
return item;
|
|
301
273
|
});
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
if (param.in) {
|
|
311
|
-
switch (param.in) {
|
|
312
|
-
case 'path':
|
|
313
|
-
defaultParam.type = param.type;
|
|
314
|
-
api.req_params.push(defaultParam);
|
|
315
|
-
break;
|
|
316
|
-
|
|
317
|
-
case 'query':
|
|
318
|
-
defaultParam.type = param.type;
|
|
319
|
-
api.req_query.push(defaultParam);
|
|
320
|
-
break;
|
|
321
|
-
|
|
322
|
-
case 'body':
|
|
323
|
-
handleBodyPamras(param.schema, api);
|
|
324
|
-
break;
|
|
325
|
-
|
|
326
|
-
case 'formData':
|
|
327
|
-
defaultParam.type = param.type === 'file' ? 'file' : 'text';
|
|
328
|
-
|
|
329
|
-
if (param.example) {
|
|
330
|
-
defaultParam.example = param.example;
|
|
274
|
+
for (var i = 0, l = keys.length; i < l; i++) {
|
|
275
|
+
try {
|
|
276
|
+
json = json[keys[i]];
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
json = '';
|
|
280
|
+
break;
|
|
331
281
|
}
|
|
332
|
-
|
|
333
|
-
api.req_body_form.push(defaultParam);
|
|
334
|
-
break;
|
|
335
|
-
|
|
336
|
-
case 'header':
|
|
337
|
-
api.req_headers.push(defaultParam);
|
|
338
|
-
break;
|
|
339
|
-
|
|
340
|
-
default:
|
|
341
|
-
break;
|
|
342
282
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
283
|
+
return json;
|
|
284
|
+
}
|
|
285
|
+
if (data.parameters && Array.isArray(data.parameters)) {
|
|
286
|
+
data.parameters.forEach(function (param) {
|
|
287
|
+
if (param && typeof param === 'object' && param.$ref) {
|
|
288
|
+
param = simpleJsonPathParse(param.$ref, {
|
|
289
|
+
parameters: SwaggerData.parameters,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
var defaultParam = {
|
|
293
|
+
name: param.name,
|
|
294
|
+
desc: param.description,
|
|
295
|
+
required: param.required ? '1' : '0',
|
|
296
|
+
};
|
|
297
|
+
if (param.in) {
|
|
298
|
+
switch (param.in) {
|
|
299
|
+
case 'path':
|
|
300
|
+
defaultParam.type = param.type;
|
|
301
|
+
api.req_params.push(defaultParam);
|
|
302
|
+
break;
|
|
303
|
+
case 'query':
|
|
304
|
+
defaultParam.type = param.type;
|
|
305
|
+
api.req_query.push(defaultParam);
|
|
306
|
+
break;
|
|
307
|
+
case 'body':
|
|
308
|
+
handleBodyPamras(param.schema, api);
|
|
309
|
+
break;
|
|
310
|
+
case 'formData':
|
|
311
|
+
defaultParam.type = param.type === 'file' ? 'file' : 'text';
|
|
312
|
+
if (param.example) {
|
|
313
|
+
defaultParam.example = param.example;
|
|
314
|
+
}
|
|
315
|
+
api.req_body_form.push(defaultParam);
|
|
316
|
+
break;
|
|
317
|
+
case 'header':
|
|
318
|
+
api.req_headers.push(defaultParam);
|
|
319
|
+
break;
|
|
320
|
+
default:
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
api.req_query.push(defaultParam);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
return api;
|
|
350
330
|
}
|
|
351
|
-
|
|
352
331
|
function isJson(json) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
332
|
+
try {
|
|
333
|
+
return JSON.parse(json);
|
|
334
|
+
}
|
|
335
|
+
catch (e) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
358
338
|
}
|
|
359
|
-
|
|
360
339
|
function handleBodyPamras(data, api) {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
340
|
+
api.req_body_other = JSON.stringify(data, null, 2);
|
|
341
|
+
if (isJson(api.req_body_other)) {
|
|
342
|
+
api.req_body_type = 'json';
|
|
343
|
+
api.req_body_is_json_schema = true;
|
|
344
|
+
}
|
|
367
345
|
}
|
|
368
|
-
|
|
369
346
|
function handleResponse(api) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
return res_body;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
var codes = Object.keys(api);
|
|
377
|
-
var curCode;
|
|
378
|
-
|
|
379
|
-
if (codes.length > 0) {
|
|
380
|
-
if (codes.indexOf('200') > -1) {
|
|
381
|
-
curCode = '200';
|
|
382
|
-
} else curCode = codes[0];
|
|
383
|
-
|
|
384
|
-
var res = api[curCode];
|
|
385
|
-
|
|
386
|
-
if (res && typeof res === 'object') {
|
|
387
|
-
if (res.schema) {
|
|
388
|
-
res_body = JSON.stringify(res.schema, null, 2);
|
|
389
|
-
} else if (res.description) {
|
|
390
|
-
res_body = res.description;
|
|
391
|
-
}
|
|
392
|
-
} else if (typeof res === 'string') {
|
|
393
|
-
res_body = res;
|
|
394
|
-
} else {
|
|
395
|
-
res_body = '';
|
|
347
|
+
var res_body = '';
|
|
348
|
+
if (!api || typeof api !== 'object') {
|
|
349
|
+
return res_body;
|
|
396
350
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
_swaggerJsonToYApiData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(data) {
|
|
410
|
-
var _yapiData$swaggerData;
|
|
411
|
-
|
|
412
|
-
var yapiData, currentTime, project, cats, interfaces;
|
|
413
|
-
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
414
|
-
while (1) {
|
|
415
|
-
switch (_context3.prev = _context3.next) {
|
|
416
|
-
case 0:
|
|
417
|
-
_context3.next = 2;
|
|
418
|
-
return run(data);
|
|
419
|
-
|
|
420
|
-
case 2:
|
|
421
|
-
yapiData = _context3.sent;
|
|
422
|
-
|
|
423
|
-
// 兼容没有分类的情况
|
|
424
|
-
if (!yapiData.cats.length) {
|
|
425
|
-
yapiData.cats = [{
|
|
426
|
-
name: 'default',
|
|
427
|
-
desc: 'default'
|
|
428
|
-
}];
|
|
429
|
-
yapiData.apis.forEach(function (api) {
|
|
430
|
-
api.catname = 'default';
|
|
431
|
-
});
|
|
351
|
+
var codes = Object.keys(api);
|
|
352
|
+
var curCode;
|
|
353
|
+
if (codes.length > 0) {
|
|
354
|
+
if (codes.indexOf('200') > -1) {
|
|
355
|
+
curCode = '200';
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
curCode = codes[0];
|
|
359
|
+
var res = api[curCode];
|
|
360
|
+
if (res && typeof res === 'object') {
|
|
361
|
+
if (res.schema) {
|
|
362
|
+
res_body = JSON.stringify(res.schema, null, 2);
|
|
432
363
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
basepath: yapiData.swaggerData.basePath || '',
|
|
440
|
-
tag: [],
|
|
441
|
-
env: [{
|
|
442
|
-
name: 'local',
|
|
443
|
-
domain: (((_yapiData$swaggerData = yapiData.swaggerData.schemes) == null ? void 0 : _yapiData$swaggerData[0]) || 'http') + "://" + (yapiData.swaggerData.host || '127.0.0.1')
|
|
444
|
-
}]
|
|
445
|
-
};
|
|
446
|
-
cats = yapiData.cats.map(function (cat, index) {
|
|
447
|
-
return {
|
|
448
|
-
_id: index + 1,
|
|
449
|
-
name: cat.name,
|
|
450
|
-
desc: cat.desc,
|
|
451
|
-
add_time: currentTime,
|
|
452
|
-
up_time: currentTime
|
|
453
|
-
};
|
|
454
|
-
});
|
|
455
|
-
interfaces = yapiData.apis.map(function (api, index) {
|
|
456
|
-
var _cats$find;
|
|
457
|
-
|
|
458
|
-
return _extends({}, api, {
|
|
459
|
-
_id: index + 1,
|
|
460
|
-
project_id: 0,
|
|
461
|
-
catid: ((_cats$find = cats.find(function (cat) {
|
|
462
|
-
return cat.name === api.catname;
|
|
463
|
-
})) == null ? void 0 : _cats$find._id) || -1,
|
|
464
|
-
tag: api.tag || [],
|
|
465
|
-
add_time: currentTime,
|
|
466
|
-
up_time: currentTime
|
|
467
|
-
});
|
|
468
|
-
});
|
|
469
|
-
return _context3.abrupt("return", {
|
|
470
|
-
project: project,
|
|
471
|
-
cats: cats,
|
|
472
|
-
interfaces: interfaces
|
|
473
|
-
});
|
|
474
|
-
|
|
475
|
-
case 9:
|
|
476
|
-
case "end":
|
|
477
|
-
return _context3.stop();
|
|
364
|
+
else if (res.description) {
|
|
365
|
+
res_body = res.description;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
else if (typeof res === 'string') {
|
|
369
|
+
res_body = res;
|
|
478
370
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
371
|
+
else {
|
|
372
|
+
res_body = '';
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
res_body = '';
|
|
377
|
+
}
|
|
378
|
+
return res_body;
|
|
379
|
+
}
|
|
380
|
+
export function swaggerJsonToYApiData(data) {
|
|
381
|
+
var _a;
|
|
382
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
383
|
+
var yapiData, currentTime, project, cats, interfaces;
|
|
384
|
+
return __generator(this, function (_b) {
|
|
385
|
+
switch (_b.label) {
|
|
386
|
+
case 0: return [4 /*yield*/, run(data)
|
|
387
|
+
// 兼容没有分类的情况
|
|
388
|
+
];
|
|
389
|
+
case 1:
|
|
390
|
+
yapiData = _b.sent();
|
|
391
|
+
// 兼容没有分类的情况
|
|
392
|
+
if (!yapiData.cats.length) {
|
|
393
|
+
yapiData.cats = [
|
|
394
|
+
{
|
|
395
|
+
name: 'default',
|
|
396
|
+
desc: 'default',
|
|
397
|
+
},
|
|
398
|
+
];
|
|
399
|
+
yapiData.apis.forEach(function (api) {
|
|
400
|
+
api.catname = 'default';
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
currentTime = dayjs().unix();
|
|
404
|
+
project = {
|
|
405
|
+
_id: 0,
|
|
406
|
+
name: yapiData.swaggerData.info.title,
|
|
407
|
+
desc: yapiData.swaggerData.info.description || '',
|
|
408
|
+
basepath: yapiData.swaggerData.basePath || '',
|
|
409
|
+
tag: [],
|
|
410
|
+
env: [
|
|
411
|
+
{
|
|
412
|
+
name: 'local',
|
|
413
|
+
domain: "".concat(((_a = yapiData.swaggerData.schemes) === null || _a === void 0 ? void 0 : _a[0]) || 'http', "://").concat(yapiData.swaggerData.host || '127.0.0.1'),
|
|
414
|
+
},
|
|
415
|
+
],
|
|
416
|
+
};
|
|
417
|
+
cats = yapiData.cats.map(function (cat, index) { return ({
|
|
418
|
+
_id: index + 1,
|
|
419
|
+
name: cat.name,
|
|
420
|
+
desc: cat.desc,
|
|
421
|
+
add_time: currentTime,
|
|
422
|
+
up_time: currentTime,
|
|
423
|
+
}); });
|
|
424
|
+
interfaces = yapiData.apis.map(function (api, index) {
|
|
425
|
+
var _a;
|
|
426
|
+
return (__assign(__assign({}, api), { _id: index + 1, project_id: 0, catid: ((_a = cats.find(function (cat) { return cat.name === api.catname; })) === null || _a === void 0 ? void 0 : _a._id) || -1, tag: api.tag || [], add_time: currentTime, up_time: currentTime }));
|
|
427
|
+
});
|
|
428
|
+
return [2 /*return*/, { project: project, cats: cats, interfaces: interfaces }];
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
}
|