@domain.js/main 0.3.11 → 0.3.14

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.
@@ -3,6 +3,7 @@ import * as cache from "./cache";
3
3
  import * as checker from "./checker";
4
4
  import * as counter from "./counter";
5
5
  import * as cron from "./cron";
6
+ import * as frequency from "./frequency";
6
7
  import * as graceful from "./graceful";
7
8
  import * as hash from "./hash";
8
9
  import * as logger from "./logger";
@@ -20,6 +21,7 @@ declare const _default: {
20
21
  checker: typeof checker;
21
22
  counter: typeof counter;
22
23
  cron: typeof cron;
24
+ frequency: typeof frequency;
23
25
  graceful: typeof graceful;
24
26
  hash: typeof hash;
25
27
  logger: typeof logger;
@@ -24,6 +24,7 @@ const cache = __importStar(require("./cache"));
24
24
  const checker = __importStar(require("./checker"));
25
25
  const counter = __importStar(require("./counter"));
26
26
  const cron = __importStar(require("./cron"));
27
+ const frequency = __importStar(require("./frequency"));
27
28
  const graceful = __importStar(require("./graceful"));
28
29
  const hash = __importStar(require("./hash"));
29
30
  const logger = __importStar(require("./logger"));
@@ -41,6 +42,7 @@ module.exports = {
41
42
  checker: checker,
42
43
  counter: counter,
43
44
  cron: cron,
45
+ frequency: frequency,
44
46
  graceful: graceful,
45
47
  hash: hash,
46
48
  logger: logger,
@@ -0,0 +1,14 @@
1
+ import { Redis } from "ioredis";
2
+ interface Cnf {
3
+ frequency: {
4
+ key: string;
5
+ };
6
+ }
7
+ export declare const Deps: readonly ["redis"];
8
+ interface Deps {
9
+ redis: Pick<Redis, "hincrby" | "expire">;
10
+ }
11
+ export declare function Main(cnf: Cnf, deps: Deps): {
12
+ check: (filed: string, ms: number, limit: number) => Promise<void>;
13
+ };
14
+ export {};
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Main = exports.Deps = void 0;
4
+ exports.Deps = ["redis"];
5
+ function Main(cnf, deps) {
6
+ const { redis } = deps;
7
+ const { frequency: { key: KEY }, } = cnf;
8
+ /**
9
+ * 检测频次控制,超过显示过抛出异常
10
+ * @param filed 控频字段key
11
+ * @param ms 控频周期长度 毫秒
12
+ * @param limit 控频极限次数
13
+ */
14
+ const check = async (filed, ms, limit) => {
15
+ const now = Date.now();
16
+ const t = Math.floor(now / ms);
17
+ const key = `${KEY}-${ms}-${t}`;
18
+ const val = await redis.hincrby(key, filed, 1);
19
+ if (val === 1)
20
+ await redis.expire(key, Math.floor((ms * (t + 1) - now) / 1000));
21
+ if (val > limit)
22
+ throw Error("Too many request");
23
+ };
24
+ return {
25
+ check,
26
+ };
27
+ }
28
+ exports.Main = Main;
@@ -83,5 +83,7 @@ export declare class ModelBase<Attrs extends {} = any, Attrs4Create extends {} =
83
83
  };
84
84
  /** 联合唯一列名称集合,用来自动恢复软删除的资源 */
85
85
  static unique?: string[];
86
+ /** cache 是否开启 */
87
+ static cache: boolean;
86
88
  }
87
89
  export {};
@@ -61,3 +61,5 @@ ModelBase.sort = {
61
61
  defaultDirection: "DESC",
62
62
  allow: ["id"],
63
63
  };
64
+ /** cache 是否开启 */
65
+ ModelBase.cache = true;
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
21
21
  checker: typeof import("./deps/checker");
22
22
  counter: typeof import("./deps/counter");
23
23
  cron: typeof import("./deps/cron");
24
+ frequency: typeof import("./deps/frequency");
24
25
  graceful: typeof import("./deps/graceful");
25
26
  hash: typeof import("./deps/hash");
26
27
  logger: typeof import("./deps/logger");
@@ -32,12 +33,13 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
32
33
  schema: typeof import("./deps/schema");
33
34
  sequelize: typeof import("./deps/sequelize");
34
35
  signer: typeof import("./deps/signer");
