mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-03-01 16:20:25 +00:00
feat: 标准化
This commit is contained in:
@@ -4,9 +4,10 @@ import { NapCatCore } from '@/core';
|
||||
import { NapCatOneBot11Adapter, OB11Return } from '@/onebot';
|
||||
import { NetworkAdapterConfig } from '../config/config';
|
||||
import { TSchema } from '@sinclair/typebox';
|
||||
import { StreamPacket, StreamPacketBasic, StreamStatus } from './stream/StreamBasic';
|
||||
|
||||
export class OB11Response {
|
||||
private static createResponse<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null): OB11Return<T> {
|
||||
private static createResponse<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null, useStream: boolean = false): OB11Return<T> {
|
||||
return {
|
||||
status,
|
||||
retcode,
|
||||
@@ -14,23 +15,24 @@ export class OB11Response {
|
||||
message,
|
||||
wording: message,
|
||||
echo,
|
||||
stream: useStream ? 'stream-action' : 'normal-action'
|
||||
};
|
||||
}
|
||||
|
||||
static res<T>(data: T, status: string, retcode: number, message: string = ''): OB11Return<T> {
|
||||
return this.createResponse(data, status, retcode, message);
|
||||
static res<T>(data: T, status: string, retcode: number, message: string = '', echo: unknown = null, useStream: boolean = false): OB11Return<T> {
|
||||
return this.createResponse(data, status, retcode, message, echo, useStream);
|
||||
}
|
||||
|
||||
static ok<T>(data: T, echo: unknown = null): OB11Return<T> {
|
||||
return this.createResponse(data, 'ok', 0, '', echo);
|
||||
static ok<T>(data: T, echo: unknown = null, useStream: boolean = false): OB11Return<T> {
|
||||
return this.createResponse(data, 'ok', 0, '', echo, useStream);
|
||||
}
|
||||
|
||||
static error(err: string, retcode: number, echo: unknown = null): OB11Return<null> {
|
||||
return this.createResponse(null, 'failed', retcode, err, echo);
|
||||
static error(err: string, retcode: number, echo: unknown = null, useStream: boolean = false): OB11Return<null | StreamPacketBasic> {
|
||||
return this.createResponse(useStream ? { type: StreamStatus.Error, data_type: 'error' } : null, 'failed', retcode, err, echo, useStream);
|
||||
}
|
||||
}
|
||||
export interface OneBotRequestToolkit<T = unknown> {
|
||||
send: (data: T) => Promise<void>;
|
||||
export abstract class OneBotRequestToolkit {
|
||||
abstract send<T>(packet: StreamPacket<T>): Promise<void>;
|
||||
}
|
||||
export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||
actionName: typeof ActionName[keyof typeof ActionName] = ActionName.Unknown;
|
||||
@@ -38,6 +40,7 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||
private validate?: ValidateFunction<unknown> = undefined;
|
||||
payloadSchema?: TSchema = undefined;
|
||||
obContext: NapCatOneBot11Adapter;
|
||||
useStream: boolean = false;
|
||||
|
||||
constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||
this.obContext = obContext;
|
||||
@@ -59,31 +62,31 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
public async handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit = { send: async () => { } }, echo?: string): Promise<OB11Return<ReturnDataType | null>> {
|
||||
public async handle(payload: PayloadType, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit = { send: async () => { } }, echo?: string): Promise<OB11Return<ReturnDataType | StreamPacketBasic | 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, req);
|
||||
return OB11Response.ok(resData, echo);
|
||||
return OB11Response.ok(resData, echo, this.useStream);
|
||||
} catch (e: unknown) {
|
||||
this.core.context.logger.logError('发生错误', e);
|
||||
return OB11Response.error((e as Error).message.toString() || (e as Error)?.stack?.toString() || '未知错误,可能操作超时', 200, echo);
|
||||
return OB11Response.error((e as Error).message.toString() || (e as Error)?.stack?.toString() || '未知错误,可能操作超时', 200, echo, this.useStream);
|
||||
}
|
||||
}
|
||||
|
||||
public async websocketHandle(payload: PayloadType, echo: unknown, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit = { send: async () => { } }): Promise<OB11Return<ReturnDataType | null>> {
|
||||
public async websocketHandle(payload: PayloadType, echo: unknown, adaptername: string, config: NetworkAdapterConfig, req: OneBotRequestToolkit = { send: async () => { } }): Promise<OB11Return<ReturnDataType | StreamPacketBasic | null>> {
|
||||
const result = await this.check(payload);
|
||||
if (!result.valid) {
|
||||
return OB11Response.error(result.message, 1400, echo);
|
||||
return OB11Response.error(result.message, 1400, echo, this.useStream);
|
||||
}
|
||||
try {
|
||||
const resData = await this._handle(payload, adaptername, config, req);
|
||||
return OB11Response.ok(resData, echo);
|
||||
return OB11Response.ok(resData, echo, this.useStream);
|
||||
} catch (e: unknown) {
|
||||
this.core.context.logger.logError('发生错误', e);
|
||||
return OB11Response.error(((e as Error).message.toString() || (e as Error).stack?.toString()) ?? 'Error', 1200, echo);
|
||||
return OB11Response.error(((e as Error).message.toString() || (e as Error).stack?.toString()) ?? 'Error', 1200, echo, this.useStream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user