gen-api-types 1.0.17 โ 1.0.18
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 +6 -14
- package/README.md +6 -14
- package/package.json +3 -2
- package/src/cli/index.ts +21 -111
- package/src/constant/index.ts +3 -0
- package/src/decotators/index.ts +22 -43
- package/src/generation/index.ts +11 -0
- package/src/generation/moduleLoader.ts +13 -0
- package/src/generation/registry.ts +25 -0
- package/src/generation/runner.ts +25 -0
- package/src/generation/scanner.ts +52 -0
- package/src/generation/types.ts +11 -0
- package/src/transformer/index.ts +14 -59
- package/src/transformer/typeTransformer.ts +62 -0
- package/src/utils/decoratorHelper.ts +38 -0
- package/src/utils/index.ts +26 -1
- package/src/state/index.ts +0 -45
package/README.en.md
CHANGED
|
@@ -11,7 +11,7 @@ With this tool, we can mark request interface classes and methods through TypeSc
|
|
|
11
11
|
> Note:
|
|
12
12
|
>
|
|
13
13
|
> 1. Because this tool uses TypeScript method decorators, APIs must be written as **API classes + API methods**.
|
|
14
|
-
> 2. The CLI dynamically imports
|
|
14
|
+
> 2. The CLI dynamically imports modules containing marked methods. Module-level code runs during import; decorators only register tasks, and the CLI calls the registered API methods afterward.
|
|
15
15
|
> 3. API methods are executed for real in the CLI process. Make sure required environment variables, network access, and authentication are available.
|
|
16
16
|
|
|
17
17
|
#### Installation
|
|
@@ -51,10 +51,10 @@ 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.
|
|
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
55
|
- `args: any[]` contains the arguments passed to the method. Both static and non-static methods are supported.
|
|
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
|
|
|
59
59
|
Decorator options may span multiple lines:
|
|
60
60
|
|
|
@@ -101,21 +101,13 @@ You can also use it by configuring scripts in package.json:
|
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
The CLI scans `.ts` files in the input directories
|
|
104
|
+
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 declaration file. Each API method has a default execution timeout of 5 seconds; registered methods run concurrently.
|
|
105
105
|
|
|
106
106
|
Command output:
|
|
107
107
|
|
|
108
108
|
```shell
|
|
109
|
-
๐ Start generating API types...
|
|
110
109
|
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
111
|
-
|
|
112
|
-
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
113
|
-
โ (index) โ Values โ
|
|
114
|
-
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
115
|
-
โ โ๏ธ successList โ 'TestApi.getList TestApi.getWeather' โ
|
|
116
|
-
โ โ errorList โ '' โ
|
|
117
|
-
โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
118
|
-
โ
API type generation completed
|
|
110
|
+
โ
API type generation completed: /path/to/api-types.d.ts
|
|
119
111
|
```
|
|
120
112
|
|
|
121
113
|
##### 3. Using the types
|
|
@@ -178,7 +170,7 @@ export default defineConfig({
|
|
|
178
170
|
})
|
|
179
171
|
```
|
|
180
172
|
|
|
181
|
-
The plugin processes `.ts` and `.tsx` files and supports single-line and multi-line decorator options.
|
|
173
|
+
The plugin processes `.ts` and `.tsx` files and supports single-line and multi-line decorator options. It removes decorators by the names `gen_type_c` and `gen_type_m`.
|
|
182
174
|
|
|
183
175
|
The Vite plugin only affects Vite's transform pipeline. It is not involved when the CLI imports and executes API modules.
|
|
184
176
|
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
> ๆณจๆ๏ผ
|
|
12
12
|
>
|
|
13
13
|
> 1. ็ฑไบ้่ฆไฝฟ็จ TypeScript ๆนๆณ่ฃ
้ฅฐๅจ๏ผๆฅๅฃ้่ฆไปฅ **API ็ฑป + API ๆนๆณ** ็ๅฝขๅผไนฆๅใ
|
|
14
|
-
> 2. CLI
|
|
14
|
+
> 2. CLI ไผๅจๆๅฏผๅ
ฅๅ
ๅซๆ ่ฎฐๆนๆณ็ๆจกๅใๆจกๅ้กถๅฑไปฃ็ ไผๅจๅฏผๅ
ฅๆถๆง่ก๏ผ่ฃ
้ฅฐๅจๅช็ป่ฎฐไปปๅก๏ผCLI ้ๅๆ่ฐ็จๅทฒ็ป่ฎฐ็ API ๆนๆณใ
|
|
15
15
|
> 3. API ๆนๆณไผๅจ CLI ่ฟ็จไธญ็ๅฎๆง่ก๏ผ่ฏท็กฎไฟ่ฟ่กๆ้็็ฏๅขๅ้ใ็ฝ็ปๆ้ๅ้ดๆ้
็ฝฎๅทฒ็ปๅๅคๅฅฝใ
|
|
16
16
|
|
|
17
17
|
#### ๅฎ่ฃ
ๆ็จ
|
|
@@ -51,10 +51,10 @@ export class TestApi {
|
|
|
51
51
|
ๅฆไธ้ขไปฃ็ ๆ็คบ:
|
|
52
52
|
|
|
53
53
|
- `@gen_type_c()` ๆ ่ฎฐ API ็ฑปใ
|
|
54
|
-
- `@gen_type_m()`
|
|
54
|
+
- `@gen_type_m()` ๆ ่ฎฐ้่ฆๆง่กๅนถ็ๆ็ฑปๅ็ๆนๆณ๏ผ่ฃ
้ฅฐๅจ่ด่ดฃ็ป่ฎฐไปปๅก๏ผAPI ๆนๆณไผๅจ CLI ๅ ่ฝฝๅฎๆจกๅๅ็ฑ Runner ่ฐ็จใ
|
|
55
55
|
- `args: any[]` ๆฏ่ฐ็จๆนๆณๆถไผ ๅ
ฅ็ๅๆฐ๏ผๆฏๆ้ๆๆนๆณๅ้้ๆๆนๆณใ
|
|
56
56
|
- `typeName: string` ๆฏ็ๆ็็ฑปๅๅ็งฐ๏ผไธๆๅฎๆถ้ป่ฎคไธบ `Response_${็ฑปๅ}_${ๆนๆณๅ}`ใ
|
|
57
|
-
-
|
|
57
|
+
- ๆ ่ฎฐ็ฑปไป
็จไบๆฟ่ฝฝ่ฃ
้ฅฐๅจ๏ผRunner ไธไผๅๅปบ็ฑปๅฎไพใๆฎ้ๆนๆณไธ่ฆไพ่ต `this` ๆๅฎไพๅญๆฎตใ
|
|
58
58
|
|
|
59
59
|
่ฃ
้ฅฐๅจๅๆฐๅฏไปฅ่ทจๅค่กไนฆๅ๏ผ
|
|
60
60
|
|
|
@@ -101,21 +101,13 @@ Options:
|
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
CLI ไผๆซๆ่พๅ
ฅ็ฎๅฝไธญ็ `.ts`
|
|
104
|
+
CLI ไผๆซๆ่พๅ
ฅ็ฎๅฝไธญ็ `.ts` ๆไปถ๏ผๆพๅฐๆ ่ฎฐ็็ฑปๅๆนๆณๅๅ
ถๆๅฑๆจกๅใ้ๅๅจๆๅฏผๅ
ฅ่ฟไบๆจกๅ๏ผไฝฟ่ฃ
้ฅฐๅจ็ป่ฎฐไปปๅก๏ผๅ
จ้จๆจกๅๅ ่ฝฝๅฎๆๅ๏ผRunner ๆ่ฐ็จ็ป่ฎฐ็ API ๆนๆณ๏ผๅนถๅฐๆๅ็ปๆ่ฝฌๆขไธบๅฃฐๆๆไปถใๆฏไธช API ๆนๆณ็้ป่ฎคๆง่ก่ถ
ๆถไธบ 5 ็ง๏ผๅคไธชๅทฒ็ป่ฎฐๆนๆณไผๅนถๅๆง่กใ
|
|
105
105
|
|
|
106
106
|
ๅฝไปค่พๅบ๏ผ
|
|
107
107
|
|
|
108
108
|
```shell
|
|
109
|
-
๐ ๅผๅง็ๆAPI็ฑปๅ...
|
|
110
109
|
sourceFilesGlob [ 'src\\**\\*.ts' ]
|
|
111
|
-
|
|
112
|
-
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
113
|
-
โ (index) โ Values โ
|
|
114
|
-
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
115
|
-
โ โ๏ธ successList โ 'TestApi.getList TestApi.getWeather' โ
|
|
116
|
-
โ โ errorList โ '' โ
|
|
117
|
-
โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
118
|
-
โ
API ็ฑปๅ็ๆๅฎๆ
|
|
110
|
+
โ
API ็ฑปๅ็ๆๅฎๆ๏ผ /path/to/api-types.d.ts
|
|
119
111
|
```
|
|
120
112
|
|
|
121
113
|
##### 3. ไฝฟ็จ็ฑปๅ
|
|
@@ -178,7 +170,7 @@ export default defineConfig({
|
|
|
178
170
|
})
|
|
179
171
|
```
|
|
180
172
|
|
|
181
|
-
ๆไปถๅชๅค็ `.ts`
|
|
173
|
+
ๆไปถๅชๅค็ `.ts` ๆไปถ๏ผๅนถๆฏๆๅ่กๆๅค่ก่ฃ
้ฅฐๅจๅๆฐใๆไปถๆ `gen_type_c`ใ`gen_type_m` ่ฟไธคไธช่ฃ
้ฅฐๅจๅ็งฐ็งป้ค่ฃ
้ฅฐๅจใ
|
|
182
174
|
|
|
183
175
|
Vite ๆไปถๅชๅฝฑๅ Vite ็่ฝฌๆขๆต็จ๏ผไธๅไธ CLI ็ API ๆง่กๆต็จใ
|
|
184
176
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gen-api-types",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"description": "ไธไธช่ชๅจ็ๆ่ฏทๆฑๆฅๅฃ่ฟๅ็ฑปๅ็ cli ๅฐๅทฅๅ
ท",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"exports": {
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"generate types",
|
|
27
27
|
"TypeScript",
|
|
28
28
|
"Cli",
|
|
29
|
-
"API"
|
|
29
|
+
"API",
|
|
30
|
+
"gat"
|
|
30
31
|
],
|
|
31
32
|
"files": [
|
|
32
33
|
"bin",
|
package/src/cli/index.ts
CHANGED
|
@@ -2,137 +2,47 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
import * as path from 'path';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { TypeTransformer } from '../transformer';
|
|
11
|
-
import { formatResultList } from '../utils';
|
|
12
|
-
|
|
13
|
-
// console.log('positionals', positionals)
|
|
14
|
-
|
|
15
|
-
const sourceFilesGlob = positionals.map(dir => path.normalize(`${dir}/**/*.ts`))
|
|
16
|
-
const out_put_target = path.resolve(output_dir, output_file)
|
|
17
|
-
|
|
18
|
-
// console.log('arg', _arg)
|
|
19
|
-
// console.log('sourceFilesGlob', sourceFilesGlob)
|
|
20
|
-
// debugger
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
function getModulePathSet() {
|
|
24
|
-
console.log('sourceFilesGlob', sourceFilesGlob)
|
|
25
|
-
const modulePathSet: Set<string> = new Set()
|
|
26
|
-
// 2. ไฝฟ็จts-morphๅๅปบ้กน็ฎ๏ผไพฟไบ่งฃๆๆบ็
|
|
27
|
-
// const project = new Project({ tsConfigFilePath: ts_config_path });
|
|
28
|
-
const project = new Project({
|
|
29
|
-
tsConfigFilePath: ts_config_path
|
|
30
|
-
});
|
|
31
|
-
project.addSourceFilesAtPaths(sourceFilesGlob);
|
|
32
|
-
// console.log('project.getSourceFiles().length', project.getSourceFiles().length)
|
|
33
|
-
// debugger
|
|
34
|
-
// 3. ้ๅๆๆๆบๆไปถ
|
|
35
|
-
for (const sourceFile of project.getSourceFiles()) {
|
|
36
|
-
|
|
37
|
-
const classes = sourceFile.getClasses();
|
|
38
|
-
for (const classDeclaration of classes) {
|
|
39
|
-
const c_deco = classDeclaration.getDecorator(DECO_NAME_C)
|
|
40
|
-
if (!c_deco) continue
|
|
41
|
-
const methods = classDeclaration.getMethods();
|
|
42
|
-
for (const method of methods) {
|
|
43
|
-
const className = classDeclaration.getName()!;
|
|
44
|
-
const methodName = method.getName();
|
|
45
|
-
const fullMethodName = `${className}.${methodName}`;
|
|
46
|
-
|
|
47
|
-
// 4. ๆฃๆฅๆนๆณๆฏๅฆ่ขซๆไปฌ็่ฃ
้ฅฐๅจๆ ่ฎฐ
|
|
48
|
-
const m_deco = method.getDecorator(DECO_NAME_M)
|
|
49
|
-
if (!m_deco) continue
|
|
50
|
-
executeState.emit(executeState.ADD_TASK, fullMethodName)
|
|
51
|
-
const modulePath = sourceFile.getFilePath()
|
|
52
|
-
if (!modulePathSet.has(modulePath)) modulePathSet.add(modulePath)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return modulePathSet
|
|
59
|
-
}
|
|
5
|
+
import { isExported, output_dir, output_file, positionals } from '../argv';
|
|
6
|
+
import { runner, scanner } from '../generation';
|
|
7
|
+
import { importApiModule } from '../generation/moduleLoader';
|
|
8
|
+
import { createDeclarationFile } from '../transformer';
|
|
9
|
+
import { formatResultList, getSourceFilesGlobFromEntrances, logResultList } from '../utils';
|
|
60
10
|
|
|
61
11
|
|
|
62
|
-
/**
|
|
63
|
-
* ๅผๅ
ฅๅ
ๅซๆ ่ฎฐ็ๆนๆณ็ๆจกๅ๏ผ่งฆๅ่ฃ
้ฅฐๅจๆง่ก๏ผ่ฎฐๅฝๆง่ก็ปๆ
|
|
64
|
-
* @param modulePathSet
|
|
65
|
-
* @returns
|
|
66
|
-
*/
|
|
67
|
-
async function importApiModule<T extends string>(modulePathSet: Set<T>) {
|
|
68
|
-
return Promise.all(
|
|
69
|
-
Array.from(modulePathSet).map(modulePath => import(pathToFileURL(modulePath).href))
|
|
70
|
-
)
|
|
71
12
|
|
|
72
|
-
|
|
13
|
+
async function main() {
|
|
73
14
|
|
|
74
15
|
|
|
75
|
-
|
|
76
|
-
const ttf = new TypeTransformer({ filePath: out_put_target, isExported })
|
|
77
|
-
const tasks = successList.map(async ({ data, typeName, fullMethodName }) => {
|
|
78
|
-
try {
|
|
79
|
-
await ttf.transform(data, typeName)
|
|
80
|
-
return { fullMethodName }
|
|
81
|
-
} catch (error) {
|
|
82
|
-
console.error('transform error', error)
|
|
83
|
-
return { fullMethodName, error }
|
|
84
|
-
}
|
|
85
|
-
})
|
|
86
|
-
return Promise.all(tasks)
|
|
87
|
-
}
|
|
16
|
+
try {
|
|
88
17
|
|
|
18
|
+
const sourceFilesGlob = getSourceFilesGlobFromEntrances(positionals)
|
|
19
|
+
console.log('sourceFilesGlob', sourceFilesGlob,)
|
|
89
20
|
|
|
21
|
+
const modules = scanner.scan(sourceFilesGlob)
|
|
22
|
+
if (modules.size === 0) {
|
|
23
|
+
console.warn('โ ๏ธ ๆชๆพๅฐ้่ฆ่ฝฌๆข็API,่ฏทๆฃๆฅapi_dir ๅ gen_type่ฃ
้ฅฐๅจๆ ๆณจๆฏๅฆๆญฃ็กฎ!')
|
|
24
|
+
process.exit(0)
|
|
25
|
+
}
|
|
90
26
|
|
|
27
|
+
await importApiModule(Array.from(modules.keys()))
|
|
91
28
|
|
|
92
|
-
|
|
93
|
-
console.log('๐ ๅผๅง็ๆAPI็ฑปๅ...');
|
|
94
|
-
const modulePathSet = getModulePathSet();
|
|
95
|
-
if (modulePathSet.size == 0) {
|
|
29
|
+
const executeResultList = await runner.run()
|
|
96
30
|
|
|
97
|
-
console.error('โ ๏ธ ๆชๆพๅฐ้่ฆ่ฝฌๆข็API,่ฏทๆฃๆฅapi_dir ๅ gen_type่ฃ
้ฅฐๅจๆ ๆณจๆฏๅฆๆญฃ็กฎ!')
|
|
98
|
-
process.exit(1)
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
await importApiModule(modulePathSet);
|
|
102
|
-
// executeState.addListener(executeState.TASKLIST_CLEAR, executeResultList=>{
|
|
103
|
-
// })
|
|
104
|
-
|
|
105
|
-
const executeResultList = await executeState.promise
|
|
106
31
|
const { successList: executeSuccessList, errorList: executeErrorList } = formatResultList(executeResultList)
|
|
107
|
-
if (executeErrorList.length)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
"โ executeErrorList": executeErrorList.map(item => item.fullMethodName).join(' ')
|
|
112
|
-
})
|
|
113
|
-
console.groupEnd()
|
|
114
|
-
}
|
|
115
|
-
const transformList = await createDeclarationFile(executeSuccessList)
|
|
32
|
+
if (executeErrorList.length) logResultList('่ฏทๆฑ็ปๆ๏ผ', executeSuccessList, executeErrorList)
|
|
33
|
+
|
|
34
|
+
const out_put_target = path.resolve(output_dir, output_file)
|
|
35
|
+
const transformList = await createDeclarationFile(executeSuccessList, out_put_target, isExported)
|
|
116
36
|
const { successList: transformSuccessList, errorList: transformErrorList } = formatResultList(transformList)
|
|
117
37
|
|
|
118
|
-
if (transformErrorList.length)
|
|
119
|
-
console.group('่ฝฌๆข็ปๆ๏ผ')
|
|
120
|
-
console.table({
|
|
121
|
-
"โ๏ธ transformSuccessList": transformSuccessList.map(item => item.fullMethodName).join(' '),
|
|
122
|
-
"โ transformErrorList": transformErrorList.map(item => item.fullMethodName).join(' ')
|
|
123
|
-
})
|
|
124
|
-
console.groupEnd()
|
|
38
|
+
if (transformErrorList.length) logResultList('่ฝฌๆข็ปๆ๏ผ', transformSuccessList, transformErrorList)
|
|
125
39
|
|
|
126
|
-
}
|
|
127
40
|
if (transformSuccessList.length) console.log('โ
API ็ฑปๅ็ๆๅฎๆ๏ผ', out_put_target);
|
|
128
41
|
} catch (error) {
|
|
129
42
|
console.error('โ ๅบ้ไบ', error)
|
|
130
43
|
process.exitCode = 1
|
|
131
44
|
}
|
|
132
45
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
46
|
}
|
|
137
47
|
|
|
138
48
|
main()
|
package/src/constant/index.ts
CHANGED
package/src/decotators/index.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
// import 'reflect-metadata';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { executeApiMethod } from "../utils/index.ts";
|
|
3
|
+
import { registry } from "../generation/index.ts";
|
|
4
|
+
import { createClassDecorator, createMethodDecorator } from "../utils/decoratorHelper.ts";
|
|
6
5
|
|
|
7
|
-
// ๅฎไนไธไธชๅฏไธ็metadata key
|
|
8
6
|
|
|
9
7
|
|
|
10
8
|
export interface GenTypeOptions {
|
|
@@ -13,43 +11,24 @@ export interface GenTypeOptions {
|
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
|
|
16
|
-
const EXEC_TIMEOUT = 5_000
|
|
17
|
-
export class GatDecorator {
|
|
18
|
-
static [DECO_NAME_C]() {
|
|
19
|
-
return function <T>(target: T) {
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
static [DECO_NAME_M]({ args = [], typeName }: GenTypeOptions = {}) {
|
|
23
|
-
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
(async () => {
|
|
27
|
-
const className = typeof target == 'function' ? target.name : target.constructor.name
|
|
28
|
-
const fullMethodName = `${className}.${propertyKey}`
|
|
29
|
-
console.log('fullMethodName', fullMethodName)
|
|
30
|
-
typeName ??= `Response_${className}_${propertyKey}`
|
|
31
|
-
let resultInfo = null
|
|
32
|
-
try {
|
|
33
|
-
const apiMethod = descriptor.value as Function
|
|
34
|
-
// console.log(`๐ Calling ${fullMethodName} with args:`, args);
|
|
35
|
-
|
|
36
|
-
const data = await executeApiMethod({
|
|
37
|
-
timeout: EXEC_TIMEOUT,
|
|
38
|
-
method: () => apiMethod.apply(target, args)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
resultInfo = { data, typeName, fullMethodName }
|
|
42
|
-
} catch (error) {
|
|
43
|
-
console.error(`โ ${fullMethodName} execute error:`, error)
|
|
44
|
-
resultInfo = { error, fullMethodName, typeName }
|
|
45
|
-
} finally {
|
|
46
|
-
executeState.emit(executeState.EXECUTE_END, resultInfo)
|
|
47
|
-
}
|
|
48
|
-
})();
|
|
49
|
-
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
14
|
|
|
54
|
-
|
|
55
|
-
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
export const gen_type_c = createClassDecorator((ctx, options) => {
|
|
19
|
+
|
|
20
|
+
})
|
|
21
|
+
export const gen_type_m = createMethodDecorator(async (
|
|
22
|
+
{ target, propertyKey, descriptor },
|
|
23
|
+
{ typeName, args }: GenTypeOptions = { args: [] }
|
|
24
|
+
) => {
|
|
25
|
+
const className = typeof target == 'function' ? target.name : target.constructor.name
|
|
26
|
+
const fullMethodName = `${className}.${propertyKey}`
|
|
27
|
+
typeName ??= `Response_${className}_${propertyKey}`
|
|
28
|
+
const handler = () => (descriptor.value).apply(target, args)
|
|
29
|
+
registry.register({
|
|
30
|
+
key: descriptor.value,
|
|
31
|
+
meta: { typeName, name: fullMethodName, handler }
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ๅผๅ
ฅๅ
ๅซๆ ่ฎฐ็ๆนๆณ็ๆจกๅ๏ผ่งฆๅ่ฃ
้ฅฐๅจๆง่ก
|
|
5
|
+
* @param modulePathSet
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export function importApiModule(modulePathSet: string[]) {
|
|
9
|
+
return Promise.all(
|
|
10
|
+
Array.from(modulePathSet).map(modulePath => import(pathToFileURL(modulePath).href))
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ApiMethod } from "./types"
|
|
2
|
+
|
|
3
|
+
type Task = {
|
|
4
|
+
key: string | symbol | object
|
|
5
|
+
meta: {
|
|
6
|
+
name: string
|
|
7
|
+
handler: ApiMethod
|
|
8
|
+
typeName: string
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class Registry {
|
|
13
|
+
private _taskMap: Map<Task['key'], Task['meta']> = new Map()
|
|
14
|
+
get taskMetas() {
|
|
15
|
+
return Array.from(this._taskMap.values())
|
|
16
|
+
}
|
|
17
|
+
get taskeys() {
|
|
18
|
+
return Array.from(this._taskMap.keys())
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
register(task: Task) {
|
|
22
|
+
this._taskMap.set(task.key, task.meta)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { EXEC_TIMEOUT } from "../constant"
|
|
2
|
+
import { executeApiMethod } from "../utils"
|
|
3
|
+
import { Registry } from "./registry"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class Runner {
|
|
7
|
+
constructor(private registry: Registry) {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
run() {
|
|
11
|
+
return Promise.all(
|
|
12
|
+
this.registry.taskMetas.map(async ({ name: fullMethodName, handler, typeName }) => {
|
|
13
|
+
try {
|
|
14
|
+
const data = await executeApiMethod({ timeout: EXEC_TIMEOUT, method: handler })
|
|
15
|
+
return { data, typeName, fullMethodName }
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error(`โ ${fullMethodName} execute error:`, error)
|
|
18
|
+
return { error, fullMethodName, typeName }
|
|
19
|
+
} finally {
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Project } from "ts-morph";
|
|
2
|
+
import { DECO_NAME_C, DECO_NAME_M } from "../constant";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
type ScanResult = Map<string, Set<string>>
|
|
6
|
+
|
|
7
|
+
export class Scanner {
|
|
8
|
+
constructor(public ts_config_path?: string) { }
|
|
9
|
+
public cacheResult: ScanResult | null = null
|
|
10
|
+
/**
|
|
11
|
+
* ๆซๆๆๅฎ็ๆบๆไปถ่ทฏๅพ๏ผๆฅๆพ่ขซ่ฃ
้ฅฐๅจๆ ่ฎฐ็็ฑปๅๆนๆณ๏ผๅนถ่ฟๅไธไธชๆ ๅฐ๏ผ้ฎไธบๆจกๅ่ทฏๅพ๏ผๅผไธบ่ฏฅๆจกๅไธญ่ขซๆ ่ฎฐ็ๆนๆณ้ๅใ
|
|
12
|
+
* @param sourceFilesGlob
|
|
13
|
+
* @returns modulepath -> methods
|
|
14
|
+
*/
|
|
15
|
+
scan(sourceFilesGlob: string[]) {
|
|
16
|
+
const moduleMap: ScanResult = new Map()
|
|
17
|
+
const project = new Project({ tsConfigFilePath: this.ts_config_path, });
|
|
18
|
+
project.addSourceFilesAtPaths(sourceFilesGlob);
|
|
19
|
+
for (const sourceFile of project.getSourceFiles()) {
|
|
20
|
+
|
|
21
|
+
const classes = sourceFile.getClasses();
|
|
22
|
+
for (const classDeclaration of classes) {
|
|
23
|
+
const c_deco = classDeclaration.getDecorator(DECO_NAME_C)
|
|
24
|
+
if (!c_deco) continue
|
|
25
|
+
const methods = classDeclaration.getMethods();
|
|
26
|
+
for (const method of methods) {
|
|
27
|
+
const className = classDeclaration.getName()!;
|
|
28
|
+
const methodName = method.getName();
|
|
29
|
+
const fullMethodName = `${className}.${methodName}`;
|
|
30
|
+
|
|
31
|
+
const m_deco = method.getDecorator(DECO_NAME_M)
|
|
32
|
+
if (!m_deco) continue
|
|
33
|
+
const modulePath = sourceFile.getFilePath()
|
|
34
|
+
|
|
35
|
+
const methods = moduleMap.get(modulePath) ?? new Set()
|
|
36
|
+
methods.add(fullMethodName)
|
|
37
|
+
moduleMap.set(modulePath, methods)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
this.cacheResult = moduleMap
|
|
42
|
+
return moduleMap
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get methods() {
|
|
46
|
+
return Array.from(this.cacheResult?.values()!).flatMap(item => [...item])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
}
|
package/src/transformer/index.ts
CHANGED
|
@@ -1,62 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ExecuteApiMethodResult } from "../generation/types"
|
|
2
|
+
import { TypeTransformer } from "./typeTransformer"
|
|
2
3
|
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
} else if (typeof value === "boolean") {
|
|
16
|
-
return "boolean";
|
|
17
|
-
} else if (value === null) {
|
|
18
|
-
return "null";
|
|
19
|
-
} else if (!value) {
|
|
20
|
-
return "undefined";
|
|
21
|
-
} else {
|
|
22
|
-
return "any";
|
|
23
|
-
}
|
|
4
|
+
export function createDeclarationFile(executeList: ExecuteApiMethodResult[], out_put_target: string, isExported: boolean) {
|
|
5
|
+
const ttf = new TypeTransformer({ filePath: out_put_target, isExported })
|
|
6
|
+
const tasks = executeList.map(async ({ data, typeName, fullMethodName }) => {
|
|
7
|
+
try {
|
|
8
|
+
await ttf.transform(data, typeName)
|
|
9
|
+
return { fullMethodName }
|
|
10
|
+
} catch (error) {
|
|
11
|
+
console.error('transform error', error)
|
|
12
|
+
return { fullMethodName, error }
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
return Promise.all(tasks)
|
|
24
16
|
}
|
|
25
17
|
|
|
26
|
-
|
|
27
|
-
export class TypeTransformer {
|
|
28
|
-
project: Project;
|
|
29
|
-
sourceFile: SourceFile;
|
|
30
|
-
isExported: boolean
|
|
31
|
-
constructor({ projectOptions = {}, filePath = "types.d.ts", isExported = false } = {}) {
|
|
32
|
-
this.project = new Project(projectOptions);
|
|
33
|
-
this.sourceFile = this.createSourceFile(filePath);
|
|
34
|
-
this.isExported = isExported
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
createSourceFile(filePath: string) {
|
|
38
|
-
// try {
|
|
39
|
-
// return project.addSourceFileAtPath(filePath);
|
|
40
|
-
// } catch (error) {
|
|
41
|
-
// return project.createSourceFile(filePath);
|
|
42
|
-
// }
|
|
43
|
-
return this.project.createSourceFile(filePath, '', { overwrite: true });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// ๆ นๆฎ่ฟ่กๆถๅฏน่ฑก็ๆ็ฑปๅๅฎไน
|
|
47
|
-
|
|
48
|
-
generateTypeFromObject(val: any, typeName: string) {
|
|
49
|
-
// 4. ๆทปๅ ็ฑปๅๅซๅ
|
|
50
|
-
this.sourceFile.addTypeAlias({
|
|
51
|
-
name: typeName,
|
|
52
|
-
isExported: this.isExported,
|
|
53
|
-
type: inferType(val)
|
|
54
|
-
});
|
|
55
|
-
// if (typeName == 'Res_Weather') throw Error('res_weather test error')
|
|
56
|
-
}
|
|
57
|
-
async transform(res: any, typeName: string, isAsync = true) {
|
|
58
|
-
this.generateTypeFromObject(res, typeName)
|
|
59
|
-
await this.sourceFile.save();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Project, SourceFile } from "ts-morph";
|
|
2
|
+
|
|
3
|
+
function inferType(value: any): string {
|
|
4
|
+
if (Array.isArray(value)) {
|
|
5
|
+
if (value.length === 0) return "any[]";
|
|
6
|
+
// ๅ็ฌฌไธไธชๅ
็ด ็ฑปๅ๏ผๅ่ฎพๆฐ็ปๅ
็ด ็ฑปๅไธ่ด
|
|
7
|
+
return `${inferType(value[0])}[]`;
|
|
8
|
+
} else if (typeof value === "object" && value !== null) {
|
|
9
|
+
const props = Object.entries(value).map(([k, v]) => `${k}: ${inferType(v)}`).join(";\n");
|
|
10
|
+
return `{${props}}`;
|
|
11
|
+
} else if (typeof value === "string") {
|
|
12
|
+
return "string";
|
|
13
|
+
} else if (typeof value === "number") {
|
|
14
|
+
return "number";
|
|
15
|
+
} else if (typeof value === "boolean") {
|
|
16
|
+
return "boolean";
|
|
17
|
+
} else if (value === null) {
|
|
18
|
+
return "null";
|
|
19
|
+
} else if (!value) {
|
|
20
|
+
return "undefined";
|
|
21
|
+
} else {
|
|
22
|
+
return "any";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export class TypeTransformer {
|
|
28
|
+
project: Project;
|
|
29
|
+
sourceFile: SourceFile;
|
|
30
|
+
isExported: boolean
|
|
31
|
+
constructor({ projectOptions = {}, filePath = "types.d.ts", isExported = false } = {}) {
|
|
32
|
+
this.project = new Project(projectOptions);
|
|
33
|
+
this.sourceFile = this.createSourceFile(filePath);
|
|
34
|
+
this.isExported = isExported
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
createSourceFile(filePath: string) {
|
|
38
|
+
// try {
|
|
39
|
+
// return project.addSourceFileAtPath(filePath);
|
|
40
|
+
// } catch (error) {
|
|
41
|
+
// return project.createSourceFile(filePath);
|
|
42
|
+
// }
|
|
43
|
+
return this.project.createSourceFile(filePath, '', { overwrite: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ๆ นๆฎ่ฟ่กๆถๅฏน่ฑก็ๆ็ฑปๅๅฎไน
|
|
47
|
+
|
|
48
|
+
generateTypeFromObject(val: any, typeName: string) {
|
|
49
|
+
// 4. ๆทปๅ ็ฑปๅๅซๅ
|
|
50
|
+
this.sourceFile.addTypeAlias({
|
|
51
|
+
name: typeName,
|
|
52
|
+
isExported: this.isExported,
|
|
53
|
+
type: inferType(val)
|
|
54
|
+
});
|
|
55
|
+
// if (typeName == 'Res_Weather') throw Error('res_weather test error')
|
|
56
|
+
}
|
|
57
|
+
async transform(res: any, typeName: string, isAsync = true) {
|
|
58
|
+
this.generateTypeFromObject(res, typeName)
|
|
59
|
+
await this.sourceFile.save();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
interface MethodContext {
|
|
6
|
+
target: any,
|
|
7
|
+
propertyKey: string,
|
|
8
|
+
descriptor: PropertyDescriptor
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export function createMethodDecorator<Options>(
|
|
13
|
+
handler: (ctx: MethodContext, options?: Options) => any,
|
|
14
|
+
defaultOptions?: Options
|
|
15
|
+
) {
|
|
16
|
+
|
|
17
|
+
return (options?: Options) =>
|
|
18
|
+
(target: any, propertyKey: string, descriptor: PropertyDescriptor) =>
|
|
19
|
+
handler({ target, propertyKey, descriptor }, options ?? defaultOptions)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
interface ClassContext {
|
|
24
|
+
target: any
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
export function createClassDecorator<Options>(
|
|
29
|
+
handler: (ctx: ClassContext, options?: Options,) => any,
|
|
30
|
+
defaultOptions?: Options
|
|
31
|
+
) {
|
|
32
|
+
|
|
33
|
+
return (options?: Options) => (target: any) => handler({ target }, options ?? defaultOptions)
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
package/src/utils/index.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import path from "path"
|
|
2
|
+
import { ApiMethod } from "../generation/types"
|
|
3
|
+
|
|
1
4
|
type ResultInfoBase = { error?: any }
|
|
2
5
|
export function formatResultList<T extends ResultInfoBase>(list: T[]) {
|
|
3
6
|
return list.reduce((acc, item) => {
|
|
@@ -20,11 +23,15 @@ export function promiseWithResolvers<T>() {
|
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
23
30
|
export async function executeApiMethod<T>({
|
|
24
31
|
method,
|
|
25
32
|
timeout = 10 * 1000
|
|
26
33
|
}: {
|
|
27
|
-
method:
|
|
34
|
+
method: ApiMethod<T>
|
|
28
35
|
timeout?: number
|
|
29
36
|
}): Promise<T> {
|
|
30
37
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
@@ -45,3 +52,21 @@ export async function executeApiMethod<T>({
|
|
|
45
52
|
}
|
|
46
53
|
}
|
|
47
54
|
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
export function getSourceFilesGlobFromEntrances(entrances: string[]): string[] {
|
|
61
|
+
return entrances.map(dir => path.normalize(`${dir}/**/*.ts`))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
export function logResultList(title: string, successLis: any[], errorList: any[]) {
|
|
66
|
+
console.group(`\r\n${title}`)
|
|
67
|
+
console.table({
|
|
68
|
+
"โ๏ธ Success": successLis.map(item => item.fullMethodName).join(' '),
|
|
69
|
+
"โ Error": errorList.map(item => item.fullMethodName).join(' ')
|
|
70
|
+
})
|
|
71
|
+
console.groupEnd()
|
|
72
|
+
}
|
package/src/state/index.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "events"
|
|
2
|
-
import { promiseWithResolvers } from "../utils/index.ts"
|
|
3
|
-
|
|
4
|
-
export type ExecuteApiMethodResult = {
|
|
5
|
-
data?: any, typeName: string, fullMethodName: string, error?: any
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export class ExecuteState extends EventEmitter {
|
|
14
|
-
|
|
15
|
-
EXECUTE_END = 'executeEnd'
|
|
16
|
-
TASKLIST_CLEAR = 'taskClear'
|
|
17
|
-
ADD_TASK = 'addTask'
|
|
18
|
-
private resultList: ExecuteApiMethodResult[] = []
|
|
19
|
-
private taskList: string[] = []
|
|
20
|
-
promiseWithResolvers = promiseWithResolvers<ExecuteApiMethodResult[]>()
|
|
21
|
-
constructor() {
|
|
22
|
-
super()
|
|
23
|
-
this.init()
|
|
24
|
-
}
|
|
25
|
-
init() {
|
|
26
|
-
this.addListener(this.EXECUTE_END, (result: ExecuteApiMethodResult) => {
|
|
27
|
-
this.resultList.push(result)
|
|
28
|
-
if (this.resultList.length === this.taskList.length) {
|
|
29
|
-
this.emit(this.TASKLIST_CLEAR, this.resultList)
|
|
30
|
-
this.promiseWithResolvers?.resolve(this.resultList)
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
this.addListener(this.ADD_TASK, (taskName) => {
|
|
35
|
-
this.taskList.push(taskName)
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
get promise() {
|
|
40
|
-
return this.promiseWithResolvers?.promise
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const executeState = new ExecuteState()
|