Skip to content

Commit

Permalink
feat: 新增 runPushAllInOne 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 9, 2024
1 parent 1f481bf commit 664ca21
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"server-chan",
"serverchan",
"server-chan-turbo",
"server-chan-v3",
"email",
"custom-email",
"nodemailer",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './push/wechat-app'
export * from './push/wechat-robot'
export * from './push/xi-zhi'

export * from './one'
49 changes: 49 additions & 0 deletions src/one.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CustomEmail, Dingtalk, Discord, IGot, OneBot, PushDeer, PushPlus, Qmsg, ServerChanTurbo, ServerChanV3, Telegram, WechatApp, WechatRobot, XiZhi } from '../src'
import { SendResponse } from '../src/interfaces/response'

const PushAllInOne = {
CustomEmail,
Dingtalk,
Discord,
IGot,
OneBot,
PushDeer,
PushPlus,
Qmsg,
ServerChanTurbo,
ServerChanV3,
Telegram,
WechatApp,
WechatRobot,
XiZhi,
} as const

export type IPushAllInOne = typeof PushAllInOne

export type PushType = keyof IPushAllInOne

export type MetaPushConfig<T extends PushType = PushType> = {
type: T
config: ConstructorParameters<IPushAllInOne[T]>[0]
option: Parameters<IPushAllInOne[T]['prototype']['send']>[2]
}

/**
* 从传入变量中读取配置,并选择一个渠道推送
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @template T
* @param title 推送标题
* @param desp 推送内容
* @param pushConfig 推送配置
*/
export async function runPushAllInOne<T extends PushType>(title: string, desp: string, pushConfig: MetaPushConfig<T>): Promise<SendResponse<any>> {
const { type, config, option } = pushConfig
if (PushAllInOne[type]) {
const push = new PushAllInOne[type](config as any)
return push.send(title, desp, option as any)
}
throw new Error('未匹配到任何推送方式!')
}
5 changes: 4 additions & 1 deletion src/push/dingtalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { SendResponse } from '@/interfaces/response'

const Debugger = debug('push:dingtalk')

export type DINGTALK_MSG_TYPE = 'text' | 'markdown' | 'link' | 'actionCard' | 'feedCard'

export interface DingtalkConfig {
/**
* 钉钉机器人 access_token。官方文档:https://developers.dingtalk.com/document/app/custom-robot-access
Expand All @@ -23,6 +25,7 @@ export interface DingtalkConfig {
}

export interface DingtalkOption {
[key: string]: any
}

export interface DingtalkResponse {
Expand Down Expand Up @@ -110,7 +113,7 @@ export class Dingtalk implements Send {
* @returns
*/
async send(title: string, desp?: string, option?: DingtalkOption): Promise<SendResponse<DingtalkResponse>> {
Debugger('title: "%s", desp: "%s"', title, desp)
Debugger('title: "%s", desp: "%s", option: %O', title, desp, option)
if (!desp) {
return this.push(new Text(title))
}
Expand Down

0 comments on commit 664ca21

Please sign in to comment.