yapi-to-typescript2 1.0.0 → 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.
@@ -1,230 +1,206 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
- exports.__esModule = true;
6
- exports.FileData = void 0;
7
- exports.defineConfig = defineConfig;
8
- exports.parseRequestData = parseRequestData;
9
- exports.prepare = prepare;
10
-
11
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
12
-
13
- var _types = require("./types");
14
-
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.prepare = exports.parseRequestData = exports.FileData = exports.defineConfig = void 0;
15
+ var types_1 = require("./types");
15
16
  /**
16
17
  * 定义配置。
17
18
  *
18
19
  * @param config 配置
19
20
  */
20
21
  function defineConfig(config, hooks) {
21
- if (hooks) {
22
- Object.defineProperty(config, 'hooks', {
23
- value: hooks,
24
- configurable: false,
25
- enumerable: false,
26
- writable: false
27
- });
28
- }
29
-
30
- return config;
22
+ if (hooks) {
23
+ Object.defineProperty(config, 'hooks', {
24
+ value: hooks,
25
+ configurable: false,
26
+ enumerable: false,
27
+ writable: false,
28
+ });
29
+ }
30
+ return config;
31
31
  }
32
-
33
- var FileData = /*#__PURE__*/function () {
34
- /**
35
- * 原始文件数据。
36
- */
37
-
38
- /**
39
- * 选项。
40
- */
41
-
42
- /**
43
- * 文件数据辅助类,统一网页、小程序等平台的文件上传。
44
- *
45
- * @param originalFileData 原始文件数据
46
- * @param options 若使用内部的 getFormData,则选项会被其使用
47
- */
48
- function FileData(originalFileData, options) {
49
- this.originalFileData = void 0;
50
- this.options = void 0;
51
- this.originalFileData = originalFileData;
52
- this.options = options;
53
- }
54
- /**
55
- * 获取原始文件数据。
56
- *
57
- * @returns 原始文件数据
58
- */
59
-
60
-
61
- var _proto = FileData.prototype;
62
-
63
- _proto.getOriginalFileData = function getOriginalFileData() {
64
- return this.originalFileData;
65
- }
66
- /**
67
- * 获取选项。
68
- */
69
- ;
70
-
71
- _proto.getOptions = function getOptions() {
72
- return this.options;
73
- };
74
-
75
- return FileData;
76
- }();
32
+ exports.defineConfig = defineConfig;
33
+ var FileData = /** @class */ (function () {
34
+ /**
35
+ * 文件数据辅助类,统一网页、小程序等平台的文件上传。
36
+ *
37
+ * @param originalFileData 原始文件数据
38
+ * @param options 若使用内部的 getFormData,则选项会被其使用
39
+ */
40
+ function FileData(originalFileData, options) {
41
+ this.originalFileData = originalFileData;
42
+ this.options = options;
43
+ }
44
+ /**
45
+ * 获取原始文件数据。
46
+ *
47
+ * @returns 原始文件数据
48
+ */
49
+ FileData.prototype.getOriginalFileData = function () {
50
+ return this.originalFileData;
51
+ };
52
+ /**
53
+ * 获取选项。
54
+ */
55
+ FileData.prototype.getOptions = function () {
56
+ return this.options;
57
+ };
58
+ return FileData;
59
+ }());
60
+ exports.FileData = FileData;
77
61
  /**
78
62
  * 解析请求数据,从请求数据中分离出普通数据和文件数据。
79
63
  *
80
64
  * @param requestData 要解析的请求数据
81
65
  * @returns 包含普通数据(data)和文件数据(fileData)的对象,data、fileData 为空对象时,表示没有此类数据
82
66
  */
