@jayfong/x-server 1.31.1 → 1.32.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 CHANGED
@@ -2,6 +2,22 @@
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.32.0](https://github.com/jfWorks/x-server/compare/v1.31.3...v1.32.0) (2022-05-31)
6
+
7
+
8
+ ### Features
9
+
10
+ * add DingtalkService ([3cbe91c](https://github.com/jfWorks/x-server/commit/3cbe91cd82c6cfc5fa4c5bcf2d8c7eb93711ef7c))
11
+
12
+ ### [1.31.3](https://github.com/jfWorks/x-server/compare/v1.31.2...v1.31.3) (2022-05-30)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * defineCron ([5d72533](https://github.com/jfWorks/x-server/commit/5d72533c039645833391ce31f0403c96b2c5ef77))
18
+
19
+ ### [1.31.2](https://github.com/jfWorks/x-server/compare/v1.31.1...v1.31.2) (2022-05-30)
20
+
5
21
  ### [1.31.1](https://github.com/jfWorks/x-server/compare/v1.31.0...v1.31.1) (2022-05-30)
6
22
 
7
23
  ## [1.31.0](https://github.com/jfWorks/x-server/compare/v1.30.2...v1.31.0) (2022-05-30)
@@ -8,7 +8,8 @@ var _cron = require("cron");
8
8
  function defineCron(options) {
9
9
  return new _cron.CronJob({
10
10
  cronTime: options.expression,
11
- runOnInit: true,
11
+ runOnInit: options.runOnInit,
12
+ start: true,
12
13
  onTick: options.handle
13
14
  });
14
15
  }
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;
@@ -2,7 +2,8 @@ import { CronJob } from 'cron';
2
2
  export function defineCron(options) {
3
3
  return new CronJob({
4
4
  cronTime: options.expression,
5
- runOnInit: true,
5
+ runOnInit: options.runOnInit,
6
+ start: true,
6
7
  onTick: options.handle
7
8
  });
8
9
  }
@@ -139,12 +139,6 @@ export declare namespace XTask {
139
139
  * 任务名称
140
140
  */
141
141
  name: string;
142
- /**
143
- * 是否作为定时任务
144
- *
145
- * @see https://crontab.guru/
146
- */
147
- cron?: string;
148
142
  /**
149
143
  * 处理器
150
144
  */
@@ -162,6 +156,10 @@ export declare namespace XCron {
162
156
  * @see https://crontab.cronhub.io/
163
157
  */
164
158
  expression: string;
159
+ /**
160
+ * 是否初始化后执行一次
161
+ */
162
+ runOnInit?: boolean;
165
163
  /**
166
164
  * 处理器
167
165
  */
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.31.1",
3
+ "version": "1.32.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",