35
- }[Include<"schema", RemoveReadonlyArray<T>> | Include<"logger", RemoveReadonlyArray<T>> | Include<"aes", RemoveReadonlyArray<T>> | Include<"request", RemoveReadonlyArray<T>> | Include<"sequelize", RemoveReadonlyArray<T>> | Include<"cache", RemoveReadonlyArray<T>> | Include<"redis", RemoveReadonlyArray<T>> | Include<"counter", RemoveReadonlyArray<T>> | Include<"cron", RemoveReadonlyArray<T>> | Include<"myCia", RemoveReadonlyArray<T>> | Include<"hash", RemoveReadonlyArray<T>> | Include<"rest", RemoveReadonlyArray<T>> | Include<"parallel", RemoveReadonlyArray<T>> | Include<"graceful", RemoveReadonlyArray<T>> | Include<"checker", RemoveReadonlyArray<T>> | Include<"signer", RemoveReadonlyArray<T>>]["Main"] extends (arg: infer R, ...args: any[]) => any ? R : {}>) => { [k in keyof Pick<{
36
+ }[Include<"frequency", RemoveReadonlyArray<T>> | Include<"schema", RemoveReadonlyArray<T>> | Include<"logger", RemoveReadonlyArray<T>> | Include<"aes", RemoveReadonlyArray<T>> | Include<"request", RemoveReadonlyArray<T>> | Include<"sequelize", RemoveReadonlyArray<T>> | Include<"cache", RemoveReadonlyArray<T>> | Include<"redis", RemoveReadonlyArray<T>> | Include<"counter", RemoveReadonlyArray<T>> | Include<"cron", RemoveReadonlyArray<T>> | Include<"myCia", RemoveReadonlyArray<T>> | Include<"hash", RemoveReadonlyArray<T>> | Include<"rest", RemoveReadonlyArray<T>> | Include<"parallel", RemoveReadonlyArray<T>> | Include<"graceful", RemoveReadonlyArray<T>> | Include<"checker", RemoveReadonlyArray<T>> | Include<"signer", RemoveReadonlyArray<T>>]["Main"] extends (arg: infer R, ...args: any[]) => any ? R : {}>) => { [k in keyof Pick<{
36
37
  aes: typeof import("./deps/aes");
37
38
  cache: typeof import("./deps/cache");
38
39
  checker: typeof import("./deps/checker");
39
40
  counter: typeof import("./deps/counter");
40
41
  cron: typeof import("./deps/cron");
42
+ frequency: typeof import("./deps/frequency");
41
43
  graceful: typeof import("./deps/graceful");
42
44
  hash: typeof import("./deps/hash");
43
45
  logger: typeof import("./deps/logger");
@@ -55,6 +57,7 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
55
57
  checker: typeof import("./deps/checker");
56
58
  counter: typeof import("./deps/counter");
57
59
  cron: typeof import("./deps/cron");
60
+ frequency: typeof import("./deps/frequency");
58
61
  graceful: typeof import("./deps/graceful");
59
62
  hash: typeof import("./deps/hash");
60
63
  logger: typeof import("./deps/logger");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.11",
3
+ "version": "0.3.14",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -18,16 +18,6 @@
18
18
  "author": "Redstone Zhao",
19
19
  "license": "MIT",
20
20
  "devDependencies": {
21
- "@types/async": "^3.2.10",
22
- "@types/crypto-js": "^4.0.2",
23
- "@types/ioredis": "^4.28.1",
24
- "@types/jest": "^27.0.3",
25
- "@types/lodash": "^4.14.177",
26
- "@types/lru-cache": "^5.1.1",
27
- "@types/restify-errors": "^4.3.4",
28
- "@types/restify": "^8.5.4",
29
- "@types/uuid": "^8.3.3",
30
- "@types/validator": "^13.7.0",
31
21
  "@typescript-eslint/eslint-plugin": "^5.11.0",
32
22
  "@typescript-eslint/parser": "^5.11.0",
33
23
  "babel-eslint": "^10.1.0",
@@ -62,6 +52,16 @@
62
52
  ]
63
53
  },
64
54
  "dependencies": {
55
+ "@types/async": "^3.2.10",
56
+ "@types/crypto-js": "^4.0.2",
57
+ "@types/ioredis": "^4.28.1",
58
+ "@types/jest": "^27.0.3",
59
+ "@types/lodash": "^4.14.177",
60
+ "@types/lru-cache": "^5.1.1",
61
+ "@types/restify-errors": "^4.3.4",
62
+ "@types/restify": "^8.5.4",
63
+ "@types/uuid": "^8.3.3",
64
+ "@types/validator": "^13.7.0",
65
65
  "ajv": "^8.8.1",
66
66
  "ajv-formats": "^2.1.1",
67
67
  "ajv-keywords": "^5.1.0",