83
-
84
-
85
- exports.FileData = FileData;
86
-
87
67
  function parseRequestData(requestData) {
88
- var result = {
89
- data: {},
90
- fileData: {}
91
- };
92
- /* istanbul ignore else */
93
-
94
- if (requestData != null) {
95
- if (typeof requestData === 'object' && !Array.isArray(requestData)) {
96
- Object.keys(requestData).forEach(function (key) {
97
- if (requestData[key] && requestData[key] instanceof FileData) {
98
- result.fileData[key] = requestData[key].getOriginalFileData();
99
- } else {
100
- result.data[key] = requestData[key];
68
+ var result = {
69
+ data: {},
70
+ fileData: {},
71
+ };
72
+ /* istanbul ignore else */
73
+ if (requestData != null) {
74
+ if (typeof requestData === 'object' && !Array.isArray(requestData)) {
75
+ Object.keys(requestData).forEach(function (key) {
76
+ if (requestData[key] && requestData[key] instanceof FileData) {
77
+ result.fileData[key] = requestData[key].getOriginalFileData();
78
+ }
79
+ else {
80
+ result.data[key] = requestData[key];
81
+ }
82
+ });
83
+ }
84
+ else {
85
+ result.data = requestData;
101
86
  }
102
- });
103
- } else {
104
- result.data = requestData;
105
87
  }
106
- }
107
-
108
- return result;
88
+ return result;
109
89
  }
110
-
111
- var queryStringify = function queryStringify(key, value, arrayFormat) {
112
- var str = '';
113
-
114
- if (value != null) {
115
- if (!Array.isArray(value)) {
116
- str = encodeURIComponent(key) + "=" + encodeURIComponent(value);
117
- } else if (arrayFormat === _types.QueryStringArrayFormat.indices) {
118
- str = value.map(function (v, i) {
119
- return encodeURIComponent(key + "[" + i + "]") + "=" + encodeURIComponent(v);
120
- }).join('&');
121
- } else if (arrayFormat === _types.QueryStringArrayFormat.repeat) {
122
- str = value.map(function (v) {
123
- return encodeURIComponent(key) + "=" + encodeURIComponent(v);
124
- }).join('&');
125
- } else if (arrayFormat === _types.QueryStringArrayFormat.comma) {
126
- str = encodeURIComponent(key) + "=" + encodeURIComponent(value.join(','));
127
- } else if (arrayFormat === _types.QueryStringArrayFormat.json) {
128
- str = encodeURIComponent(key) + "=" + encodeURIComponent(JSON.stringify(value));
129
- } else {
130
- str = value.map(function (v) {
131
- return encodeURIComponent(key + "[]") + "=" + encodeURIComponent(v);
132
- }).join('&');
90
+ exports.parseRequestData = parseRequestData;
91
+ var queryStringify = function (key, value, arrayFormat) {
92
+ var str = '';
93
+ if (value != null) {
94
+ if (!Array.isArray(value)) {
95
+ str = "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
96
+ }
97
+ else if (arrayFormat === types_1.QueryStringArrayFormat.indices) {
98
+ str = value
99
+ .map(function (v, i) {
100
+ return "".concat(encodeURIComponent("".concat(key, "[").concat(i, "]")), "=").concat(encodeURIComponent(v));
101
+ })
102
+ .join('&');
103
+ }
104
+ else if (arrayFormat === types_1.QueryStringArrayFormat.repeat) {
105
+ str = value
106
+ .map(function (v) { return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(v)); })
107
+ .join('&');
108
+ }
109
+ else if (arrayFormat === types_1.QueryStringArrayFormat.comma) {
110
+ str = "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value.join(',')));
111
+ }
112
+ else if (arrayFormat === types_1.QueryStringArrayFormat.json) {
113
+ str = "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(JSON.stringify(value)));
114
+ }
115
+ else {
116
+ str = value
117
+ .map(function (v) { return "".concat(encodeURIComponent("".concat(key, "[]")), "=").concat(encodeURIComponent(v)); })
118
+ .join('&');
119
+ }
133
120
  }
134
- }
135
-
136
- return str;
121
+ return str;
137
122
  };
138
123
  /**
139
124
  * 准备要传给请求函数的参数。
140
125
  */
141
-
142
-
143
126
  function prepare(requestConfig, requestData) {
144
- var requestPath = requestConfig.path;
145
-
146
- var _parseRequestData = parseRequestData(requestData),
147
- data = _parseRequestData.data,
148
- fileData = _parseRequestData.fileData;
149
-
150
- var dataIsObject = data != null && typeof data === 'object' && !Array.isArray(data);
151
-
152
- if (dataIsObject) {
153
- // 替换路径参数
154
- if (Array.isArray(requestConfig.paramNames) && requestConfig.paramNames.length > 0) {
155
- Object.keys(data).forEach(function (key) {
156
- if (requestConfig.paramNames.indexOf(key) >= 0) {
157
- // ref: https://github.com/YMFE/yapi/blob/master/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js#L465
158
- requestPath = requestPath.replace(new RegExp("\\{" + key + "\\}", 'g'), data[key]).replace(new RegExp("/:" + key + "(?=/|$)", 'g'), "/" + data[key]);
159
- delete data[key];
127
+ var requestPath = requestConfig.path;
128
+ var _a = parseRequestData(requestData), data = _a.data, fileData = _a.fileData;
129
+ var dataIsObject = data != null && typeof data === 'object' && !Array.isArray(data);
130
+ if (dataIsObject) {
131
+ // 替换路径参数
132
+ if (Array.isArray(requestConfig.paramNames) &&
133
+ requestConfig.paramNames.length > 0) {
134
+ Object.keys(data).forEach(function (key) {
135
+ if (requestConfig.paramNames.indexOf(key) >= 0) {
136
+ // ref: https://github.com/YMFE/yapi/blob/master/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js#L465
137
+ requestPath = requestPath
138
+ .replace(new RegExp("\\{".concat(key, "\\}"), 'g'), data[key])
139
+ .replace(new RegExp("/:".concat(key, "(?=/|$)"), 'g'), "/".concat(data[key]));
140
+ delete data[key];
141
+ }
142
+ });
160
143
  }
161
- });
162
- } // 追加查询参数到路径上
163
-
164
-
165
- var queryString = '';
166
-
167
- if (Array.isArray(requestConfig.queryNames) && requestConfig.queryNames.length > 0) {
168
- Object.keys(data).forEach(function (key) {
169
- if (requestConfig.queryNames.indexOf(key) >= 0) {
170
- if (data[key] != null) {
171
- queryString += "" + (queryString ? '&' : '') + queryStringify(key, data[key], requestConfig.queryStringArrayFormat);
172
- }
173
-
174
- delete data[key];
144
+ // 追加查询参数到路径上
145
+ var queryString_1 = '';
146
+ if (Array.isArray(requestConfig.queryNames) &&
147
+ requestConfig.queryNames.length > 0) {
148
+ Object.keys(data).forEach(function (key) {
149
+ if (requestConfig.queryNames.indexOf(key) >= 0) {
150
+ if (data[key] != null) {
151
+ queryString_1 += "".concat(queryString_1 ? '&' : '').concat(queryStringify(key, data[key], requestConfig.queryStringArrayFormat));
152
+ }
153
+ delete data[key];
154
+ }
155
+ });
156
+ }
157
+ if (queryString_1) {
158
+ requestPath += "".concat(requestPath.indexOf('?') > -1 ? '&' : '?').concat(queryString_1);
175
159
  }
176
- });
177
- }
178
-
179
- if (queryString) {
180
- requestPath += "" + (requestPath.indexOf('?') > -1 ? '&' : '?') + queryString;
181
- }
182
- } // 全部数据
183
-
184
-
185
- var allData = (0, _extends2.default)({}, dataIsObject ? data : {}, fileData); // 获取表单数据
186
-
187
- var getFormData = function getFormData() {
188
- var useNativeFormData = typeof FormData !== 'undefined';
189
- var useNodeFormData = !useNativeFormData && // https://github.com/fjc0k/vtils/blob/master/src/utils/inNodeJS.ts
190
- typeof global === 'object' && typeof global['process'] === 'object' && typeof global['process']['versions'] === 'object' && global['process']['versions']['node'] != null;
191
- var UniFormData = useNativeFormData ? FormData : undefined;
192
-
193
- if (useNodeFormData) {
194
- try {
195
- // Dynamic require to prevent bundlers from including form-data in browser builds
196
- // @ts-ignore
197
- // eslint-disable-next-line
198
- UniFormData = require('form-data');
199
- } catch (e) {// form-data module not available
200
- }
201
- }
202
-
203
- if (!UniFormData) {
204
- throw new Error('当前环境不支持 FormData');
205
160
  }
206
-
207
- var formData = new UniFormData();
208
- Object.keys(data).forEach(function (key) {
209
- formData.append(key, data[key]);
210
- });
211
- Object.keys(fileData).forEach(function (key) {
212
- var options = requestData[key].getOptions();
213
- var files = Array.isArray(fileData[key]) ? fileData[key] : [fileData[key]];
214
- files.forEach(function (file) {
215
- formData.append(key, file, useNativeFormData ? options == null ? void 0 : options.filename : options);
216
- });
217
- });
218
- return formData;
219
- };
220
-
221
- return (0, _extends2.default)({}, requestConfig, {
222
- path: requestPath,
223
- rawData: requestData,
224
- data: data,
225
- hasFileData: fileData && Object.keys(fileData).length > 0,
226
- fileData: fileData,
227
- allData: allData,
228
- getFormData: getFormData
229
- });
230
- }
161
+ // 全部数据
162
+ var allData = __assign(__assign({}, (dataIsObject ? data : {})), fileData);
163
+ // 获取表单数据
164
+ var getFormData = function () {
165
+ var useNativeFormData = typeof FormData !== 'undefined';
166
+ var useNodeFormData = !useNativeFormData &&
167
+ // https://github.com/fjc0k/vtils/blob/master/src/utils/inNodeJS.ts
168
+ typeof global === 'object' &&
169
+ typeof global['process'] === 'object' &&
170
+ typeof global['process']['versions'] === 'object' &&
171
+ global['process']['versions']['node'] != null;
172
+ var UniFormData = useNativeFormData
173
+ ? FormData
174
+ : undefined;
175
+ if (useNodeFormData) {
176
+ try {
177
+ // Dynamic require to prevent bundlers from including form-data in browser builds
178
+ // @ts-ignore
179
+ // eslint-disable-next-line
180
+ UniFormData = require('form-data');
181
+ }
182
+ catch (e) {
183
+ // form-data module not available
184
+ }
185
+ }
186
+ if (!UniFormData) {
187
+ throw new Error('当前环境不支持 FormData');
188
+ }
189
+ var formData = new UniFormData();
190
+ Object.keys(data).forEach(function (key) {
191
+ formData.append(key, data[key]);
192
+ });
193
+ Object.keys(fileData).forEach(function (key) {
194
+ var options = requestData[key].getOptions();
195
+ var files = Array.isArray(fileData[key])
196
+ ? fileData[key]
197
+ : [fileData[key]];
198
+ files.forEach(function (file) {
199
+ formData.append(key, file, useNativeFormData ? options === null || options === void 0 ? void 0 : options.filename : options);
200
+ });
201
+ });
202
+ return formData;
203
+ };
204
+ return __assign(__assign({}, requestConfig), { path: requestPath, rawData: requestData, data: data, hasFileData: fileData && Object.keys(fileData).length > 0, fileData: fileData, allData: allData, getFormData: getFormData });
205
+ }
206
+ exports.prepare = prepare;
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './helpers';
package/lib/cjs/index.js CHANGED
@@ -1,19 +1,18 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
-
5
- var _types = require("./types");
6
-
7
- Object.keys(_types).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _types[key]) return;
10
- exports[key] = _types[key];
11
- });
12
-
13
- var _helpers = require("./helpers");
14
-
15
- Object.keys(_helpers).forEach(function (key) {
16
- if (key === "default" || key === "__esModule") return;
17
- if (key in exports && exports[key] === _helpers[key]) return;
18
- exports[key] = _helpers[key];
19
- });
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./helpers"), exports);
@@ -0,0 +1,6 @@
1
+ import { Category, Interface, Project } from './types';
2
+ export declare function swaggerJsonToYApiData(data: any): Promise<{
3
+ project: Project;
4
+ cats: Category[];
5
+ interfaces: Interface[];
6
+ }>;