@jayfong/x-server 1.31.3 → 1.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/lib/_cjs/core/define_task.js +1 -1
- package/lib/_cjs/core/server.js +3 -0
- package/lib/_cjs/index.js +8 -0
- package/lib/_cjs/services/dingtalk.js +65 -0
- package/lib/core/define_task.js +1 -1
- package/lib/core/server.js +3 -0
- package/lib/core/types.d.ts +6 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/services/dingtalk.d.ts +39 -0
- package/lib/services/dingtalk.js +52 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [1.33.0](https://github.com/jfWorks/x-server/compare/v1.32.1...v1.33.0) (2022-06-11)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* defineTask concurrency ([d8174aa](https://github.com/jfWorks/x-server/commit/d8174aa302e9013610d41265d937512a02412359))
|
|
11
|
+
|
|
12
|
+
### [1.32.1](https://github.com/jfWorks/x-server/compare/v1.32.0...v1.32.1) (2022-06-02)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* trustProxy: true ([b116aaa](https://github.com/jfWorks/x-server/commit/b116aaac3dd90364d5aaf939bf66e50a6911aad2))
|
|
18
|
+
|
|
19
|
+
## [1.32.0](https://github.com/jfWorks/x-server/compare/v1.31.3...v1.32.0) (2022-05-31)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
* add DingtalkService ([3cbe91c](https://github.com/jfWorks/x-server/commit/3cbe91cd82c6cfc5fa4c5bcf2d8c7eb93711ef7c))
|
|
25
|
+
|
|
5
26
|
### [1.31.3](https://github.com/jfWorks/x-server/compare/v1.31.2...v1.31.3) (2022-05-30)
|
|
6
27
|
|
|
7
28
|
|
package/lib/_cjs/core/server.js
CHANGED
package/lib/_cjs/index.js
CHANGED
|
@@ -170,6 +170,14 @@ Object.keys(_captcha).forEach(function (key) {
|
|
|
170
170
|
exports[key] = _captcha[key];
|
|
171
171
|
});
|
|
172
172
|
|
|
173
|
+
var _dingtalk = require("./services/dingtalk");
|
|
174
|
+
|
|
175
|
+
Object.keys(_dingtalk).forEach(function (key) {
|
|
176
|
+
if (key === "default" || key === "__esModule") return;
|
|
177
|
+
if (key in exports && exports[key] === _dingtalk[key]) return;
|
|
178
|
+
exports[key] = _dingtalk[key];
|
|
179
|
+
});
|
|
180
|
+
|
|
173
181
|
var _dispose = require("./services/dispose");
|
|
174
182
|
|
|
175
183
|
Object.keys(_dispose).forEach(function (key) {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.DingtalkService = void 0;
|
|
7
|
+
|
|
8
|
+
var _crypto = _interopRequireDefault(require("crypto"));
|
|
9
|
+
|
|
10
|
+
var _got = _interopRequireDefault(require("got"));
|
|
11
|
+
|
|
12
|
+
class DingtalkService {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.serviceName = 'dingtalk';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async send(options) {
|
|
19
|
+
const timestamp = Date.now();
|
|
20
|
+
|
|
21
|
+
const sign = _crypto.default.createHmac('sha256', this.options.secret).update(`${timestamp}\n${this.options.secret}`).digest('base64');
|
|
22
|
+
|
|
23
|
+
const data = {
|
|
24
|
+
msgtype: 'markdown',
|
|
25
|
+
markdown: {
|
|
26
|
+
title: options.title,
|
|
27
|
+
text: options.content
|
|
28
|
+
},
|
|
29
|
+
at: {
|
|
30
|
+
atMobiles: Array.isArray(options.at) ? options.at : [],
|
|
31
|
+
isAtAll: options.at === 'all'
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (!data.at.isAtAll && data.at.atMobiles.length) {
|
|
36
|
+
const atMobiles = data.at.atMobiles.filter(mobile => !data.markdown.text.includes(`@${mobile}`));
|
|
37
|
+
|
|
38
|
+
if (atMobiles.length) {
|
|
39
|
+
const atMobilesText = atMobiles.map(mobile => `@${mobile}`).join(' ');
|
|
40
|
+
data.markdown.text += `<!-- ${atMobilesText} -->`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const {
|
|
45
|
+
body
|
|
46
|
+
} = await _got.default.post('https://oapi.dingtalk.com/robot/send', {
|
|
47
|
+
responseType: 'json',
|
|
48
|
+
searchParams: {
|
|
49
|
+
access_token: this.options.accessToken,
|
|
50
|
+
timestamp: timestamp,
|
|
51
|
+
sign: sign
|
|
52
|
+
},
|
|
53
|
+
json: data
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (body.errcode !== 0) {
|
|
57
|
+
throw new Error(body.errmsg);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return body;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.DingtalkService = DingtalkService;
|
package/lib/core/define_task.js
CHANGED
package/lib/core/server.js
CHANGED
|
@@ -33,6 +33,9 @@ export class Server {
|
|
|
33
33
|
logger: process.env.NODE_ENV === 'development' ? {
|
|
34
34
|
prettyPrint: true
|
|
35
35
|
} : false,
|
|
36
|
+
// 信任 X-Forwarded-For 头以获取正确的 IP,
|
|
37
|
+
// 否则通过 nginx 代理后 remoteAddr 将始终为 127.0.0.1
|
|
38
|
+
trustProxy: true,
|
|
36
39
|
...this.options.fastifyOptions
|
|
37
40
|
});
|
|
38
41
|
}
|
package/lib/core/types.d.ts
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export * from './plugins/xml_parser';
|
|
|
19
19
|
export * from './services/base';
|
|
20
20
|
export * from './services/cache';
|
|
21
21
|
export * from './services/captcha';
|
|
22
|
+
export * from './services/dingtalk';
|
|
22
23
|
export * from './services/dispose';
|
|
23
24
|
export * from './services/jwt';
|
|
24
25
|
export * from './services/mail';
|
package/lib/index.js
CHANGED
|
@@ -20,6 +20,7 @@ export * from "./plugins/xml_parser";
|
|
|
20
20
|
export * from "./services/base";
|
|
21
21
|
export * from "./services/cache";
|
|
22
22
|
export * from "./services/captcha";
|
|
23
|
+
export * from "./services/dingtalk";
|
|
23
24
|
export * from "./services/dispose";
|
|
24
25
|
export * from "./services/jwt";
|
|
25
26
|
export * from "./services/mail";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BaseService } from './base';
|
|
2
|
+
export interface DingtalkServiceOptions {
|
|
3
|
+
/**
|
|
4
|
+
* 访问口令。
|
|
5
|
+
*/
|
|
6
|
+
accessToken: string;
|
|
7
|
+
/**
|
|
8
|
+
* 访问密钥。
|
|
9
|
+
*/
|
|
10
|
+
secret: string;
|
|
11
|
+
}
|
|
12
|
+
export interface DingtalkServiceSendOptions {
|
|
13
|
+
/**
|
|
14
|
+
* 通知标题。
|
|
15
|
+
*/
|
|
16
|
+
title: string;
|
|
17
|
+
/**
|
|
18
|
+
* 通知内容,Markdown 格式。
|
|
19
|
+
*/
|
|
20
|
+
content: string;
|
|
21
|
+
/**
|
|
22
|
+
* 要提醒的人员,设为 `all` 表示 @ 所有人,设为手机号数组只 @ 指定人。
|
|
23
|
+
*/
|
|
24
|
+
at?: 'all' | string[];
|
|
25
|
+
}
|
|
26
|
+
export declare class DingtalkService implements BaseService {
|
|
27
|
+
private options;
|
|
28
|
+
serviceName: string;
|
|
29
|
+
constructor(options: DingtalkServiceOptions);
|
|
30
|
+
send(options: DingtalkServiceSendOptions): Promise<{
|
|
31
|
+
errcode: number;
|
|
32
|
+
errmsg: string;
|
|
33
|
+
}>;
|
|
34
|
+
}
|
|
35
|
+
declare module '../x' {
|
|
36
|
+
interface X {
|
|
37
|
+
dingtalk: DingtalkService;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import got from 'got';
|
|
3
|
+
export class DingtalkService {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.options = options;
|
|
6
|
+
this.serviceName = 'dingtalk';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async send(options) {
|
|
10
|
+
const timestamp = Date.now();
|
|
11
|
+
const sign = crypto.createHmac('sha256', this.options.secret).update(`${timestamp}\n${this.options.secret}`).digest('base64');
|
|
12
|
+
const data = {
|
|
13
|
+
msgtype: 'markdown',
|
|
14
|
+
markdown: {
|
|
15
|
+
title: options.title,
|
|
16
|
+
text: options.content
|
|
17
|
+
},
|
|
18
|
+
at: {
|
|
19
|
+
atMobiles: Array.isArray(options.at) ? options.at : [],
|
|
20
|
+
isAtAll: options.at === 'all'
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
if (!data.at.isAtAll && data.at.atMobiles.length) {
|
|
25
|
+
const atMobiles = data.at.atMobiles.filter(mobile => !data.markdown.text.includes(`@${mobile}`));
|
|
26
|
+
|
|
27
|
+
if (atMobiles.length) {
|
|
28
|
+
const atMobilesText = atMobiles.map(mobile => `@${mobile}`).join(' ');
|
|
29
|
+
data.markdown.text += `<!-- ${atMobilesText} -->`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const {
|
|
34
|
+
body
|
|
35
|
+
} = await got.post('https://oapi.dingtalk.com/robot/send', {
|
|
36
|
+
responseType: 'json',
|
|
37
|
+
searchParams: {
|
|
38
|
+
access_token: this.options.accessToken,
|
|
39
|
+
timestamp: timestamp,
|
|
40
|
+
sign: sign
|
|
41
|
+
},
|
|
42
|
+
json: data
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (body.errcode !== 0) {
|
|
46
|
+
throw new Error(body.errmsg);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return body;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|