Skip to content

Commit

Permalink
fix: 文档错误,类型错误
Browse files Browse the repository at this point in the history
  • Loading branch information
class-liu-fullstack committed Feb 1, 2025
1 parent ce5d5b1 commit f00b11a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const bot = new Bot({
'GUILD_MEMBERS', // 频道成员变更事件
'DIRECT_MESSAGE', // 频道私信事件
], // (必填)
mode:'weboook',
mode:'webhook',
port: 3000, // webhook监听端口
path: '/webhook', // webhook监听路径
})
Expand Down
2 changes: 1 addition & 1 deletion docs/src/guide/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const bot = new Bot({
'GUILD_MEMBERS', // 频道成员变更事件
'DIRECT_MESSAGE', // 频道私信事件
], // (必填)
mode:'weboook',
mode:'webhook',
port: 3000, // webhook监听端口
path: '/webhook', // webhook监听路径
})
Expand Down
20 changes: 12 additions & 8 deletions src/receivers/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {DataPacket} from "@/types";
import {Ed25519} from "@/ed25519";

export type KoaContext = {
request: IncomingMessage,
response: ServerResponse
request: Record<string, any>
response: Record<string, any>
req: IncomingMessage,
res: ServerResponse
}
export type ApplicationPlatform = 'koa' | 'express'
export type Middleware<T extends ApplicationPlatform> = T extends 'koa' ? KoaMiddleware : ExpressMiddleware
Expand All @@ -22,8 +24,9 @@ export type WebhookReceiverConfig = ServerConfig
const createEd25519 = (secret: string) => {
return new Ed25519(secret)
}
const resolveBodyData = async (req: IncomingMessage) => {
const resolveBodyData = async (req: IncomingMessage,data) => {
return new Promise<string>(resolve => {
if(data) resolve(data)
const dataArr: Buffer[] = [];
req.on('data', (data) => {
dataArr.push(data)
Expand All @@ -34,8 +37,8 @@ const resolveBodyData = async (req: IncomingMessage) => {
})
}

async function webhookHandler(this: Receiver, req: IncomingMessage, res: ServerResponse, ed25519: Ed25519) {
const bodyData = await resolveBodyData(req)
async function webhookHandler<T=undefined>(this: Receiver, req: IncomingMessage, res: ServerResponse, ed25519: Ed25519,fallbackData?:T) {
const bodyData = await resolveBodyData(req,JSON.stringify(fallbackData))
if (!ed25519) return
const signature = req.headers['x-signature-ed25519']?.toString()
const timestamp = req.headers['x-signature-timestamp']?.toString()
Expand Down Expand Up @@ -97,9 +100,10 @@ function getMiddleware<T extends ApplicationPlatform>(platform: ApplicationPlatf
}>, ctx: KoaContext, next) {
next && await next()
return webhookHandler.apply(this, [
ctx.request,
ctx.response,
this.handler.ed24419
ctx.req,
ctx.res,
this.handler.ed24419,
ctx.request.body
])
}) as Middleware<T>
case 'express':
Expand Down

0 comments on commit f00b11a

Please sign in to comment.