gen-api-types 1.0.3 → 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 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
- # get-api-types
1
+ # gen-api-types
2
2
 
3
- #### Description
4
- 一个自动生成请求接口返回类型的cli工具
3
+ #### Introduction
5
4
 
6
- #### Software Architecture
7
- Software architecture description
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. xxxx
12
- 2. xxxx
13
- 3. xxxx
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
- #### Instructions
82
+ Command output:
16
83
 
17
- 1. xxxx
18
- 2. xxxx
19
- 3. xxxx
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
- #### Contribution
99
+ ##### 3. Using the types
22
100
 
23
- 1. Fork the repository
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
- #### Gitee Feature
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
- 1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
32
- 2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
33
- 3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
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<Res_TestApi_getWeather> {
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`引用文件,或者直接在接口模块文件顶部通过`/// <reference path="./index.d.ts" />`引用
109
+ 可在`tsconfig.json`中配置`include`引用文件,或者直接在接口模块文件顶部通引用:
110
+
111
+ ```ts
112
+ /// <reference path="./index.d.ts" />
113
+ ```
package/bin/index.js ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { exec,execFile,spawn } = require('child_process');
4
+ const path = require('path');
5
+ const { promisify } = require('util')
6
+ const p_exec = promisify(exec);
7
+ const args = process.argv.slice(2);
8
+ ; (async function () {
9
+
10
+ try {
11
+ await p_exec('tsx -v')
12
+ try {
13
+ const cli_path = path.resolve(__dirname,'../src/cli/index.ts')
14
+ // const cli_path = path.resolve(__dirname,'../dist/gen_api_types.cli.min.js')
15
+
16
+ const cp = spawn('tsx',[cli_path,...args],{ stdio: 'inherit',shell: true })
17
+ cp.on('error',err => console.error('执行 tsx 命令失败',err))
18
+
19
+
20
+
21
+ } catch (error) {
22
+ // console.error('执行 tsx 命令失败',error)
23
+ }
24
+
25
+
26
+ } catch (error) {
27
+ console.error('请安装 tsx',error)
28
+ }
29
+
30
+ })();
31
+
package/package.json CHANGED
@@ -1,35 +1,47 @@
1
- {
2
- "name": "gen-api-types",
3
- "version": "1.0.3",
4
- "description": "",
5
- "main": "./src/index.ts",
6
- "bin": {
7
- "gen-api-types": "src/cli/index.ts"
8
- },
9
- "scripts": {
10
- "test": "vitest",
11
- "build": "webpack",
12
- "publish:patch": "npm version patch && npm publish"
13
- },
14
- "keywords": [],
15
- "files": [
16
- "src",
17
- "tsconfig.json",
18
- "package.json"
19
- ],
20
- "author": "",
21
- "license": "ISC",
22
- "packageManager": "pnpm@10.7.1",
23
- "dependencies": {
24
- "reflect-metadata": "^0.2.2",
25
- "ts-morph": "^27.0.0",
26
- "tsx": "^4.20.5"
27
- },
28
- "devDependencies": {
29
- "@types/node": "^24.3.1",
30
- "ts-loader": "^9.5.4",
31
- "vitest": "^3.2.4",
32
- "webpack": "^5.101.0",
33
- "webpack-cli": "^6.0.1"
34
- }
35
- }
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
+ }
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "exclude": [],
3
- "include": ["src", "test", "test2/"],
3
+ "include": ["src", "test"],
4
4
  "compilerOptions": {
5
5
  /* Visit https://aka.ms/tsconfig to read more about this file */
6
6