mas-server 2.0.64 → 3.0.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.
Files changed (58) hide show
  1. package/.temp/getRouteByApiSrc.ts +2 -2
  2. package/.temp/updateRouter.ts +3 -3
  3. package/README.md +7 -1
  4. package/beforebuild.js +1 -2
  5. package/dist/index.js +12 -39
  6. package/dist/utils/createRouter.js +6 -5
  7. package/dist/utils/getRouterInfo.js +5 -10
  8. package/dist/utils/masUtils.js +1 -3
  9. package/dist/utils/meaToken.js +2 -2
  10. package/dist/utils/readApi.js +1 -1
  11. package/dist/utils/validType.js +1 -1
  12. package/package.json +53 -53
  13. package/src/index.ts +25 -72
  14. package/src/typings/index.ts +106 -124
  15. package/src/utils/apiLimit.ts +1 -1
  16. package/src/utils/createRouter.ts +5 -5
  17. package/src/utils/getRouterInfo.ts +5 -9
  18. package/src/utils/masUtils.ts +0 -1
  19. package/src/utils/meaToken.ts +56 -56
  20. package/src/utils/validRouteData.ts +3 -3
  21. package/src/utils/validToken.ts +2 -2
  22. package/test/apiTemplate/curd/del.ts +2 -2
  23. package/test/apiTemplate/curd/get.ts +2 -2
  24. package/test/apiTemplate/curd/set.ts +2 -2
  25. package/test/apiTemplate/defaultApi.ts +8 -0
  26. package/test/config/serverConfig/config.ts +29 -0
  27. package/test/config/{createSqlFormData.ts → sqlConfig/createSqlFormData.ts} +1 -2
  28. package/test/main.ts +20 -17
  29. package/test/scripts/beforeCreated.ts +27 -27
  30. package/test/scripts/createApis.ts +1 -1
  31. package/test/scripts/createSqlForm.ts +4 -4
  32. package/test/scripts/getSqlFormType.ts +1 -1
  33. package/test/src/api/example/ArrValid.ts +3 -4
  34. package/test/src/api/example/per.ts +5 -5
  35. package/test/src/api/example/tip.ts +3 -5
  36. package/test/src/api/example/validReqData.ts +3 -6
  37. package/test/src/api/index.ts +3 -6
  38. package/test/src/debug.ts +0 -5
  39. package/test/src/masState.ts +3 -3
  40. package/dist/apiDoc/assets/empty-3b5fe714.js +0 -1
  41. package/dist/apiDoc/assets/index-2d9f5a8a.js +0 -1427
  42. package/dist/apiDoc/assets/index-b04b459f.css +0 -6
  43. package/dist/apiDoc/assets/jsoneditor-icons-45c98bf0.svg +0 -749
  44. package/dist/apiDoc/favicon.ico +0 -0
  45. package/dist/apiDoc/index.html +0 -15
  46. package/dist/utils/apiDoc.js +0 -33
  47. package/src/apiDoc/assets/empty-3b5fe714.js +0 -1
  48. package/src/apiDoc/assets/index-2d9f5a8a.js +0 -1427
  49. package/src/apiDoc/assets/index-b04b459f.css +0 -6
  50. package/src/apiDoc/assets/jsoneditor-icons-45c98bf0.svg +0 -749
  51. package/src/apiDoc/favicon.ico +0 -0
  52. package/src/apiDoc/index.html +0 -15
  53. package/src/utils/apiDoc.ts +0 -34
  54. package/test/apiTemplate/api.temp.ts +0 -11
  55. package/test/config/config.ts +0 -36
  56. package/test/config/defaultApiConfig.ts +0 -7
  57. package/test/config/docNav.ts +0 -4
  58. package/test/src/sql/selectStudent.ts +0 -7
@@ -1,10 +1,10 @@
1
1
  import readApi from "./readApi";
2
2
  import c from "ansi-colors";
3
- import { apiConfigType } from "../typings";
3
+ import { ApiConfigType } from "../typings";
4
4
 
