diff --git a/.editorconfig b/.editorconfig index f22c68f..0b3a46d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,7 +11,7 @@ indent_size = 4 #换行符 end_of_line = lf #插入最终换行符 -insert_final_newline = false +insert_final_newline = true #修剪尾随空格 trim_trailing_whitespace = true # 对后缀名为 md 的文件生效 diff --git a/.eslintrc.js b/.eslintrc.js index 5d7014b..c5df7f1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,8 +13,11 @@ module.exports = { ], rules: { 'no-console': __WARN__, + 'no-shadow': 0, + '@typescript-eslint/no-shadow': 2, '@typescript-eslint/explicit-module-boundary-types': [1, { allowArgumentsExplicitlyTypedAsAny: true, }], // 要求导出函数和类的公共类方法的显式返回和参数类型 + '@typescript-eslint/comma-dangle': [2, 'always-multiline'], // 要求或禁止使用拖尾逗号 }, } diff --git a/README.md b/README.md index e6a147d..38a66ff 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,34 @@ # push-all-in-one -本项目的目标是支持 Server酱、酷推、Bark App、Telegram Bot、钉钉机器人、企业微信机器人、企业微信应用和自定义推送等多种推送方式,目前还在开发中。 \ No newline at end of file +本项目的目标是支持 Server酱、酷推、Bark App、Telegram Bot、钉钉机器人、企业微信机器人、企业微信应用和自定义推送等多种推送方式,目前还在开发中。 + +## 安装 + +```sh +npm i push-all-in-one -S +``` + +## 使用 + +```ts +import { ServerChanTurbo, CoolPush, Dingtalk, Text } from 'push-all-in-one' + +const SCTKEY = 'SCTxxxxxxxxxxxxxxxxxxx' +const serverChanTurbo = new ServerChanTurbo(SCTKEY) +serverChanTurbo.send('你好', '你好,我很可爱') + +const SKEY = '022bxxxxxxxxxxxxxxxxxx' +const coolPush = new CoolPush(SKEY) +coolPush.send('你好,我很可爱') + +const dingtalk = new Dingtalk({ + accessToken: 'xxxxxxxxxxxxxx', + secret: 'SECxxxxxxxxxxxxxxxx', +}) +const text = new Text('我就是我, @1825718XXXX 是不一样的烟火') +text.atPhone('1825718XXXX') +dingtalk.send(text) +// Dingtalk 相关更多请参考 https://github.com/CaoMeiYouRen/ts-dingtalk-robot + +``` + diff --git a/package.json b/package.json index 58c55f3..bd986c3 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "cross-env": "^7.0.3", "cz-conventional-changelog": "^3.3.0", "eslint": "^7.20.0", - "eslint-config-cmyr": "^1.0.10", + "eslint-config-cmyr": "^1.1.7", "husky": "^5.1.1", "lint-staged": "^10.5.4", "lodash": "^4.17.21", diff --git a/src/config/env.ts b/src/config/env.ts index 9ea04bf..2c5339b 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -1,2 +1,2 @@ export const __PROD__ = process.env.NODE_ENV === 'production' -export const __DEV__ = process.env.NODE_ENV === 'development' \ No newline at end of file +export const __DEV__ = process.env.NODE_ENV === 'development' diff --git a/src/index.ts b/src/index.ts index 9de86c1..4eabad3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ export * from './push/dingtalk' +export * from './push/dingtalk/index' +export * from './push/cool-push' +// export * from './push/email' export * from './push/server-chan' export * from './push/server-chan-turbo' diff --git a/src/interfaces/send.ts b/src/interfaces/send.ts index 0b954da..a248e2a 100644 --- a/src/interfaces/send.ts +++ b/src/interfaces/send.ts @@ -9,4 +9,4 @@ interface Send { send(...args: any[]): Promise } -export { Send } \ No newline at end of file +export { Send } diff --git a/src/push/cool-push.ts b/src/push/cool-push.ts new file mode 100644 index 0000000..0f4f399 --- /dev/null +++ b/src/push/cool-push.ts @@ -0,0 +1,61 @@ +import { Send } from '../interfaces/send' +import { ajax } from '@/utils/ajax' +import { AxiosResponse } from 'axios' +import debug from 'debug' + +const Debugger = debug('push:cool-push') + +/** + * 推送类型,见 [Cool Push](https://cp.xuthus.cc/)。 + * 暂不支持 一对多推送/指定特定的qq号或者群/企业微信消息推送/钉钉群消息/邮箱消息推送 + */ +type PushType = 'send' | 'group' | 'psend' | 'pgroup' | 'wx' | 'tg' + +/** + * Cool Push QQ消息推送服务。使用说明见 [Cool Push](https://cp.xuthus.cc/) + * + * @author CaoMeiYouRen + * @date 2021-02-27 + * @export + * @class CoolPush + */ +export class CoolPush implements Send { + /** + * 请前往 https://cp.xuthus.cc/ 领取 + * + * @private + */ + private SKEY: string + /** + * + * @author CaoMeiYouRen + * @date 2021-02-27 + * @param SKEY 请前往 https://cp.xuthus.cc/ 领取 + */ + constructor(SKEY: string) { + this.SKEY = SKEY + Debugger('set SKEY: "%s"', SKEY) + if (!this.SKEY) { + throw new Error('SKEY is required!') + } + } + /** + * + * + * @author CaoMeiYouRen + * @date 2021-02-27 + * @param content 要发送的内容 + * @param [type='send'] 推送类型 + * @returns + */ + send(content: string, type: PushType = 'send'): Promise> { + return ajax({ + url: `https://push.xuthus.cc/${type}/${this.SKEY}`, + query: { + c: content, + }, + method: 'POST', + }) + } + +} diff --git a/src/push/dingtalk.ts b/src/push/dingtalk.ts index 9e14912..1d42149 100644 --- a/src/push/dingtalk.ts +++ b/src/push/dingtalk.ts @@ -33,7 +33,7 @@ export class Dingtalk implements Send { private accessToken?: string private secret?: string private webhook: string = 'https://oapi.dingtalk.com/robot/send' - constructor(option: RobotOption = {}) { + constructor(option: RobotOption) { Object.assign(this, option) if (!this.accessToken) { throw new Error('accessToken is required!') @@ -81,4 +81,4 @@ export class Dingtalk implements Send { } return result } -} \ No newline at end of file +} diff --git a/src/push/email.ts b/src/push/email.ts index 13c668c..91fdc23 100644 --- a/src/push/email.ts +++ b/src/push/email.ts @@ -20,6 +20,7 @@ export class Email implements Send { } async send(mailOptions: SendMailOptions): Promise { + Debugger('mailOptions: %O', mailOptions) return new Promise((resolve: (value: unknown) => void, reject: (err: Error) => void) => { this.mail.sendMail(mailOptions, (err, info) => { if (err) { @@ -29,4 +30,4 @@ export class Email implements Send { }) }) } -} \ No newline at end of file +} diff --git a/src/utils/ajax.ts b/src/utils/ajax.ts index 0aa9ec1..6effd1a 100644 --- a/src/utils/ajax.ts +++ b/src/utils/ajax.ts @@ -33,9 +33,7 @@ export async function ajax(config: AjaxConfig): Promise> { timeout: 10000, baseURL: '', transformRequest(reqData: any, reqHeaders?: Record) { - const contentType = Object.keys(reqHeaders).find((e) => { - return e.toLocaleLowerCase().includes('Content-Type'.toLocaleLowerCase()) - }) + const contentType = Object.keys(reqHeaders).find((e) => e.toLocaleLowerCase().includes('Content-Type'.toLocaleLowerCase())) if (typeof reqData === 'object' && reqHeaders[contentType] === 'application/x-www-form-urlencoded') { return qs.stringify(reqData) } @@ -55,4 +53,4 @@ export async function ajax(config: AjaxConfig): Promise> { console.error(error) throw error } -} \ No newline at end of file +}