Skip to content

Commit

Permalink
feat: 完成 酷推 对接;文档编写;准备发布
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Feb 27, 2021
1 parent 5fc9996 commit 56923c5
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ indent_size = 4
#换行符
end_of_line = lf
#插入最终换行符
insert_final_newline = false
insert_final_newline = true
#修剪尾随空格
trim_trailing_whitespace = true
# 对后缀名为 md 的文件生效
Expand Down
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'], // 要求或禁止使用拖尾逗号
},
}
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# push-all-in-one

本项目的目标是支持 Server酱、酷推、Bark App、Telegram Bot、钉钉机器人、企业微信机器人、企业微信应用和自定义推送等多种推送方式,目前还在开发中。
本项目的目标是支持 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

```

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const __PROD__ = process.env.NODE_ENV === 'production'
export const __DEV__ = process.env.NODE_ENV === 'development'
export const __DEV__ = process.env.NODE_ENV === 'development'
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion src/interfaces/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
interface Send {
send(...args: any[]): Promise<any>
}
export { Send }
export { Send }
61 changes: 61 additions & 0 deletions src/push/cool-push.ts
Original file line number Diff line number Diff line change
@@ -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<AxiosResponse<any>> {
return ajax({
url: `https://push.xuthus.cc/${type}/${this.SKEY}`,
query: {
c: content,
},
method: 'POST',
})
}

}
4 changes: 2 additions & 2 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Expand Down Expand Up @@ -81,4 +81,4 @@ export class Dingtalk implements Send {
}
return result
}
}
}
3 changes: 2 additions & 1 deletion src/push/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Email implements Send {
}

async send(mailOptions: SendMailOptions): Promise<any> {
Debugger('mailOptions: %O', mailOptions)
return new Promise((resolve: (value: unknown) => void, reject: (err: Error) => void) => {
this.mail.sendMail(mailOptions, (err, info) => {
if (err) {
Expand All @@ -29,4 +30,4 @@ export class Email implements Send {
})
})
}
}
}
6 changes: 2 additions & 4 deletions src/utils/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export async function ajax(config: AjaxConfig): Promise<AxiosResponse<any>> {
timeout: 10000,
baseURL: '',
transformRequest(reqData: any, reqHeaders?: Record<string, unknown>) {
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)
}
Expand All @@ -55,4 +53,4 @@ export async function ajax(config: AjaxConfig): Promise<AxiosResponse<any>> {
console.error(error)
throw error
}
}
}

0 comments on commit 56923c5

Please sign in to comment.