From 7a0ed075a2074d43421e7ccf1ddc0c3288f8ef58 Mon Sep 17 00:00:00 2001 From: luochao <1055120207@qq.com> Date: Mon, 8 Sep 2025 10:40:54 +0800 Subject: [PATCH] fix: fix apifox selectedTags #459 --- .changeset/fluffy-ends-hang.md | 5 +++++ README-en_US.md | 4 ++-- README.md | 4 ++-- src/type.ts | 8 +++---- src/util.ts | 40 +++++----------------------------- 5 files changed, 19 insertions(+), 42 deletions(-) create mode 100644 .changeset/fluffy-ends-hang.md diff --git a/.changeset/fluffy-ends-hang.md b/.changeset/fluffy-ends-hang.md new file mode 100644 index 0000000..133f193 --- /dev/null +++ b/.changeset/fluffy-ends-hang.md @@ -0,0 +1,5 @@ +--- +'openapi-ts-request': patch +--- + +fix: fix apifox selectedTags #459 diff --git a/README-en_US.md b/README-en_US.md index 95b05c5..fb03aed 100644 --- a/README-en_US.md +++ b/README-en_US.md @@ -272,8 +272,8 @@ openapi -i ./spec.json -o ./apis | apifoxToken | string | [get](https://docs.apifox.com/doc-5723694) | true | | local | string | language(default:zh-CN) | false | | apifoxVersion | string | default: 2024-03-28, [current apifox version](https://api.apifox.com/v1/versions) | false | -| includeTags | \* or string[] | default: \* | false | -| excludeTags | string[] | default: [] | false | +| selectedTags | \* or string[] | default: \* | false | +| excludedByTags | string[] | default: [] | false | | 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' | | exportFormat | string | specify the format of the exported OpenAPI file, can have values such as 'JSON' or 'YAML' | 'JSON' | | includeApifoxExtensionProperties | boolean | specify whether to include the OpenAPI specification extension fields `x-apifox` | false | diff --git a/README.md b/README.md index 8b584ef..20e59eb 100644 --- a/README.md +++ b/README.md @@ -274,8 +274,8 @@ openapi --i ./spec.json --o ./apis | apifoxToken | string | [获取](https://docs.apifox.com/doc-5723694) | true | | local | string | 语言(默认: zh-CN) | false | | apifoxVersion | string | 默认: 2024-03-28, [获取当前版本](https://api.apifox.com/v1/versions) | false | -| includeTags | \* 或 string[] | 默认: \* | false | -| excludeTags | string[] | 默认: [] | false | +| selectedTags | \* 或 string[] | 默认: \* | false | +| excludedByTags | string[] | 默认: [] | false | | oasVersion | string | 指定用于导出的 OpenAPI 规范的版本,可以有值如 "2.0"、"3.0"、"3.1" | '3.0' | | exportFormat | string | 指定导出的 OpenAPI 文件的格式,可以有值如 'JSON' 或 'YAML' | 'JSON' | | includeApifoxExtensionProperties | boolean | 指定是否包含 Apifox 的 OpenAPI 规范扩展字段 `x-apifox` | false | diff --git a/src/type.ts b/src/type.ts index aab1266..0fa3b50 100644 --- a/src/type.ts +++ b/src/type.ts @@ -122,8 +122,8 @@ export type ReadConfigOptions = MutuallyExclusiveWithFallback< export interface APIFoxBody { scope?: { type?: 'ALL' | 'SELECTED_TAGS'; - includeTags?: string[]; - excludeTags?: string[]; + selectedTags?: string[]; + excludedByTags?: string[]; }; options?: { includeApifoxExtensionProperties?: boolean; @@ -144,6 +144,6 @@ export interface GetSchemaByApifoxProps apifoxToken: string; locale?: string; apifoxVersion?: string; - includeTags?: (string | RegExp)[]; - excludeTags?: string[]; + selectedTags?: string[]; + excludedByTags?: string[]; } diff --git a/src/util.ts b/src/util.ts index 374e2ab..8aabdb1 100644 --- a/src/util.ts +++ b/src/util.ts @@ -6,6 +6,7 @@ import * as yaml from 'js-yaml'; import { camelCase as _camelCase_, forEach, + isEmpty, isObject, keys, map, @@ -32,35 +33,6 @@ export const getImportStatement = (requestLibPath: string) => { return `import { request } from 'axios';`; }; -const getApifoxIncludeTags = (tags?: (string | RegExp)[]): '*' | string[] => { - let _tags_: string | string[] = '*'; - if (tags && Array.isArray(tags)) { - if (!tags.length) { - return '*'; - } - - _tags_ = []; - for (const tag of tags) { - if (typeof tag === 'string') { - if (tag === '*') { - _tags_ = '*'; - break; - } - } else if (tag instanceof RegExp) { - _tags_ = '*'; - break; - // TODO:后期添加支持判断字符串是否为正则 - } else { - _tags_.push(tag); - } - } - } else if (tags) { - _tags_ = [tags as unknown as string]; - } - - return _tags_ as '*'; -}; - /** * 通过 apifox 获取 openapi 文档 * @param params {object} @@ -73,8 +45,8 @@ const getSchemaByApifox = async ({ projectId, locale = 'zh-CN', apifoxVersion = '2024-03-28', - includeTags, - excludeTags = [], + selectedTags, + excludedByTags = [], apifoxToken, oasVersion = '3.0', exportFormat = 'JSON', @@ -84,7 +56,7 @@ const getSchemaByApifox = async ({ try { const body: APIFoxBody = { scope: { - excludeTags, + excludedByTags, }, options: { includeApifoxExtensionProperties, @@ -93,13 +65,13 @@ const getSchemaByApifox = async ({ oasVersion, exportFormat, }; - const tags = getApifoxIncludeTags(includeTags); + const tags = !isEmpty(selectedTags) ? selectedTags : '*'; if (tags === '*') { body.scope.type = 'ALL'; } else { body.scope.type = 'SELECTED_TAGS'; - body.scope.includeTags = tags; + body.scope.selectedTags = tags; } const res = await axios.post(