gen-api-types 1.0.4 → 1.0.5
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/LICENSE +21 -0
- package/README.en.md +100 -24
- package/README.md +7 -3
- package/bin/index.js +0 -2
- package/package.json +47 -41
- package/tsconfig.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 xuejiangping
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
CHANGED
|
@@ -1,36 +1,112 @@
|
|
|
1
|
-
#
|
|
1
|
+
# gen-api-types
|
|
2
2
|
|
|
3
|
-
####
|
|
4
|
-
一个自动生成请求接口返回类型的cli工具
|
|
3
|
+
#### Introduction
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
🚀 A CLI tool for automatically generating request interface return types
|
|
6
|
+
|
|
7
|
+
In TypeScript projects, you often need to write interface return types. However, it's troublesome to manually write them every time by referring to the API documentation. If you encounter third-party interfaces or incomplete documentation, you need to debug the interface first before writing the return types, which is quite a headache.
|
|
8
|
+
|
|
9
|
+
With this tool, we can mark request interface classes and methods through TypeScript decorators, then dynamically call these interfaces and convert the returned data into TypeScript type definition files, which can be directly used in projects.
|
|
10
|
+
|
|
11
|
+
> Note:
|
|
12
|
+
> This tool needs to dynamically execute TypeScript code (calling interface modules in the project) and depends on the `tsx` execution tool. Please make sure to install `tsx` globally and ensure the `tsx` command is available.
|
|
8
13
|
|
|
9
14
|
#### Installation
|
|
10
15
|
|
|
11
|
-
1.
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
1. npm installation
|
|
17
|
+
|
|
18
|
+
```shell
|
|
19
|
+
npm install tsx -g
|
|
20
|
+
npm install gen-api-types -D
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
#### Usage
|
|
24
|
+
|
|
25
|
+
##### 1. Mark interface class names and methods
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { gen_type_c, gen_type_m } from 'gen-api-types'
|
|
29
|
+
|
|
30
|
+
@gen_type_c()
|
|
31
|
+
export class TestApi {
|
|
32
|
+
@gen_type_m({ args: [100], typeName: 'XXX' })
|
|
33
|
+
static async getList(id: number): Promise<XXX> {
|
|
34
|
+
return asleep(1000).then(() => {
|
|
35
|
+
return { name: 'zs', id }
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@gen_type_m()
|
|
40
|
+
static getWeather(): Promise<Response_TestApi_getWeather> {
|
|
41
|
+
return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
As shown in the code above:
|
|
47
|
+
|
|
48
|
+
- `@gen_type_c` decorator function is used to mark interface classes. Since the tool dynamically analyzes all ts files in the specified directory, marking interface classes helps quickly locate them.
|
|
49
|
+
- `@gen_type_m` decorator function marks the request methods that need to be converted. It can accept a configuration object with two fields:
|
|
50
|
+
1. `typeName: string` Interface return type name. If not specified, the default name will be: `Response_${ClassName}_${MethodName}`
|
|
51
|
+
2. `args: any[]` Method parameter list. The tool will pass this list when calling the request method.
|
|
52
|
+
|
|
53
|
+
##### 2. Execute command
|
|
54
|
+
|
|
55
|
+
```shell
|
|
56
|
+
npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Parameter description:
|
|
60
|
+
|
|
61
|
+
```shell
|
|
62
|
+
Usage: npx gen-api-types [options] [api_dirs...]
|
|
63
|
+
|
|
64
|
+
Options:
|
|
65
|
+
-h, --help Output help information
|
|
66
|
+
-r, --project_root <path> Project root directory
|
|
67
|
+
-O, --output_file <path> Output file name
|
|
68
|
+
-o, --output_dir <path> Output directory
|
|
69
|
+
-t, --ts_config_path <path> Path to tsconfig.json file
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
You can also use it by configuring scripts in package.json:
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"scripts": {
|
|
77
|
+
"gen_types": "gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
14
81
|
|
|
15
|
-
|
|
82
|
+
Command output:
|
|
16
83
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
84
|
+
```shell
|
|
85
|
+
🚀 Start generating API types...
|
|
86
|
+
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
87
|
+
📋 Processing UserApi.getList ...
|
|
88
|
+
📋 Processing UserApi.getWeather ...
|
|
89
|
+
Request results:
|
|
90
|
+
┌────────────────┬──────────────────────────────────────┐
|
|
91
|
+
│ (index) │ Values │
|
|
92
|
+
├────────────────┼──────────────────────────────────────┤
|
|
93
|
+
│ ✔️ successList │ 'UserApi.getList UserApi.getWeather' │
|
|
94
|
+
│ ❌ errorList │ '' │
|
|
95
|
+
└────────────────┴──────────────────────────────────────┘
|
|
96
|
+
✅ API type generation completed
|
|
97
|
+
```
|
|
20
98
|
|
|
21
|
-
|
|
99
|
+
##### 3. Using the types
|
|
22
100
|
|
|
23
|
-
|
|
24
|
-
2. Create Feat_xxx branch
|
|
25
|
-
3. Commit your code
|
|
26
|
-
4. Create Pull Request
|
|
101
|
+
By default, a type definition file index.d.ts is generated without exports:
|
|
27
102
|
|
|
103
|
+
```ts
|
|
104
|
+
type XXX = { name: string };
|
|
105
|
+
type Response_UserApi_getWeather = {...}
|
|
106
|
+
```
|
|
28
107
|
|
|
29
|
-
|
|
108
|
+
You can configure `include` in tsconfig.json to reference the file, or directly reference it at the top of the interface module file with:
|
|
30
109
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
4. The most valuable open source project [GVP](https://gitee.com/gvp)
|
|
35
|
-
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
|
|
36
|
-
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
|
110
|
+
```ts
|
|
111
|
+
/// <reference path="./index.d.ts" />
|
|
112
|
+
```
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
#### 介绍
|
|
4
4
|
|
|
5
|
-
🚀 一个自动生成请求接口返回类型的 cli
|
|
5
|
+
🚀 一个自动生成请求接口返回类型的 cli 小工具
|
|
6
6
|
|
|
7
7
|
在 ts 项目中,经常需要编写接口返回类型。但是每次都要查看接口文档,手动编写非常麻烦。如果遇到一些第三方接口或者接口文档不全的情况,还需要先调试接口后,再编写接口返回类型,很令人头疼
|
|
8
8
|
|
|
@@ -38,7 +38,7 @@ export class TestApi {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
@gen_type_m()
|
|
41
|
-
static getWeather(): Promise<
|
|
41
|
+
static getWeather(): Promise<Response_TestApi_getWeather> {
|
|
42
42
|
return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
|
|
43
43
|
}
|
|
44
44
|
}
|
|
@@ -106,4 +106,8 @@ type XXX = { name: string };
|
|
|
106
106
|
type Response_UserApi_getWeather = {...}
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
-
可在`tsconfig.json`中配置`include
|
|
109
|
+
可在`tsconfig.json`中配置`include`引用文件,或者直接在接口模块文件顶部通引用:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
/// <reference path="./index.d.ts" />
|
|
113
|
+
```
|
package/bin/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,41 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "gen-api-types",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "./src/index.ts",
|
|
6
|
-
"bin": {
|
|
7
|
-
"gen-api-types": "src/cli/index.ts",
|
|
8
|
-
"gat": "bin/index.js"
|
|
9
|
-
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "gen-api-types",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "一个自动生成请求接口返回类型的 cli 小工具",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"bin": {
|
|
7
|
+
"gen-api-types": "src/cli/index.ts",
|
|
8
|
+
"gat": "bin/index.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/xuejiangping/gen-api-types.git"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "vitest",
|
|
16
|
+
"build": "webpack",
|
|
17
|
+
"publish:patch": "npm version patch && git push --tags && npm publish",
|
|
18
|
+
"preinstall": "echo preinstall 。。。",
|
|
19
|
+
"postinstall": "echo postinstall 。。。"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"generate types",
|
|
23
|
+
"TypeScript",
|
|
24
|
+
"Cli",
|
|
25
|
+
"API"
|
|
26
|
+
],
|
|
27
|
+
"files": [
|
|
28
|
+
"src",
|
|
29
|
+
"tsconfig.json",
|
|
30
|
+
"package.json"
|
|
31
|
+
],
|
|
32
|
+
"author": "",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"packageManager": "pnpm@10.7.1",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"reflect-metadata": "^0.2.2",
|
|
37
|
+
"ts-morph": "^27.0.0",
|
|
38
|
+
"tsx": "^4.20.5"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^24.3.1",
|
|
42
|
+
"ts-loader": "^9.5.4",
|
|
43
|
+
"vitest": "^3.2.4",
|
|
44
|
+
"webpack": "^5.101.0",
|
|
45
|
+
"webpack-cli": "^6.0.1"
|
|
46
|
+
}
|
|
47
|
+
}
|