Skip to content

Commit

Permalink
feat: 增加 ConfigSchema 和 OptionSchema 声明;重构 Config 校验
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 19, 2024
1 parent a0e0d2a commit b7436ed
Show file tree
Hide file tree
Showing 16 changed files with 846 additions and 75 deletions.
5 changes: 3 additions & 2 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const dingtalkConfigSchema: DingtalkConfigSchema = {
required: false,
default: '',
},
}
} as const

export type DingtalkOption = Partial<(Text | Markdown | Link | FeedCard | ActionCard)>

Expand All @@ -64,6 +64,7 @@ type TempDingtalkOption = {
feedCard?: FeedCard['feedCard']

at?: Text['at']
// [key: string]: any
}

export type DingtalkOptionSchema = OptionSchema<TempDingtalkOption>
Expand Down Expand Up @@ -147,7 +148,7 @@ export const dingtalkOptionSchema: DingtalkOptionSchema = {
links: [],
},
},
}
} as const

export interface DingtalkResponse {
errcode: number
Expand Down
50 changes: 46 additions & 4 deletions src/push/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import debug from 'debug'
import { Send } from '@/interfaces/send'
import { ajax } from '@/utils/ajax'
import { SendResponse } from '@/interfaces/response'
import { ConfigSchema, OptionSchema } from '@/interfaces/schema'
import { validate } from '@/utils/validate'

const Debugger = debug('push:discord')

Expand All @@ -17,6 +19,27 @@ export interface DiscordConfig {
PROXY_URL?: string
}

export type DiscordConfigSchema = ConfigSchema<DiscordConfig>

export const discordConfigSchema: DiscordConfigSchema = {
DISCORD_WEBHOOK: {
type: 'string',
title: 'Webhook Url',
description: 'Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取',
required: true,
},
PROXY_URL: {
type: 'string',
title: '代理地址',
description: '代理地址',
required: false,
},
} as const

/**
* Discord 额外选项
* 由于参数过多,因此请参考官方文档进行配置
*/
export type DiscordOption = {
/**
* 机器人显示的名称
Expand All @@ -26,9 +49,26 @@ export type DiscordOption = {
* 机器人头像的 Url
*/
avatar_url?: string
[key: string]: any
// [key: string]: any
}

export type DiscordOptionSchema = OptionSchema<DiscordOption>

export const discordOptionSchema: DiscordOptionSchema = {
username: {
type: 'string',
title: '机器人显示的名称',
description: '机器人显示的名称',
required: false,
},
avatar_url: {
type: 'string',
title: '机器人头像的 Url',
description: '机器人头像的 Url',
required: false,
},
} as const

export interface DiscordResponse { }

/**
Expand All @@ -40,6 +80,9 @@ export interface DiscordResponse { }
* @class Discord
*/
export class Discord implements Send {

static configSchema = discordConfigSchema
static optionSchema = discordOptionSchema
/**
* Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取
*
Expand All @@ -64,9 +107,8 @@ export class Discord implements Send {
if (PROXY_URL) {
this.proxyUrl = PROXY_URL
}
if (!this.DISCORD_WEBHOOK) {
throw new Error('DISCORD_WEBHOOK 是必须的!')
}
// 根据 configSchema 验证 config
validate(config, Discord.configSchema)
}

/**
Expand Down
64 changes: 59 additions & 5 deletions src/push/i-got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import debug from 'debug'
import { Send } from '@/interfaces/send'
import { ajax } from '@/utils/ajax'
import { SendResponse } from '@/interfaces/response'
import { ConfigSchema, OptionSchema } from '@/interfaces/schema'
import { validate } from '@/utils/validate'

const Debugger = debug('push:i-got')

Expand All @@ -12,6 +14,18 @@ export interface IGotConfig {
I_GOT_KEY: string
}

export type IGotConfigSchema = ConfigSchema<IGotConfig>

export const iGotConfigSchema: IGotConfigSchema = {
I_GOT_KEY: {
type: 'string',
title: 'iGot 推送key',
description: 'iGot 推送key',
required: true,
default: '',
},
} as const

export interface IGotOption {
/**
* 链接; 点开消息后会主动跳转至此地址
Expand All @@ -33,9 +47,49 @@ export interface IGotOption {
* 主题; 订阅链接下有效;对推送内容分类,用户可选择性订阅
*/
topic?: string
[key: string]: any
// [key: string]: any
}

export type IGotOptionSchema = OptionSchema<IGotOption>

export const iGotOptionSchema: IGotOptionSchema = {
url: {
type: 'string',
title: '链接',
description: '链接; 点开消息后会主动跳转至此地址',
required: false,
default: '',
},
automaticallyCopy: {
type: 'number',
title: '是否自动复制',
description: '是否自动复制; 为1自动复制',
required: false,
default: 0,
},
urgent: {
type: 'number',
title: '紧急消息',
description: '紧急消息,为1表示紧急。此消息将置顶在小程序内, 同时会在推送的消息内做一定的特殊标识',
required: false,
default: 0,
},
copy: {
type: 'string',
title: '需要自动复制的文本内容',
description: '需要自动复制的文本内容',
required: false,
default: '',
},
topic: {
type: 'string',
title: '主题',
description: '主题; 订阅链接下有效;对推送内容分类,用户可选择性订阅',
required: false,
default: '',
},
} as const

export interface IGotResponse {
/**
* 状态码; 0为正常
Expand Down Expand Up @@ -65,7 +119,8 @@ export interface IGotResponse {
* @class IGot
*/
export class IGot implements Send {

static configSchema = iGotConfigSchema
static optionSchema = iGotOptionSchema
/**
* 微信搜索小程序“iGot”获取推送key
*
Expand All @@ -81,9 +136,8 @@ export class IGot implements Send {
const { I_GOT_KEY } = config
this.I_GOT_KEY = I_GOT_KEY
Debugger('set I_GOT_KEY: "%s"', I_GOT_KEY)
if (!this.I_GOT_KEY) {
throw new Error('I_GOT_KEY 是必须的!')
}
// 根据 configSchema 验证 config
validate(config, IGot.configSchema)
}
/**
*
Expand Down
78 changes: 73 additions & 5 deletions src/push/one-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Send } from '@/interfaces/send'
import { ajax } from '@/utils/ajax'
import { warn } from '@/utils/helper'
import { SendResponse } from '@/interfaces/response'
import { ConfigSchema, OptionSchema } from '@/interfaces/schema'
import { validate } from '@/utils/validate'

const Debugger = debug('push:one-bot')

export type OneBotMsgType = 'private' | 'group'

export interface OneBotConfig {
/**
* OneBot HTTP 基础路径
Expand All @@ -20,6 +20,22 @@ export interface OneBotConfig {
ONE_BOT_ACCESS_TOKEN?: string
}

export type OneBotConfigSchema = ConfigSchema<OneBotConfig>
export const oneBotConfigSchema: OneBotConfigSchema = {
ONE_BOT_BASE_URL: {
type: 'string',
title: 'OneBot HTTP 基础路径',
description: 'OneBot HTTP 基础路径',
required: true,
},
ONE_BOT_ACCESS_TOKEN: {
type: 'string',
title: 'OneBot AccessToken',
description: '出于安全原因,请务必设置 AccessToken',
required: false,
},
} as const

export interface OneBotPrivateMsgOption {
/**
* 消息类型
Expand Down Expand Up @@ -50,6 +66,56 @@ export type OneBotOption = (OneBotPrivateMsgOption | OneBotGroupMsgOption) & {
auto_escape?: boolean
}

export type OneBotMsgType = OneBotOption['message_type']

export type OneBotOptionSchema = OptionSchema<{
// 消息类型,private 或 group
message_type: OneBotMsgType
// 如果为 private,对方 QQ 号
user_id?: number
// 如果为 group,群号
group_id?: number
// 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效
auto_escape?: boolean
}>

export const oneBotOptionSchema: OneBotOptionSchema = {
message_type: {
type: 'select',
title: '消息类型',
description: '消息类型',
required: true,
options: [
{
label: '私聊',
value: 'private',
},
{
label: '群聊',
value: 'group',
},
],
},
user_id: {
type: 'number',
title: '对方 QQ 号',
description: '对方 QQ 号',
required: false,
},
group_id: {
type: 'number',
title: '群号',
description: '群号',
required: false,
},
auto_escape: {
type: 'boolean',
title: '消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效',
description: '消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效',
required: false,
},
} as const

export interface OneBotData {
ClassType: string
// 消息 ID
Expand All @@ -73,6 +139,9 @@ export interface OneBotResponse {
*/
export class OneBot implements Send {

static configSchema = oneBotConfigSchema
static optionSchema = oneBotOptionSchema

/**
* OneBot 协议版本号
*
Expand Down Expand Up @@ -111,9 +180,8 @@ export class OneBot implements Send {
this.ONE_BOT_BASE_URL = ONE_BOT_BASE_URL
this.ONE_BOT_ACCESS_TOKEN = ONE_BOT_ACCESS_TOKEN
Debugger('set ONE_BOT_BASE_URL: "%s", ONE_BOT_ACCESS_TOKEN: "%s"', ONE_BOT_BASE_URL, ONE_BOT_ACCESS_TOKEN)
if (!this.ONE_BOT_BASE_URL) {
throw new Error('ONE_BOT_BASE_URL 是必须的!')
}
// 根据 configSchema 验证 config
validate(config, OneBot.configSchema)
if (!this.ONE_BOT_ACCESS_TOKEN) {
warn('未提供 ONE_BOT_ACCESS_TOKEN !出于安全原因,请务必设置 AccessToken!')
}
Expand Down
Loading

0 comments on commit b7436ed

Please sign in to comment.