@domain.js/main 0.1.0 → 0.1.5

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.
Files changed (64) hide show
  1. package/.husky/pre-commit +1 -1
  2. package/dist/Errors/index.d.ts +16 -0
  3. package/dist/Errors/index.js +27 -0
  4. package/dist/basic-errors.d.ts +1 -0
  5. package/dist/basic-errors.js +10 -0
  6. package/dist/cfg/index.d.ts +6 -0
  7. package/dist/cfg/index.js +26 -0
  8. package/dist/cli/index.d.ts +1 -1
  9. package/dist/cli/index.js +5 -5
  10. package/dist/cli/schema2ts.d.ts +1 -0
  11. package/dist/cli/schema2ts.js +37 -0
  12. package/dist/defaults.d.ts +102 -0
  13. package/dist/defaults.js +36 -0
  14. package/dist/deps/cache/After.d.ts +1 -2
  15. package/dist/deps/cache/Before.js +3 -3
  16. package/dist/deps/cache/Define.d.ts +2 -0
  17. package/dist/deps/cache/index.js +2 -2
  18. package/dist/deps/checker/index.d.ts +4 -0
  19. package/dist/deps/checker/index.js +2 -3
  20. package/dist/deps/cia/index.d.ts +8 -0
  21. package/dist/deps/cia/index.js +3 -6
  22. package/dist/deps/cron/index.d.ts +4 -0
  23. package/dist/deps/cron/index.js +2 -4
  24. package/dist/deps/defines.d.ts +2 -2
  25. package/dist/deps/defines.js +2 -2
  26. package/dist/deps/logger/index.d.ts +12 -1
  27. package/dist/deps/logger/index.js +6 -6
  28. package/dist/deps/parallel/index.d.ts +22 -4
  29. package/dist/deps/parallel/index.js +20 -11
  30. package/dist/deps/redis/index.d.ts +12 -1
  31. package/dist/deps/redis/index.js +12 -4
  32. package/dist/deps/request/index.d.ts +43 -0
  33. package/dist/deps/{axios → request}/index.js +17 -6
  34. package/dist/deps/rest/index.d.ts +16 -3
  35. package/dist/deps/rest/index.js +41 -2
  36. package/dist/deps/rest/stats.d.ts +8 -1
  37. package/dist/deps/rest/stats.js +9 -2
  38. package/dist/deps/rest/utils.d.ts +8 -0
  39. package/dist/deps/rest/utils.js +1 -3
  40. package/dist/deps/schema/index.d.ts +20 -5
  41. package/dist/deps/schema/index.js +35 -11
  42. package/dist/deps/sequelize/index.d.ts +7 -2
  43. package/dist/deps/sequelize/index.js +3 -4
  44. package/dist/deps/signer/index.d.ts +33 -8
  45. package/dist/deps/signer/index.js +50 -27
  46. package/dist/dm/index.d.ts +17 -2
  47. package/dist/dm/index.js +15 -0
  48. package/dist/http/defines.d.ts +45 -0
  49. package/dist/http/defines.js +2 -0
  50. package/dist/http/index.d.ts +2 -1
  51. package/dist/http/index.js +2 -1
  52. package/dist/http/router.d.ts +2 -1
  53. package/dist/http/router.js +3 -3
  54. package/dist/index.d.ts +12 -8
  55. package/dist/index.js +11 -4
  56. package/dist/types/index.d.ts +1 -0
  57. package/dist/types/index.js +2 -0
  58. package/dist/utils/index.d.ts +55 -13
  59. package/dist/utils/index.js +61 -15
  60. package/jest.config.js +8 -1
  61. package/package.json +18 -13
  62. package/dist/deps/axios/index.d.ts +0 -22
  63. package/dist/dm/dm.d.ts +0 -21
  64. package/dist/dm/dm.js +0 -57
@@ -2,35 +2,58 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Deps = exports.Main = void 0;
4
4
  const crypto = require("crypto");
