openapi-ts-request 0.3.3 → 0.4.0
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 +112 -48
- package/dist/{cli.js → bin/cli.js} +1 -1
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +57 -0
- package/dist/generator/file.js +1 -1
- package/dist/index.d.ts +10 -10
- package/package.json +4 -2
- package/templates/serviceIndex.njk +0 -1
- /package/dist/{cli.d.ts → bin/cli.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
##
|
|
1
|
+
## 介绍
|
|
2
2
|
|
|
3
3
|
[](https://github.com/openapi-ui/openapi-ts-request)
|
|
4
4
|
[](https://www.npmjs.com/package/openapi-ts-request)
|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README-en_US.md">English</a> | 简体中文
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
根据 [Swagger2/OpenAPI3](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 TS 类型, 客户端请求函数, 模拟请求响应服务, 枚举, 类型字段翻译, JSON Schemas
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
* generate TypeScript interface, reuquest client, request mock service, enum, type field label, JSON Schemas
|
|
13
|
-
* support custom request function, Fetch、Axios、Uniapp-Request、Node.js、XHR client available
|
|
14
|
-
* support filter specification by tags
|
|
15
|
-
* support JSON specification
|
|
11
|
+
## 功能
|
|
16
12
|
|
|
17
|
-
|
|
13
|
+
* 支持 Swagger2.0/OpenAPI 3.0,3.1 定义
|
|
14
|
+
* 生成 TS 类型, 请求客户端, 请求模拟服务, 枚举, 类型字段翻译, JSON Schemas
|
|
15
|
+
* 支持通过 npx、CLI、Nodejs 的方式使用
|
|
16
|
+
* 支持自定义请求工具函数, 支持 Fetch、Axios、UniApp-Request、Node.js、XHR 客户端
|
|
17
|
+
* 支持通过 tags 过滤生成结果
|
|
18
|
+
* 支持 JSON 定义文件
|
|
19
|
+
|
|
20
|
+
## 使用
|
|
18
21
|
|
|
19
22
|
```bash
|
|
20
23
|
npm i openapi-ts-request --save-dev
|
|
@@ -33,7 +36,7 @@ export default {
|
|
|
33
36
|
}
|
|
34
37
|
```
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
支持传入数组配置进行生成
|
|
37
40
|
|
|
38
41
|
```ts
|
|
39
42
|
export default [
|
|
@@ -48,9 +51,9 @@ export default [
|
|
|
48
51
|
]
|
|
49
52
|
```
|
|
50
53
|
|
|
51
|
-
在 ```package.json``` 的 ```script```
|
|
54
|
+
在 ```package.json``` 的 ```script``` 中添加命令: ```"openapi": "openapi-ts-request",```
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
生成结果:
|
|
54
57
|
|
|
55
58
|
```bash
|
|
56
59
|
npm run openapi
|
|
@@ -69,9 +72,9 @@ generateService({
|
|
|
69
72
|
})
|
|
70
73
|
```
|
|
71
74
|
|
|
72
|
-
在 ```package.json``` 的 ```script```
|
|
75
|
+
在 ```package.json``` 的 ```script``` 中添加命令: ```"openapi": "node xxx/xxx/openapi-ts-request.config.js"```
|
|
73
76
|
|
|
74
|
-
|
|
77
|
+
生成结果:
|
|
75
78
|
|
|
76
79
|
```bash
|
|
77
80
|
npm run openapi
|
|
@@ -90,47 +93,91 @@ generateService({
|
|
|
90
93
|
})
|
|
91
94
|
```
|
|
92
95
|
|
|
93
|
-
在 ```package.json``` 的 ```script```
|
|
96
|
+
在 ```package.json``` 的 ```script``` 中添加命令: ```"openapi": "ts-node xxx/xxx/openapi-ts-request.config.ts",```
|
|
97
|
+
|
|
98
|
+
生成结果:
|
|
94
99
|
|
|
95
|
-
生成 api
|
|
96
100
|
```bash
|
|
97
101
|
npm run openapi
|
|
98
102
|
```
|
|
99
103
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
104
|
+
### NPX
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
npx openapi-ts-request openapi -i ./openapi.json -o ./apis
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### CLI
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
npm i openapi-ts-request -g
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
$ openapi --help
|
|
118
|
+
|
|
119
|
+
Usage: openapi [options]
|
|
120
|
+
|
|
121
|
+
Options:
|
|
122
|
+
-V, --version output the version number
|
|
123
|
+
-i, --input <string> OpenAPI specification, can be a path, url (required)
|
|
124
|
+
-o, --output <string> Output directory (required)
|
|
125
|
+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
|
|
126
|
+
--allowedTags <string[]> Generate results from allowed tags
|
|
127
|
+
--requestOptionsType <string> Custom request method options parameter type (default: "{ [key:
|
|
128
|
+
string]: unknown }")
|
|
129
|
+
--requestImportStatement <string> custom request import statement, for example: "const request =
|
|
130
|
+
require(`@/request`)"
|
|
131
|
+
--apiPrefix <string> Custom the prefix of the api path, for example: "api"(variable),
|
|
132
|
+
`"api"`(string)
|
|
133
|
+
--isDisplayTypeLabel <boolean> Generate label matching type field (default: false) (default: false)
|
|
134
|
+
--isGenJsonSchemas <boolean> Generate JSON Schemas (default: false) (default: false)
|
|
135
|
+
--mockFolder <string> Mock file path, (default: "./mocks")
|
|
136
|
+
--nullable <boolean> null instead of optional (default: false) (default: false)
|
|
137
|
+
--isCamelCase <boolean> CamelCase naming of controller files and request client (default: true)
|
|
138
|
+
(default: true)
|
|
139
|
+
-h, --help display help for command
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
生成结果:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
openapi --i ./spec.json --o ./apis
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## 参数
|
|
149
|
+
|
|
150
|
+
| 属性 | 必填 | 类型 | 默认值 | 说明 |
|
|
151
|
+
| ---------------------- | ----- | -------- | ------------ | ------ |
|
|
152
|
+
| schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
|
|
153
|
+
| serversPath | 否 | string | './src/apis' | 生成结果的文件夹路径 |
|
|
154
|
+
| requestLibPath | 否 | string | - | 自定义请求方法路径,例如:'@/request', 'node-fetch' |
|
|
155
|
+
| allowedTags | 否 | string[] | - | 根据指定的 tags 生成结果 |
|
|
156
|
+
| requestOptionsType | 否 | string | '{ [key: string]: unknown }' | 自定义请求方法 options 参数类型 |
|
|
157
|
+
| requestImportStatement | 否 | string | - | 自定义请求方法表达式,例如:"const request = require('@/request')" |
|
|
158
|
+
| apiPrefix | 否 | string | - | api path的前缀,例如:'api'(动态变量), "'api'"(字符串) |
|
|
159
|
+
| isDisplayTypeLabel | 否 | boolean | false | 是否生成 type 对应的label |
|
|
160
|
+
| isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
|
|
161
|
+
| mockFolder | 否 | string | './mocks' | mock文件路径 |
|
|
162
|
+
| nullable | 否 | boolean | false | 使用 null 代替可选 |
|
|
163
|
+
| isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
|
|
164
|
+
| hook | 否 | [Custom Hook](#Custom-Hook) | - | 自定义 hook |
|
|
165
|
+
|
|
166
|
+
## 自定义 Hook
|
|
167
|
+
|
|
168
|
+
| 属性 | 类型 | 说明 |
|
|
169
|
+
| ---------------------- | ---- | ------------------ |
|
|
170
|
+
| afterOpenApiDataInited | (openAPIData: OpenAPIObject) => OpenAPIObject | 自定义 OpenAPI 数据 |
|
|
171
|
+
| customFunctionName | (data: APIDataType) => string | 自定义请求方法函数名称 |
|
|
172
|
+
| customTypeName | (data: APIDataType) => string | 自定义类型名称 |
|
|
173
|
+
| customClassName | (tagName: string) => string | 自定义标签名 |
|
|
174
|
+
| customType | (<br>schemaObject: SchemaObject \| ReferenceObject,<br>namespace: string,<br>originGetType:(schemaObject: SchemaObject \| ReferenceObject, namespace: string) => string,<br>) => string | 自定义类型 <br> *返回非字符串将使用默认方法获取type* |
|
|
175
|
+
| customFileNames | (<br>operationObject: OperationObject,<br>apiPath: string,<br>apiMethod: string,<br>) => string[] | 自定义生成的请求客户端文件名称,可以返回多个文件名称的数组(表示生成多个文件). <br> *返回为空,则使用默认的方法获取* |
|
|
129
176
|
|
|
130
177
|
## JSON Schemas
|
|
131
178
|
|
|
132
|
-
- 默认生成 [components.schemas](https://spec.openapis.org/oas/latest.html#components-object) 下面的 JSON Schemas
|
|
133
|
-
- 提供一个解析 schema
|
|
179
|
+
- 默认生成 [components.schemas](https://spec.openapis.org/oas/latest.html#components-object) 下面的 JSON Schemas,[paths](https://spec.openapis.org/oas/latest.html#paths-object) 对应的 JSON Schemas 目前需自行解析
|
|
180
|
+
- 提供一个解析 schema 的函数,用于将 `$ref`,`$allOf` 的引用填充到 `当前schema`
|
|
134
181
|
|
|
135
182
|
```ts
|
|
136
183
|
export declare function patchSchema<T extends object>(schema: ISchemaObject, schemas: ComponentsObject["schemas"]): T;
|
|
@@ -140,7 +187,24 @@ export declare function patchSchema<T extends object>(schema: ISchemaObject, sch
|
|
|
140
187
|
|
|
141
188
|
目前使用 [mockjs](http://mockjs.com) 生成 mock 数据,mocks 文件启动需要借助 [@umijs/server](https://umijs.org/docs/guides/mock),后面会寻找其他方案以达到更好的 mock 体验
|
|
142
189
|
|
|
143
|
-
##
|
|
190
|
+
## 贡献
|
|
191
|
+
|
|
192
|
+
### 环境要求
|
|
193
|
+
|
|
194
|
+
* node 18+
|
|
195
|
+
* pnpm 9+
|
|
196
|
+
|
|
197
|
+
### 提交 Pull Request
|
|
198
|
+
|
|
199
|
+
1. 熟悉 [Pull Request]("https://help.github.com/articles/using-pull-requests") 规范
|
|
200
|
+
2. fork 此仓库
|
|
201
|
+
3. 开一个新分支修改代码:`git checkout -b my-branch main`
|
|
202
|
+
4. 确保你的代码可以通过所有测试用例(新增功能需要添加新的功能测试用例):`pnpm test`
|
|
203
|
+
5. 创建 changeset 文件通过命令:`pnpm changeset`
|
|
204
|
+
6. 使用 commit 提交你的修改(需遵循 commitlint 规范)
|
|
205
|
+
7. 发起 Pull Request
|
|
206
|
+
|
|
207
|
+
## 感谢
|
|
144
208
|
|
|
145
209
|
- [openapi2typescript](https://github.com/chenshuai2144/openapi2typescript)
|
|
146
210
|
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
7
|
const cosmiconfig_1 = require("cosmiconfig");
|
|
8
|
-
const index_1 = require("
|
|
8
|
+
const index_1 = require("../index");
|
|
9
9
|
const explorerSync = (0, cosmiconfig_1.cosmiconfigSync)('openapi-ts-request');
|
|
10
10
|
const config = (_a = explorerSync.search()) === null || _a === void 0 ? void 0 : _a.config;
|
|
11
11
|
function run() {
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const commander_1 = require("commander");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const pkg = tslib_1.__importStar(require("../../package.json"));
|
|
8
|
+
const index_1 = require("../index");
|
|
9
|
+
const params = commander_1.program
|
|
10
|
+
.name('openapi')
|
|
11
|
+
.usage('[options]')
|
|
12
|
+
.version(pkg.version)
|
|
13
|
+
.requiredOption('-i, --input <string>', 'OpenAPI specification, can be a path, url (required)')
|
|
14
|
+
.requiredOption('-o, --output <string>', 'Output directory (required)')
|
|
15
|
+
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch"')
|
|
16
|
+
.option('--allowedTags <string[]>', 'Generate results from allowed tags')
|
|
17
|
+
.option('--requestOptionsType <string>', 'Custom request method options parameter type (default: "{ [key: string]: unknown }")')
|
|
18
|
+
.option('--requestImportStatement <string>', 'custom request import statement, for example: "const request = require(`@/request`)"')
|
|
19
|
+
.option('--apiPrefix <string>', 'Custom the prefix of the api path, for example: "api"(variable), `"api"`(string)')
|
|
20
|
+
.option('--isDisplayTypeLabel <boolean>', 'Generate label matching type field (default: false)', false)
|
|
21
|
+
.option('--isGenJsonSchemas <boolean>', 'Generate JSON Schemas (default: false)', false)
|
|
22
|
+
.option('--mockFolder <string>', 'Mock file path, (default: "./mocks")')
|
|
23
|
+
.option('--nullable <boolean>', 'null instead of optional (default: false)', false)
|
|
24
|
+
.option('--isCamelCase <boolean>', 'CamelCase naming of controller files and request client (default: true)', true)
|
|
25
|
+
.parse(process.argv)
|
|
26
|
+
.opts();
|
|
27
|
+
function getPath(path) {
|
|
28
|
+
const cwd = process.cwd();
|
|
29
|
+
return (0, path_1.join)(cwd, path);
|
|
30
|
+
}
|
|
31
|
+
function run() {
|
|
32
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
try {
|
|
34
|
+
const input = getPath(params.input);
|
|
35
|
+
const output = getPath(params.output);
|
|
36
|
+
yield (0, index_1.generateService)({
|
|
37
|
+
schemaPath: input,
|
|
38
|
+
serversPath: output,
|
|
39
|
+
requestLibPath: params.requestLibPath,
|
|
40
|
+
allowedTags: params.allowedTags,
|
|
41
|
+
requestOptionsType: params.requestOptionsType,
|
|
42
|
+
apiPrefix: params.apiPrefix,
|
|
43
|
+
isDisplayTypeLabel: JSON.parse(params.isDisplayTypeLabel) === true,
|
|
44
|
+
isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas) === true,
|
|
45
|
+
mockFolder: params.mockFolder,
|
|
46
|
+
nullable: JSON.parse(params.nullable) === true,
|
|
47
|
+
isCamelCase: JSON.parse(params.isCamelCase) === true,
|
|
48
|
+
});
|
|
49
|
+
process.exit(0);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error('this is error: ', error);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
void run();
|
package/dist/generator/file.js
CHANGED
|
@@ -10,7 +10,7 @@ const prettierFile = (content) => {
|
|
|
10
10
|
let result = content;
|
|
11
11
|
let hasError = false;
|
|
12
12
|
try {
|
|
13
|
-
result = (0, sync_1.format)(content, Object.assign(Object.assign({}, prettier_config_cjs_1.default), { parser: 'typescript',
|
|
13
|
+
result = (0, sync_1.format)(content, Object.assign(Object.assign({}, prettier_config_cjs_1.default), { parser: 'typescript', plugins: [] }));
|
|
14
14
|
}
|
|
15
15
|
catch (error) {
|
|
16
16
|
hasError = true;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ComponentsObject, OpenAPIObject, OperationObject, ReferenceObject, Sche
|
|
|
3
3
|
export * from './generator/patchSchema';
|
|
4
4
|
export type GenerateServiceProps = {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Swagger2/OpenAPI3 地址
|
|
7
7
|
*/
|
|
8
8
|
schemaPath: string;
|
|
9
9
|
/**
|
|
@@ -49,7 +49,7 @@ export type GenerateServiceProps = {
|
|
|
49
49
|
isGenJsonSchemas?: boolean;
|
|
50
50
|
/**
|
|
51
51
|
* response中数据字段
|
|
52
|
-
* example: ['result'
|
|
52
|
+
* for example: ['result']
|
|
53
53
|
*/
|
|
54
54
|
dataFields?: string[];
|
|
55
55
|
/**
|
|
@@ -84,7 +84,7 @@ export type GenerateServiceProps = {
|
|
|
84
84
|
customTypeName?: (data: APIDataType) => string;
|
|
85
85
|
/** 自定义 options 默认值 */
|
|
86
86
|
customOptionsDefaultValue?: (data: OperationObject) => Record<string, any> | undefined;
|
|
87
|
-
/**
|
|
87
|
+
/** 自定义 tag 名称 */
|
|
88
88
|
customClassName?: (tagName: string) => string;
|
|
89
89
|
/**
|
|
90
90
|
* 自定义获取type hook
|
|
@@ -103,14 +103,14 @@ export type GenerateServiceProps = {
|
|
|
103
103
|
originGetType: (schemaObject: SchemaObject, namespace: string) => string;
|
|
104
104
|
}) => string;
|
|
105
105
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
106
|
+
* 自定义生成文件名,可返回多个,表示生成多个文件;
|
|
107
|
+
* 返回为空,则使用默认的获取方法获取;
|
|
108
108
|
* @example 使用operationId生成文件名
|
|
109
|
-
* function customFileNames(operationObject,apiPath){
|
|
110
|
-
* const operationId=operationObject.operationId;
|
|
109
|
+
* function customFileNames(operationObject, apiPath){
|
|
110
|
+
* const operationId = operationObject.operationId;
|
|
111
111
|
* if (!operationId) {
|
|
112
112
|
* console.warn('[Warning] no operationId', apiPath);
|
|
113
|
-
* return;
|
|
113
|
+
* return null;
|
|
114
114
|
* }
|
|
115
115
|
* const res = operationId.split('_');
|
|
116
116
|
* if (res.length > 1) {
|
|
@@ -124,11 +124,11 @@ export type GenerateServiceProps = {
|
|
|
124
124
|
* if (controllerName) {
|
|
125
125
|
* return [controllerName];
|
|
126
126
|
* }
|
|
127
|
-
* return;
|
|
127
|
+
* return null;
|
|
128
128
|
* }
|
|
129
129
|
* }
|
|
130
130
|
*/
|
|
131
|
-
customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[];
|
|
131
|
+
customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[] | null;
|
|
132
132
|
};
|
|
133
133
|
};
|
|
134
134
|
export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, ...rest }: GenerateServiceProps): Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Swagger2/OpenAPI3 to TypeScript, request client, request mock service, enum, type field label, JSON Schemas",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"main": "dist/index.js",
|
|
12
12
|
"types": "dist/index.d.ts",
|
|
13
13
|
"bin": {
|
|
14
|
-
"openapi
|
|
14
|
+
"openapi": "dist/bin/index.js",
|
|
15
|
+
"openapi-ts-request": "dist/bin/cli.js"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
18
|
"dist",
|
|
@@ -23,6 +24,7 @@
|
|
|
23
24
|
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
24
25
|
"axios": "^1.7.2",
|
|
25
26
|
"chalk": "^4.1.2",
|
|
27
|
+
"commander": "^12.1.0",
|
|
26
28
|
"cosmiconfig": "^9.0.0",
|
|
27
29
|
"glob": "^10.4.2",
|
|
28
30
|
"lodash": "^4.17.21",
|
|
@@ -4,7 +4,6 @@ import * as {{ namespace }} from './{{ interfaceFileName }}';
|
|
|
4
4
|
{%- if isGenJsonSchemas %}
|
|
5
5
|
import * as schemas from './{{ schemaFileName }}';
|
|
6
6
|
{%- endif %}
|
|
7
|
-
|
|
8
7
|
{% for api in list -%}
|
|
9
8
|
import * as {{ api.controllerName }} from './{{ api.fileName }}';
|
|
10
9
|
{% endfor -%}
|
|
File without changes
|