feat: 装饰器与装饰器路由注册

This commit is contained in:
手瓜一十雪 committed 2025-11-14 19:49:13 +08:00
1 parent 4f850cd4dc
commit eaa23b4719
13 files changed
+233 -28

No files matched your search

@@ -0,0 +1,6 @@
import { OneBotAction } from './OneBotAction';
export const AutoRegisterRouter: Array<new (...args: any[]) => OneBotAction<unknown, unknown>> = [];
export function ActionHandler(target: new (...args: any[]) => OneBotAction<unknown, unknown>) {
AutoRegisterRouter.push(target);
}
+30 -24
View File
@@ -136,8 +136,9 @@ import { DownloadFileRecordStream } from './stream/DownloadFileRecordStream';
import { DownloadFileImageStream } from './stream/DownloadFileImageStream';
import { TestDownloadStream } from './stream/TestStreamDownload';
import { UploadFileStream } from './stream/UploadFileStream';
import { AutoRegisterRouter } from './auto-register';
export function createActionMap (obContext: NapCatOneBot11Adapter, core: NapCatCore) {
export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
const actionHandlers = [
new CleanStreamTempFile(obContext, core),
new DownloadFileStream(obContext, core),
@@ -288,30 +289,35 @@ export function createActionMap (obContext: NapCatOneBot11Adapter, core: NapCatC
new GetCollectionList(obContext, core),
];
type HandlerUnion = typeof actionHandlers[number];
type MapType = {
[H in HandlerUnion as H['actionName']]: H;
} & {
[H in HandlerUnion as `${H['actionName']}_async`]: H;
} & {
[H in HandlerUnion as `${H['actionName']}_rate_limited`]: H;
};
type HandlerUnion = typeof actionHandlers[number];
type MapType = {
[H in HandlerUnion as H['actionName']]: H;
} & {
[H in HandlerUnion as `${H['actionName']}_async`]: H;
} & {
[H in HandlerUnion as `${H['actionName']}_rate_limited`]: H;
};
const _map = new Map<keyof MapType, HandlerUnion>();
const _map = new Map<keyof MapType, HandlerUnion>();
actionHandlers.forEach(h => {
_map.set(h.actionName as keyof MapType, h);
_map.set(`${h.actionName}_async` as keyof MapType, h);
_map.set(`${h.actionName}_rate_limited` as keyof MapType, h);
});
// function get<K extends keyof MapType>(key: K): MapType[K];
// function get<K extends keyof MapType>(key: K): null;
// function get<K extends keyof MapType>(key: K): HandlerUnion | null | MapType[K]
function get<K extends keyof MapType> (key: K): MapType[K] | undefined {
return _map.get(key as keyof MapType) as MapType[K] | undefined;
}
return { get };
actionHandlers.forEach(h => {
_map.set(h.actionName as keyof MapType, h);
_map.set(`${h.actionName}_async` as keyof MapType, h);
_map.set(`${h.actionName}_rate_limited` as keyof MapType, h);
});
AutoRegisterRouter.forEach((ActionClass) => {
const handlerInstance = new ActionClass(obContext, core); // 延迟实例化
const actionName = handlerInstance.actionName; // 获取 actionName
_map.set(actionName as keyof MapType, handlerInstance as HandlerUnion);
_map.set(`${actionName}_async` as keyof MapType, handlerInstance as HandlerUnion);
_map.set(`${actionName}_rate_limited` as keyof MapType, handlerInstance as HandlerUnion);
});
// function get<K extends keyof MapType>(key: K): MapType[K];
// function get<K extends keyof MapType>(key: K): null;
// function get<K extends keyof MapType>(key: K): HandlerUnion | null | MapType[K]
function get<K extends keyof MapType>(key: K): MapType[K] | undefined {
return _map.get(key as keyof MapType) as MapType[K] | undefined;
}
return { get };
}
export type ActionMap = ReturnType<typeof createActionMap>;
+2
View File
@@ -10,6 +10,8 @@ export interface InvalidCheckResult {
}
export const ActionName = {
TestAutoRegister01: 'test_auto_register_01',
TestAutoRegister02: 'test_auto_register_02',
// 所有 Normal Stream Api 表示并未流传输 表示与流传输有关
CleanStreamTempFile: 'clean_stream_temp_file',
@@ -0,0 +1,17 @@
# 自动注册路由测试
## 参与工具
vite-auto-include 自动化收集所有文件并引入
@vitejs/plugin-react-swc 包含对装饰器的支持
## 支持示例
```typescript
@ActionHandler
export default class TestAutoRegister02 extends OneBotAction<void, string> {
override actionName = ActionName.TestAutoRegister02;
async _handle (_payload: void): Promise<string> {
return 'AutoRegister Router Test';
}
}
```
@@ -0,0 +1,12 @@
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { ActionName } from '@/napcat-onebot/action/router';
import { ActionHandler } from '../auto-register';
@ActionHandler
export default class TestAutoRegister01 extends OneBotAction<void, string> {
override actionName = ActionName.TestAutoRegister01;
async _handle (_payload: void): Promise<string> {
return 'AutoRegister Router Test';
}
}
@@ -0,0 +1,12 @@
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { ActionName } from '@/napcat-onebot/action/router';
import { ActionHandler } from '../auto-register';
@ActionHandler
export default class TestAutoRegister02 extends OneBotAction<void, string> {
override actionName = ActionName.TestAutoRegister02;
async _handle (_payload: void): Promise<string> {
return 'AutoRegister Router Test';
}
}