yapi-apifox-to-typescript 4.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018-present Jay Fong <fjc0kb@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # YApi to TypeScript
2
+
3
+ 本项目基于 [yapi-to-typescript v3.38.0](https://github.com/fjc0k/yapi-to-typescript)
4
+
5
+ 在此基础上补充了 Apifox 接口的转换,增加了一些基础配置项以减轻生成的代码量。
@@ -0,0 +1,299 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ exports.__esModule = true;
6
+ exports.ApifoxToYApiData = void 0;
7
+
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+
10
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _axios = _interopRequireDefault(require("axios"));
15
+
16
+ var _types = require("./types");
17
+
18
+ var ApifoxToYApiData = /*#__PURE__*/function () {
19
+ function ApifoxToYApiData(options) {
20
+ this.options = options;
21
+ this.schemas = void 0;
22
+ this.folders = void 0;
23
+ this.project = {
24
+ _id: 0,
25
+ _url: '',
26
+ name: 'ApifoxProject',
27
+ desc: '',
28
+ basepath: '',
29
+ tag: [],
30
+ env: [{
31
+ name: '',
32
+ domain: ''
33
+ }]
34
+ };
35
+ this.schemas = {};
36
+ this.folders = [];
37
+ }
38
+ /**
39
+ * http-api-tree 这个接口拿到所有的folder(category)
40
+ * data-schemas 这个接口拿到所用通用的schema
41
+ */
42
+
43
+
44
+ var _proto = ApifoxToYApiData.prototype;
45
+
46
+ _proto.getProjectInfo =
47
+ /*#__PURE__*/
48
+ function () {
49
+ var _getProjectInfo = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
50
+ var _this = this;
51
+
52
+ var folders, schemas, cats;
53
+ return _regenerator.default.wrap(function _callee$(_context) {
54
+ while (1) {
55
+ switch (_context.prev = _context.next) {
56
+ case 0:
57
+ _context.next = 2;
58
+ return _axios.default.get("https://apifox.com/api/v1/shared-docs/" + this.options.token + "/http-api-tree");
59
+
60
+ case 2:
61
+ folders = _context.sent;
62
+ _context.next = 5;
63
+ return _axios.default.get("https://apifox.com/api/v1/shared-docs/" + this.options.token + "/data-schemas");
64
+
65
+ case 5:
66
+ schemas = _context.sent;
67
+ // 初始化
68
+ // this.schemas = schemas.data.data
69
+ schemas.data.data.forEach(function (item) {
70
+ _this.schemas[item.id] = item.jsonSchema;
71
+ });
72
+ this.folders = folders.data.data;
73
+ cats = folders.data.data.map(function (item) {
74
+ return {
75
+ _id: item.folder.id,
76
+ _url: '',
77
+ name: item.name,
78
+ desc: item.key,
79
+ list: [],
80
+ add_time: 0,
81
+ up_time: 0
82
+ };
83
+ });
84
+ this.project._id = schemas.data.data[0].projectId;
85
+ return _context.abrupt("return", (0, _extends2.default)({}, this.project, {
86
+ cats: cats,
87
+ getMockUrl: function getMockUrl() {
88
+ return '';
89
+ },
90
+ getDevUrl: function getDevUrl() {
91
+ return '';
92
+ },
93
+ getProdUrl: function getProdUrl() {
94
+ return '';
95
+ }
96
+ }));
97
+
98
+ case 11:
99
+ case "end":
100
+ return _context.stop();
101
+ }
102
+ }
103
+ }, _callee, this);
104
+ }));
105
+
106
+ function getProjectInfo() {
107
+ return _getProjectInfo.apply(this, arguments);
108
+ }
109
+
110
+ return getProjectInfo;
111
+ }()
112
+ /**
113
+ * 由于yapi中基本没有处理ref的方案
114
+ * 都是直接由具体的参数值管理,所以这里需要对所有的ref进行转换
115
+ * TODO:目前这里只考虑了一个层级,如果是多个层级,需要再递归处理
116
+ */
117
+ ;
118
+
119
+ _proto.getInterfaceList =
120
+ /*#__PURE__*/
121
+ function () {
122
+ var _getInterfaceList = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(syntheticalConfig) {
123
+ var _this2 = this;
124
+
125
+ var cat, transformRefToSchema;
126
+ return _regenerator.default.wrap(function _callee3$(_context3) {
127
+ while (1) {
128
+ switch (_context3.prev = _context3.next) {
129
+ case 0:
130
+ // syntheticalConfig.id 就是catId
131
+ cat = this.folders.find(function (item) {
132
+ return item.folder.id === syntheticalConfig.id;
133
+ });
134
+
135
+ if (cat) {
136
+ _context3.next = 3;
137
+ break;
138
+ }
139
+
140
+ return _context3.abrupt("return", []);
141
+
142
+ case 3:
143
+ transformRefToSchema = function transformRefToSchema(refString) {
144
+ var refId = refString.split('/').pop();
145
+ return _this2.schemas[refId];
146
+ };
147
+
148
+ return _context3.abrupt("return", Promise.all(cat.children.map( /*#__PURE__*/function () {
149
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(item) {
150
+ var _resultData$responses, _resultData$responses2, _resultData$responses3, _resultData$responses4, _resultData$responses5, _resultData$responses6, _resultData$responses7, _resultData$requestBo, _resultData$requestBo2, _resultData$parameter, _resultData$parameter2;
151
+
152
+ var result, resultData, resbodyIndex, respref, dataProperties, _item, _dataProperties$_item, dataPropRef, reqref;
153
+
154
+ return _regenerator.default.wrap(function _callee2$(_context2) {
155
+ while (1) {
156
+ switch (_context2.prev = _context2.next) {
157
+ case 0:
158
+ _context2.next = 2;
159
+ return _axios.default.get("https://apifox.com/api/v1/shared-docs/" + _this2.options.token + "/http-apis/" + item.api.id);
160
+
161
+ case 2:
162
+ result = _context2.sent;
163
+ resultData = result.data.data; // 根据result对应的param查询制定的schema
164
+
165
+ // 根据result对应的param查询制定的schema
166
+ resbodyIndex = resultData.responses.findIndex(function (item) {
167
+ return item.code === 200;
168
+ }); // 处理response重可能存在ref
169
+
170
+ // 处理response重可能存在ref
171
+ respref = (_resultData$responses = resultData.responses[resbodyIndex].jsonSchema) == null ? void 0 : (_resultData$responses2 = _resultData$responses.properties) == null ? void 0 : (_resultData$responses3 = _resultData$responses2.data) == null ? void 0 : (_resultData$responses4 = _resultData$responses3.items) == null ? void 0 : _resultData$responses4.$ref;
172
+
173
+ if (respref) {
174
+ // @ts-ignore
175
+ resultData.responses[resbodyIndex].jsonSchema.properties.data.items = transformRefToSchema(respref);
176
+ } // 处理data中可能存在ref的问题
177
+ // 这里只考虑了一层,如果存在多层,需要递归处理
178
+
179
+
180
+ // 处理data中可能存在ref的问题
181
+ // 这里只考虑了一层,如果存在多层,需要递归处理
182
+ dataProperties = (_resultData$responses5 = resultData.responses[resbodyIndex].jsonSchema) == null ? void 0 : (_resultData$responses6 = _resultData$responses5.properties) == null ? void 0 : (_resultData$responses7 = _resultData$responses6.data) == null ? void 0 : _resultData$responses7.properties;
183
+
184
+ if (dataProperties) {
185
+ for (_item in dataProperties) {
186
+ // @ts-ignore
187
+ dataPropRef = (_dataProperties$_item = dataProperties[_item].items) == null ? void 0 : _dataProperties$_item['$ref']; // @ts-ignore
188
+
189
+ // @ts-ignore
190
+ if (dataProperties[_item].type === 'array' && dataPropRef) {
191
+ // @ts-ignore
192
+ resultData.responses[resbodyIndex].jsonSchema.properties.data.properties[_item].items = transformRefToSchema(dataPropRef);
193
+ }
194
+ }
195
+ } // 处理request中可能存在的ref
196
+
197
+
198
+ // 处理request中可能存在的ref
199
+ if ((_resultData$requestBo = resultData.requestBody.jsonSchema) != null && _resultData$requestBo['x-apifox-refs'] && (_resultData$requestBo2 = resultData.requestBody.jsonSchema) != null && _resultData$requestBo2['x-apifox-orders']) {
200
+ // @ts-ignore
201
+ reqref = resultData.requestBody.jsonSchema['x-apifox-refs'][resultData.requestBody.jsonSchema['x-apifox-orders'][0]];
202
+ resultData.requestBody.jsonSchema.properties = transformRefToSchema(reqref['$ref']).properties; // @ts-ignore
203
+
204
+ // @ts-ignore
205
+ resultData.requestBody.jsonSchema['required'] = reqref.required;
206
+ delete resultData.requestBody.jsonSchema['x-apifox-refs'];
207
+ delete resultData.requestBody.jsonSchema['x-apifox-orders'];
208
+ }
209
+
210
+ return _context2.abrupt("return", {
211
+ _id: resultData.id,
212
+ _category: {
213
+ _id: syntheticalConfig.id,
214
+ _url: '',
215
+ name: cat.name,
216
+ desc: '',
217
+ add_time: 0,
218
+ up_time: 0,
219
+ list: []
220
+ },
221
+ _project: (0, _extends2.default)({}, _this2.project),
222
+ _url: '',
223
+ title: resultData.name,
224
+ status: 'undone',
225
+ markdown: resultData.operationId,
226
+ path: resultData.path,
227
+ method: resultData.method.toUpperCase(),
228
+ project_id: resultData.projectId,
229
+ catid: syntheticalConfig.id,
230
+ tag: resultData.tags,
231
+ req_headers: resultData.requestBody.type ? [{
232
+ name: 'Content-Type',
233
+ value: resultData.requestBody.type,
234
+ desc: '',
235
+ example: '',
236
+ required: _types.Required.true
237
+ }] : [],
238
+ req_params: ((_resultData$parameter = resultData.parameters.path) == null ? void 0 : _resultData$parameter.map(function (item) {
239
+ return {
240
+ name: item.name,
241
+ desc: item.description,
242
+ example: '',
243
+ required: item.required ? _types.Required.true : _types.Required.false,
244
+ type: item.type === 'string' ? _types.RequestParamType.string : _types.RequestParamType.number
245
+ };
246
+ })) || [],
247
+ req_query: ((_resultData$parameter2 = resultData.parameters.query) == null ? void 0 : _resultData$parameter2.map(function (item) {
248
+ return {
249
+ name: item.name,
250
+ desc: item.description,
251
+ example: '',
252
+ required: item.required ? _types.Required.true : _types.Required.false,
253
+ type: item.type === 'string' ? _types.RequestQueryType.string : _types.RequestQueryType.number
254
+ };
255
+ })) || [],
256
+ req_body_type: resultData.requestBody.jsonSchema ? _types.RequestBodyType.json : _types.RequestBodyType.raw,
257
+ req_body_is_json_schema: !!resultData.requestBody.jsonSchema,
258
+ req_body_form: [],
259
+ req_body_other: resultData.requestBody.jsonSchema ? JSON.stringify(resultData.requestBody.jsonSchema) : '',
260
+ res_body_type: _types.ResponseBodyType.json,
261
+ res_body_is_json_schema: true,
262
+ res_body: JSON.stringify(resultData.responses[resbodyIndex].jsonSchema),
263
+ add_time: Math.floor(new Date(resultData.createdAt).getTime() / 1000),
264
+ up_time: Math.floor(new Date(resultData.updatedAt).getTime() / 1000),
265
+ uid: 0
266
+ });
267
+
268
+ case 11:
269
+ case "end":
270
+ return _context2.stop();
271
+ }
272
+ }
273
+ }, _callee2);
274
+ }));
275
+
276
+ return function (_x2) {
277
+ return _ref.apply(this, arguments);
278
+ };
279
+ }())));
280
+
281
+ case 5:
282
+ case "end":
283
+ return _context3.stop();
284
+ }
285
+ }
286
+ }, _callee3, this);
287
+ }));
288
+
289
+ function getInterfaceList(_x) {
290
+ return _getInterfaceList.apply(this, arguments);
291
+ }
292
+
293
+ return getInterfaceList;
294
+ }();
295
+
296
+ return ApifoxToYApiData;
297
+ }();
298
+
299
+ exports.ApifoxToYApiData = ApifoxToYApiData;