yapi-apifox-to-typescript 4.2.3 → 5.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/lib/cjs/ApifoxToYApiData.js +312 -588
- package/lib/cjs/Generator.js +513 -1171
- package/lib/cjs/SwaggerToYApiServer.js +125 -336
- package/lib/cjs/_virtual/_rolldown/runtime.js +23 -0
- package/lib/cjs/cli.js +171 -280
- package/lib/cjs/helpers.js +125 -209
- package/lib/cjs/index.js +15 -19
- package/lib/cjs/mini-ut.js +220 -0
- package/lib/cjs/swaggerJsonToYApiData.js +268 -482
- package/lib/cjs/types.js +93 -93
- package/lib/cjs/utils.js +364 -769
- package/lib/esm/ApifoxToYApiData.d.ts +49 -0
- package/lib/esm/ApifoxToYApiData.js +310 -572
- package/lib/esm/Generator.d.ts +53 -0
- package/lib/esm/Generator.js +509 -1146
- package/lib/esm/SwaggerToYApiServer.d.ts +23 -0
- package/lib/esm/SwaggerToYApiServer.js +119 -317
- package/lib/esm/_virtual/_rolldown/runtime.js +7 -0
- package/lib/esm/cli.d.ts +4 -0
- package/lib/esm/cli.js +162 -251
- package/lib/esm/helpers.d.ts +49 -0
- package/lib/esm/helpers.js +125 -198
- package/lib/esm/index.d.ts +2 -897
- package/lib/esm/index.js +3 -2
- package/lib/esm/mini-ut.d.ts +105 -0
- package/lib/esm/mini-ut.js +198 -0
- package/lib/esm/swaggerJsonToYApiData.d.ts +6 -0
- package/lib/esm/swaggerJsonToYApiData.js +265 -469
- package/lib/esm/types.d.ts +695 -0
- package/lib/esm/types.js +87 -73
- package/lib/esm/utils.d.ts +95 -0
- package/lib/esm/utils.js +350 -720
- package/package.json +49 -59
package/lib/esm/helpers.js
CHANGED
|
@@ -1,204 +1,131 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { QueryStringArrayFormat } from "./types.js";
|
|
2
|
+
//#region src/helpers.ts
|
|
4
3
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return config;
|
|
4
|
+
* 定义配置。
|
|
5
|
+
*
|
|
6
|
+
* @param config 配置
|
|
7
|
+
*/
|
|
8
|
+
function defineConfig(config, hooks) {
|
|
9
|
+
if (hooks) Object.defineProperty(config, "hooks", {
|
|
10
|
+
value: hooks,
|
|
11
|
+
configurable: false,
|
|
12
|
+
enumerable: false,
|
|
13
|
+
writable: false
|
|
14
|
+
});
|
|
15
|
+
return config;
|
|
20
16
|
}
|
|
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
|
-
var _proto = FileData.prototype;
|
|
50
|
-
|
|
51
|
-
_proto.getOriginalFileData = function getOriginalFileData() {
|
|
52
|
-
return this.originalFileData;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* 获取选项。
|
|
56
|
-
*/
|
|
57
|
-
;
|
|
58
|
-
|
|
59
|
-
_proto.getOptions = function getOptions() {
|
|
60
|
-
return this.options;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
return FileData;
|
|
64
|
-
}();
|
|
17
|
+
var FileData = class {
|
|
18
|
+
/**
|
|
19
|
+
* 文件数据辅助类,统一网页、小程序等平台的文件上传。
|
|
20
|
+
*
|
|
21
|
+
* @param originalFileData 原始文件数据
|
|
22
|
+
* @param options 若使用内部的 getFormData,则选项会被其使用
|
|
23
|
+
*/
|
|
24
|
+
constructor(originalFileData, options) {
|
|
25
|
+
this.originalFileData = originalFileData;
|
|
26
|
+
this.options = options;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 获取原始文件数据。
|
|
30
|
+
*
|
|
31
|
+
* @returns 原始文件数据
|
|
32
|
+
*/
|
|
33
|
+
getOriginalFileData() {
|
|
34
|
+
return this.originalFileData;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 获取选项。
|
|
38
|
+
*/
|
|
39
|
+
getOptions() {
|
|
40
|
+
return this.options;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
65
43
|
/**
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
result.data[key] = requestData[key];
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
} else {
|
|
89
|
-
result.data = requestData;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return result;
|
|
44
|
+
* 解析请求数据,从请求数据中分离出普通数据和文件数据。
|
|
45
|
+
*
|
|
46
|
+
* @param requestData 要解析的请求数据
|
|
47
|
+
* @returns 包含普通数据(data)和文件数据(fileData)的对象,data、fileData 为空对象时,表示没有此类数据
|
|
48
|
+
*/
|
|
49
|
+
function parseRequestData(requestData) {
|
|
50
|
+
const result = {
|
|
51
|
+
data: {},
|
|
52
|
+
fileData: {}
|
|
53
|
+
};
|
|
54
|
+
/* istanbul ignore else */
|
|
55
|
+
if (requestData != null) {
|
|
56
|
+
if (typeof requestData === "object" && !Array.isArray(requestData)) Object.keys(requestData).forEach((key) => {
|
|
57
|
+
if (requestData[key] && requestData[key] instanceof FileData) result.fileData[key] = requestData[key].getOriginalFileData();
|
|
58
|
+
else result.data[key] = requestData[key];
|
|
59
|
+
});
|
|
60
|
+
else result.data = requestData;
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
94
63
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
} else if (arrayFormat === QueryStringArrayFormat.repeat) {
|
|
107
|
-
str = value.map(function (v) {
|
|
108
|
-
return encodeURIComponent(key) + "=" + encodeURIComponent(v);
|
|
109
|
-
}).join('&');
|
|
110
|
-
} else if (arrayFormat === QueryStringArrayFormat.comma) {
|
|
111
|
-
str = encodeURIComponent(key) + "=" + encodeURIComponent(value.join(','));
|
|
112
|
-
} else if (arrayFormat === QueryStringArrayFormat.json) {
|
|
113
|
-
str = encodeURIComponent(key) + "=" + encodeURIComponent(JSON.stringify(value));
|
|
114
|
-
} else {
|
|
115
|
-
str = value.map(function (v) {
|
|
116
|
-
return encodeURIComponent(key + "[]") + "=" + encodeURIComponent(v);
|
|
117
|
-
}).join('&');
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return str;
|
|
64
|
+
var queryStringify = (key, value, arrayFormat) => {
|
|
65
|
+
let str = "";
|
|
66
|
+
if (value != null) {
|
|
67
|
+
if (!Array.isArray(value)) str = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
68
|
+
else if (arrayFormat === QueryStringArrayFormat.indices) str = value.map((v, i) => `${encodeURIComponent(`${key}[${i}]`)}=${encodeURIComponent(v)}`).join("&");
|
|
69
|
+
else if (arrayFormat === QueryStringArrayFormat.repeat) str = value.map((v) => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`).join("&");
|
|
70
|
+
else if (arrayFormat === QueryStringArrayFormat.comma) str = `${encodeURIComponent(key)}=${encodeURIComponent(value.join(","))}`;
|
|
71
|
+
else if (arrayFormat === QueryStringArrayFormat.json) str = `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`;
|
|
72
|
+
else str = value.map((v) => `${encodeURIComponent(`${key}[]`)}=${encodeURIComponent(v)}`).join("&");
|
|
73
|
+
}
|
|
74
|
+
return str;
|
|
122
75
|
};
|
|
123
76
|
/**
|
|
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
|
-
var formData = new UniFormData();
|
|
182
|
-
Object.keys(data).forEach(function (key) {
|
|
183
|
-
formData.append(key, data[key]);
|
|
184
|
-
});
|
|
185
|
-
Object.keys(fileData).forEach(function (key) {
|
|
186
|
-
var options = requestData[key].getOptions();
|
|
187
|
-
var files = Array.isArray(fileData[key]) ? fileData[key] : [fileData[key]];
|
|
188
|
-
files.forEach(function (file) {
|
|
189
|
-
formData.append(key, file, useNativeFormData ? options == null ? void 0 : options.filename : options);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
return formData;
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
return _extends({}, requestConfig, {
|
|
196
|
-
path: requestPath,
|
|
197
|
-
rawData: requestData,
|
|
198
|
-
data: data,
|
|
199
|
-
hasFileData: fileData && Object.keys(fileData).length > 0,
|
|
200
|
-
fileData: fileData,
|
|
201
|
-
allData: allData,
|
|
202
|
-
getFormData: getFormData
|
|
203
|
-
});
|
|
204
|
-
}
|
|
77
|
+
* 准备要传给请求函数的参数。
|
|
78
|
+
*/
|
|
79
|
+
function prepare(requestConfig, requestData) {
|
|
80
|
+
let requestPath = requestConfig.path;
|
|
81
|
+
const { data, fileData } = parseRequestData(requestData);
|
|
82
|
+
const dataIsObject = data != null && typeof data === "object" && !Array.isArray(data);
|
|
83
|
+
if (dataIsObject) {
|
|
84
|
+
if (requestConfig.paramNames && Array.isArray(requestConfig.paramNames) && requestConfig.paramNames.length > 0) Object.keys(data).forEach((key) => {
|
|
85
|
+
if (requestConfig.paramNames.indexOf(key) >= 0) {
|
|
86
|
+
requestPath = requestPath.replace(new RegExp(`\\{${key}\\}`, "g"), data[key]).replace(new RegExp(`/:${key}(?=/|$)`, "g"), `/${data[key]}`);
|
|
87
|
+
delete data[key];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
let queryString = "";
|
|
91
|
+
if (requestConfig.queryNames && Array.isArray(requestConfig.queryNames) && requestConfig.queryNames.length > 0) Object.keys(data).forEach((key) => {
|
|
92
|
+
if (requestConfig.queryNames.indexOf(key) >= 0) {
|
|
93
|
+
if (data[key] != null) queryString += `${queryString ? "&" : ""}${queryStringify(key, data[key], requestConfig.queryStringArrayFormat || QueryStringArrayFormat.brackets)}`;
|
|
94
|
+
delete data[key];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
if (queryString) requestPath += `${requestPath.indexOf("?") > -1 ? "&" : "?"}${queryString}`;
|
|
98
|
+
}
|
|
99
|
+
const allData = {
|
|
100
|
+
...dataIsObject ? data : {},
|
|
101
|
+
...fileData
|
|
102
|
+
};
|
|
103
|
+
const getFormData = () => {
|
|
104
|
+
const useNativeFormData = typeof FormData !== "undefined";
|
|
105
|
+
const UniFormData = useNativeFormData ? FormData : void 0;
|
|
106
|
+
if (!UniFormData) throw new Error("当前环境不支持 FormData");
|
|
107
|
+
const formData = new UniFormData();
|
|
108
|
+
Object.keys(data).forEach((key) => {
|
|
109
|
+
formData.append(key, data[key]);
|
|
110
|
+
});
|
|
111
|
+
Object.keys(fileData).forEach((key) => {
|
|
112
|
+
const options = requestData[key].getOptions();
|
|
113
|
+
(Array.isArray(fileData[key]) ? fileData[key] : [fileData[key]]).forEach((file) => {
|
|
114
|
+
formData.append(key, file, useNativeFormData ? options?.filename : options);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
return formData;
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
...requestConfig,
|
|
121
|
+
path: requestPath,
|
|
122
|
+
rawData: requestData,
|
|
123
|
+
data,
|
|
124
|
+
hasFileData: fileData && Object.keys(fileData).length > 0,
|
|
125
|
+
fileData,
|
|
126
|
+
allData,
|
|
127
|
+
getFormData
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
export { FileData, defineConfig, parseRequestData, prepare };
|