5
+ /**
6
+ * API interface encryption signature algorithm module, based on sha256
7
+ * @returns generator And request Methods
8
+ */
5
9
  function Main() {
6
- const generator = (opt, secret) => {
7
- const string = Object.keys(opt)
8
- .map((k) => `${k}=${encodeURIComponent(opt[k])}`)
9
- .sort()
10
- .join("&");
11
- const h = crypto.createHmac("sha256", secret);
12
- return h.update(string).digest("base64");
10
+ /**
11
+ * API interface encryption signature algorithm module, based on sha256
12
+ */
13
+ const returns = {
14
+ /**
15
+ * Core encryption algorithm, the result is returned as Base64 string
16
+ * @param opt Objects to be encrypted
17
+ * @param secret Calculate the private key of the signature
18
+ * @returns The result of the signature is in the format of Base64 string
19
+ */
20
+ generator(opt, secret) {
21
+ const string = Object.keys(opt)
22
+ .map((k) => `${k}=${encodeURIComponent(opt[k])}`)
23
+ .sort()
24
+ .join("&");
25
+ const h = crypto.createHmac("sha256", secret);
26
+ return h.update(string).digest("base64");
27
+ },
28
+ /**
29
+ * Get all the signature encryption information required for a request
30
+ * @param uri The URI does not contain the root path part
31
+ * @param method Method name of the request interface, internal domain method name not HTTP verb
32
+ * @param key Key for signature calculation
33
+ * @param secret Calculate the private key of the signature
34
+ * @returns
35
+ */
36
+ request(uri, method, key, secret) {
37
+ const opt = {
38
+ uri,
39
+ key,
40
+ timestamp: (Date.now() / 1000) | 0,
41
+ signMethod: "HmacSHA256",
42
+ signVersion: "1",
43
+ method,
44
+ };
45
+ const signature = returns.generator(opt, secret);
46
+ return {
47
+ "x-auth-signature": signature,
48
+ "x-auth-key": key,
49
+ "x-auth-method": method,
50
+ "x-auth-timestamp": opt.timestamp,
51
+ "x-auth-sign-method": opt.signMethod,
52
+ "x-auth-sign-version": opt.signVersion,
53
+ };
54
+ },
13
55
  };
14
- const request = (uri, method, key, secret) => {
15
- const opt = {
16
- uri,
17
- key,
18
- timestamp: (Date.now() / 1000) | 0,
19
- signMethod: "HmacSHA256",
20
- signVersion: "1",
21
- method,
22
- };
23
- const signature = generator(opt, secret);
24
- return {
25
- "x-auth-signature": signature,
26
- "x-auth-key": key,
27
- "x-auth-method": method,
28
- "x-auth-timestamp": opt.timestamp,
29
- "x-auth-sign-method": opt.signMethod,
30
- "x-auth-sign-version": opt.signVersion,
31
- };
32
- };
33
- return { generator, request };
56
+ return returns;
34
57
  }
35
58
  exports.Main = Main;
36
59
  exports.Deps = [];
@@ -1,12 +1,27 @@
1
1
  interface ModuleInterface {
2
2
  Main: (...args: any[]) => any;
3
- Deps?: string[];
3
+ Deps?: string[] | ReadonlyArray<string>;
4
4
  Before?: (...args: any[]) => any;
5
5
  After?: (...args: any[]) => void;
6
6
  }
7
7
  declare type ModuleFn = (((...args: any[]) => any) | {}) & ModuleInterface;
8
+ /**
9
+ * Initialize a module
10
+ * @param Main The function is module main function
11
+ * @param Before The function is module before hook
12
+ * @param After The function is module after hook
13
+ * @param _args The array is module function arguments
14
+ * @returns Main function returns
15
+ */
8
16
  export declare function exec<Args extends any[], BeforeFn extends (...args: Args) => any[], MainFn extends (...args: ReturnType<BeforeFn>) => any, AfterFn extends (main: ReturnType<MainFn>, ...args: ReturnType<BeforeFn>) => void>(Main: MainFn, Before?: BeforeFn, After?: AfterFn | undefined, _args?: Args): ReturnType<MainFn>;
17
+ /**
18
+ * Automatically initialize a series of modules
19
+ * @param modules The Object that key is name of module, value is module
20
+ * @param deps This object is used to receive the object after module initialization
21
+ * @param args This array is used to initialize each module
22
+ * @returns Objects after initialization of all modules merged into deps
23
+ */
9
24
  export declare function auto<Modules extends {
10
25
  [k in string]: ModuleFn;
11
- }, Deps extends object, Args extends any[]>(modules: Modules, deps: Deps, args: Args): { [k in keyof Modules]: ReturnType<Modules[k]["Main"]>; };
26
+ }, Deps extends object, Args extends any[]>(modules: Modules, deps: Deps, args: Args): { [k in keyof Modules]: ReturnType<Modules[k]["Main"]>; } & Deps;
12
27
  export {};