5
5
  export default function getRouteByApiSrc(
6
6
  apiSrc,
7
- apiConfig: apiConfigType,
7
+ apiConfig: ApiConfigType,
8
8
  apiKey: { [key: string]: string }
9
9
  ) {
10
10
  const routerArr = readApi(apiSrc, "api");
@@ -23,8 +23,8 @@ const { newApiConfig, newApiConfigArr, isAdd, newApiKey } = getRouteByApiSrc(
23
23
  if (isAdd) {
24
24
  fs.writeFileSync(
25
25
  path.join(__dirname, "../config/api.config.ts"),
26
- `import { apiConfigType } from "@/index";
27
- const apiConfig: apiConfigType = ${
26
+ `import { ApiConfigType } from "@/index";
27
+ const apiConfig: ApiConfigType = ${
28
28
  // 构造器类型正则转译
29
29
  JSON.stringify(newApiConfig, (key, value) => {
30
30
  if (typeof value === "function") {
@@ -43,4 +43,4 @@ if (isAdd) {
43
43
  path.join(__dirname, "../config/api.key.ts"),
44
44
  "export default " + JSON.stringify(newApiKey)
45
45
  );
46
- }
46
+ }
package/README.md CHANGED
@@ -1 +1,7 @@
1
- 一款基于express面向中小型项目的后端框架
1
+ 一款基于 express 面向中小型项目的后端框架
2
+ 功能如下:
3
+ 约定式路由
4
+ 权限管理
5
+ 日志管理
6
+ IP 限流
7
+ JWT 验证
package/beforebuild.js CHANGED
@@ -6,5 +6,4 @@ if (fs.existsSync(path.join(__dirname, "./dist"))) {
6
6
  } else {
7
7
  fs.mkdirSync(path.join(__dirname, "dist"));
8
8
  }
9
- fs.cpSync(path.join(__dirname, "./src/utils/mas-encrypt.min.js"), path.join(__dirname, "./dist/utils/mas-encrypt.min.js"), { recursive: true });
10
- fs.cpSync(path.join(__dirname, "./src/apiDoc/"), path.join(__dirname, "./dist/apiDoc/"), { recursive: true });
9
+ fs.cpSync(path.join(__dirname, "./src/utils/mas-encrypt.min.js"), path.join(__dirname, "./dist/utils/mas-encrypt.min.js"), { recursive: true });
package/dist/index.js CHANGED
@@ -3,11 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.validType = exports.createToken = exports.validToken = exports.massql = exports.getsqlFormDataType = exports.getsqlFormData = exports.createSqlForm = exports.checkSqlKey = exports.sqlQuery = exports.sqlInit = exports.sqlEnd = exports.sqlState = exports.moment = exports.readApi = exports.getApp = void 0;
6
+ exports.validType = exports.createToken = exports.validToken = exports.moment = exports.readApi = void 0;
7
+ exports.getApp = getApp;
7
8
  require("module-alias/register");
8
9
  const express_1 = __importDefault(require("express"));
9
- const cors_1 = __importDefault(require("cors"));
10
- const path_1 = __importDefault(require("path"));
11
10
  const state_1 = require("./state");
12
11
  const printLog_1 = __importDefault(require("./utils/printLog"));
13
12
  const quickSend_1 = __importDefault(require("./utils/quickSend"));
@@ -20,18 +19,6 @@ const moment_1 = __importDefault(require("moment"));
20
19
  exports.moment = moment_1.default;
21
20
  require("moment/locale/zh-cn");
22
21
  moment_1.default.locale("zh-cn");
23
- // 数据库
24
- const masmysql_1 = require("masmysql");
25
- Object.defineProperty(exports, "sqlEnd", { enumerable: true, get: function () { return masmysql_1.sqlEnd; } });
26
- Object.defineProperty(exports, "sqlInit", { enumerable: true, get: function () { return masmysql_1.sqlInit; } });
27
- Object.defineProperty(exports, "sqlQuery", { enumerable: true, get: function () { return masmysql_1.sqlQuery; } });
28
- Object.defineProperty(exports, "checkSqlKey", { enumerable: true, get: function () { return masmysql_1.checkSqlKey; } });
29
- Object.defineProperty(exports, "createSqlForm", { enumerable: true, get: function () { return masmysql_1.createSqlForm; } });
30
- Object.defineProperty(exports, "massql", { enumerable: true, get: function () { return masmysql_1.massql; } });
31
- Object.defineProperty(exports, "sqlState", { enumerable: true, get: function () { return masmysql_1.state; } });
32
- Object.defineProperty(exports, "getsqlFormDataType", { enumerable: true, get: function () { return masmysql_1.getsqlFormDataType; } });
33
- Object.defineProperty(exports, "getsqlFormData", { enumerable: true, get: function () { return masmysql_1.getsqlFormData; } });
34
- const apiDoc_1 = __importDefault(require("./utils/apiDoc"));
35
22
  const meaToken_1 = require("./utils/meaToken");
36
23
  Object.defineProperty(exports, "createToken", { enumerable: true, get: function () { return meaToken_1.createToken; } });
37
24
  Object.defineProperty(exports, "validToken", { enumerable: true, get: function () { return meaToken_1.validToken; } });
@@ -42,27 +29,20 @@ const getRouterInfo_1 = __importDefault(require("./utils/getRouterInfo"));
42
29
  const logs_1 = __importDefault(require("./utils/logs"));
43
30
  const apiLimit_1 = __importDefault(require("./utils/apiLimit"));
44
31
  // =================================================================
45
- function getApp(DIRNAME, beforeMounted) {
46
- var _a, _b;
47
- // 路径和配置放入state
48
- state_1.state.DIRNAME = DIRNAME;
49
- const config = require(path_1.default.join(DIRNAME, "/config/config.ts")).default;
32
+ function getApp(proConfig,
33
+ /** 接口生成之前 */
34
+ beforeMounted) {
35
+ var _a;
36
+ const config = require(proConfig.configPath).default;
50
37
  // 获取路由信息
51
- (0, getRouterInfo_1.default)(path_1.default.join(state_1.state.DIRNAME, "src/api"), path_1.default.join(state_1.state.DIRNAME, "config/defaultApiConfig.ts"));
38
+ (0, getRouterInfo_1.default)(proConfig.apisPath, proConfig.defalutApiPath);
52
39
  state_1.state.config = config;
53
- state_1.state.apiCN =
54
- ((_a = require(path_1.default.join(state_1.state.DIRNAME, "config/docNav.ts"))) === null || _a === void 0 ? void 0 : _a.default) || {};
55
40
  // new express
56
41
  const app = (0, express_1.default)();
57
42
  // ip限制
58
- if ((_b = config.apiLimit) === null || _b === void 0 ? void 0 : _b.open) {
43
+ if ((_a = config.apiLimit) === null || _a === void 0 ? void 0 : _a.open) {
59
44
  (0, apiLimit_1.default)(app, config.apiLimit || {});
60
45
  }
61
- // 是否允许跨域
62
- config.cors && app.use((0, cors_1.default)());
63
- if (beforeMounted) {
64
- beforeMounted(app);
65
- }
66
46
  // 初始化app
67
47
  app.use(express_1.default.urlencoded({ extended: true }));
68
48
  app.use(express_1.default.json());
@@ -82,8 +62,7 @@ function getApp(DIRNAME, beforeMounted) {
82
62
  }
83
63
  // 存储日志
84
64
  if (config.logs.open) {
85
- // app.use(printLog);
86
- (0, logs_1.default)(app, path_1.default.join(DIRNAME + `/logs/`));
65
+ (0, logs_1.default)(app, proConfig.logPath);
87
66
  }
88
67
  // 权限管理
89
68
  if (config.token.open) {
@@ -91,17 +70,12 @@ function getApp(DIRNAME, beforeMounted) {
91
70
  }
92
71
  // 数据效验
93
72
  app.use(validRouteData_1.default);
94
- // 使用接口文档
95
- if (config.apidoc) {
96
- app.use("/apidoc", express_1.default.static(path_1.default.join(__dirname, "/apiDoc")));
97
- (0, apiDoc_1.default)(app);
98
- }
99
73
  // 创建路由
100
- (0, createRouter_1.default)(app);
74
+ (0, createRouter_1.default)(app, proConfig.apisPath);
101
75
  // 运行debug
102
76
  setTimeout(() => {
103
77
  try {
104
- require(path_1.default.join(DIRNAME + "/src/debug"));
78
+ require(proConfig.degbugPath);
105
79
  }
106
80
  catch (error) {
107
81
  console.info(ansi_colors_1.default.red("debug.js报错!!!"));
@@ -110,4 +84,3 @@ function getApp(DIRNAME, beforeMounted) {
110
84
  }, config.debugTime || 1000);
111
85
  return app;
112
86
  }
113
- exports.getApp = getApp;
@@ -26,20 +26,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.default = createRouter;
29
30
  const state_1 = require("../state");
30
31
  const path_1 = __importDefault(require("path"));
31
32
  const fs = __importStar(require("fs"));
32
33
  const ansi_colors_1 = __importDefault(require("ansi-colors"));
33
- function createRouter(app) {
34
- const { Router, DIRNAME } = state_1.state;
34
+ function createRouter(app, apiDir) {
35
+ const { Router } = state_1.state;
35
36
  // 遍历api路由
36
37
  for (const item in Router) {
37
- let isExist = fs.existsSync(path_1.default.join(DIRNAME, "src", item) + ".ts");
38
+ const filePath = path_1.default.join(apiDir, "..", item) + ".ts";
39
+ let isExist = fs.existsSync(filePath);
38
40
  if (!isExist) {
39
41
  console.info(ansi_colors_1.default.red(`${item} not found`));
40
42
  continue;
41
43
  }
42
- const apiFunc = require(path_1.default.join(DIRNAME, "src", item)).default;
44
+ const apiFunc = require(filePath).default;
43
45
  if (!apiFunc) {
44
46
  console.info(ansi_colors_1.default.yellow(`${item} 没有导出函数,已跳过..`));
45
47
  }
@@ -48,4 +50,3 @@ function createRouter(app) {
48
50
  }
49
51
  }
50
52
  }
51
- exports.default = createRouter;
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = getRouterInfo;
6
7
  const state_1 = require("../state");
7
8
  const readApi_1 = __importDefault(require("./readApi"));
8
9
  const path_1 = __importDefault(require("path"));
@@ -10,25 +11,19 @@ const fs_1 = __importDefault(require("fs"));
10
11
  const ansi_colors_1 = __importDefault(require("ansi-colors"));
11
12
  function getRouterInfo(apiDir, defaullConfigDir) {
12
13
  const apiConfig = {};
13
- const { DIRNAME } = state_1.state;
14
14
  const routerDir = (0, readApi_1.default)(apiDir, "api");
15
- const defaullConfig = require(defaullConfigDir).default;
15
+ const defaullConfig = require(defaullConfigDir).config;
16
16
  // 遍历api路由
17
17
  for (const item of routerDir) {
18
- let isExist = fs_1.default.existsSync(path_1.default.join(DIRNAME, "src", item) + ".ts");
18
+ const filePath = path_1.default.join(apiDir, "..", item) + ".ts";
19
+ let isExist = fs_1.default.existsSync(filePath);
19
20
  if (!isExist) {
20
21
  console.info(ansi_colors_1.default.red(`${item} not found`));
21
22
  continue;
22
23
  }
23
- const apiItemConfig = require(path_1.default.join(DIRNAME, "src", item)).config;
24
+ const apiItemConfig = require(filePath).config;
24
25
  apiConfig[item] = apiItemConfig || defaullConfig;
25
- // if (!apiFunc) {
26
- // console.info(c.yellow(`${item} 没有导出函数,已跳过..`));
27
- // } else {
28
- // apiFunc && app[Router[item].methods](item, apiFunc);
29
- // }
30
26
  }
31
27
  state_1.state.Router = apiConfig;
32
28
  return apiConfig;
33
29
  }
34
- exports.default = getRouterInfo;
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getType = void 0;
3
+ exports.getType = getType;
4
4
  function getType(obj) {
5
- // "".toLocaleLowerCase
6
5
  return Object.prototype.toString
7
6
  .call(obj)
8
7
  .slice(8)
9
8
  .replace("]", "")
10
9
  .toLocaleLowerCase();
11
10
  }
12
- exports.getType = getType;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createToken = exports.validToken = void 0;
3
+ exports.validToken = void 0;
4
+ exports.createToken = createToken;
4
5
  const { decode, isJSON, encode } = require("./mas-encrypt.min.js");
5
6
  const getTimestamp = () => new Date().getTime();
6
7
  /**
@@ -54,4 +55,3 @@ function createToken({ data = null, time = 0, permission = [] } = {}, key = "808
54
55
  time !== 0 && (token._masTime = time * 1000 + getTimestamp());
55
56
  return encode(token, key);
56
57
  }
57
- exports.createToken = createToken;
@@ -23,6 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.default = readFilesRecursively;
26
27
  const fs = __importStar(require("fs"));
27
28
  const path = __importStar(require("path"));
28
29
  function readFilesRecursively(folderPath, routerPrefix = "") {
@@ -47,4 +48,3 @@ function readFilesRecursively(folderPath, routerPrefix = "") {
47
48
  });
48
49
  return files;
49
50
  }
50
- exports.default = readFilesRecursively;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = validType;
3
4
  const masUtils_1 = require("../utils/masUtils");
4
5
  const vliadBase = (obj, typeMapping) => {
5
6
  return (0, masUtils_1.getType)(obj) == typeMapping.name.toLowerCase();
@@ -81,4 +82,3 @@ function validType(obj, typeMapping, strict = false) {
81
82
  ? validTypeObjStrict(obj, typeMapping)
82
83
  : validTypeObj(obj, typeMapping);
83
84
  }
84
- exports.default = validType;
package/package.json CHANGED
@@ -1,53 +1,53 @@
1
- {
2
- "name": "mas-server",
3
- "version": "2.0.64",
4
- "description": "一款基于express面向中小型项目的后端框架",
5
- "main": "dist/index.js",
6
- "typings": "src/index.ts",
7
- "scripts": {
8
- "test": "npx ts-node ./test/scripts/beforeCreated.ts -end && npx ts-node ./test/scripts/run.ts",
9
- "testmon": "npx nodemon ./test/scripts/run.ts",
10
- "testTS": "npx nodemon ./test/test.ts",
11
- "createSql": "npx ts-node ./test/scripts/createSqlForm.ts",
12
- "createApi": "npx ts-node ./test/scripts/createApiFile.ts",
13
- "createApis": "npx ts-node ./test/scripts/createApis.ts",
14
- "init": "npx nodemon ./test/scripts/beforeCreated.ts -end",
15
- "gsft": "npx nodemon ./test/scripts/getSqlFormType.ts",
16
- "build": "node ./beforebuild.js && tsc --project ./tsconfig-build.json",
17
- "sync": "npm run build && npm publish && cnpm sync mas-server && explorer \"https://npmmirror.com/package/mas-server\""
18
- },
19
- "keywords": [],
20
- "author": "tingxi8087",
21
- "license": "ISC",
22
- "dependencies": {
23
- "@types/express": "^4.17.17",
24
- "ansi-colors": "^4.1.3",
25
- "axios": "^1.3.6",
26
- "cors": "^2.8.5",
27
- "express": "^4.18.2",
28
- "express-ip": "^1.0.4",
29
- "express-rate-limit": "^7.1.4",
30
- "fs-extra": "^11.1.1",
31
- "glob": "^10.3.1",
32
- "masmysql": "^2.0.56",
33
- "module-alias": "^2.2.2",
34
- "moment": "^2.29.4",
35
- "moment-timezone": "^0.5.44",
36
- "morgan": "^1.10.0",
37
- "mysql": "^2.18.1",
38
- "nodemon": "^2.0.22",
39
- "rotating-file-stream": "^3.1.1",
40
- "ts-node": "^10.9.1",
41
- "ts-node-dev": "^2.0.0",
42
- "typescript": "^5.0.4"
43
- },
44
- "_moduleAliases": {
45
- "@": "src/",
46
- "@@": "."
47
- },
48
- "devDependencies": {
49
- "@types/cors": "^2.8.13",
50
- "@types/express": "^4.17.17",
51
- "@types/moment": "^2.13.0"
52
- }
53
- }
1
+ {
2
+ "name": "mas-server",
3
+ "version": "3.0.0",
4
+ "description": "一款基于express面向中小型项目的后端框架",
5
+ "main": "dist/index.js",
6
+ "typings": "src/index.ts",
7
+ "scripts": {
8
+ "test": "npx ts-node ./test/scripts/beforeCreated.ts -end && npx ts-node ./test/scripts/run.ts",
9
+ "testmon": "npx nodemon ./test/scripts/run.ts",
10
+ "testTS": "npx nodemon ./test/test.ts",
11
+ "createSql": "npx ts-node ./test/scripts/createSqlForm.ts",
12
+ "createApi": "npx ts-node ./test/scripts/createApiFile.ts",
13
+ "createApis": "npx ts-node ./test/scripts/createApis.ts",
14
+ "init": "npx nodemon ./test/scripts/beforeCreated.ts -end",
15
+ "gsft": "npx nodemon ./test/scripts/getSqlFormType.ts",
16
+ "build": "node ./beforebuild.js && tsc --project ./tsconfig-build.json",
17
+ "sync": "npm run build && npm publish && cnpm sync mas-server && explorer \"https://npmmirror.com/package/mas-server\""
18
+ },
19
+ "keywords": [],
20
+ "author": "tingxi8087",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "@types/express": "^4.17.21",
24
+ "ansi-colors": "^4.1.3",
25
+ "axios": "^1.3.6",
26
+ "cors": "^2.8.5",
27
+ "express": "^4.18.2",
28
+ "express-ip": "^1.0.4",
29
+ "express-rate-limit": "^7.1.4",
30
+ "fs-extra": "^11.1.1",
31
+ "glob": "^10.3.1",
32
+ "masmysql": "^2.0.56",
33
+ "module-alias": "^2.2.2",
34
+ "moment": "^2.29.4",
35
+ "moment-timezone": "^0.5.44",
36
+ "morgan": "^1.10.0",
37
+ "mysql": "^2.18.1",
38
+ "nodemon": "^2.0.22",
39
+ "rotating-file-stream": "^3.1.1",
40
+ "ts-node": "^10.9.1",
41
+ "ts-node-dev": "^2.0.0",
42
+ "typescript": "^5.0.4"
43
+ },
44
+ "_moduleAliases": {
45
+ "@": "src/",
46
+ "@@": "."
47
+ },
48
+ "devDependencies": {
49
+ "@types/cors": "^2.8.13",
50
+ "@types/express": "^4.17.17",
51
+ "@types/moment": "^2.13.0"
52
+ }
53
+ }
package/src/index.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  import "module-alias/register";
2
2
  import express from "express";
3
3
  import type { Express } from "express";
4
- import cors from "cors";
5
- import path from "path";
6
- import * as fs from "fs";
7
4
  import { state } from "./state";
8
5
  import printLog from "./utils/printLog";
9
6
  import quickSend from "./utils/quickSend";
@@ -11,30 +8,16 @@ import readApi from "./utils/readApi";
11
8
  import validRouteData from "./utils/validRouteData";
12
9
  import validTokenUse from "./utils/validToken";
13
10
  import type {
14
- masReq,
15
- masRes,
16
- apiConfigType,
17
- configType,
18
- apiConfigTypeItem,
11
+ MasReq,
12
+ MasRes,
13
+ ApiConfigType,
14
+ ConfigType,
15
+ ApiConfigTypeItem,
19
16
  } from "./typings";
20
17
  import c from "ansi-colors";
21
18
  import moment from "moment";
22
19
  import "moment/locale/zh-cn";
23
20
  moment.locale("zh-cn");
24
- // 数据库
25
- import {
26
- sqlEnd,
27
- sqlInit,
28
- sqlQuery,
29
- checkSqlKey,
30
- createSqlForm,
31
- massql,
32
- state as sqlState,
33
- getsqlFormDataType,
34
- getsqlFormData,
35
- } from "masmysql";
36
- import type { createSqlFormType, sqlForm } from "masmysql";
37
- import apiDoc from "./utils/apiDoc";
38
21
  import { createToken, validToken } from "./utils/meaToken";
39
22
  import validType from "./utils/validType";
40
23
  import createRouter from "./utils/createRouter";
@@ -44,31 +27,31 @@ import apiLimit from "./utils/apiLimit";
44
27
 
45
28
  // =================================================================
46
29
  export function getApp(
47
- DIRNAME: string,
30
+ proConfig: {
31
+ /** 配置路径 */
32
+ configPath: string;
33
+ /** 接口路径 */
34
+ apisPath: string;
35
+ /** 兜底接口文件路径 */
36
+ defalutApiPath: string;
37
+ /** 日志路径 */
38
+ logPath: string;
39
+ /** 调试文件路径 */
40
+ degbugPath: string;
41
+ },
42
+ /** 接口生成之前 */
48
43
  beforeMounted?: (app: Express) => void
49
44
  ) {
50
- // 路径和配置放入state
51
- state.DIRNAME = DIRNAME;
52
- const config = require(path.join(DIRNAME, "/config/config.ts")).default;
45
+ const config = require(proConfig.configPath).default;
53
46
  // 获取路由信息
54
- getRouterInfo(
55
- path.join(state.DIRNAME, "src/api"),
56
- path.join(state.DIRNAME, "config/defaultApiConfig.ts")
57
- );
47
+ getRouterInfo(proConfig.apisPath, proConfig.defalutApiPath);
58
48
  state.config = config;
59
- state.apiCN =
60
- require(path.join(state.DIRNAME, "config/docNav.ts"))?.default || {};
61
49
  // new express
62
50
  const app = express();
63
51
  // ip限制
64
52
  if (config.apiLimit?.open) {
65
53
  apiLimit(app, config.apiLimit || {});
66
54
  }
67
- // 是否允许跨域
68
- config.cors && app.use(cors());
69
- if (beforeMounted) {
70
- beforeMounted(app);
71
- }
72
55
  // 初始化app
73
56
  app.use(express.urlencoded({ extended: true }));
74
57
  app.use(express.json());
@@ -88,27 +71,20 @@ export function getApp(
88
71
  }
89
72
  // 存储日志
90
73
  if (config.logs.open) {
91
- // app.use(printLog);
92
- getLogs(app, path.join(DIRNAME + `/logs/`));
74
+ getLogs(app, proConfig.logPath);
93
75
  }
94
-
95
76
  // 权限管理
96
77
  if (config.token.open) {
97
78
  app.use(validTokenUse);
98
79
  }
99
80
  // 数据效验
100
81
  app.use(validRouteData);
101
- // 使用接口文档
102
- if (config.apidoc) {
103
- app.use("/apidoc", express.static(path.join(__dirname, "/apiDoc")));
104
- apiDoc(app);
105
- }
106
82
  // 创建路由
107
- createRouter(app);
83
+ createRouter(app, proConfig.apisPath);
108
84
  // 运行debug
109
85
  setTimeout(() => {
110
86
  try {
111
- require(path.join(DIRNAME + "/src/debug"));
87
+ require(proConfig.degbugPath);
112
88
  } catch (error) {
113
89
  console.info(c.red("debug.js报错!!!"));
114
90
  console.info(error);
@@ -116,28 +92,5 @@ export function getApp(
116
92
  }, config.debugTime || 1000);
117
93
  return app;
118
94
  }
119
- export {
120
- readApi,
121
- moment,
122
- sqlState,
123
- sqlEnd,
124
- sqlInit,
125
- sqlQuery,
126
- checkSqlKey,
127
- createSqlForm,
128
- getsqlFormData,
129
- getsqlFormDataType,
130
- massql,
131
- validToken,
132
- createToken,
133
- validType,
134
- };
135
- export type {
136
- masReq,
137
- masRes,
138
- apiConfigType,
139
- configType,
140
- createSqlFormType,
141
- sqlForm,
142
- apiConfigTypeItem,
143
- };
95
+ export { readApi, moment, validToken, createToken, validType };
96
+ export type { MasReq, MasRes, ApiConfigType, ConfigType, ApiConfigTypeItem };