openapi-ts-request 1.12.0 → 1.12.2
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 +4 -2
- package/dist/bin/cli.js +4 -5
- package/dist/bin/openapi.js +4 -5
- package/dist/bin/utils.d.ts +8 -0
- package/dist/bin/utils.js +13 -0
- package/dist/generator/serviceGenarator.js +1 -0
- package/dist/generator/serviceGeneratorHelper.d.ts +1 -0
- package/dist/generator/serviceGeneratorHelper.js +22 -5
- package/dist/index.d.ts +15 -10
- package/dist/index.js +6 -3
- package/dist/type.d.ts +4 -2
- package/dist/util.js +7 -1
- package/package.json +2 -2
- package/templates/enum.njk +3 -0
- package/templates/interface.njk +3 -0
package/README.md
CHANGED
|
@@ -225,7 +225,8 @@ openapi --i ./spec.json --o ./apis
|
|
|
225
225
|
|
|
226
226
|
| 属性 | 必填 | 类型 | 默认值 | 说明 |
|
|
227
227
|
| --- | --- | --- | --- | --- |
|
|
228
|
-
| schemaPath |
|
|
228
|
+
| schemaPath | 否\* | string | - | Swagger2/OpenAPI3 地址(与 apifoxConfig 互斥,两者必填其一) |
|
|
229
|
+
| apifoxConfig | 否\* | [Apifox Config](#Apifox-Config) | - | apifox 配置(与 schemaPath 互斥,两者必填其一) |
|
|
229
230
|
| serversPath | 否 | string | './src/apis' | 运行结果文件夹路径 |
|
|
230
231
|
| requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
|
|
231
232
|
| isSplitTypesByModule | 否 | boolean | false | 按模块拆分类型文件,开启后会生成:{module}.type.ts(各模块类型)、common.type.ts(公共类型)、enum.ts(枚举类型)、types.ts(统一导出) |
|
|
@@ -248,7 +249,6 @@ openapi --i ./spec.json --o ./apis
|
|
|
248
249
|
| isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
|
|
249
250
|
| mockFolder | 否 | string | - | mock文件路径,例如:'./mocks' |
|
|
250
251
|
| authorization | 否 | string | - | 文档权限凭证 |
|
|
251
|
-
| apifoxConfig | 否 | [Apifox Config](#Apifox-Config) | - | apifox 配置 |
|
|
252
252
|
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
253
253
|
| isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
|
|
254
254
|
| isOnlyGenTypeScriptType | 否 | boolean | false | 仅生成 typescript 类型 |
|
|
@@ -287,6 +287,8 @@ openapi --i ./spec.json --o ./apis
|
|
|
287
287
|
| exportFormat | string | 指定导出的 OpenAPI 文件的格式,可以有值如 'JSON' 或 'YAML' | 'JSON' |
|
|
288
288
|
| includeApifoxExtensionProperties | boolean | 指定是否包含 Apifox 的 OpenAPI 规范扩展字段 `x-apifox` | false |
|
|
289
289
|
| addFoldersToTags | boolean | 指定是否在标签字段中包含接口的目录名称 | false |
|
|
290
|
+
| branchId | number | 分支ID | false |
|
|
291
|
+
| moduleId | number | 模块ID | false |
|
|
290
292
|
|
|
291
293
|
## JSON Schemas
|
|
292
294
|
|
package/dist/bin/cli.js
CHANGED
|
@@ -7,6 +7,7 @@ const commander_1 = require("commander");
|
|
|
7
7
|
const index_1 = require("../index");
|
|
8
8
|
const log_1 = require("../log");
|
|
9
9
|
const readConfig_1 = require("../readConfig");
|
|
10
|
+
const utils_1 = require("./utils");
|
|
10
11
|
commander_1.program
|
|
11
12
|
.option('-cfn, --configFileName <string>', 'config file name')
|
|
12
13
|
.option('-cfp, --configFilePath <string>', 'config file path');
|
|
@@ -38,10 +39,7 @@ function run() {
|
|
|
38
39
|
(0, prompts_1.intro)('🎉 欢迎使用 openapi-ts-request 生成器');
|
|
39
40
|
const selected = yield (0, prompts_1.multiselect)({
|
|
40
41
|
message: '请选择要生成的 service',
|
|
41
|
-
options:
|
|
42
|
-
value: config,
|
|
43
|
-
label: config.describe || config.schemaPath,
|
|
44
|
-
})),
|
|
42
|
+
options: (0, utils_1.createMultiselectOptions)(configs),
|
|
45
43
|
});
|
|
46
44
|
if ((0, prompts_1.isCancel)(selected)) {
|
|
47
45
|
(0, prompts_1.cancel)('👋 Has cancelled');
|
|
@@ -58,7 +56,8 @@ function run() {
|
|
|
58
56
|
const result = results[i];
|
|
59
57
|
if (result.status === 'rejected') {
|
|
60
58
|
const cnf = configs[i];
|
|
61
|
-
const label = cnf.describe ||
|
|
59
|
+
const label = cnf.describe ||
|
|
60
|
+
('schemaPath' in cnf ? cnf.schemaPath : 'Apifox Config');
|
|
62
61
|
errorMsg += `${label}${label && ':'}${result.reason}\n`;
|
|
63
62
|
}
|
|
64
63
|
}
|
package/dist/bin/openapi.js
CHANGED
|
@@ -10,6 +10,7 @@ const pkg = tslib_1.__importStar(require("../../package.json"));
|
|
|
10
10
|
const index_1 = require("../index");
|
|
11
11
|
const log_1 = require("../log");
|
|
12
12
|
const readConfig_1 = require("../readConfig");
|
|
13
|
+
const utils_1 = require("./utils");
|
|
13
14
|
const params = commander_1.program
|
|
14
15
|
.name('openapi')
|
|
15
16
|
.usage('[options]')
|
|
@@ -112,10 +113,7 @@ function run() {
|
|
|
112
113
|
(0, prompts_1.intro)('🎉 欢迎使用 openapi-ts-request 生成器');
|
|
113
114
|
const selected = yield (0, prompts_1.multiselect)({
|
|
114
115
|
message: '请选择要生成的 service',
|
|
115
|
-
options:
|
|
116
|
-
value: config,
|
|
117
|
-
label: config.describe || config.schemaPath,
|
|
118
|
-
})),
|
|
116
|
+
options: (0, utils_1.createMultiselectOptions)(configs),
|
|
119
117
|
});
|
|
120
118
|
if ((0, prompts_1.isCancel)(selected)) {
|
|
121
119
|
(0, prompts_1.cancel)('👋 Has cancelled');
|
|
@@ -132,7 +130,8 @@ function run() {
|
|
|
132
130
|
const result = results[i];
|
|
133
131
|
if (result.status === 'rejected') {
|
|
134
132
|
const cnf = configs[i];
|
|
135
|
-
const label = cnf.describe ||
|
|
133
|
+
const label = cnf.describe ||
|
|
134
|
+
('schemaPath' in cnf ? cnf.schemaPath : 'Apifox Config');
|
|
136
135
|
errorMsg += `${label}${label && ':'}${result.reason}\n`;
|
|
137
136
|
}
|
|
138
137
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GenerateServiceProps } from '../index';
|
|
2
|
+
/**
|
|
3
|
+
* Helper function to create multiselect options with proper typing
|
|
4
|
+
*/
|
|
5
|
+
export declare function createMultiselectOptions(configs: GenerateServiceProps[]): Array<{
|
|
6
|
+
value: GenerateServiceProps;
|
|
7
|
+
label: string;
|
|
8
|
+
}>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMultiselectOptions = createMultiselectOptions;
|
|
4
|
+
/**
|
|
5
|
+
* Helper function to create multiselect options with proper typing
|
|
6
|
+
*/
|
|
7
|
+
function createMultiselectOptions(configs) {
|
|
8
|
+
return configs.map((config) => ({
|
|
9
|
+
value: config,
|
|
10
|
+
label: config.describe ||
|
|
11
|
+
('schemaPath' in config ? config.schemaPath : 'Apifox Config'),
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
@@ -145,19 +145,36 @@ function resolveAllOfObject(params) {
|
|
|
145
145
|
*/
|
|
146
146
|
function getProps(params) {
|
|
147
147
|
var _a;
|
|
148
|
-
const { schemaObject, getType } = params;
|
|
148
|
+
const { schemaObject, getType, openAPIData } = params;
|
|
149
149
|
const requiredPropKeys = (_a = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.required) !== null && _a !== void 0 ? _a : false;
|
|
150
150
|
const properties = schemaObject.properties;
|
|
151
151
|
return (0, lodash_1.keys)(properties).map((propKey) => {
|
|
152
|
+
var _a, _b;
|
|
152
153
|
const schema = ((properties === null || properties === void 0 ? void 0 : properties[propKey]) || config_2.DEFAULT_SCHEMA);
|
|
153
154
|
// 剔除属性键值中的特殊符号,因为函数入参变量存在特殊符号会导致解析文件失败
|
|
154
155
|
// eslint-disable-next-line no-useless-escape
|
|
155
156
|
propKey = propKey.replace(/[\[|\]]/g, '');
|
|
157
|
+
// 获取描述信息,如果是 $ref 引用,尝试获取引用对象的描述
|
|
158
|
+
let desc = [schema.title, schema.description]
|
|
159
|
+
.filter((item) => item)
|
|
160
|
+
.join(' ')
|
|
161
|
+
.replace(config_2.lineBreakReg, '');
|
|
162
|
+
// 如果是 $ref 引用,尝试获取引用对象的描述
|
|
163
|
+
if ((0, util_1.isReferenceObject)(schema) && openAPIData) {
|
|
164
|
+
const refName = (0, util_1.getLastRefName)(schema.$ref);
|
|
165
|
+
const refSchema = (_b = (_a = openAPIData.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[refName];
|
|
166
|
+
if (refSchema) {
|
|
167
|
+
const refDesc = [refSchema.title, refSchema.description]
|
|
168
|
+
.filter((item) => item)
|
|
169
|
+
.join(' ')
|
|
170
|
+
.replace(config_2.lineBreakReg, '');
|
|
171
|
+
if (refDesc) {
|
|
172
|
+
desc = desc + refDesc;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
156
176
|
// 复用 schema 部分字段
|
|
157
|
-
return Object.assign(Object.assign({}, schema), { name: propKey, type: getType(schema), desc:
|
|
158
|
-
.filter((item) => item)
|
|
159
|
-
.join(' ')
|
|
160
|
-
.replace(config_2.lineBreakReg, ''),
|
|
177
|
+
return Object.assign(Object.assign({}, schema), { name: propKey, type: getType(schema), desc: desc,
|
|
161
178
|
// 如果没有 required 信息,默认全部是非必填
|
|
162
179
|
required: requiredPropKeys
|
|
163
180
|
? requiredPropKeys.some((key) => key === propKey)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { TypescriptFileType } from './generator/config';
|
|
2
2
|
import type { APIDataType, ControllerType, ISchemaItem, ITypeItem } from './generator/type';
|
|
3
|
-
import type { ComponentsObject, IPriorityRule, IReactQueryMode, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
|
|
3
|
+
import type { ComponentsObject, IPriorityRule, IReactQueryMode, MutuallyExclusive, OpenAPIObject, OperationObject, ReferenceObject, SchemaObject } from './type';
|
|
4
4
|
import { type GetSchemaByApifoxProps } from './type';
|
|
5
5
|
export * from './generator/patchSchema';
|
|
6
|
-
export type
|
|
7
|
-
/**
|
|
8
|
-
* Swagger2/OpenAPI3 地址
|
|
9
|
-
*/
|
|
10
|
-
schemaPath: string;
|
|
6
|
+
export type GenerateServicePropsBase = {
|
|
11
7
|
/**
|
|
12
8
|
* 生成的文件夹的路径
|
|
13
9
|
*/
|
|
@@ -113,10 +109,6 @@ export type GenerateServiceProps = {
|
|
|
113
109
|
* 文档权限凭证
|
|
114
110
|
*/
|
|
115
111
|
authorization?: string;
|
|
116
|
-
/**
|
|
117
|
-
* apifox 配置
|
|
118
|
-
*/
|
|
119
|
-
apifoxConfig?: GetSchemaByApifoxProps;
|
|
120
112
|
/**
|
|
121
113
|
* 默认为false,true时使用null代替可选值
|
|
122
114
|
*/
|
|
@@ -291,6 +283,19 @@ export type GenerateServiceProps = {
|
|
|
291
283
|
};
|
|
292
284
|
};
|
|
293
285
|
};
|
|
286
|
+
/**
|
|
287
|
+
* Swagger2/OpenAPI3 地址或 Apifox 配置,两者必须填写一个
|
|
288
|
+
*/
|
|
289
|
+
export type GenerateServiceProps = GenerateServicePropsBase & MutuallyExclusive<{
|
|
290
|
+
/**
|
|
291
|
+
* Swagger2/OpenAPI3 地址
|
|
292
|
+
*/
|
|
293
|
+
schemaPath: string;
|
|
294
|
+
/**
|
|
295
|
+
* apifox 配置
|
|
296
|
+
*/
|
|
297
|
+
apifoxConfig: GetSchemaByApifoxProps;
|
|
298
|
+
}>;
|
|
294
299
|
export declare function generateService({ requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, includePaths, excludePaths, authorization, isTranslateToEnglishTag, priorityRule, timeout, reactQueryMode, apifoxConfig, ...rest }: GenerateServiceProps): Promise<void>;
|
|
295
300
|
/**
|
|
296
301
|
* Defines the configuration for openapi-ts-request.
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,10 @@ function generateService(_a) {
|
|
|
13
13
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
14
14
|
var { requestLibPath, schemaPath, mockFolder, includeTags, excludeTags, includePaths, excludePaths, authorization, isTranslateToEnglishTag, priorityRule = config_1.PriorityRule.include, timeout = 60000, reactQueryMode = config_1.ReactQueryMode.react, apifoxConfig } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "includeTags", "excludeTags", "includePaths", "excludePaths", "authorization", "isTranslateToEnglishTag", "priorityRule", "timeout", "reactQueryMode", "apifoxConfig"]);
|
|
15
15
|
if (!schemaPath && !apifoxConfig) {
|
|
16
|
-
|
|
16
|
+
throw new Error('Either schemaPath or apifoxConfig must be provided. Please provide at least one configuration option.');
|
|
17
|
+
}
|
|
18
|
+
if (schemaPath && apifoxConfig) {
|
|
19
|
+
throw new Error('schemaPath and apifoxConfig cannot be provided at the same time. Please provide only one configuration option.');
|
|
17
20
|
}
|
|
18
21
|
let openAPI = null;
|
|
19
22
|
if (apifoxConfig) {
|
|
@@ -34,7 +37,7 @@ function generateService(_a) {
|
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
|
|
37
|
-
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, enableLogging: false, priorityRule, includeTags: includeTags
|
|
40
|
+
const serviceGenerator = new serviceGenarator_1.default(Object.assign(Object.assign(Object.assign({}, (schemaPath ? { schemaPath } : { apifoxConfig })), { serversPath: './src/apis', requestImportStatement, enableLogging: false, priorityRule, includeTags: includeTags
|
|
38
41
|
? includeTags
|
|
39
42
|
: priorityRule === config_1.PriorityRule.include ||
|
|
40
43
|
priorityRule === config_1.PriorityRule.both
|
|
@@ -44,7 +47,7 @@ function generateService(_a) {
|
|
|
44
47
|
: priorityRule === config_1.PriorityRule.include ||
|
|
45
48
|
priorityRule === config_1.PriorityRule.both
|
|
46
49
|
? [/.*/g]
|
|
47
|
-
: 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);
|
|
50
|
+
: 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);
|
|
48
51
|
serviceGenerator.genFile();
|
|
49
52
|
if (mockFolder) {
|
|
50
53
|
(0, mockGenarator_1.mockGenerator)({
|
package/dist/type.d.ts
CHANGED
|
@@ -96,13 +96,15 @@ export interface APIFoxBody {
|
|
|
96
96
|
oasVersion?: '2.0' | '3.0' | '3.1';
|
|
97
97
|
exportFormat?: 'JSON' | 'YAML';
|
|
98
98
|
environmentIds?: string[];
|
|
99
|
+
branchId?: number;
|
|
100
|
+
moduleId?: number;
|
|
99
101
|
}
|
|
100
|
-
export
|
|
102
|
+
export type GetSchemaByApifoxProps = {
|
|
101
103
|
projectId: string;
|
|
102
104
|
apifoxToken: string;
|
|
103
105
|
locale?: string;
|
|
104
106
|
apifoxVersion?: string;
|
|
105
107
|
selectedTags?: string[];
|
|
106
108
|
excludedByTags?: string[];
|
|
107
|
-
}
|
|
109
|
+
} & Pick<APIFoxBody, 'oasVersion' | 'exportFormat' | 'branchId' | 'moduleId'> & APIFoxBody['options'];
|
|
108
110
|
export {};
|
package/dist/util.js
CHANGED
|
@@ -32,7 +32,7 @@ exports.getImportStatement = getImportStatement;
|
|
|
32
32
|
* @param params.apifoxVersion {string} apifox 版本 目前固定为 2024-03-28 可通过 https://api.apifox.com/v1/versions 获取最新版本
|
|
33
33
|
* @returns
|
|
34
34
|
*/
|
|
35
|
-
const getSchemaByApifox = (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ projectId, locale = 'zh-CN', apifoxVersion = '2024-03-28', selectedTags, excludedByTags = [], apifoxToken, oasVersion = '3.0', exportFormat = 'JSON', includeApifoxExtensionProperties = false, addFoldersToTags = false, }) {
|
|
35
|
+
const getSchemaByApifox = (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ projectId, locale = 'zh-CN', apifoxVersion = '2024-03-28', selectedTags, excludedByTags = [], apifoxToken, oasVersion = '3.0', exportFormat = 'JSON', includeApifoxExtensionProperties = false, addFoldersToTags = false, branchId, moduleId, }) {
|
|
36
36
|
try {
|
|
37
37
|
const body = {
|
|
38
38
|
scope: {
|
|
@@ -45,6 +45,12 @@ const getSchemaByApifox = (_a) => tslib_1.__awaiter(void 0, [_a], void 0, functi
|
|
|
45
45
|
oasVersion,
|
|
46
46
|
exportFormat,
|
|
47
47
|
};
|
|
48
|
+
if (branchId !== undefined) {
|
|
49
|
+
body.branchId = branchId;
|
|
50
|
+
}
|
|
51
|
+
if (moduleId !== undefined) {
|
|
52
|
+
body.moduleId = moduleId;
|
|
53
|
+
}
|
|
48
54
|
const tags = !(0, lodash_1.isEmpty)(selectedTags) ? selectedTags : '*';
|
|
49
55
|
if (tags === '*') {
|
|
50
56
|
body.scope.type = 'ALL';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "1.12.
|
|
3
|
+
"version": "1.12.2",
|
|
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",
|
|
@@ -96,6 +96,6 @@
|
|
|
96
96
|
"lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
|
|
97
97
|
"lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
|
|
98
98
|
"test:unit": "vitest",
|
|
99
|
-
"openapi-ts-request": "openapi
|
|
99
|
+
"openapi-ts-request": "openapi"
|
|
100
100
|
}
|
|
101
101
|
}
|
package/templates/enum.njk
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
{%- if list.length > 0 %}
|
|
5
5
|
{% for type in list -%}
|
|
6
6
|
{%- if type.isEnum %}
|
|
7
|
+
{%- if type.description%}
|
|
8
|
+
/** {{ type.description }} */
|
|
9
|
+
{%- endif %}
|
|
7
10
|
export enum {{ type.typeName | safe }} {{ type.type }};
|
|
8
11
|
|
|
9
12
|
export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}
|
package/templates/interface.njk
CHANGED
|
@@ -43,6 +43,9 @@
|
|
|
43
43
|
{%- endfor %}
|
|
44
44
|
{%- else %}
|
|
45
45
|
{%- if type.isEnum %}
|
|
46
|
+
{%- if type.description%}
|
|
47
|
+
/** {{ type.description }} */
|
|
48
|
+
{%- endif %}
|
|
46
49
|
export enum {{ type.typeName | safe }} {{ type.type }};
|
|
47
50
|
|
|
48
51
|
export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}
|