@domain.js/main 0.3.17 → 0.3.18

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.
@@ -9,10 +9,10 @@ interface Deps {
9
9
  redis: Pick<Redis, "hincrby" | "expire" | "hget" | "hdel">;
10
10
  }
11
11
  export declare function Main(cnf: Cnf, deps: Deps): {
12
- control: (field: string, ms: number, limit: number) => Promise<number>;
13
- check: (field: string, ms: number, limit: number) => Promise<number>;
12
+ control: (field: string, ms: number, limit: number, msg?: string) => Promise<number>;
13
+ check: (field: string, ms: number, limit: number, msg?: string) => Promise<number>;
14
14
  incr: (field: string, ms: number) => Promise<number>;
15
- generate: (field: string, ms: number, limit: number) => {
15
+ generate: (field: string, ms: number, limit: number, msg?: string) => {
16
16
  /** 全流程控制,会自动累加次数 */
17
17
  control(): Promise<number>;
18
18
  /** 检测是否超限 */
@@ -28,8 +28,9 @@ function Main(cnf, deps) {
28
28
  * @param field 控频字段key
29
29
  * @param ms 控频周期长度 毫秒
30
30
  * @param limit 控频极限次数
31
+ * @param msg 报错信息
31
32
  */
32
- const control = async (field, ms, limit) => {
33
+ const control = async (field, ms, limit, msg = "Too many requests") => {
33
34
  const now = Date.now();
34
35
  const key = getKey(ms, now);
35
36
  const val = await redis.hincrby(key, field, 1);
@@ -44,13 +45,14 @@ function Main(cnf, deps) {
44
45
  * @param filed 控频字段key
45
46
  * @param ms 控频周期长度 毫秒
46
47
  * @param limit 控频极限次数
48
+ * @param msg 报错信息
47
49
  */
48
- const check = async (field, ms, limit) => {
50
+ const check = async (field, ms, limit, msg = "Too many requests") => {
49
51
  const key = getKey(ms);
50
52
  const val = await redis.hget(key, field);
51
53
  const value = Number(val) | 0;
52
54
  if (value > limit)
53
- throw Error("Too many request");
55
+ throw Error(msg);
54
56
  return value;
55
57
  };
56
58
  /**
@@ -70,15 +72,16 @@ function Main(cnf, deps) {
70
72
  * @param filed 控频字段key
71
73
  * @param ms 控频周期长度 毫秒
72
74
  * @param limit 控频极限次数
75
+ * @param msg 报错信息
73
76
  */
74
- const generate = (field, ms, limit) => ({
77
+ const generate = (field, ms, limit, msg = "Too many requests") => ({
75
78
  /** 全流程控制,会自动累加次数 */
76
79
  control() {
77
- return control(field, ms, limit);
80
+ return control(field, ms, limit, msg);
78
81
  },
79
82
  /** 检测是否超限 */
80
83
  check() {
81
- return check(field, ms, limit);
84
+ return check(field, ms, limit, msg);
82
85
  },
83
86
  /** 仅做累加 */
84
87
  incr() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.17",
3
+ "version": "0.3.18",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {