Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-ends-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-ts-request': patch
---

fix: fix apifox selectedTags #459
4 changes: 2 additions & 2 deletions README-en_US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
8 changes: 4 additions & 4 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -144,6 +144,6 @@ export interface GetSchemaByApifoxProps
apifoxToken: string;
locale?: string;
apifoxVersion?: string;
includeTags?: (string | RegExp)[];
excludeTags?: string[];
selectedTags?: string[];
excludedByTags?: string[];
}
40 changes: 6 additions & 34 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as yaml from 'js-yaml';
import {
camelCase as _camelCase_,
forEach,
isEmpty,
isObject,
keys,
map,
Expand All @@ -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}
Expand All @@ -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',
Expand All @@ -84,7 +56,7 @@ const getSchemaByApifox = async ({
try {
const body: APIFoxBody = {
scope: {
excludeTags,
excludedByTags,
},
options: {
includeApifoxExtensionProperties,
Expand All @@ -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(
Expand Down