gen-api-types 1.0.5 โ 1.0.9
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 +149 -112
- package/README.md +150 -113
- package/bin/index.js +21 -31
- package/package.json +51 -47
- package/src/argv/index.ts +75 -70
- package/src/cli/index.ts +196 -196
- package/src/constant/index.ts +5 -3
- package/src/decotators/index.ts +31 -27
- package/src/index.ts +3 -2
- package/src/transformer/index.ts +62 -62
- package/src/utils/index.ts +8 -8
- package/tsconfig.json +115 -115
package/README.en.md
CHANGED
|
@@ -1,112 +1,149 @@
|
|
|
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
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
- `@
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
> Note:
|
|
55
|
+
|
|
56
|
+
If TypeScript reports the decorator error: "The runtime will invoke the decorator with 2 arguments, but the decorator expects 3", set `compilerOptions.experimentalDecorators` to `true` in tsconfig.
|
|
57
|
+
|
|
58
|
+
##### 2. Execute command
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Parameter description:
|
|
65
|
+
|
|
66
|
+
```shell
|
|
67
|
+
Usage: npx gen-api-types [options] [api_dirs...]
|
|
68
|
+
|
|
69
|
+
Options:
|
|
70
|
+
-h, --help Output help information
|
|
71
|
+
-r, --project_root <path> Project root directory
|
|
72
|
+
-O, --output_file <path> Output file name
|
|
73
|
+
-o, --output_dir <path> Output directory
|
|
74
|
+
-t, --ts_config_path <path> Path to tsconfig.json file
|
|
75
|
+
--isExported Generate exported type declarations
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
You can also use it by configuring scripts in package.json:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
"scripts": {
|
|
83
|
+
"gen_types": "gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Command output:
|
|
89
|
+
|
|
90
|
+
```shell
|
|
91
|
+
๐ Start generating API types...
|
|
92
|
+
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
93
|
+
๐ Processing UserApi.getList ...
|
|
94
|
+
๐ Processing UserApi.getWeather ...
|
|
95
|
+
Request results:
|
|
96
|
+
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
97
|
+
โ (index) โ Values โ
|
|
98
|
+
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
99
|
+
โ โ๏ธ successList โ 'UserApi.getList UserApi.getWeather' โ
|
|
100
|
+
โ โ errorList โ '' โ
|
|
101
|
+
โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
102
|
+
โ
API type generation completed
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
##### 3. Using the types
|
|
106
|
+
|
|
107
|
+
By default, a type definition file index.d.ts is generated, and the type declarations are not exported:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
type XXX = { name: string };
|
|
111
|
+
type Response_TestApi_getWeather = {...}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
You can configure `include` in tsconfig.json to reference the file:
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
// tsconfig.json
|
|
118
|
+
{
|
|
119
|
+
"include": ["index.d.ts"]
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Or reference it directly at the top of the interface module file:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
/// <reference path="./index.d.ts" />
|
|
127
|
+
export class TestApi {
|
|
128
|
+
@gen_type_m()
|
|
129
|
+
static getWeather(): Promise<Response_TestApi_getWeather> {
|
|
130
|
+
return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// data is now typed as Response_TestApi_getWeather
|
|
135
|
+
const data = await TestApi.getWeather()
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
If you want to generate exported type declarations, add `--isExported` when running the command:
|
|
139
|
+
|
|
140
|
+
```shell
|
|
141
|
+
npx gen-api-types --isExported -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Generated output example:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
export type XXX = { name: string };
|
|
148
|
+
export type Response_TestApi_getWeather = {...}
|
|
149
|
+
```
|
package/README.md
CHANGED
|
@@ -1,113 +1,150 @@
|
|
|
1
|
-
# gen-api-types
|
|
2
|
-
|
|
3
|
-
#### ไป็ป
|
|
4
|
-
|
|
5
|
-
๐ ไธไธช่ชๅจ็ๆ่ฏทๆฑๆฅๅฃ่ฟๅ็ฑปๅ็ cli ๅฐๅทฅๅ
ท
|
|
6
|
-
|
|
7
|
-
ๅจ ts ้กน็ฎไธญ๏ผ็ปๅธธ้่ฆ็ผๅๆฅๅฃ่ฟๅ็ฑปๅใไฝๆฏๆฏๆฌก้ฝ่ฆๆฅ็ๆฅๅฃๆๆกฃ๏ผๆๅจ็ผๅ้ๅธธ้บป็ฆใๅฆๆ้ๅฐไธไบ็ฌฌไธๆนๆฅๅฃๆ่
ๆฅๅฃๆๆกฃไธๅ
จ็ๆ
ๅต๏ผ่ฟ้่ฆๅ
่ฐ่ฏๆฅๅฃๅ๏ผๅ็ผๅๆฅๅฃ่ฟๅ็ฑปๅ๏ผๅพไปคไบบๅคด็ผ
|
|
8
|
-
|
|
9
|
-
ๅๅฉ่ฟไธชๅทฅๅ
ท๏ผๆไปฌๅฏไปฅ้่ฟ ts ่ฃ
้ฅฐๅจๆฅๆ ่ฎฐ่ฏทๆฑๆฅๅฃ็็ฑปๅๆนๆณ๏ผ็ถๅๅจๆ่ฐ็จ่ฟไบๆฅๅฃ๏ผๅนถๅฐๆฅๅฃ่ฟๅ็ๆฐๆฎ่ฝฌๆขๆ ts ็ฑปๅๆไปถ๏ผ่ฟๆ ทๆไปฌๅฐฑๅฏไปฅๅจ้กน็ฎไธญ็ดๆฅไฝฟ็จไบ
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- `@
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
+
> ๆณจๆ๏ผ
|
|
56
|
+
|
|
57
|
+
่ฅไฝฟ็จ่ฃ
้ฅฐๅจๆถtsๆฅ้: "่ฟ่กๆถๅฐไฝฟ็จ 2 ไธช่ชๅ้่ฐ็จไฟฎ้ฅฐๅจ๏ผไฝไฟฎ้ฅฐๅจ้่ฆ 3 ไธช",่ฏทๅฐtsconfigไธญ`compilerOptions.experimentalDecorators`่ฎพ็ฝฎไธบ`true`
|
|
58
|
+
|
|
59
|
+
##### 2. ๆง่กๅฝไปค
|
|
60
|
+
|
|
61
|
+
```shell
|
|
62
|
+
npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
ๅๆฐ่ฏดๆ๏ผ
|
|
66
|
+
|
|
67
|
+
```shell
|
|
68
|
+
Usage: npx gen-api-types [options] [api_dirs...]
|
|
69
|
+
|
|
70
|
+
Options:
|
|
71
|
+
-h, --help ่พๅบๅธฎๅฉไฟกๆฏ
|
|
72
|
+
-r, --project_root <path> ้กน็ฎๆ น็ฎๅฝ
|
|
73
|
+
-O, --output_file <path> ่พๅบๆไปถๅ
|
|
74
|
+
-o, --output_dir <path> ่พๅบ็ฎๅฝ
|
|
75
|
+
-t, --ts_config_path <path> tsconfig.json ๆไปถ่ทฏๅพ
|
|
76
|
+
--isExported ็ๆๅฏผๅบ็็ฑปๅๅฃฐๆ
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
ๅฝ็ถ๏ผไนๅฏไปฅ้่ฟ้
็ฝฎ package.json ไธญ็ scripts ๆฅไฝฟ็จ
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"scripts": {
|
|
84
|
+
"gen_types": "gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
ๅฝไปค่พๅบ๏ผ
|
|
90
|
+
|
|
91
|
+
```shell
|
|
92
|
+
๐ ๅผๅง็ๆAPI็ฑปๅ...
|
|
93
|
+
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
94
|
+
๐ ๅค็ TestApi.getList ...
|
|
95
|
+
๐ ๅค็ TestApi.getWeather ...
|
|
96
|
+
่ฏทๆฑ็ปๆ๏ผ
|
|
97
|
+
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
98
|
+
โ (index) โ Values โ
|
|
99
|
+
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
100
|
+
โ โ๏ธ successList โ 'TestApi.getList TestApi.getWeather' โ
|
|
101
|
+
โ โ errorList โ '' โ
|
|
102
|
+
โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
103
|
+
โ
API ็ฑปๅ็ๆๅฎๆ
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
##### 3. ไฝฟ็จ็ฑปๅ
|
|
107
|
+
|
|
108
|
+
้ป่ฎค็ๆ็ฑปๅๆไปถ index.d.ts๏ผไธ็ฑปๅๅฃฐๆๆฒกๆๅฏผๅบ
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
type XXX = { name: string };
|
|
112
|
+
type Response_TestApi_getWeather = {...}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
ๅฏๅจ`tsconfig.json`ไธญ้
็ฝฎ`include`ๅผ็จๆไปถ
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
// tsconfig.json
|
|
119
|
+
{
|
|
120
|
+
"include": ["index.d.ts"]
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
ๆ่
็ดๆฅๅจๆฅๅฃๆจกๅๆไปถ้กถ้จ้่ฟ reference ๅผ็จ:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
/// <reference path="./index.d.ts" />
|
|
128
|
+
export class TestApi {
|
|
129
|
+
@gen_type_m()
|
|
130
|
+
static getWeather(): Promise<Response_TestApi_getWeather> {
|
|
131
|
+
return fetch('http://t.weather.sojson.com/api/weather/city/101030100').then(r => r.json())
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
//ๆญคๆถdata็็ฑปๅไธบResponse_TestApi_getWeather
|
|
136
|
+
const data = await TestApi.getWeather()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
ๅฆๆๅธๆ็ๆๅฏๅฏผๅบ็็ฑปๅๅฃฐๆ๏ผๅฏไปฅๅจๆง่กๅฝไปคๆถๆทปๅ `--isExported`๏ผ
|
|
140
|
+
|
|
141
|
+
```shell
|
|
142
|
+
npx gen-api-types --isExported -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
็ๆ็ปๆ็คบไพ๏ผ
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
export type XXX = { name: string };
|
|
149
|
+
export type Response_TestApi_getWeather = {...}
|
|
150
|
+
```
|
package/bin/index.js
CHANGED
|
@@ -1,31 +1,21 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
const path = require('path');
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
+
});
|