@domain.js/main 0.1.8 → 0.1.12

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/.eslintrc.js CHANGED
@@ -1,67 +1,32 @@
1
1
  module.exports = {
2
- parser: "@typescript-eslint/parser",
3
- plugins: ["@typescript-eslint"],
4
- extends: ["airbnb", "eslint:recommended", "plugin:@typescript-eslint/recommended"],
5
- ignorePatterns: [".eslintrc.js"],
6
- root: true,
2
+ parser: "@typescript-eslint/parser", // 解析ts 代码编译器,将ts 语法树转化为 eslint 期望的
3
+ extends: ["alloy", "alloy/typescript"],
4
+ plugins: ["simple-import-sort", "unused-imports"],
7
5
  env: {
8
- es6: true,
6
+ // Your environments (which contains several predefined global variables)
7
+ //
9
8
  node: true,
10
- jest: true,
9
+ commonjs: true,
10
+ es6: true,
11
11
  },
12
- settings: {
13
- react: {
14
- version: "999.999.999",
15
- },
12
+ globals: {
13
+ // Your global variables (setting to false means it's not allowed to be reassigned)
14
+ //
15
+ // myGlobal: false
16
16
  },
17
17
  rules: {
18
- "nonblock-statement-body-position": "off",
19
- "operator-linebreak": ["error", "after"],
20
- "prefer-destructuring": ["error", { object: true, array: true }],
21
- indent: ["error", 2],
22
- "no-use-before-define": ["error", { functions: true, classes: true, variables: true }],
23
- "no-param-reassign": 0,
24
- "no-continue": 0,
25
- "no-console": 0,
26
- "no-debugger": 1,
27
- "no-var": 1,
28
- semi: ["error", "always"],
29
- "no-trailing-spaces": "error",
30
- "eol-last": 1,
31
- "no-underscore-dangle": 0,
32
- "no-empty": "error",
33
- "no-loop-func": "error",
34
- "no-bitwise": "off", // "error",
35
- quotes: ["error", "double"],
36
- "comma-dangle": ["error", "only-multiline"],
37
- "arrow-parens": ["error", "always"],
38
- "object-curly-newline": 0,
39
- "function-paren-newline": 0,
40
- "implicit-arrow-linebreak": 0,
41
- curly: 0,
42
- "no-mixed-operators": "error",
43
- "no-restricted-syntax": 0,
44
- "no-lone-blocks": "error",
45
- "lines-between-class-members": "off",
46
- yoda: "error",
47
- "no-plusplus": 0,
48
- "import/export": 0,
49
- "no-cond-assign": "error",
50
- "no-shadow": 0,
51
- camelcase: 0,
52
- "prefer-destructuring": 0,
53
- "import/prefer-default-export": "off",
54
- "newline-per-chained-call": 0,
55
- "@typescript-eslint/interface-name-prefix": "off",
56
- "@typescript-eslint/explicit-function-return-type": "off",
57
- "@typescript-eslint/explicit-module-boundary-types": "off",
58
- "@typescript-eslint/no-explicit-any": "off",
59
- "@typescript-eslint/no-namespace": 0,
60
- "@typescript-eslint/ban-types": 0,
61
- "import/export": 0,
62
- "import/no-unresolved": 0,
63
- "import/extensions": 0,
64
- "@typescript-eslint/no-var-requires": 0,
65
- "@typescript-eslint/no-this-alias": 0,
18
+ // Customize your rules
19
+ "spaced-comment": "off", // 注释前有空白检查
20
+ "simple-import-sort/imports": "error", // 优化import 顺序
21
+ "simple-import-sort/exports": "error", // 优化 export 顺序
22
+ "no-multiple-empty-lines": ["warn", { max: 2 }], // 强制最大连续空行数为 2
23
+ complexity: ["error", 20], // https://cn.eslint.org/docs/rules/complexity
24
+ "no-template-curly-in-string": "error", //禁止在常规字符串中出现模板字面量占位符语法
25
+ "unused-imports/no-unused-imports": "warn",
26
+ "@typescript-eslint/explicit-member-accessibility": "off", // 不强制设置类成员的可访问性
27
+ "@typescript-eslint/member-ordering": "off", // 不严格限制类成员顺序
28
+ "max-params": ["error", 6], // 函数最多6个参数
29
+ "@typescript-eslint/no-require-imports": "off", // 部分第三方库不支持 esModuleInterop 编译选项
30
+ "@typescript-eslint/prefer-optional-chain": "off", // 过多使用可选操作符降低代码可读性 foo?.a?.b?.c
66
31
  },
67
32
  };
package/.husky/pre-commit CHANGED
@@ -5,3 +5,4 @@ fi
5
5
  . "$(dirname "$0")/_/husky.sh"
6
6
 
7
7
  ts-node src/cli/index.ts loadDeps ./src/deps ts && npm run lint-staged && npm run build
8
+ npm tst
@@ -16,7 +16,7 @@ interface callbackArg {
16
16
  interface Deps {
17
17
  cronParser: typeof cronParser;
18
18
  humanInterval: typeof human;
19
- cia: {
19
+ myCia: {
20
20
  regist: (name: string, validator: any, waiters: waiter[]) => void;
21
21
  submit: (name: string, times: number, callback: (arg: callbackArg) => void) => void;
22
22
  };
@@ -4,7 +4,7 @@ exports.Deps = exports.Main = void 0;
4
4
  function Main(cnf, deps) {
5
5
  const { cron = {} } = cnf;
6
6
  const ciaTaskType = "cronJob";
7
- const { cia, humanInterval: human, cronParser: parser } = deps;
7
+ const { myCia, humanInterval: human, cronParser: parser } = deps;
8
8
  const { tz = "Asia/Shanghai" } = cron;
9
9
  // 注册信息
10
10
  const registed = {};
@@ -40,7 +40,7 @@ function Main(cnf, deps) {
40
40
  setTimeout(() => {
41
41
  opt.times += 1;
42
42
  opt.triggeredAt = Date.now();
43
- cia.submit(`Cron::${name}`, opt.times, ({ cronJob: [err, , totalMS] }) => {
43
+ myCia.submit(`Cron::${name}`, opt.times, ({ cronJob: [err, , totalMS] }) => {
44
44
  if (err) {
45
45
  opt.failds += 1;
46
46
  }
@@ -72,7 +72,7 @@ function Main(cnf, deps) {
72
72
  };
73
73
  // 注册到cia上, 为了借助cia的能力自动下发任务
74
74
  // 增加 Cron:: 前缀是为了避免和其他任务名称冲突
75
- cia.regist(`Cron::${name}`, null, [{ type: ciaTaskType }]);
75
+ myCia.regist(`Cron::${name}`, null, [{ type: ciaTaskType }]);
76
76
  };
77
77
  const start = () => {
78
78
  if (startedAt)
@@ -24,14 +24,23 @@ function Main(cnf, deps, utils) {
24
24
  * @param _cols Allow columns to be updated
25
25
  * @returns The resource that has been updated
26
26
  */
27
- const modify = (Model, model, params, isAdmin = false, _cols) => {
27
+ const modify = async (Model, model, params, isAdmin = false, _cols) => {
28
28
  const cols = _cols || Model.editableCols || Model.writableCols || [];
29
29
  const attr = pickParams(params, cols, Model, isAdmin);
30
30
  // 避免id 被篡改,强制删除id属性
31
31
  if (attr.id)
32
32
  delete attr.id;
33
33
  Object.assign(model, attr);
34
- return model.save();
34
+ const fields = model.changed();
35
+ if (!Array.isArray(fields) || !fields.length)
36
+ return model;
37
+ try {
38
+ return await model.save();
39
+ }
40
+ catch (e) {
41
+ Object.assign(model, model.previous());
42
+ throw e;
43
+ }
35
44
  };
36
45
  /**
37
46
  * Restful add(C of CRUD) for create a resource
@@ -0,0 +1,14 @@
1
+ import * as TypeORM from "typeorm";
2
+ interface Cnf {
3
+ typeorm: {
4
+ [propName: string]: Parameters<typeof TypeORM.createConnection>[0];
5
+ };
6
+ }
7
+ interface Deps {
8
+ TypeORM: typeof TypeORM;
9
+ }
10
+ export declare function Main(cnf: Cnf, deps: Deps): Promise<{
11
+ [propName: string]: TypeORM.Connection;
12
+ }>;
13
+ export declare const Deps: string[];
14
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Deps = exports.Main = void 0;
4
+ async function Main(cnf, deps) {
5
+ // 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
6
+ const { typeorm: dbs } = cnf;
7
+ const { TypeORM } = deps;
8
+ const links = {};
9
+ for await (const k of Object.keys(dbs)) {
10
+ const db = dbs[k];
11
+ links[k] = await TypeORM.createConnection(db);
12
+ }
13
+ return links;
14
+ }
15
+ exports.Main = Main;
16
+ exports.Deps = ["TypeORM"];
@@ -22,6 +22,8 @@ export interface Profile {
22
22
  userAgent: string;
23
23
  startedAt: Date;
24
24
  requestId: string;
25
+ /** 自由挂载信息的节点 */
26
+ extra: Record<string, any>;
25
27
  revision?: string;
26
28
  uuid?: string;
27
29
  token?: string;
@@ -49,6 +49,7 @@ function Utils(cnf) {
49
49
  userAgent: req.userAgent(),
50
50
  startedAt: new Date(),
51
51
  requestId: req.id(),
52
+ extra: {},
52
53
  };
53
54
  const token = req.headers["x-auth-token"] || req.query.access_token || req.query.accessToken;
54
55
  // token 和签名认证只能二选一
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.1.8",
3
+ "version": "0.1.12",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -22,21 +22,24 @@
22
22
  "@typescript-eslint/parser": "^4.22.0",
23
23
  "babel-eslint": "^10.1.0",
24
24
  "codecov": "^3.8.3",
25
- "eslint": "^7.11.0",
25
+ "eslint": "^8.8.0",
26
26
  "eslint-config-airbnb": "^18.2.0",
27
+ "eslint-config-alloy": "^4.4.0",
27
28
  "eslint-config-prettier": "^8.3.0",
28
29
  "eslint-plugin-import": "^2.22.1",
29
30
  "eslint-plugin-jsx-a11y": "^6.3.1",
30
31
  "eslint-plugin-prettier": "^3.4.0",
31
32
  "eslint-plugin-react": "^7.21.4",
33
+ "eslint-plugin-simple-import-sort": "^7.0.0",
34
+ "eslint-plugin-unused-imports": "^2.0.0",
32
35
  "husky": "^7.0.0",
33
36
  "jest": "^27.3.1",
34
37
  "lint-staged": "^11.0.0",
35
38
  "prettier": "^2.3.0",
36
39
  "sequelize-json-schema": "^2.1.1",
37
40
  "ts-jest": "^27.0.7",
38
- "ts-node": "^10.4.0",
39
- "typescript": "^4.5.2"
41
+ "ts-node": "^10.5.0",
42
+ "typescript": "^4.5.5"
40
43
  },
41
44
  "prettier": {
42
45
  "printWidth": 100,
package/tsconfig.json CHANGED
@@ -6,7 +6,8 @@
6
6
  "declaration": true,
7
7
  "outDir": "./dist",
8
8
  "strict": true,
9
- "rootDir": "./src/"
9
+ "rootDir": "./src/",
10
+ "esModuleInterop": true
10
11
  },
11
12
  "include": ["./"],
12
13
  "exclude": ["./src/**/*.test.ts", "./src/**/samples/*", "node_modules", "dist"]