Skip to content

Commit

Permalink
refactor: 优化 OneBot 和 Qmsg 的 option 校验
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 17, 2024
1 parent 1ae4203 commit d415eac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/push/one-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export const oneBotOptionSchema: OneBotOptionSchema = {
message_type: {
type: 'select',
title: '消息类型',
description: '消息类型',
description: '消息类型,private 或 group,默认为 private',
required: true,
default: 'private',
options: [
{
label: '私聊',
Expand Down Expand Up @@ -198,7 +199,16 @@ export class OneBot implements Send {
*/
async send(title: string, desp: string, option: OneBotOption): Promise<SendResponse<OneBotResponse>> {
Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option)
const { message_type, ...args } = option || {}
// !由于 OneBot 的 option 中带有必填项,所以需要校验
// 根据 optionSchema 验证 option
validate(option, OneBot.optionSchema as OptionSchema<OneBotOption>)
if (option.message_type === 'private' && !option.user_id) {
throw new Error('OneBot 私聊消息类型必须提供 user_id')
}
if (option.message_type === 'group' && !option.group_id) {
throw new Error('OneBot 群聊消息类型必须提供 group_id')
}
const { message_type = 'private', ...args } = option || {}
const message = `${title}${desp ? `\n${desp}` : ''}`
return ajax<OneBotResponse>({
baseURL: this.ONE_BOT_BASE_URL,
Expand Down
3 changes: 3 additions & 0 deletions src/push/qmsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export class Qmsg implements Send {
*/
async send(title: string, desp: string, option: QmsgOption): Promise<SendResponse<QmsgResponse>> {
Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option)
// !由于 Qmsg 酱的 option 中带有必填项,所以需要校验
// 根据 optionSchema 验证 option
validate(option, Qmsg.optionSchema)
const { qq, type = 'send', bot } = option || {}
const msg = `${title}${desp ? `\n${desp}` : ''}`
return ajax({
Expand Down

0 comments on commit d415eac

Please sign in to comment.