mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
fix: Fix var type of echo
This commit is contained in:
parent
8dfc71ab6d
commit
30e488aeaf
@ -194,3 +194,7 @@ export async function encodeSilk(filePath: string) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isNull(value: any) {
|
||||||
|
return value === undefined || value === null;
|
||||||
|
}
|
@ -23,7 +23,7 @@ class BaseAction<PayloadType, ReturnDataType> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async websocketHandle(payload: PayloadType, echo: string): Promise<OB11Return<ReturnDataType | null>> {
|
public async websocketHandle(payload: PayloadType, echo: any): Promise<OB11Return<ReturnDataType | null>> {
|
||||||
const result = await this.check(payload)
|
const result = await this.check(payload)
|
||||||
if (!result.valid) {
|
if (!result.valid) {
|
||||||
return OB11Response.error(result.message, 1400)
|
return OB11Response.error(result.message, 1400)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import {OB11Return} from '../types';
|
import {OB11Return} from '../types';
|
||||||
|
import {isNull} from '../../common/utils';
|
||||||
|
|
||||||
export class OB11Response {
|
export class OB11Response {
|
||||||
static res<T>(data: T, status: string, retcode: number, message: string = ""): OB11Return<T> {
|
static res<T>(data: T, status: string, retcode: number, message: string = ""): OB11Return<T> {
|
||||||
@ -8,21 +9,21 @@ export class OB11Response {
|
|||||||
data: data,
|
data: data,
|
||||||
message: message,
|
message: message,
|
||||||
wording: message,
|
wording: message,
|
||||||
echo: ""
|
echo: null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ok<T>(data: T, echo: string = "") {
|
static ok<T>(data: T, echo: any = null) {
|
||||||
let res = OB11Response.res<T>(data, "ok", 0)
|
let res = OB11Response.res<T>(data, "ok", 0)
|
||||||
if (echo) {
|
if (!isNull(echo)) {
|
||||||
res.echo = echo;
|
res.echo = echo;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static error(err: string, retcode: number, echo: string = "") {
|
static error(err: string, retcode: number, echo: any = null) {
|
||||||
let res = OB11Response.res(null, "failed", retcode, err)
|
let res = OB11Response.res(null, "failed", retcode, err)
|
||||||
if (echo) {
|
if (!isNull(echo)) {
|
||||||
res.echo = echo;
|
res.echo = echo;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
@ -33,8 +33,8 @@ export class ReverseWebsocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async onmessage(msg: string) {
|
public async onmessage(msg: string) {
|
||||||
let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}}
|
let receiveData: { action: ActionName, params: any, echo?: any } = {action: null, params: {}}
|
||||||
let echo = ""
|
let echo = null
|
||||||
try {
|
try {
|
||||||
receiveData = JSON.parse(msg.toString())
|
receiveData = JSON.parse(msg.toString())
|
||||||
echo = receiveData.echo
|
echo = receiveData.echo
|
||||||
|
@ -18,7 +18,7 @@ class OB11WebsocketServer extends WebsocketServerBase {
|
|||||||
wsClient.send(JSON.stringify(OB11Response.res(null, "failed", 1403, "token验证失败")))
|
wsClient.send(JSON.stringify(OB11Response.res(null, "failed", 1403, "token验证失败")))
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleAction(wsClient: WebSocket, actionName: string, params: any, echo?: string) {
|
async handleAction(wsClient: WebSocket, actionName: string, params: any, echo?: any) {
|
||||||
const action: BaseAction<any, any> = actionMap.get(actionName);
|
const action: BaseAction<any, any> = actionMap.get(actionName);
|
||||||
if (!action) {
|
if (!action) {
|
||||||
return wsReply(wsClient, OB11Response.error("不支持的api " + actionName, 1404, echo))
|
return wsReply(wsClient, OB11Response.error("不支持的api " + actionName, 1404, echo))
|
||||||
@ -34,8 +34,8 @@ class OB11WebsocketServer extends WebsocketServerBase {
|
|||||||
onConnect(wsClient: WebSocket, url: string, req: IncomingMessage) {
|
onConnect(wsClient: WebSocket, url: string, req: IncomingMessage) {
|
||||||
if (url == "/api" || url == "/api/" || url == "/") {
|
if (url == "/api" || url == "/api/" || url == "/") {
|
||||||
wsClient.on("message", async (msg) => {
|
wsClient.on("message", async (msg) => {
|
||||||
let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}}
|
let receiveData: { action: ActionName, params: any, echo?: any } = {action: null, params: {}}
|
||||||
let echo = ""
|
let echo = null
|
||||||
try {
|
try {
|
||||||
receiveData = JSON.parse(msg.toString())
|
receiveData = JSON.parse(msg.toString())
|
||||||
echo = receiveData.echo
|
echo = receiveData.echo
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import * as websocket from "ws";
|
import * as websocket from "ws";
|
||||||
import {OB11Response} from "../../action/utils";
|
import {OB11Response} from "../../action/utils";
|
||||||
import {PostEventType} from "../postevent";
|
import {PostEventType} from "../postevent";
|
||||||
import {log} from "../../../common/utils";
|
import {isNull, log} from "../../../common/utils";
|
||||||
|
|
||||||
export function wsReply(wsClient: websocket.WebSocket, data: OB11Response | PostEventType) {
|
export function wsReply(wsClient: websocket.WebSocket, data: OB11Response | PostEventType) {
|
||||||
try {
|
try {
|
||||||
let packet = Object.assign({
|
let packet = Object.assign({
|
||||||
}, data);
|
}, data);
|
||||||
if (!packet["echo"]){
|
if (isNull(packet["echo"])){
|
||||||
delete packet["echo"];
|
delete packet["echo"];
|
||||||
}
|
}
|
||||||
wsClient.send(JSON.stringify(packet))
|
wsClient.send(JSON.stringify(packet))
|
||||||
|
@ -77,7 +77,7 @@ export interface OB11Return<DataType> {
|
|||||||
retcode: number
|
retcode: number
|
||||||
data: DataType
|
data: DataType
|
||||||
message: string,
|
message: string,
|
||||||
echo?: string, // ws调用api才有此字段
|
echo?: any, // ws调用api才有此字段
|
||||||
wording?: string, // go-cqhttp字段,错误信息
|
wording?: string, // go-cqhttp字段,错误信息
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user