Skip to content

Commit 7a0ed07

Browse files
fix: fix apifox selectedTags #459
1 parent 53d31d5 commit 7a0ed07

File tree

5 files changed

+19
-42
lines changed

5 files changed

+19
-42
lines changed

.changeset/fluffy-ends-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
fix: fix apifox selectedTags #459

README-en_US.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ openapi -i ./spec.json -o ./apis
272272
| apifoxToken | string | [get](https://docs.apifox.com/doc-5723694) | true |
273273
| local | string | language(default:zh-CN) | false |
274274
| apifoxVersion | string | default: 2024-03-28, [current apifox version](https://api.apifox.com/v1/versions) | false |
275-
| includeTags | \* or string[] | default: \* | false |
276-
| excludeTags | string[] | default: [] | false |
275+
| selectedTags | \* or string[] | default: \* | false |
276+
| excludedByTags | string[] | default: [] | false |
277277
| oasVersion | string | specify the version of the OpenAPI specification used for export, can have values such as "2.0", "3.0" or "3.1" | '3.0' |
278278
| exportFormat | string | specify the format of the exported OpenAPI file, can have values such as 'JSON' or 'YAML' | 'JSON' |
279279
| includeApifoxExtensionProperties | boolean | specify whether to include the OpenAPI specification extension fields `x-apifox` | false |

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ openapi --i ./spec.json --o ./apis
274274
| apifoxToken | string | [获取](https://docs.apifox.com/doc-5723694) | true |
275275
| local | string | 语言(默认: zh-CN) | false |
276276
| apifoxVersion | string | 默认: 2024-03-28, [获取当前版本](https://api.apifox.com/v1/versions) | false |
277-
| includeTags | \* 或 string[] | 默认: \* | false |
278-
| excludeTags | string[] | 默认: [] | false |
277+
| selectedTags | \* 或 string[] | 默认: \* | false |
278+
| excludedByTags | string[] | 默认: [] | false |
279279
| oasVersion | string | 指定用于导出的 OpenAPI 规范的版本,可以有值如 "2.0"、"3.0"、"3.1" | '3.0' |
280280
| exportFormat | string | 指定导出的 OpenAPI 文件的格式,可以有值如 'JSON' 或 'YAML' | 'JSON' |
281281
| includeApifoxExtensionProperties | boolean | 指定是否包含 Apifox 的 OpenAPI 规范扩展字段 `x-apifox` | false |

src/type.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export type ReadConfigOptions = MutuallyExclusiveWithFallback<
122122
export interface APIFoxBody {
123123
scope?: {
124124
type?: 'ALL' | 'SELECTED_TAGS';
125-
includeTags?: string[];
126-
excludeTags?: string[];
125+
selectedTags?: string[];
126+
excludedByTags?: string[];
127127
};
128128
options?: {
129129
includeApifoxExtensionProperties?: boolean;
@@ -144,6 +144,6 @@ export interface GetSchemaByApifoxProps
144144
apifoxToken: string;
145145
locale?: string;
146146
apifoxVersion?: string;
147-
includeTags?: (string | RegExp)[];
148-
excludeTags?: string[];
147+
selectedTags?: string[];
148+
excludedByTags?: string[];
149149
}

src/util.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as yaml from 'js-yaml';
66
import {
77
camelCase as _camelCase_,
88
forEach,
9+
isEmpty,
910
isObject,
1011
keys,
1112
map,
@@ -32,35 +33,6 @@ export const getImportStatement = (requestLibPath: string) => {
3233
return `import { request } from 'axios';`;
3334
};
3435

35-
const getApifoxIncludeTags = (tags?: (string | RegExp)[]): '*' | string[] => {
36-
let _tags_: string | string[] = '*';
37-
if (tags && Array.isArray(tags)) {
38-
if (!tags.length) {
39-
return '*';
40-
}
41-
42-
_tags_ = [];
43-
for (const tag of tags) {
44-
if (typeof tag === 'string') {
45-
if (tag === '*') {
46-
_tags_ = '*';
47-
break;
48-
}
49-
} else if (tag instanceof RegExp) {
50-
_tags_ = '*';
51-
break;
52-
// TODO:后期添加支持判断字符串是否为正则
53-
} else {
54-
_tags_.push(tag);
55-
}
56-
}
57-
} else if (tags) {
58-
_tags_ = [tags as unknown as string];
59-
}
60-
61-
return _tags_ as '*';
62-
};
63-
6436
/**
6537
* 通过 apifox 获取 openapi 文档
6638
* @param params {object}
@@ -73,8 +45,8 @@ const getSchemaByApifox = async ({
7345
projectId,
7446
locale = 'zh-CN',
7547
apifoxVersion = '2024-03-28',
76-
includeTags,
77-
excludeTags = [],
48+
selectedTags,
49+
excludedByTags = [],
7850
apifoxToken,
7951
oasVersion = '3.0',
8052
exportFormat = 'JSON',
@@ -84,7 +56,7 @@ const getSchemaByApifox = async ({
8456
try {
8557
const body: APIFoxBody = {
8658
scope: {
87-
excludeTags,
59+
excludedByTags,
8860
},
8961
options: {
9062
includeApifoxExtensionProperties,
@@ -93,13 +65,13 @@ const getSchemaByApifox = async ({
9365
oasVersion,
9466
exportFormat,
9567
};
96-
const tags = getApifoxIncludeTags(includeTags);
68+
const tags = !isEmpty(selectedTags) ? selectedTags : '*';
9769

9870
if (tags === '*') {
9971
body.scope.type = 'ALL';
10072
} else {
10173
body.scope.type = 'SELECTED_TAGS';
102-
body.scope.includeTags = tags;
74+
body.scope.selectedTags = tags;
10375
}
10476

10577
const res = await axios.post(

0 commit comments

Comments
 (0)