gen-api-types 1.0.18 → 1.0.19
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 +30 -7
- package/README.md +31 -6
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -19,7 +19,7 @@ With this tool, we can mark request interface classes and methods through TypeSc
|
|
|
19
19
|
1. npm installation
|
|
20
20
|
|
|
21
21
|
```shell
|
|
22
|
-
npm install gen-api-types
|
|
22
|
+
npm install gen-api-types
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
#### Usage
|
|
@@ -51,8 +51,8 @@ export class TestApi {
|
|
|
51
51
|
As shown in the code above:
|
|
52
52
|
|
|
53
53
|
- `@gen_type_c()` marks an API class.
|
|
54
|
-
- `@gen_type_m()` marks a method to execute and convert. The decorator registers the task; the Runner invokes the API method after the CLI has loaded the modules.
|
|
55
|
-
- `args: any[]` contains the arguments passed to the method.
|
|
54
|
+
- `@gen_type_m()` marks a method to execute and convert. The decorator registers the task; the Runner invokes the API method after the CLI has loaded the modules. Both static and non-static methods are supported.
|
|
55
|
+
- `args: any[]` contains the arguments passed to the method.
|
|
56
56
|
- `typeName: string` is the generated type name. If omitted, it defaults to `Response_${ClassName}_${MethodName}`.
|
|
57
57
|
- The marked class is only a decorator carrier; the Runner does not instantiate it. Non-static methods must not depend on `this` or instance fields.
|
|
58
58
|
|
|
@@ -69,9 +69,9 @@ Decorator options may span multiple lines:
|
|
|
69
69
|
|
|
70
70
|
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.json`.
|
|
71
71
|
|
|
72
|
-
The CLI
|
|
72
|
+
The CLI applies a default 5-second execution timeout to each API method. Timeouts, synchronous exceptions, and rejected promises are reported as execution failures. A timeout stops waiting for the result but cannot cancel an underlying request that has already started.
|
|
73
73
|
|
|
74
|
-
##### 2. Execute command
|
|
74
|
+
##### 2. Execute command (`gen-api-types`, `gat`)
|
|
75
75
|
|
|
76
76
|
```shell
|
|
77
77
|
npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
@@ -96,18 +96,41 @@ You can also use it by configuring scripts in package.json:
|
|
|
96
96
|
```json
|
|
97
97
|
{
|
|
98
98
|
"scripts": {
|
|
99
|
-
"
|
|
99
|
+
"gat": "gat -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
Available commands:
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
"bin": {
|
|
108
|
+
"gen-api-types": "bin/index.js",
|
|
109
|
+
"gat": "bin/index.js"
|
|
110
|
+
},
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The CLI scans `.ts` files in the input directories to find marked classes, methods, and their modules. It then imports those modules so decorators can register tasks. After all modules have loaded, the Runner invokes the registered API methods and converts successful results into a type declaration file. Each API method has a default execution timeout of 5 seconds; registered methods run concurrently.
|
|
105
114
|
|
|
106
115
|
Command output:
|
|
107
116
|
|
|
108
117
|
```shell
|
|
118
|
+
# When there are no errors
|
|
109
119
|
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
110
120
|
✅ API type generation completed: /path/to/api-types.d.ts
|
|
121
|
+
|
|
122
|
+
# If a request fails, its type is skipped; types are generated only for successful requests.
|
|
123
|
+
sourceFilesGlob [ 'test/api/**/*.ts' ]
|
|
124
|
+
❌ SampleApi.testError execute error: ReferenceError: arg is not defined
|
|
125
|
+
|
|
126
|
+
Request results:
|
|
127
|
+
┌───────────┬──────────────────────────────────────────┐
|
|
128
|
+
│ (index) │ Values │
|
|
129
|
+
├───────────┼──────────────────────────────────────────┤
|
|
130
|
+
│ ✔️ Success │ 'SampleApi.getWeather SampleApi.getList' │
|
|
131
|
+
│ ❌ Error │ 'SampleApi.testError' │
|
|
132
|
+
└───────────┴──────────────────────────────────────────┘
|
|
133
|
+
✅ API type generation completed: /path/to/api-types.d.ts
|
|
111
134
|
```
|
|
112
135
|
|
|
113
136
|
##### 3. Using the types
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
1.npm 安装
|
|
20
20
|
|
|
21
21
|
```shell
|
|
22
|
-
npm install gen-api-types
|
|
22
|
+
npm install gen-api-types
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
#### 使用说明
|
|
@@ -51,8 +51,8 @@ export class TestApi {
|
|
|
51
51
|
如上面代码所示:
|
|
52
52
|
|
|
53
53
|
- `@gen_type_c()` 标记 API 类。
|
|
54
|
-
- `@gen_type_m()` 标记需要执行并生成类型的方法;装饰器负责登记任务,API 方法会在 CLI 加载完模块后由 Runner
|
|
55
|
-
- `args: any[]`
|
|
54
|
+
- `@gen_type_m()` 标记需要执行并生成类型的方法;装饰器负责登记任务,API 方法会在 CLI 加载完模块后由 Runner 调用,支持静态方法和非静态方法
|
|
55
|
+
- `args: any[]` 调用方法时传入的参数
|
|
56
56
|
- `typeName: string` 是生成的类型名称;不指定时默认为 `Response_${类名}_${方法名}`。
|
|
57
57
|
- 标记类仅用于承载装饰器;Runner 不会创建类实例。普通方法不要依赖 `this` 或实例字段。
|
|
58
58
|
|
|
@@ -71,7 +71,7 @@ export class TestApi {
|
|
|
71
71
|
|
|
72
72
|
CLI 默认会为单个 API 方法设置 5 秒执行超时。超时、同步异常或 Promise rejection 都会被记录为该方法的执行失败;超时只能停止等待,不能取消已经发出的底层请求。
|
|
73
73
|
|
|
74
|
-
##### 2. 执行命令
|
|
74
|
+
##### 2. 执行命令 (`gen-api-types`、`gat`)
|
|
75
75
|
|
|
76
76
|
```shell
|
|
77
77
|
npx gen-api-types -o output_dir -O output_file_name ./api_dir1 ./api_dir2
|
|
@@ -96,18 +96,43 @@ Options:
|
|
|
96
96
|
```json
|
|
97
97
|
{
|
|
98
98
|
"scripts": {
|
|
99
|
-
"
|
|
99
|
+
"gat": "gat -o output_dir -O output_file_name ./api_dir1 ./api_dir2"
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
可用的的命令:
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
"bin": {
|
|
108
|
+
"gen-api-types": "bin/index.js",
|
|
109
|
+
"gat": "bin/index.js"
|
|
110
|
+
},
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
CLI 会扫描输入目录中的 `.ts` 文件,找到标记的类和方法及其所属模块。随后动态导入这些模块,使装饰器登记任务;全部模块加载完成后,Runner 才调用登记的 API 方法,并将成功结果转换为类型声明文件。每个 API 方法的默认执行超时为 5 秒;多个已登记方法会并发执行。
|
|
105
114
|
|
|
106
115
|
命令输出:
|
|
107
116
|
|
|
108
117
|
```shell
|
|
118
|
+
# 无错误时
|
|
109
119
|
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
110
120
|
✅ API 类型生成完成: /path/to/api-types.d.ts
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# 包含请求错误时,会忽略该条请求的类型,只生成 Success 的类型
|
|
124
|
+
|
|
125
|
+
sourceFilesGlob [ 'test/api/**/*.ts' ]
|
|
126
|
+
❌ SampleApi.testError execute error: ReferenceError: arg is not defined
|
|
127
|
+
|
|
128
|
+
请求结果:
|
|
129
|
+
┌───────────┬──────────────────────────────────────────┐
|
|
130
|
+
│ (index) │ Values │
|
|
131
|
+
├───────────┼──────────────────────────────────────────┤
|
|
132
|
+
│ ✔️ Success │ 'SampleApi.getWeather SampleApi.getList' │
|
|
133
|
+
│ ❌ Error │ 'SampleApi.testError' │
|
|
134
|
+
└───────────┴──────────────────────────────────────────┘
|
|
135
|
+
✅ API 类型生成完成: /Users/xuejiangping/Desktop/demo/gen-api-types/test/output/api-types.d.ts
|
|
111
136
|
```
|
|
112
137
|
|
|
113
138
|
##### 3. 使用类型
|