gen-api-types 1.0.5 โ†’ 1.0.8

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.en.md CHANGED
@@ -1,112 +1,145 @@
1
- # gen-api-types
2
-
3
- #### Introduction
4
-
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.
13
-
14
- #### Installation
15
-
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
- ```
81
-
82
- Command output:
83
-
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
- ```
98
-
99
- ##### 3. Using the types
100
-
101
- By default, a type definition file index.d.ts is generated without exports:
102
-
103
- ```ts
104
- type XXX = { name: string };
105
- type Response_UserApi_getWeather = {...}
106
- ```
107
-
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:
109
-
110
- ```ts
111
- /// <reference path="./index.d.ts" />
112
- ```
1
+ # gen-api-types
2
+
3
+ #### Introduction
4
+
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
+ >
13
+ > 1. Because this tool uses TypeScript decorators, and decorators currently (TypeScript 5.0) do not support decorating plain functions directly, APIs must be written as **API classes + static API methods**.
14
+ > 2. This tool needs to dynamically execute TypeScript code (importing API classes and calling the marked static API methods), so it runs through the bundled `tsx` dependency. No global `tsx` installation is required.
15
+
16
+ #### Installation
17
+
18
+ 1. npm installation
19
+
20
+ ```shell
21
+ npm install gen-api-types -D
22
+ ```
23
+
24
+ #### Usage
25
+
26
+ ##### 1. Mark interface class names and methods
27
+
28
+ ```ts
29
+ import { gen_type_c, gen_type_m } from 'gen-api-types'
30
+
31
+ @gen_type_c()
32
+ export class TestApi {
33
+ @gen_type_m({ args: [100], typeName: 'XXX' })
34
+ static async getList(id: number): Promise<XXX> {
35
+ return asleep(1000).then(() => {
36
+ return { name: 'zs', id }
37
+ })
38
+ }
39
+
40
+ @gen_type_m()
41
+ static getWeather(): Promise<Response_TestApi_getWeather> {
42
+ return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
43
+ }
44
+ }
45
+ ```
46
+
47
+ As shown in the code above:
48
+
49
+ - `@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.
50
+ - `@gen_type_m` decorator function marks the request methods that need to be converted. It can accept a configuration object with two fields:
51
+ 1. `typeName: string` Interface return type name. If not specified, the default name will be: `Response_${ClassName}_${MethodName}`
52
+ 2. `args: any[]` Method parameter list. The tool will pass this list when calling the request method.
53
+
54
+ ##### 2. Execute command
55
+
56
+ ```shell
57
+ npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
58
+ ```
59
+
60
+ Parameter description:
61
+
62
+ ```shell
63
+ Usage: npx gen-api-types [options] [api_dirs...]
64
+
65
+ Options:
66
+ -h, --help Output help information
67
+ -r, --project_root <path> Project root directory
68
+ -O, --output_file <path> Output file name
69
+ -o, --output_dir <path> Output directory
70
+ -t, --ts_config_path <path> Path to tsconfig.json file
71
+ --isExported Generate exported type declarations
72
+ ```
73
+
74
+ You can also use it by configuring scripts in package.json:
75
+
76
+ ```json
77
+ {
78
+ "scripts": {
79
+ "gen_types": "gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
80
+ }
81
+ }
82
+ ```
83
+
84
+ Command output:
85
+
86
+ ```shell
87
+ ๐Ÿš€ Start generating API types...
88
+ sourceFilesGlob [ 'src\\**\\*.ts' ]
89
+ ๐Ÿ“‹ Processing UserApi.getList ...
90
+ ๐Ÿ“‹ Processing UserApi.getWeather ...
91
+ Request results:
92
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
93
+ โ”‚ (index) โ”‚ Values โ”‚
94
+ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
95
+ โ”‚ โœ”๏ธ successList โ”‚ 'UserApi.getList UserApi.getWeather' โ”‚
96
+ โ”‚ โŒ errorList โ”‚ '' โ”‚
97
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
98
+ โœ… API type generation completed
99
+ ```
100
+
101
+ ##### 3. Using the types
102
+
103
+ By default, a type definition file index.d.ts is generated, and the type declarations are not exported:
104
+
105
+ ```ts
106
+ type XXX = { name: string };
107
+ type Response_TestApi_getWeather = {...}
108
+ ```
109
+
110
+ You can configure `include` in tsconfig.json to reference the file:
111
+
112
+ ```json
113
+ // tsconfig.json
114
+ {
115
+ "include": ["index.d.ts"]
116
+ }
117
+ ```
118
+
119
+ Or reference it directly at the top of the interface module file:
120
+
121
+ ```ts
122
+ /// <reference path="./index.d.ts" />
123
+ export class TestApi {
124
+ @gen_type_m()
125
+ static getWeather(): Promise<Response_TestApi_getWeather> {
126
+ return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
127
+ }
128
+ }
129
+
130
+ // data is now typed as Response_TestApi_getWeather
131
+ const data = await TestApi.getWeather()
132
+ ```
133
+
134
+ If you want to generate exported type declarations, add `--isExported` when running the command:
135
+
136
+ ```shell
137
+ npx gen-api-types --isExported -o output_dir -O output_file_name ./api_dir1 ./api_dir2
138
+ ```
139
+
140
+ Generated output example:
141
+
142
+ ```ts
143
+ export type XXX = { name: string };
144
+ export type Response_TestApi_getWeather = {...}
145
+ ```
package/README.md CHANGED
@@ -1,113 +1,146 @@
1
- # gen-api-types
2
-
3
- #### ไป‹็ป
4
-
5
- ๐Ÿš€ ไธ€ไธช่‡ชๅŠจ็”Ÿๆˆ่ฏทๆฑ‚ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹็š„ cli ๅฐๅทฅๅ…ท
6
-
7
- ๅœจ ts ้กน็›ฎไธญ๏ผŒ็ปๅธธ้œ€่ฆ็ผ–ๅ†™ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹ใ€‚ไฝ†ๆ˜ฏๆฏๆฌก้ƒฝ่ฆๆŸฅ็œ‹ๆŽฅๅฃๆ–‡ๆกฃ๏ผŒๆ‰‹ๅŠจ็ผ–ๅ†™้žๅธธ้บป็ƒฆใ€‚ๅฆ‚ๆžœ้‡ๅˆฐไธ€ไบ›็ฌฌไธ‰ๆ–นๆŽฅๅฃๆˆ–่€…ๆŽฅๅฃๆ–‡ๆกฃไธๅ…จ็š„ๆƒ…ๅ†ต๏ผŒ่ฟ˜้œ€่ฆๅ…ˆ่ฐƒ่ฏ•ๆŽฅๅฃๅŽ๏ผŒๅ†็ผ–ๅ†™ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹๏ผŒๅพˆไปคไบบๅคด็–ผ
8
-
9
- ๅ€ŸๅŠฉ่ฟ™ไธชๅทฅๅ…ท๏ผŒๆˆ‘ไปฌๅฏไปฅ้€š่ฟ‡ ts ่ฃ…้ฅฐๅ™จๆฅๆ ‡่ฎฐ่ฏทๆฑ‚ๆŽฅๅฃ็š„็ฑปๅ’Œๆ–นๆณ•๏ผŒ็„ถๅŽๅŠจๆ€่ฐƒ็”จ่ฟ™ไบ›ๆŽฅๅฃ๏ผŒๅนถๅฐ†ๆŽฅๅฃ่ฟ”ๅ›ž็š„ๆ•ฐๆฎ่ฝฌๆขๆˆ ts ็ฑปๅž‹ๆ–‡ไปถ๏ผŒ่ฟ™ๆ ทๆˆ‘ไปฌๅฐฑๅฏไปฅๅœจ้กน็›ฎไธญ็›ดๆŽฅไฝฟ็”จไบ†
10
-
11
- > ๆณจๆ„๏ผš
12
- > ่ฏฅๅทฅๅ…ท้œ€่ฆๅŠจๆ€ๆ‰ง่กŒ ts ไปฃ็ ๏ผˆ่ฐƒ็”จ้กน็›ฎไธญ็š„ๆŽฅๅฃๆจกๅ—๏ผ‰๏ผŒๅฟ…้กปไพ่ต– `tsx` ๆ‰ง่กŒๅทฅๅ…ท๏ผŒ่ฏทๅŠกๅฟ…ๅ…ˆๅ…จๅฑ€ๅฎ‰่ฃ… `tsx`๏ผŒ็กฎไฟ`tsx`ๅ‘ฝไปคๅฏ็”จใ€‚
13
-
14
- #### ๅฎ‰่ฃ…ๆ•™็จ‹
15
-
16
- 1.npm ๅฎ‰่ฃ…
17
-
18
- ```shell
19
- npm install tsx -g
20
- npm install gen-api-types -D
21
-
22
- ```
23
-
24
- #### ไฝฟ็”จ่ฏดๆ˜Ž
25
-
26
- ##### 1. ๆ ‡่ฎฐๆŽฅๅฃ็ฑปๅๅ’Œๆ–นๆณ•
27
-
28
- ```ts
29
- import { gen_type_c, gen_type_m } from 'gen-api-types'
30
-
31
- @gen_type_c()
32
- export class TestApi {
33
- @gen_type_m({ args: [100], typeName: 'XXX' })
34
- static async getList(id: number): Promise<XXX> {
35
- return asleep(1000).then(() => {
36
- return { name: 'zs', id }
37
- })
38
- }
39
-
40
- @gen_type_m()
41
- static getWeather(): Promise<Response_TestApi_getWeather> {
42
- return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
43
- }
44
- }
45
- ```
46
-
47
- ๅฆ‚ไธŠ้ขไปฃ็ ๆ‰€็คบ:
48
-
49
- - `@gen_type_c`่ฃ…้ฅฐๅ™จๅ‡ฝๆ•ฐ๏ผŒ็”จๆฅๆ ‡่ฎฐๆŽฅๅฃ็ฑปใ€‚ๅ› ไธบๅทฅๅ…ทไผšๅŠจๆ€ๅˆ†ๆžๆŒ‡ๅฎš็›ฎๅฝ•ไธ‹็š„ๆ‰€ๆœ‰ ts ๆ–‡ไปถ๏ผŒๆ ‡่ฎฐๆŽฅๅฃ็ฑป๏ผŒๅฏไปฅๅธฎๅŠฉๆˆ‘ไปฌๅฟซ้€ŸๅฎšไฝๆŽฅๅฃ็ฑป
50
- - `@gen_type_m`่ฃ…้ฅฐๅ™จๅ‡ฝๆ•ฐๆ ‡่ฎฐ้œ€่ฆ่ฝฌๆข็š„่ฏทๆฑ‚ๆ–นๆณ•ใ€‚ๅฎƒๅฏไปฅๆŽฅๆ”ถไธ€ไธช้…็ฝฎๅฏน่ฑก๏ผŒๅŒ…ๅซไธคไธชๅญ—ๆฎตใ€‚
51
- 1. `typeName: string` ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹ๅ็งฐ๏ผŒ่‹ฅไธๆŒ‡ๅฎš่ฏฅๅญ—ๆฎต๏ผŒ้ป˜่ฎค็”Ÿๆˆๅ็งฐไธบ๏ผš `Response_${็ฑปๅ}_${ๆ–นๆณ•ๅ}`
52
- 2. `args๏ผšany[] ` ๆ–นๆณ•ๅ‚ๆ•ฐๅˆ—่กจ,ๅทฅๅ…ท่ฐƒ็”จ่ฏทๆฑ‚ๆ–นๆณ•ๆ—ถ๏ผŒไผšๅฐ†ๅ‚ๆ•ฐๅˆ—่กจไผ ๅ…ฅ
53
-
54
- ##### 2. ๆ‰ง่กŒๅ‘ฝไปค
55
-
56
- ```shell
57
- npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
58
- ```
59
-
60
- ๅ‚ๆ•ฐ่ฏดๆ˜Ž๏ผš
61
-
62
- ```shell
63
- Usage: npx gen-api-types [options] [api_dirs...]
64
-
65
- Options:
66
- -h, --help ่พ“ๅ‡บๅธฎๅŠฉไฟกๆฏ
67
- -r, --project_root <path> ้กน็›ฎๆ น็›ฎๅฝ•
68
- -O, --output_file <path> ่พ“ๅ‡บๆ–‡ไปถๅ
69
- -o, --output_dir <path> ่พ“ๅ‡บ็›ฎๅฝ•
70
- -t, --ts_config_path <path> tsconfig.json ๆ–‡ไปถ่ทฏๅพ„
71
- ```
72
-
73
- ๅฝ“็„ถ๏ผŒไนŸๅฏไปฅ้€š่ฟ‡้…็ฝฎ package.json ไธญ็š„ scripts ๆฅไฝฟ็”จ
74
-
75
- ```json
76
- {
77
- "scripts": {
78
- "gen_types": "gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
79
- }
80
- }
81
- ```
82
-
83
- ๅ‘ฝไปค่พ“ๅ‡บ๏ผš
84
-
85
- ```shell
86
- ๐Ÿš€ ๅผ€ๅง‹็”ŸๆˆAPI็ฑปๅž‹...
87
- sourceFilesGlob [ 'src\\**\\*.ts' ]
88
- ๐Ÿ“‹ ๅค„็† UserApi.getList ...
89
- ๐Ÿ“‹ ๅค„็† UserApi.getWeather ...
90
- ่ฏทๆฑ‚็ป“ๆžœ๏ผš
91
- โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
92
- โ”‚ (index) โ”‚ Values โ”‚
93
- โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
94
- โ”‚ โœ”๏ธ successList โ”‚ 'UserApi.getList UserApi.getWeather' โ”‚
95
- โ”‚ โŒ errorList โ”‚ '' โ”‚
96
- โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
97
- โœ… API ็ฑปๅž‹็”ŸๆˆๅฎŒๆˆ
98
- ```
99
-
100
- ##### 3. ไฝฟ็”จ็ฑปๅž‹
101
-
102
- ้ป˜่ฎค็”Ÿๆˆ็ฑปๅž‹ๆ–‡ไปถ index.d.ts,ไธ”ๆฒกๆœ‰ๅฏผๅ‡บ
103
-
104
- ```ts
105
- type XXX = { name: string };
106
- type Response_UserApi_getWeather = {...}
107
- ```
108
-
109
- ๅฏๅœจ`tsconfig.json`ไธญ้…็ฝฎ`include`ๅผ•็”จๆ–‡ไปถ,ๆˆ–่€…็›ดๆŽฅๅœจๆŽฅๅฃๆจกๅ—ๆ–‡ไปถ้กถ้ƒจ้€šๅผ•็”จ:
110
-
111
- ```ts
112
- /// <reference path="./index.d.ts" />
113
- ```
1
+ # gen-api-types
2
+
3
+ #### ไป‹็ป
4
+
5
+ ๐Ÿš€ ไธ€ไธช่‡ชๅŠจ็”Ÿๆˆ่ฏทๆฑ‚ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹็š„ cli ๅฐๅทฅๅ…ท
6
+
7
+ ๅœจ ts ้กน็›ฎไธญ๏ผŒ็ปๅธธ้œ€่ฆ็ผ–ๅ†™ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹ใ€‚ไฝ†ๆ˜ฏๆฏๆฌก้ƒฝ่ฆๆŸฅ็œ‹ๆŽฅๅฃๆ–‡ๆกฃ๏ผŒๆ‰‹ๅŠจ็ผ–ๅ†™้žๅธธ้บป็ƒฆใ€‚ๅฆ‚ๆžœ้‡ๅˆฐไธ€ไบ›็ฌฌไธ‰ๆ–นๆŽฅๅฃๆˆ–่€…ๆŽฅๅฃๆ–‡ๆกฃไธๅ…จ็š„ๆƒ…ๅ†ต๏ผŒ่ฟ˜้œ€่ฆๅ…ˆ่ฐƒ่ฏ•ๆŽฅๅฃๅŽ๏ผŒๅ†็ผ–ๅ†™ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹๏ผŒๅพˆไปคไบบๅคด็–ผ
8
+
9
+ ๅ€ŸๅŠฉ่ฟ™ไธชๅทฅๅ…ท๏ผŒๆˆ‘ไปฌๅฏไปฅ้€š่ฟ‡ ts ่ฃ…้ฅฐๅ™จๆฅๆ ‡่ฎฐ่ฏทๆฑ‚ๆŽฅๅฃ็š„็ฑปๅ’Œๆ–นๆณ•๏ผŒ็„ถๅŽๅŠจๆ€่ฐƒ็”จ่ฟ™ไบ›ๆŽฅๅฃ๏ผŒๅนถๅฐ†ๆŽฅๅฃ่ฟ”ๅ›ž็š„ๆ•ฐๆฎ่ฝฌๆขๆˆ ts ็ฑปๅž‹ๆ–‡ไปถ๏ผŒ่ฟ™ๆ ทๆˆ‘ไปฌๅฐฑๅฏไปฅๅœจ้กน็›ฎไธญ็›ดๆŽฅไฝฟ็”จไบ†
10
+
11
+ > ๆณจๆ„๏ผš
12
+ >
13
+ > 1. ็”ฑไบŽ้œ€่ฆไฝฟ็”จๅˆฐts่ฃ…้ฅฐๅ™จ็‰นๆ€ง๏ผŒ่€Œ่ฃ…้ฅฐๅ™จ็›ฎๅ‰๏ผˆts 5.0๏ผ‰ไธๆ”ฏๆŒ็›ดๆŽฅๆ ‡่ฎฐๆ™ฎ้€šๅ‡ฝๆ•ฐ๏ผŒๆ‰€ไปฅๆˆ‘ไปฌ็š„ๆŽฅๅฃๅฟ…้กปไปฅ **ๆŽฅๅฃ็ฑป+้™ๆ€apiๆ–นๆณ•** ็š„ๅฝขๅผไนฆๅ†™
14
+ > 2. ่ฏฅๅทฅๅ…ท้œ€่ฆๅŠจๆ€ๆ‰ง่กŒ ts ไปฃ็ ๏ผˆimportๆŽฅๅฃ็ฑป๏ผŒ็„ถๅŽ่ฐƒ็”จๆ ‡่ฎฐ็š„้™ๆ€apiๆ–นๆณ•๏ผ‰๏ผŒๅ› ๆญคไผš้€š่ฟ‡ๅ†…็ฝฎไพ่ต–็š„ `tsx` ๆ‰ง่กŒๅทฅๅ…ท่ฟ่กŒ๏ผŒๆ— ้œ€้ขๅค–ๅ…จๅฑ€ๅฎ‰่ฃ… `tsx`ใ€‚
15
+
16
+ #### ๅฎ‰่ฃ…ๆ•™็จ‹
17
+
18
+ 1.npm ๅฎ‰่ฃ…
19
+
20
+ ```shell
21
+ npm install gen-api-types -D
22
+
23
+ ```
24
+
25
+ #### ไฝฟ็”จ่ฏดๆ˜Ž
26
+
27
+ ##### 1. ๆ ‡่ฎฐๆŽฅๅฃ็ฑปๅๅ’Œๆ–นๆณ•
28
+
29
+ ```ts
30
+ import { gen_type_c, gen_type_m } from 'gen-api-types'
31
+
32
+ @gen_type_c()
33
+ export class TestApi {
34
+ @gen_type_m({ args: [100], typeName: 'XXX' })
35
+ static async getList(id: number): Promise<XXX> {
36
+ return asleep(1000).then(() => {
37
+ return { name: 'zs', id }
38
+ })
39
+ }
40
+
41
+ @gen_type_m()
42
+ static getWeather(): Promise<Response_TestApi_getWeather> {
43
+ return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
44
+ }
45
+ }
46
+ ```
47
+
48
+ ๅฆ‚ไธŠ้ขไปฃ็ ๆ‰€็คบ:
49
+
50
+ - `@gen_type_c`่ฃ…้ฅฐๅ™จๅ‡ฝๆ•ฐ๏ผŒ็”จๆฅๆ ‡่ฎฐๆŽฅๅฃ็ฑปใ€‚ๅ› ไธบๅทฅๅ…ทไผšๅŠจๆ€ๅˆ†ๆžๆŒ‡ๅฎš็›ฎๅฝ•ไธ‹็š„ๆ‰€ๆœ‰ ts ๆ–‡ไปถ๏ผŒๆ ‡่ฎฐๆŽฅๅฃ็ฑป๏ผŒๅฏไปฅๅธฎๅŠฉๆˆ‘ไปฌๅฟซ้€ŸๅฎšไฝๆŽฅๅฃ็ฑป
51
+ - `@gen_type_m`่ฃ…้ฅฐๅ™จๅ‡ฝๆ•ฐๆ ‡่ฎฐ้œ€่ฆ่ฝฌๆข็š„่ฏทๆฑ‚ๆ–นๆณ•ใ€‚ๅฎƒๅฏไปฅๆŽฅๆ”ถไธ€ไธช้…็ฝฎๅฏน่ฑก๏ผŒๅŒ…ๅซไธคไธชๅญ—ๆฎตใ€‚
52
+ 1. `typeName: string` ๆŽฅๅฃ่ฟ”ๅ›ž็ฑปๅž‹ๅ็งฐ๏ผŒ่‹ฅไธๆŒ‡ๅฎš่ฏฅๅญ—ๆฎต๏ผŒ้ป˜่ฎค็”Ÿๆˆๅ็งฐไธบ๏ผš `Response_${็ฑปๅ}_${ๆ–นๆณ•ๅ}`
53
+ 2. `args๏ผšany[] ` ๆ–นๆณ•ๅ‚ๆ•ฐๅˆ—่กจ,ๅทฅๅ…ท่ฐƒ็”จ่ฏทๆฑ‚ๆ–นๆณ•ๆ—ถ๏ผŒไผšๅฐ†ๅ‚ๆ•ฐๅˆ—่กจไผ ๅ…ฅ
54
+
55
+ ##### 2. ๆ‰ง่กŒๅ‘ฝไปค
56
+
57
+ ```shell
58
+ npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
59
+ ```
60
+
61
+ ๅ‚ๆ•ฐ่ฏดๆ˜Ž๏ผš
62
+
63
+ ```shell
64
+ Usage: npx gen-api-types [options] [api_dirs...]
65
+
66
+ Options:
67
+ -h, --help ่พ“ๅ‡บๅธฎๅŠฉไฟกๆฏ
68
+ -r, --project_root <path> ้กน็›ฎๆ น็›ฎๅฝ•
69
+ -O, --output_file <path> ่พ“ๅ‡บๆ–‡ไปถๅ
70
+ -o, --output_dir <path> ่พ“ๅ‡บ็›ฎๅฝ•
71
+ -t, --ts_config_path <path> tsconfig.json ๆ–‡ไปถ่ทฏๅพ„
72
+ --isExported ็”Ÿๆˆๅฏผๅ‡บ็š„็ฑปๅž‹ๅฃฐๆ˜Ž
73
+ ```
74
+
75
+ ๅฝ“็„ถ๏ผŒไนŸๅฏไปฅ้€š่ฟ‡้…็ฝฎ package.json ไธญ็š„ scripts ๆฅไฝฟ็”จ
76
+
77
+ ```json
78
+ {
79
+ "scripts": {
80
+ "gen_types": "gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
81
+ }
82
+ }
83
+ ```
84
+
85
+ ๅ‘ฝไปค่พ“ๅ‡บ๏ผš
86
+
87
+ ```shell
88
+ ๐Ÿš€ ๅผ€ๅง‹็”ŸๆˆAPI็ฑปๅž‹...
89
+ sourceFilesGlob [ 'src\\**\\*.ts' ]
90
+ ๐Ÿ“‹ ๅค„็† TestApi.getList ...
91
+ ๐Ÿ“‹ ๅค„็† TestApi.getWeather ...
92
+ ่ฏทๆฑ‚็ป“ๆžœ๏ผš
93
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
94
+ โ”‚ (index) โ”‚ Values โ”‚
95
+ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
96
+ โ”‚ โœ”๏ธ successList โ”‚ 'TestApi.getList TestApi.getWeather' โ”‚
97
+ โ”‚ โŒ errorList โ”‚ '' โ”‚
98
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
99
+ โœ… API ็ฑปๅž‹็”ŸๆˆๅฎŒๆˆ
100
+ ```
101
+
102
+ ##### 3. ไฝฟ็”จ็ฑปๅž‹
103
+
104
+ ้ป˜่ฎค็”Ÿๆˆ็ฑปๅž‹ๆ–‡ไปถ index.d.ts๏ผŒไธ”็ฑปๅž‹ๅฃฐๆ˜Žๆฒกๆœ‰ๅฏผๅ‡บ
105
+
106
+ ```ts
107
+ type XXX = { name: string };
108
+ type Response_TestApi_getWeather = {...}
109
+ ```
110
+
111
+ ๅฏๅœจ`tsconfig.json`ไธญ้…็ฝฎ`include`ๅผ•็”จๆ–‡ไปถ
112
+
113
+ ```json
114
+ // tsconfig.json
115
+ {
116
+ "include": ["index.d.ts"]
117
+ }
118
+ ```
119
+
120
+ ๆˆ–่€…็›ดๆŽฅๅœจๆŽฅๅฃๆจกๅ—ๆ–‡ไปถ้กถ้ƒจ้€š่ฟ‡ reference ๅผ•็”จ:
121
+
122
+ ```ts
123
+ /// <reference path="./index.d.ts" />
124
+ export class TestApi {
125
+ @gen_type_m()
126
+ static getWeather(): Promise<Response_TestApi_getWeather> {
127
+ return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
128
+ }
129
+ }
130
+
131
+ //ๆญคๆ—ถdata็š„็ฑปๅž‹ไธบResponse_TestApi_getWeather
132
+ const data = await TestApi.getWeather()
133
+ ```
134
+
135
+ ๅฆ‚ๆžœๅธŒๆœ›็”Ÿๆˆๅฏๅฏผๅ‡บ็š„็ฑปๅž‹ๅฃฐๆ˜Ž๏ผŒๅฏไปฅๅœจๆ‰ง่กŒๅ‘ฝไปคๆ—ถๆทปๅŠ  `--isExported`๏ผš
136
+
137
+ ```shell
138
+ npx gen-api-types --isExported -o output_dir -O output_file_name ./api_dir1 ./api_dir2
139
+ ```
140
+
141
+ ็”Ÿๆˆ็ป“ๆžœ็คบไพ‹๏ผš
142
+
143
+ ```ts
144
+ export type XXX = { name: string };
145
+ export type Response_TestApi_getWeather = {...}
146
+ ```
package/bin/index.js CHANGED
@@ -1,31 +1,21 @@
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
-
1
+ #!/usr/bin/env node
2
+
3
+ const { spawn } = require('child_process');
4
+ const path = require('path');
5
+
6
+ const args = process.argv.slice(2);
7
+ const cliPath = path.resolve(__dirname, '../src/cli/index.ts');
8
+ const tsxCliPath = require.resolve('tsx/cli');
9
+
10
+ const cp = spawn(process.execPath, [tsxCliPath, cliPath, ...args], {
11
+ stdio: 'inherit',
12
+ });
13
+
14
+ cp.on('exit', code => {
15
+ process.exit(code ?? 1);
16
+ });
17
+
18
+ cp.on('error', err => {
19
+ console.error('ๆ‰ง่กŒ gen-api-types ๅคฑ่ดฅ', err);
20
+ process.exit(1);
21
+ });