package/dist/dm/index.js CHANGED
@@ -2,6 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.auto = exports.exec = void 0;
4
4
  const _ = require("lodash");
5
+ /**
6
+ * Initialize a module
7
+ * @param Main The function is module main function
8
+ * @param Before The function is module before hook
9
+ * @param After The function is module after hook
10
+ * @param _args The array is module function arguments
11
+ * @returns Main function returns
12
+ */
5
13
  function exec(Main, Before = ((...args) => args), After = undefined, _args = []) {
6
14
  const args = Before(..._args);
7
15
  const main = Main(...args);
@@ -10,6 +18,13 @@ function exec(Main, Before = ((...args) => args), After = undefined, _args = [])
10
18
  return main;
11
19
  }
12
20
  exports.exec = exec;
21
+ /**
22
+ * Automatically initialize a series of modules
23
+ * @param modules The Object that key is name of module, value is module
24
+ * @param deps This object is used to receive the object after module initialization
25
+ * @param args This array is used to initialize each module
26
+ * @returns Objects after initialization of all modules merged into deps
27
+ */
13
28
  function auto(modules, deps, args) {
14
29
  // 获取全部即将初始化的模块名称,此时的 modules 是扁平的一级结构
15
30
  const names = new Set(Object.keys(modules));
@@ -0,0 +1,45 @@
1
+ export interface Cnf {
2
+ proxyIps?: string;
3
+ port?: number;
4
+ host?: string;
5
+ bodyMaxBytes?: number;
6
+ apisRoute?: string;
7
+ swaggerApiPath?: string;
8
+ [propName: string]: any;
9
+ }
10
+ interface Sign {
11
+ signature: string;
12
+ uri: string;
13
+ key: string;
14
+ timestamp: number;
15
+ signMethod: string;
16
+ signVersion: string;
17
+ method: string;
18
+ }
19
+ export interface Profile {
20
+ clientIp: string;
21
+ remoteIp: string;
22
+ realIp: string;
23
+ userAgent: string;
24
+ startedAt: Date;
25
+ requestId: string;
26
+ revision?: string;
27
+ uuid?: string;
28
+ token?: string;
29
+ sign?: Sign;
30
+ }
31
+ export interface HttpCodes {
32
+ [propName: string]: number;
33
+ }
34
+ export interface Domain {
35
+ [propName: string]: (profile: Profile, params: any) => any | Domain;
36
+ }
37
+ export interface GetSchemaByPath {
38
+ (methodPath: string): [any, any];
39
+ }
40
+ export interface Err {
41
+ message: string;
42
+ code?: number | string;
43
+ data?: any;
44
+ }
45
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,10 +1,11 @@
1
1
  import * as restify from "restify";
2
2
  import { Router } from "./router";
3
- import { Cnf, Domain, Profile, HttpCodes } from "./defines";
3
+ import { Cnf, Domain, Profile, HttpCodes, GetSchemaByPath } from "./defines";
4
4
  interface Deps {
5
5
  routers(r: ReturnType<typeof Router>): void;
6
6
  domain: Domain;
7
7
  httpCodes: HttpCodes;
8
+ getSchemaByPath: GetSchemaByPath;
8
9
  swaggerDocJson?: any;
9
10
  makeProfileHook?: (obj: Profile, req: restify.Request) => any;
10
11
  }
@@ -6,7 +6,7 @@ const router_1 = require("./router");
6
6
  const utils_1 = require("./utils");
7
7
  function Main(cnf, deps) {
8
8
  const utils = (0, utils_1.Utils)(cnf);
9
- const { routers, domain, httpCodes, swaggerDocJson, makeProfileHook } = deps;
9
+ const { routers, getSchemaByPath, domain, httpCodes, swaggerDocJson, makeProfileHook } = deps;
10
10
  const server = restify.createServer();
11
11
  server.use(restify.plugins.queryParser());
12
12
  server.use(restify.plugins.bodyParser({
@@ -18,6 +18,7 @@ function Main(cnf, deps) {
18
18
  server,
19
19
  httpCodes,
20
20
  makeProfileHook,
21
+ getSchemaByPath,
21
22
  domain,
22
23
  apisRoute: cnf.apisRoute,
23
24
  swagger: [cnf.swaggerApiPath, swaggerDocJson],
@@ -1,8 +1,9 @@
1
1
  import * as restify from "restify";
2
2
  import { Utils } from "./utils";
3
- import { HttpCodes, Domain, Profile } from "./defines";
3
+ import { HttpCodes, Domain, Profile, GetSchemaByPath } from "./defines";
4
4
  interface Deps {
5
5
  domain: Domain;
6
+ getSchemaByPath: GetSchemaByPath;
6
7
  utils: ReturnType<typeof Utils>;
7
8
  server: restify.Server;
8
9
  httpCodes: HttpCodes;
@@ -5,7 +5,7 @@ const _ = require("lodash");
5
5
  const errors = require("restify-errors");
6
6
  const swaggerUi = require("swagger-ui-restify");
7
7
  function Router(deps) {
8
- const { domain, apisRoute, utils, server, httpCodes = {}, makeProfileHook, swagger = ["", {}], } = deps;
8
+ const { domain, apisRoute, getSchemaByPath, utils, server, httpCodes = {}, makeProfileHook, swagger = ["", {}], } = deps;
9
9
  const { ucwords, makeParams, makeProfile, outputCSV, jsonSchema2Swagger } = utils;
10
10
  // 改写 HttpErrorToJSON 处理 data
11
11
  const HttpErrorToJSON = errors.HttpError.prototype.toJSON;
@@ -56,7 +56,7 @@ function Router(deps) {
56
56
  const { path } = req.query;
57
57
  try {
58
58
  const { all } = req.query;
59
- const schema = domain._getSchemaByPath(path);
59
+ const schema = getSchemaByPath(path);
60
60
  res.send(all === undefined ? schema[1] : schema);
61
61
  }
62
62
  catch (e) {
@@ -72,7 +72,7 @@ function Router(deps) {
72
72
  let apiSchema = [];
73
73
  let desc = "";
74
74
  try {
75
- apiSchema = domain._getSchemaByPath(methodPath);
75
+ apiSchema = getSchemaByPath(methodPath);
76
76
  desc = apiSchema[1] ? apiSchema[1].description : "unknow";
77
77
  apiSchema = jsonSchema2Swagger(apiSchema[1] ? apiSchema[1] : {}, verb, methodPath, swaggerDocJson);
78
78
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,11 @@
1
+ import { Defaults } from "./defaults";
1
2
  import Deps = require("./deps/defines");
2
3
  export { Main as Http } from "./http";
4
+ export { Errors } from "./Errors";
3
5
  export * as DM from "./dm";
4
6
  export * as utils from "./utils";
7
+ export { Main as Cfg } from "./cfg";
8
+ export declare const basicErrors: Readonly<Record<"notFound" | "notAllowed" | "noAuth", import("./Errors").ErrorFn>>;
5
9
  declare type TDeps = typeof Deps;
6
10
  declare type Merge<T> = {
7
11
  [k in keyof T]: T[k];
@@ -13,7 +17,6 @@ declare type Include<T, U> = T extends U ? T : never;
13
17
  declare type RemoveReadonlyArray<T> = T extends ReadonlyArray<infer T1> ? T1 : false;
14
18
  export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T): (cnf: Merge<{
15
19
  aes: typeof import("./deps/aes");
16
- axios: typeof import("./deps/axios");
17
20
  cache: typeof import("./deps/cache");
18
21
  checker: typeof import("./deps/checker");
19
22
  cia: typeof import("./deps/cia");
@@ -23,14 +26,14 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
23
26
  hash: typeof import("./deps/hash");
24
27
  logger: typeof import("./deps/logger");
25
28
  parallel: typeof import("./deps/parallel");
26
- redis: typeof import("./deps/redis");
29
+ redis: typeof import("./deps/redis"); /** 模块名称联合类型 */
30
+ request: typeof import("./deps/request");
27
31
  rest: typeof import("./deps/rest");
28
32
  schema: typeof import("./deps/schema");
29
33
  sequelize: typeof import("./deps/sequelize");
30
34
  signer: typeof import("./deps/signer");
31
- }[Include<"logger", RemoveReadonlyArray<T>> | Include<"aes", RemoveReadonlyArray<T>> | Include<"cache", RemoveReadonlyArray<T>> | Include<"redis", RemoveReadonlyArray<T>> | Include<"graceful", RemoveReadonlyArray<T>> | Include<"cia", RemoveReadonlyArray<T>> | Include<"counter", RemoveReadonlyArray<T>> | Include<"cron", RemoveReadonlyArray<T>> | Include<"hash", RemoveReadonlyArray<T>> | Include<"parallel", RemoveReadonlyArray<T>> | Include<"sequelize", RemoveReadonlyArray<T>> | Include<"schema", RemoveReadonlyArray<T>> | Include<"rest", RemoveReadonlyArray<T>> | Include<"axios", RemoveReadonlyArray<T>> | Include<"checker", RemoveReadonlyArray<T>> | Include<"signer", RemoveReadonlyArray<T>>]["Main"] extends (arg: infer R, ...args: any[]) => any ? R : {}>) => { [k in keyof Pick<{
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<"rest", RemoveReadonlyArray<T>> | Include<"parallel", RemoveReadonlyArray<T>> | Include<"graceful", RemoveReadonlyArray<T>> | Include<"cia", RemoveReadonlyArray<T>> | Include<"counter", RemoveReadonlyArray<T>> | Include<"cron", RemoveReadonlyArray<T>> | Include<"hash", RemoveReadonlyArray<T>> | Include<"checker", RemoveReadonlyArray<T>> | Include<"signer", RemoveReadonlyArray<T>>]["Main"] extends (arg: infer R, ...args: any[]) => any ? R : {}>) => { [k in keyof Pick<{
32
36
  aes: typeof import("./deps/aes");
33
- axios: typeof import("./deps/axios");
34
37
  cache: typeof import("./deps/cache");
35
38
  checker: typeof import("./deps/checker");
36
39
  cia: typeof import("./deps/cia");
@@ -40,14 +43,14 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
40
43
  hash: typeof import("./deps/hash");
41
44
  logger: typeof import("./deps/logger");
42
45
  parallel: typeof import("./deps/parallel");
43
- redis: typeof import("./deps/redis");
46
+ redis: typeof import("./deps/redis"); /** 模块名称联合类型 */
47
+ request: typeof import("./deps/request");
44
48
  rest: typeof import("./deps/rest");
45
49
  schema: typeof import("./deps/schema");
46
50
  sequelize: typeof import("./deps/sequelize");
47
51
  signer: typeof import("./deps/signer");
48
52
  }, RemoveReadonlyArray<T>>]: ReturnType<Pick<{
49
53
  aes: typeof import("./deps/aes");
50
- axios: typeof import("./deps/axios");
51
54
  cache: typeof import("./deps/cache");
52
55
  checker: typeof import("./deps/checker");
53
56
  cia: typeof import("./deps/cia");
@@ -57,9 +60,10 @@ export declare function Main<T extends Readonly<Array<keyof TDeps>>>(features: T
57
60
  hash: typeof import("./deps/hash");
58
61
  logger: typeof import("./deps/logger");
59
62
  parallel: typeof import("./deps/parallel");
60
- redis: typeof import("./deps/redis");
63
+ redis: typeof import("./deps/redis"); /** 模块名称联合类型 */
64
+ request: typeof import("./deps/request");
61
65
  rest: typeof import("./deps/rest");
62
66
  schema: typeof import("./deps/schema");
63
67
  sequelize: typeof import("./deps/sequelize");
64
68
  signer: typeof import("./deps/signer");
65
- }, RemoveReadonlyArray<T>>[k]["Main"]>; };
69
+ }, RemoveReadonlyArray<T>>[k]["Main"]>; } & Defaults;
package/dist/index.js CHANGED
@@ -1,17 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Main = exports.utils = exports.DM = exports.Http = void 0;
4
- const _ = require("lodash");
3
+ exports.Main = exports.basicErrors = exports.Cfg = exports.utils = exports.DM = exports.Errors = exports.Http = void 0;
4
+ const defaults_1 = require("./defaults");
5
5
  const DM = require("./dm");
6
6
  const Deps = require("./deps/defines");
7
7
  var http_1 = require("./http");
8
8
  Object.defineProperty(exports, "Http", { enumerable: true, get: function () { return http_1.Main; } });
9
+ var Errors_1 = require("./Errors");
10
+ Object.defineProperty(exports, "Errors", { enumerable: true, get: function () { return Errors_1.Errors; } });
9
11
  exports.DM = require("./dm");
10
12
  exports.utils = require("./utils");
13
+ var cfg_1 = require("./cfg");
14
+ Object.defineProperty(exports, "Cfg", { enumerable: true, get: function () { return cfg_1.Main; } });
15
+ exports.basicErrors = defaults_1.defaults.errors;
11
16
  function Main(features) {
17
+ const { _ } = defaults_1.defaults;
12
18
  return (cnf) => {
13
- const deps = {};
14
- const modules = DM.auto(_.pick(Deps, features), deps, [cnf, deps]);
19
+ /** 这里之所以要浅拷贝,是为了避免多次初始化之间互相干扰 */
20
+ const _deps = { ...defaults_1.defaults };
21
+ const modules = DM.auto(_.pick(Deps, features) /** 要启用的内部模块 */, _deps /** 初始的模块依赖对象 */, [cnf, _deps] /** 内部模块初始化参数 */);
15
22
  return modules;
16
23
  };
17
24
  }
@@ -0,0 +1 @@
1
+ export declare type ReadonlyArray2union<T extends ReadonlyArray<any>> = T extends ReadonlyArray<infer A> ? A : never;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,36 +1,78 @@
1
- /** 计算给定字符串的md5值 */
1
+ /**
2
+ * Calculates the MD5 value of the given string
3
+ * @param str string or An object that contains a toString method that returns a string
4
+ * @returns hex md5 string
5
+ */
2
6
  export declare const md5: (str: {
3
7
  toString: () => string;
4
8
  }) => string;
5
9
  /**
6
- 生成随机字符串,
7
- @type: "strong" 强壮型 包括特殊字符
8
- @type: "normal" 普通型 不包括特殊字符
9
- @type: string 随机串字典手动指定
10
- */
10
+ * Return random string
11
+ * @param len string length
12
+ * @param type "strong" or "normal" or other string that custom character range string
13
+ */
11
14
  export declare function randStr(len: number, type: "strong"): string;
12
15
  export declare function randStr(len: number, type: "normal"): string;
13
16
  export declare function randStr(len: number, type: string): string;
14
- /** 将字符串里的换行,制表符替换为普通空格 */
17
+ /**
18
+ * Replace line breaks and tabs in the string with ordinary spaces
19
+ * @param value string that will be replaced
20
+ * @returns has been replaced string
21
+ */
15
22
  export declare const nt2space: (value: string) => string;
16
- /** 首字符大写 */
23
+ /**
24
+ * The first character of the string is capitalized
25
+ * @param value string
26
+ * @returns string
27
+ * @example ufrist("hello"); // Return a string is: "Hello"
28
+ * @see lcfirst
29
+ */
17
30
  export declare const ucfirst: (value: string) => string;
18
- /** 首字符小写 */
31
+ /**
32
+ * The first character of the string is lowercase
33
+ * @param value string
34
+ * @returns string
35
+ * @example ufrist("Hello"); // Return a string is: "hello"
36
+ * @see ucfirst
37
+ */
19
38
  export declare const lcfirst: (value: string) => string;
20
- /** 睡眠等待 */
39
+ /**
40
+ * Pause, waiting
41
+ * @param ms The time you want to wait, in milliseconds
42
+ * @returns None
43
+ */
21
44
  export declare const sleep: (ms: number) => Promise<unknown>;
22
- /** 深度冻结一个对象,防止被不小心篡改 */
45
+ /**
46
+ * Freeze a object and deepth
47
+ * @param object The object that will be freezed
48
+ * @returns freezed object
49
+ */
23
50
  export declare const deepFreeze: <T>(object: T) => Readonly<T>;
24
51
  /** try catch 包裹执行, 记录错误日志 */
25
52
  declare type TryCatchLogFn = <T extends (...args: any[]) => Promise<void>, L extends (...args: any[]) => void>(fn: T, errorLog: L) => (...args: Parameters<T>) => Promise<void>;
53
+ /**
54
+ * Mask exceptions of functions
55
+ * @param fn The function will be mask exceptions
56
+ * @param errorLog Error handle function, when has happed throw exception
57
+ * @returns Wrapped function
58
+ */
26
59
  export declare const tryCatchLog: TryCatchLogFn;
27
60
  /**
28
- 判断某个秒级时间戳是否已过期,基于当前时间
61
+ * Determine whether a second timestamp has expired
62
+ * @param time timestamp
63
+ * @param life Effective time, seconds
64
+ * @returns true or false
29
65
  */
30
66
  export declare const inExpired: (time: number, life: number) => boolean;
31
67
  declare type Params = {
32
68
  [K: string]: string;
33
69
  };
34
- /** 修改指定url上添加一些参数 */
70
+ /**
71
+ * Modify a URL address, add some attributes and delete some attributes
72
+ * @param address URL address
73
+ * @param adds The params will be expand to address
74
+ * @param removes The string list will be remove from address
75
+ * @returns Modified address
76
+ */
35
77
  export declare const modifiyURL: (address: string, adds?: Params | undefined, removes?: string[] | undefined) => string;
36
78
  export {};
@@ -7,7 +7,11 @@ const RAND_STR_DICT = {
7
7
  normal: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
8
8
  strong: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&’()*+,-./:;<=>?@[]^_`{|}~",
9
9
  };
10
- /** 计算给定字符串的md5值 */
10
+ /**
11
+ * Calculates the MD5 value of the given string
12
+ * @param str string or An object that contains a toString method that returns a string
13
+ * @returns hex md5 string
14
+ */
11
15
  const md5 = (str) => {
12
16
  const hash = crypto.createHash("md5");
13
17
  return hash.update(str.toString()).digest().toString("hex");
@@ -25,19 +29,43 @@ function randStr(len, type) {
25
29
  .join("");
26
30
  }
27
31
  exports.randStr = randStr;
28
- /** 将字符串里的换行,制表符替换为普通空格 */
32
+ /**
33
+ * Replace line breaks and tabs in the string with ordinary spaces
34
+ * @param value string that will be replaced
35
+ * @returns has been replaced string
36
+ */
29
37
  const nt2space = (value) => value.replace(/(\\[ntrfv]|\s)+/g, " ").trim();
30
38
  exports.nt2space = nt2space;
31
- /** 首字符大写 */
39
+ /**
40
+ * The first character of the string is capitalized
41
+ * @param value string
42
+ * @returns string
43
+ * @example ufrist("hello"); // Return a string is: "Hello"
44
+ * @see lcfirst
45
+ */
32
46
  const ucfirst = (value) => value[0].toUpperCase() + value.substring(1);
33
47
  exports.ucfirst = ucfirst;
34
- /** 首字符小写 */
48
+ /**
49
+ * The first character of the string is lowercase
50
+ * @param value string
51
+ * @returns string
52
+ * @example ufrist("Hello"); // Return a string is: "hello"
53
+ * @see ucfirst
54
+ */
35
55
  const lcfirst = (value) => value[0].toLowerCase() + value.substring(1);
36
56
  exports.lcfirst = lcfirst;
37
- /** 睡眠等待 */
57
+ /**
58
+ * Pause, waiting
59
+ * @param ms The time you want to wait, in milliseconds
60
+ * @returns None
61
+ */
38
62
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
39
63
  exports.sleep = sleep;
40
- /** 深度冻结一个对象,防止被不小心篡改 */
64
+ /**
65
+ * Freeze a object and deepth
66
+ * @param object The object that will be freezed
67
+ * @returns freezed object
68
+ */
41
69
  const deepFreeze = (object) => {
42
70
  // Retrieve the property names defined on object
43
71
  const propNames = Object.getOwnPropertyNames(object);
@@ -50,24 +78,42 @@ const deepFreeze = (object) => {
50
78
  return Object.freeze(object);
51
79
  };
52
80
  exports.deepFreeze = deepFreeze;
53
- const tryCatchLog = (fn, errorLog) => async (...args) => {
54
- try {
55
- await fn(...args);
56
- }
57
- catch (e) {
58
- errorLog(e);
59
- }
81
+ /**
82
+ * Mask exceptions of functions
83
+ * @param fn The function will be mask exceptions
84
+ * @param errorLog Error handle function, when has happed throw exception
85
+ * @returns Wrapped function
86
+ */
87
+ const tryCatchLog = (fn, errorLog) => {
88
+ const wrapped = async (...args) => {
89
+ try {
90
+ await fn(...args);
91
+ }
92
+ catch (e) {
93
+ errorLog(e);
94
+ }
95
+ };
96
+ return wrapped;
60
97
  };
61
98
  exports.tryCatchLog = tryCatchLog;
62
99
  /**
63
- 判断某个秒级时间戳是否已过期,基于当前时间
100
+ * Determine whether a second timestamp has expired
101
+ * @param time timestamp
102
+ * @param life Effective time, seconds
103
+ * @returns true or false
64
104
  */
65
105
  const inExpired = (time, life) => {
66
106
  const now = (Date.now() / 1000) | 0;
67
107
  return time < now - life;
68
108
  };
69
109
  exports.inExpired = inExpired;
70
- /** 修改指定url上添加一些参数 */
110
+ /**
111
+ * Modify a URL address, add some attributes and delete some attributes
112
+ * @param address URL address
113
+ * @param adds The params will be expand to address
114
+ * @param removes The string list will be remove from address
115
+ * @returns Modified address
116
+ */
71
117
  const modifiyURL = (address, adds, removes) => {
72
118
  const obj = new URL(address);
73
119
  if (typeof adds === "object") {
package/jest.config.js CHANGED
@@ -2,5 +2,12 @@
2
2
  module.exports = {
3
3
  preset: "ts-jest",
4
4
  testEnvironment: "node",
5
- forceExit: true,
5
+ // forceExit: true,
6
+ collectCoverageFrom: [
7
+ "src/utils/*.ts",
8
+ "src/deps/**/*.ts",
9
+ "src/dm/index.ts",
10
+ "src/http/**/*.ts",
11
+ "src/cli/*.ts",
12
+ ],
6
13
  };