Skip to content

Commit

Permalink
refactor: 调整 自定义邮件/Discord/IGot 的接口类型声明
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 7f73e1e commit 2c30bc6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
14 changes: 8 additions & 6 deletions src/push/custom-email.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debug from 'debug'
import nodemailer from 'nodemailer'
import SMTPTransport from 'nodemailer/lib/smtp-transport'
import Mail from 'nodemailer/lib/mailer'
import { Send } from '../interfaces/send'
import { SendResponse } from '@/interfaces/response'

Expand Down Expand Up @@ -34,7 +35,7 @@ export interface CustomEmailConfig {
EMAIL_PORT: number
}

export type CustomEmailOption = Pick<CustomEmailConfig, 'EMAIL_TYPE' | 'EMAIL_TO_ADDRESS'>
export type CustomEmailOption = Mail.Options

/**
* 自定义邮件。官方文档: https://github.com/nodemailer/nodemailer
Expand Down Expand Up @@ -87,25 +88,26 @@ export class CustomEmail implements Send {
* @date 2024-11-08
* @param title 消息的标题
* @param [desp] 消息的内容,支持 html
* @param [option] 选项,可以覆盖默认的同名配置
* @param [option] 额外选项
*/
async send(title: string, desp?: string, option?: CustomEmailOption): Promise<SendResponse<SMTPTransport.SentMessageInfo>> {
Debugger('title: "%s", desp: "%s", option: %o', title, desp, option)
const { EMAIL_TYPE, EMAIL_TO_ADDRESS, EMAIL_AUTH_USER } = this.config

if (!await this.transporter.verify()) {
throw new Error('自定义邮件的发件配置无效')
}
const { to: _to, ...args } = option || {}
const from = EMAIL_AUTH_USER
const to = option?.EMAIL_TO_ADDRESS || EMAIL_TO_ADDRESS
const type = option?.EMAIL_TYPE || EMAIL_TYPE
const to = _to || EMAIL_TO_ADDRESS
const type = EMAIL_TYPE
const response = await this.transporter.sendMail({
from,
to,
subject: title,
[type]: desp,
...args,
})
if (!Symbol.dispose) { // 如果不支持 Symbol.dispose ,则手动释放
if (typeof Symbol.dispose === 'undefined') { // 如果不支持 Symbol.dispose ,则手动释放
this.transporter.close()
}
Debugger('CustomEmail Response: %o', response)
Expand Down
44 changes: 16 additions & 28 deletions src/push/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ export interface DiscordConfig {
* Webhook Url 可在服务器设置 -> 整合 -> Webhook -> 创建 Webhook 中获取
*/
DISCORD_WEBHOOK: string

/**
* 机器人显示的名称
* 代理地址
*/
DISCORD_USERNAME?: string
PROXY_URL?: string
}

export type DiscordOption = {
/**
* 机器人头像的 Url
* 机器人显示的名称
*/
DISCORD_AVATAR_URL?: string

username: string
/**
* 代理地址
* 机器人头像的 Url
*/
PROXY_URL?: string
avatar_url: string
[key: string]: any
}

export type DiscordOption = Pick<DiscordConfig, 'DISCORD_USERNAME' | 'PROXY_URL' | 'DISCORD_AVATAR_URL'>

export interface DiscordResponse { }

/**
Expand All @@ -48,15 +49,6 @@ export class Discord implements Send {
*/
private DISCORD_WEBHOOK: string

/**
* 机器人显示的名称
*
* @author CaoMeiYouRen
* @date 2023-09-17
* @private
*/
private DISCORD_USERNAME?: string

proxyUrl: string

/**
Expand All @@ -66,12 +58,9 @@ export class Discord implements Send {
* @param config 配置
*/
constructor(config: DiscordConfig) {
const { DISCORD_WEBHOOK, DISCORD_USERNAME, PROXY_URL } = config
Debugger('DISCORD_WEBHOOK: %s, DISCORD_USERNAME: %s, PROXY_URL: %s', DISCORD_WEBHOOK, DISCORD_USERNAME, PROXY_URL)
const { DISCORD_WEBHOOK, PROXY_URL } = config
Debugger('DISCORD_WEBHOOK: %s, PROXY_URL: %s', DISCORD_WEBHOOK, PROXY_URL)
this.DISCORD_WEBHOOK = DISCORD_WEBHOOK
if (DISCORD_USERNAME) {
this.DISCORD_USERNAME = DISCORD_USERNAME
}
if (PROXY_URL) {
this.proxyUrl = PROXY_URL
}
Expand All @@ -87,14 +76,12 @@ export class Discord implements Send {
* @date 2024-11-08
* @param title 消息的标题
* @param [desp] 消息的描述。最多 2000 个字符
* @param [option] 选项,可以覆盖默认的同名配置
* @param [option] 额外选项
*/
async send(title: string, desp: string = '', option?: DiscordOption): Promise<SendResponse<DiscordResponse>> {
Debugger('title: "%s", desp: "%s", option: %o', title, desp, option)
const { DISCORD_USERNAME, PROXY_URL, DISCORD_AVATAR_URL } = option || {}
const username = DISCORD_USERNAME || this.DISCORD_USERNAME
const avatar_url = DISCORD_AVATAR_URL
const proxyUrl = PROXY_URL || this.proxyUrl
const { username, avatar_url, ...args } = option || {}
const proxyUrl = this.proxyUrl
const content = `${title}\n${desp}`.trim()
return ajax({
url: this.DISCORD_WEBHOOK,
Expand All @@ -104,6 +91,7 @@ export class Discord implements Send {
username,
content,
avatar_url,
...args,
},
})
}
Expand Down
1 change: 1 addition & 0 deletions src/push/i-got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class IGot implements Send {
* @param title 请求标题
* @param [content] 请求正文
* @param [url] 推送携带的url
* @param [option] 额外选项
* @returns
*/
send(title: string, desp?: string, option?: IGotOption): Promise<SendResponse<IGotResponse>> {
Expand Down

0 comments on commit 2c30bc6

Please sign in to comment.