mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-12-19 05:05:44 +08:00
feat: 实现runtime信息注入
This commit is contained in:
parent
6421bb4f5c
commit
1d816ea36b
@ -4,7 +4,9 @@ import { NapCatCore } from '@/core';
|
||||
import { NapCatOneBot11Adapter, OB11Return } from '@/onebot';
|
||||
import { NetworkAdapterConfig } from '../config/config';
|
||||
import { TSchema } from '@sinclair/typebox';
|
||||
|
||||
export interface OneBotRuntimeContext {
|
||||
protocol_new: boolean;
|
||||
}
|
||||
export class OB11Response {
|
||||
private static createResponse<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null): OB11Return<T> {
|
||||
return {
|
||||
@ -57,13 +59,18 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
public async handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig): Promise<OB11Return<ReturnDataType | null>> {
|
||||
public async handle(
|
||||
payload: PayloadType,
|
||||
adaptername: string,
|
||||
config: NetworkAdapterConfig,
|
||||
runtime: OneBotRuntimeContext = { protocol_new: false }
|
||||
): Promise<OB11Return<ReturnDataType | null>> {
|
||||
const result = await this.check(payload);
|
||||
if (!result.valid) {
|
||||
return OB11Response.error(result.message, 400);
|
||||
}
|
||||
try {
|
||||
const resData = await this._handle(payload, adaptername, config);
|
||||
const resData = await this._handle(payload, adaptername, config, runtime);
|
||||
return OB11Response.ok(resData);
|
||||
} catch (e: unknown) {
|
||||
this.core.context.logger.logError('发生错误', e);
|
||||
@ -71,13 +78,19 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||
}
|
||||
}
|
||||
|
||||
public async websocketHandle(payload: PayloadType, echo: unknown, adaptername: string, config: NetworkAdapterConfig): Promise<OB11Return<ReturnDataType | null>> {
|
||||
public async websocketHandle(
|
||||
payload: PayloadType,
|
||||
echo: unknown,
|
||||
adaptername: string,
|
||||
config: NetworkAdapterConfig,
|
||||
runtime: OneBotRuntimeContext
|
||||
): Promise<OB11Return<ReturnDataType | null>> {
|
||||
const result = await this.check(payload);
|
||||
if (!result.valid) {
|
||||
return OB11Response.error(result.message, 1400, echo);
|
||||
}
|
||||
try {
|
||||
const resData = await this._handle(payload, adaptername, config);
|
||||
const resData = await this._handle(payload, adaptername, config, runtime);
|
||||
return OB11Response.ok(resData, echo);
|
||||
} catch (e: unknown) {
|
||||
this.core.context.logger.logError('发生错误', e);
|
||||
@ -85,5 +98,5 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||
}
|
||||
}
|
||||
|
||||
abstract _handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig): Promise<ReturnDataType>;
|
||||
abstract _handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig, runtime: OneBotRuntimeContext): Promise<ReturnDataType>;
|
||||
}
|
||||
|
||||
@ -116,12 +116,16 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
||||
hello.message = 'NapCat4 Is Running';
|
||||
return res.json(hello);
|
||||
}
|
||||
const actionName = req.path.split('/')[1];
|
||||
let actionName = req.path.split('/')[1];
|
||||
const isV2 = actionName === 'v2'
|
||||
actionName = isV2 ? actionName = req.path.split('/')[2] : actionName;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const action = this.actions.get(actionName as any);
|
||||
if (action) {
|
||||
try {
|
||||
const result = await action.handle(payload, this.name, this.config);
|
||||
const result = await action.handle(payload, this.name, this.config, {
|
||||
protocol_new: isV2
|
||||
});
|
||||
return res.json(result);
|
||||
} catch (error: unknown) {
|
||||
return res.json(OB11Response.error((error as Error)?.stack?.toString() || (error as Error)?.message || 'Error Handle', 200));
|
||||
@ -131,6 +135,7 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async handleRequest(req: Request, res: Response) {
|
||||
if (!this.isEnable) {
|
||||
this.core.context.logger.log('[OneBot] [HTTP Server Adapter] Server is closed');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user