openapi-ts-request 1.7.3 → 1.8.1
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/README.md +2 -0
- package/dist/bin/openapi.js +4 -1
- package/dist/generator/serviceGenarator.d.ts +1 -0
- package/dist/generator/serviceGenarator.js +42 -24
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -4
- package/package.json +1 -1
- package/templates/reactQuery.njk +1 -1
- package/templates/serviceController.njk +1 -1
package/README.md
CHANGED
|
@@ -194,6 +194,7 @@ $ openapi --help
|
|
|
194
194
|
-f, --full <boolean> full replacement (default: true)
|
|
195
195
|
--enableLogging <boolean> open the log (default: false)
|
|
196
196
|
--priorityRule <string> priority rule, include/exclude/both (default: "include")
|
|
197
|
+
--filterCaseInsensitive <boolean> whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters
|
|
197
198
|
--includeTags <(string|RegExp)[]> generate code from include tags
|
|
198
199
|
--includePaths <(string|RegExp)[]> generate code from include paths
|
|
199
200
|
--excludeTags <(string|RegExp)[]> generate code from exclude tags
|
|
@@ -232,6 +233,7 @@ openapi --i ./spec.json --o ./apis
|
|
|
232
233
|
| full | 否 | boolean | true | 是否全量替换 |
|
|
233
234
|
| enableLogging | 否 | boolean | false | 是否开启日志 |
|
|
234
235
|
| priorityRule | 否 | string | 'include' | 模式规则,可选include/exclude/both |
|
|
236
|
+
| filterCaseInsensitive | 否 | boolean | false | 执行 includeTags、includePaths、excludeTags、excludePaths 过滤时是否忽略大小写 |
|
|
235
237
|
| includeTags | 否 | (string\|RegExp)[] | - | 根据指定的 tags 生成代码, priorityRule=include则必填 |
|
|
236
238
|
| includePaths | 否 | (string\|RegExp)[] | - | 根据指定的 paths 生成代码 |
|
|
237
239
|
| excludeTags | 否 | (string\|RegExp)[] | - | 根据指定的 tags 不生成代码 |
|
package/dist/bin/openapi.js
CHANGED
|
@@ -21,7 +21,9 @@ const params = commander_1.program
|
|
|
21
21
|
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch" (default: "axios")')
|
|
22
22
|
.option('-f, --full <boolean>', 'full replacement', true)
|
|
23
23
|
.option('--enableLogging <boolean>', 'open the log', false)
|
|
24
|
-
.option('--
|
|
24
|
+
.option('--filterCaseInsensitive <boolean>', 'whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters', false)
|
|
25
|
+
.option('--includeTags <(string|RegExp)[]>', 'generate code from include tags')
|
|
26
|
+
.option('--filterCaseInsensitive <boolean>', 'whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters', false)
|
|
25
27
|
.option('--includeTags <(string|RegExp)[]>', 'generate code from include tags')
|
|
26
28
|
.option('--includePaths <(string|RegExp)[]>', 'generate code from include paths')
|
|
27
29
|
.option('--excludeTags <(string|RegExp)[]>', 'generate code from exclude tags')
|
|
@@ -60,6 +62,7 @@ const baseGenerate = (_params_) => {
|
|
|
60
62
|
full: JSON.parse(_params_.full) === true,
|
|
61
63
|
enableLogging: JSON.parse(_params_.enableLogging) === true,
|
|
62
64
|
priorityRule: _params_.priorityRule,
|
|
65
|
+
filterCaseInsensitive: JSON.parse(_params_.filterCaseInsensitive) === true,
|
|
63
66
|
includeTags: _params_.includeTags,
|
|
64
67
|
includePaths: _params_.includePaths,
|
|
65
68
|
excludeTags: _params_.excludeTags,
|
|
@@ -91,19 +91,18 @@ class ServiceGenerator {
|
|
|
91
91
|
if (!tag) {
|
|
92
92
|
return;
|
|
93
93
|
}
|
|
94
|
-
const tagLowerCase = tag.toLowerCase();
|
|
95
94
|
if (priorityRule === config_1.PriorityRule.include) {
|
|
96
95
|
// includeTags 为空,不会匹配任何path,故跳过
|
|
97
96
|
if ((0, lodash_1.isEmpty)(includeTags)) {
|
|
98
97
|
this.log('priorityRule include need includeTags or includePaths');
|
|
99
98
|
return;
|
|
100
99
|
}
|
|
101
|
-
if (!this.validateRegexp(
|
|
100
|
+
if (!this.validateRegexp(tag, includeTags)) {
|
|
102
101
|
return;
|
|
103
102
|
}
|
|
104
103
|
}
|
|
105
104
|
if (priorityRule === config_1.PriorityRule.exclude) {
|
|
106
|
-
if (this.validateRegexp(
|
|
105
|
+
if (this.validateRegexp(tag, excludeTags)) {
|
|
107
106
|
return;
|
|
108
107
|
}
|
|
109
108
|
}
|
|
@@ -113,10 +112,8 @@ class ServiceGenerator {
|
|
|
113
112
|
this.log('priorityRule both need includeTags or includePaths');
|
|
114
113
|
return;
|
|
115
114
|
}
|
|
116
|
-
const outIncludeTags = !(0, lodash_1.isEmpty)(includeTags) &&
|
|
117
|
-
|
|
118
|
-
const inExcludeTags = !(0, lodash_1.isEmpty)(excludeTags) &&
|
|
119
|
-
this.validateRegexp(tagLowerCase, excludeTags);
|
|
115
|
+
const outIncludeTags = !(0, lodash_1.isEmpty)(includeTags) && !this.validateRegexp(tag, includeTags);
|
|
116
|
+
const inExcludeTags = !(0, lodash_1.isEmpty)(excludeTags) && this.validateRegexp(tag, excludeTags);
|
|
120
117
|
if (outIncludeTags || inExcludeTags) {
|
|
121
118
|
return;
|
|
122
119
|
}
|
|
@@ -276,33 +273,42 @@ class ServiceGenerator {
|
|
|
276
273
|
(0, lodash_1.isEmpty)(includePaths)) {
|
|
277
274
|
return;
|
|
278
275
|
}
|
|
279
|
-
const flag = this.validateRegexp((0, lodash_1.filter)(
|
|
276
|
+
const flag = this.validateRegexp((0, lodash_1.filter)(tags, (tag) => !!tag), includeTags);
|
|
280
277
|
const pathFlag = this.validateRegexp(pathKey, includePaths);
|
|
281
278
|
if (!flag || !pathFlag) {
|
|
282
279
|
return;
|
|
283
280
|
}
|
|
284
281
|
// 筛选出 pathItem 包含的 $ref 对应的schema
|
|
285
282
|
(0, util_2.markAllowedSchema)(JSON.stringify(pathItem), this.openAPIData);
|
|
286
|
-
operationObject.parameters = (_b = operationObject.parameters) === null || _b === void 0 ? void 0 : _b.filter((item) =>
|
|
283
|
+
operationObject.parameters = (_b = operationObject.parameters) === null || _b === void 0 ? void 0 : _b.filter((item) => {
|
|
284
|
+
const parameter = this.resolveParameterRef(item);
|
|
285
|
+
return (parameter === null || parameter === void 0 ? void 0 : parameter.in) !== `${config_2.parametersInsEnum.header}`;
|
|
286
|
+
});
|
|
287
287
|
const props = [];
|
|
288
|
-
(_c = operationObject.parameters) === null || _c === void 0 ? void 0 : _c.forEach((
|
|
288
|
+
(_c = operationObject.parameters) === null || _c === void 0 ? void 0 : _c.forEach((param) => {
|
|
289
289
|
var _a;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
290
|
+
const parameter = this.resolveParameterRef(param);
|
|
291
|
+
if (parameter) {
|
|
292
|
+
props.push({
|
|
293
|
+
name: parameter.name,
|
|
294
|
+
desc: ((_a = parameter.description) !== null && _a !== void 0 ? _a : '').replace(config_2.lineBreakReg, ''),
|
|
295
|
+
required: parameter.required || false,
|
|
296
|
+
type: this.getType(parameter.schema),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
296
299
|
});
|
|
297
300
|
// parameters may be in path
|
|
298
|
-
(_d = pathItem.parameters) === null || _d === void 0 ? void 0 : _d.forEach((
|
|
301
|
+
(_d = pathItem.parameters) === null || _d === void 0 ? void 0 : _d.forEach((param) => {
|
|
299
302
|
var _a;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
303
|
+
const parameter = this.resolveParameterRef(param);
|
|
304
|
+
if (parameter) {
|
|
305
|
+
props.push({
|
|
306
|
+
name: parameter.name,
|
|
307
|
+
desc: ((_a = parameter.description) !== null && _a !== void 0 ? _a : '').replace(config_2.lineBreakReg, ''),
|
|
308
|
+
required: parameter.required,
|
|
309
|
+
type: this.getType(parameter.schema),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
306
312
|
});
|
|
307
313
|
const typeName = this.getFunctionParamsTypeName(Object.assign(Object.assign({}, operationObject), { method, path: pathKey }));
|
|
308
314
|
if (props.length > 0 && typeName) {
|
|
@@ -1000,6 +1006,16 @@ class ServiceGenerator {
|
|
|
1000
1006
|
: false });
|
|
1001
1007
|
});
|
|
1002
1008
|
}
|
|
1009
|
+
resolveParameterRef(param) {
|
|
1010
|
+
var _a, _b;
|
|
1011
|
+
if (!(0, util_2.isReferenceObject)(param)) {
|
|
1012
|
+
return param;
|
|
1013
|
+
}
|
|
1014
|
+
// 解析 $ref 引用,从 components.parameters 中获取实际定义
|
|
1015
|
+
const refName = (0, util_2.getLastRefName)(param.$ref);
|
|
1016
|
+
const parameter = (_b = (_a = this.openAPIData.components) === null || _a === void 0 ? void 0 : _a.parameters) === null || _b === void 0 ? void 0 : _b[refName];
|
|
1017
|
+
return parameter || null;
|
|
1018
|
+
}
|
|
1003
1019
|
resolveRefObject(refObject) {
|
|
1004
1020
|
if (!(0, util_2.isReferenceObject)(refObject)) {
|
|
1005
1021
|
return refObject;
|
|
@@ -1061,7 +1077,9 @@ class ServiceGenerator {
|
|
|
1061
1077
|
// 提取匹配逻辑到单独的函数
|
|
1062
1078
|
matches(item, reg) {
|
|
1063
1079
|
if (typeof reg === 'string') {
|
|
1064
|
-
return (0, minimatch_1.minimatch)(item, reg
|
|
1080
|
+
return (0, minimatch_1.minimatch)(item, reg, {
|
|
1081
|
+
nocase: this.config.filterCaseInsensitive,
|
|
1082
|
+
});
|
|
1065
1083
|
}
|
|
1066
1084
|
else if (reg instanceof RegExp) {
|
|
1067
1085
|
reg.lastIndex = 0; // 重置正则表达式的 lastIndex 属性
|
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,10 @@ export type GenerateServiceProps = {
|
|
|
32
32
|
* 优先规则, include(只允许include列表) | exclude(只排除exclude列表) | both(允许include列表,排除exclude列表)
|
|
33
33
|
*/
|
|
34
34
|
priorityRule?: IPriorityRule;
|
|
35
|
+
/**
|
|
36
|
+
* includeTags、includePaths、excludeTags、excludePaths 过滤器执行时是否忽略大小写
|
|
37
|
+
*/
|
|
38
|
+
filterCaseInsensitive?: boolean;
|
|
35
39
|
/**
|
|
36
40
|
* 只解析归属于 tags 集合的 api 和 schema
|
|
37
41
|
*/
|
package/dist/index.js
CHANGED
|
@@ -34,7 +34,7 @@ function generateService(_a) {
|
|
|
34
34
|
}
|
|
35
35
|
const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
|
|
36
36
|
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, enableLogging: false, priorityRule, includeTags: includeTags
|
|
37
|
-
?
|
|
37
|
+
? includeTags
|
|
38
38
|
: priorityRule === config_1.PriorityRule.include ||
|
|
39
39
|
priorityRule === config_1.PriorityRule.both
|
|
40
40
|
? [/.*/g]
|
|
@@ -43,9 +43,7 @@ function generateService(_a) {
|
|
|
43
43
|
: priorityRule === config_1.PriorityRule.include ||
|
|
44
44
|
priorityRule === config_1.PriorityRule.both
|
|
45
45
|
? [/.*/g]
|
|
46
|
-
: null, excludeTags: excludeTags
|
|
47
|
-
? (0, lodash_1.map)(excludeTags, (item) => (0, lodash_1.isString)(item) ? item.toLowerCase() : item)
|
|
48
|
-
: null, excludePaths: excludePaths ? excludePaths : null, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', isGenReactQuery: false, reactQueryMode, isGenJavaScript: false, isDisplayTypeLabel: false, isGenJsonSchemas: false, nullable: false, isOnlyGenTypeScriptType: false, isCamelCase: true, isSupportParseEnumDesc: false, full: true }, rest), openAPI);
|
|
46
|
+
: null, excludeTags: excludeTags ? excludeTags : null, excludePaths: excludePaths ? excludePaths : null, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', isGenReactQuery: false, reactQueryMode, isGenJavaScript: false, isDisplayTypeLabel: false, isGenJsonSchemas: false, nullable: false, isOnlyGenTypeScriptType: false, isCamelCase: true, isSupportParseEnumDesc: false, full: true }, rest), openAPI);
|
|
49
47
|
serviceGenerator.genFile();
|
|
50
48
|
if (mockFolder) {
|
|
51
49
|
(0, mockGenarator_1.mockGenerator)({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18.0.0",
|
package/templates/reactQuery.njk
CHANGED
|
@@ -60,7 +60,7 @@ import * as apis from './{{ className }}';
|
|
|
60
60
|
{%- endif -%}
|
|
61
61
|
) {
|
|
62
62
|
return queryOptions({
|
|
63
|
-
queryFn:
|
|
63
|
+
queryFn: ({ queryKey }) => {
|
|
64
64
|
return apis.{{ api.functionName }}(queryKey[1]{{ " as typeof options" if genType === "ts" }});
|
|
65
65
|
},
|
|
66
66
|
queryKey: ['{{ api.functionName }}', options],
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
{{ api.data }}
|
|
13
13
|
{% else %}
|
|
14
14
|
/** {{ api.desc if api.desc else '此处后端没有提供注释' }} {{ api.method | upper }} {{ api.pathInComment | safe }}{{ ' ' if api.apifoxRunLink else '' }}{{ api.apifoxRunLink }} */
|
|
15
|
-
export
|
|
15
|
+
export function {{ api.functionName }}({
|
|
16
16
|
{%- if api.params and api.hasParams %}
|
|
17
17
|
params
|
|
18
18
|
{%- if api.hasParams -%}
|