mas-server 1.0.3 → 2.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 (153) hide show
  1. package/.temp/a.ts +4 -0
  2. package/.temp/as.ts +1 -0
  3. package/.temp/b.ts +3 -0
  4. package/{src/index.js → .temp/i.js} +3 -3
  5. package/.temp/test.ts +2 -0
  6. package/.temp/validType copy.ts +149 -0
  7. package/.temp/validType.ts +96 -0
  8. package/.temp/validTypeOptimize.ts +98 -0
  9. package/.vscode/settings.json +0 -109
  10. package/dist/index.js +136 -0
  11. package/dist/masmysql/checkSqlkey.js +67 -0
  12. package/dist/masmysql/createSqlForm.js +44 -0
  13. package/dist/masmysql/esaysql.js +53 -0
  14. package/dist/masmysql/index.js +111 -0
  15. package/dist/masmysql/masSql.js +741 -0
  16. package/dist/masmysql/state.js +5 -0
  17. package/dist/masmysql/type.js +2 -0
  18. package/dist/masmysql/utils.js +12 -0
  19. package/dist/state.js +4 -0
  20. package/dist/typings/index.js +2 -0
  21. package/dist/utils/apiDoc.js +18 -0
  22. package/dist/utils/getLog.js +18 -0
  23. package/dist/utils/getRouteByApiSrc.js +54 -0
  24. package/dist/utils/masUtils.js +12 -0
  25. package/dist/utils/meaToken.js +57 -0
  26. package/dist/utils/quickSend.js +32 -0
  27. package/dist/utils/readApi.js +50 -0
  28. package/dist/utils/validRouteData.js +37 -0
  29. package/dist/utils/validToken.js +24 -0
  30. package/dist/utils/validType.js +101 -0
  31. package/package.json +34 -15
  32. package/src/apiDoc/assets/empty-907982e4.js +1 -0
  33. package/src/apiDoc/assets/index-b56e6bc6.js +1414 -0
  34. package/src/apiDoc/assets/index-bb16d834.css +6 -0
  35. package/src/{public/doc/img/jsoneditor-icons.256e3abc.svg → apiDoc/assets/jsoneditor-icons-45c98bf0.svg} +213 -357
  36. package/src/apiDoc/favicon.ico +0 -0
  37. package/src/apiDoc/index.html +15 -0
  38. package/src/index.ts +132 -0
  39. package/src/masmysql/checkSqlkey.ts +55 -0
  40. package/src/masmysql/createSqlForm.ts +37 -0
  41. package/src/masmysql/esaysql.ts +44 -0
  42. package/src/masmysql/index.ts +68 -0
  43. package/src/masmysql/masSql.ts +736 -0
  44. package/src/masmysql/state.ts +5 -0
  45. package/src/masmysql/type.ts +34 -0
  46. package/src/masmysql/utils.ts +8 -0
  47. package/src/state.ts +1 -0
  48. package/src/typings/index.ts +114 -0
  49. package/src/utils/apiDoc.ts +18 -0
  50. package/src/utils/getLog.ts +20 -0
  51. package/src/utils/getRouteByApiSrc.ts +55 -0
  52. package/src/utils/mas-encrypt.min.js +1 -0
  53. package/src/utils/masUtils.ts +8 -0
  54. package/src/utils/meaToken.ts +56 -0
  55. package/src/utils/quickSend.ts +19 -0
  56. package/src/utils/readApi.ts +30 -0
  57. package/src/utils/validRouteData.ts +46 -0
  58. package/src/utils/validToken.ts +22 -0
  59. package/src/utils/validType.ts +98 -0
  60. package/test/apiTemplate/api.temp.ts +6 -0
  61. package/test/apiTemplate/curd/del.ts +9 -0
  62. package/test/apiTemplate/curd/get.ts +9 -0
  63. package/test/apiTemplate/curd/set.ts +9 -0
  64. package/test/config/api.config.ts +3 -0
  65. package/test/config/api.key.ts +14 -0
  66. package/test/config/config.ts +30 -0
  67. package/test/config/createSqlFormData.ts +14 -0
  68. package/test/logs/.keep +0 -0
  69. package/test/main.ts +25 -0
  70. package/test/scripts/beforeCreated.ts +67 -0
  71. package/test/scripts/createApiFile.ts +27 -0
  72. package/test/scripts/createApis.ts +109 -0
  73. package/test/scripts/createSqlForm.ts +5 -0
  74. package/test/scripts/getSqlFormType.ts +34 -0
  75. package/test/scripts/run.ts +4 -0
  76. package/test/scripts/updateRouter.ts +46 -0
  77. package/test/src/api/curd/student/del.ts +9 -0
  78. package/test/src/api/curd/student/get.ts +9 -0
  79. package/test/src/api/curd/student/set.ts +9 -0
  80. package/test/src/api/index.ts +4 -0
  81. package/test/src/api/per.ts +6 -0
  82. package/test/src/api/person/del.ts +4 -0
  83. package/test/src/api/person/get.ts +4 -0
  84. package/test/src/api/person/set.ts +4 -0
  85. package/test/src/api/test.ts +7 -0
  86. package/test/src/api/validtest/boolArr.ts +4 -0
  87. package/test/src/debug.ts +56 -0
  88. package/test/src/masState.ts +4 -0
  89. package/test/src/sql/selectStudent.ts +7 -0
  90. package/test/src/sqlform.json +1 -0
  91. package/test/test.ts +13 -0
  92. package/test/typings/.keep +0 -0
  93. package/test/typings/studentSF.ts +6 -0
  94. package/test/typings/test11SF.ts +1 -0
  95. package/test/typings/test22SF.ts +1 -0
  96. package/test/typings/testformSF.ts +1 -0
  97. package/test//345/276/205/345/274/200/345/217/221.md +12 -0
  98. package/tsconfig.json +23 -0
  99. package/tsconfigschema.json +1263 -0
  100. package/src/config/api.res.json +0 -1
  101. package/src/config/api.tree.json +0 -1
  102. package/src/config/apiModules/del.module +0 -6
  103. package/src/config/apiModules/get.module +0 -6
  104. package/src/config/apiModules/set.module +0 -6
  105. package/src/config/config.schema.js +0 -41
  106. package/src/public/doc/css/app.722f60c4.css +0 -1
  107. package/src/public/doc/css/chunk-287b9965.80548a4b.css +0 -1
  108. package/src/public/doc/css/chunk-vendors.a16c4353.css +0 -1
  109. package/src/public/doc/favicon.ico +0 -0
  110. package/src/public/doc/fonts/element-icons.535877f5.woff +0 -0
  111. package/src/public/doc/fonts/element-icons.732389de.ttf +0 -0
  112. package/src/public/doc/index.html +0 -1
  113. package/src/public/doc/js/app.9ce10690.js +0 -2
  114. package/src/public/doc/js/app.9ce10690.js.map +0 -1
  115. package/src/public/doc/js/chunk-287b9965.77cfd27f.js +0 -32
  116. package/src/public/doc/js/chunk-287b9965.77cfd27f.js.map +0 -1
  117. package/src/public/doc/js/chunk-vendors.7c84e9a7.js +0 -35
  118. package/src/public/doc/js/chunk-vendors.7c84e9a7.js.map +0 -1
  119. package/src/public/index.html +0 -15
  120. package/src/state.js +0 -6
  121. package/src/utils/DataVerified.js +0 -35
  122. package/src/utils/beforeCreate.js +0 -19
  123. package/src/utils/createApi.js +0 -34
  124. package/src/utils/createApiFile.js +0 -40
  125. package/src/utils/createSqlForm.js +0 -7
  126. package/src/utils/encrypt.js +0 -1
  127. package/src/utils/getDir.js +0 -60
  128. package/src/utils/logs.js +0 -16
  129. package/src/utils/masDoc.js +0 -37
  130. package/src/utils/masRoute.js +0 -29
  131. package/src/utils/quickSend.js +0 -18
  132. package/src/utils/token.js +0 -57
  133. package/test/.utils/api.js +0 -4
  134. package/test/.utils/apis.js +0 -5
  135. package/test/.utils/beforeIndex.js +0 -4
  136. package/test/.utils/createForm.js +0 -4
  137. package/test/config/api.config.json +0 -7
  138. package/test/config/api.config.k.json +0 -1
  139. package/test/config/config.js +0 -22
  140. package/test/config/sqlform.config.js +0 -23
  141. package/test/index.js +0 -12
  142. package/test/public/1.txt +0 -1
  143. package/test/serverLogs/2022-10-29.log +0 -6
  144. package/test/serverLogs/2022-10-30.log +0 -58
  145. package/test/src/$form.js +0 -1
  146. package/test/src/api/index.js +0 -3
  147. package/test/src/api/test.js +0 -9
  148. package/test/src/api/vip/del.js +0 -6
  149. package/test/src/api/vip/get.js +0 -6
  150. package/test/src/api/vip/set.js +0 -6
  151. package/test/src/api/vip666.js +0 -4
  152. package/test/src/debug.js +0 -2
  153. /package/{old → .temp/logs/.keep} +0 -0
package/.temp/a.ts ADDED
@@ -0,0 +1,4 @@
1
+ import * as fs from "fs";
2
+ import path from "path";
3
+ fs.writeFileSync(path.join(__dirname, "as.ts"), `export default{name:"test"}`);
4
+ export {};
package/.temp/as.ts ADDED
@@ -0,0 +1 @@
1
+ export default{name:"test"}
package/.temp/b.ts ADDED
@@ -0,0 +1,3 @@
1
+ import test from "./as";
2
+ console.log(test);
3
+ export {};
@@ -24,7 +24,7 @@ const createCode = (dirname) => {
24
24
  const createApis = (dirname) => {
25
25
  createApiFile(dirname);
26
26
  };
27
- let token=null
27
+ let token = null;
28
28
  const getApp = (dirname) => {
29
29
  const app = express();
30
30
  const config = require(path.join(dirname, "/config", "config.js"));
@@ -86,8 +86,8 @@ const getApp = (dirname) => {
86
86
  * @param {number} time token过期时间,设置此项传入数据必须为json数据,单位为s,默认为0,不过期
87
87
  * @return {*}
88
88
  */
89
- function createToken(data,permission,time) {
90
- return token.createToken(data,time,permission,state.config)
89
+ function createToken(data, permission, time) {
90
+ return token.createToken(data, time, permission, state.config);
91
91
  }
92
92
  module.exports = {
93
93
  getApp,
package/.temp/test.ts ADDED
@@ -0,0 +1,2 @@
1
+ import "./a";
2
+ import "./b";
@@ -0,0 +1,149 @@
1
+ type BasicTypeDescriptor =
2
+ | StringConstructor
3
+ | NumberConstructor
4
+ | BooleanConstructor
5
+ | ObjectConstructor;
6
+ type TypeDescriptor = BasicTypeDescriptor | Array<any>;
7
+
8
+ interface TypeMapping {
9
+ [key: string]:
10
+ | TypeDescriptor
11
+ | TypeMapping
12
+ | Array<TypeDescriptor | TypeMapping>;
13
+ }
14
+ import { getType } from "../src/utils/masUtils";
15
+ function getTypeName(descriptor: TypeDescriptor): string {
16
+ if (Array.isArray(descriptor)) {
17
+ return "array";
18
+ } else {
19
+ return (descriptor as BasicTypeDescriptor).name.toLowerCase();
20
+ }
21
+ }
22
+
23
+ function validTypeObj(obj: any, typeMapping: TypeMapping): boolean {
24
+ for (const key in typeMapping) {
25
+ if (!obj.hasOwnProperty(key)) {
26
+ return false;
27
+ }
28
+
29
+ const targetType = typeMapping[key];
30
+
31
+ const objValue = obj[key];
32
+ if (Array.isArray(targetType)) {
33
+ if (!Array.isArray(objValue)) {
34
+ return false;
35
+ }
36
+
37
+ const arrayElementType = targetType[0];
38
+
39
+ for (const element of objValue) {
40
+ if (typeof arrayElementType === "object") {
41
+ if (!validType(element, arrayElementType as TypeMapping)) {
42
+ return false;
43
+ }
44
+ } else {
45
+ if (
46
+ typeof element !== getTypeName(arrayElementType as TypeDescriptor)
47
+ ) {
48
+ return false;
49
+ }
50
+ }
51
+ }
52
+ } else if (typeof targetType === "object") {
53
+ if (
54
+ typeof objValue !== "object" ||
55
+ Array.isArray(objValue) ||
56
+ !validType(objValue, targetType as TypeMapping)
57
+ ) {
58
+ return false;
59
+ }
60
+ } else {
61
+ if (typeof objValue !== getTypeName(targetType as TypeDescriptor)) {
62
+ return false;
63
+ }
64
+ }
65
+ }
66
+
67
+ return true;
68
+ }
69
+ function validTypeObjStrict(obj: any, typeMapping: TypeMapping): boolean {
70
+ if (getType(typeMapping) == "object") {
71
+ if (getType(obj) != "object") return false;
72
+ if (Object.keys(obj).length != Object.keys(typeMapping).length) {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ for (const key in typeMapping) {
78
+ if (!obj?.hasOwnProperty(key)) {
79
+ return false;
80
+ }
81
+
82
+ const targetType = typeMapping[key];
83
+ const objValue = obj[key];
84
+
85
+ if (Array.isArray(targetType)) {
86
+ if (!Array.isArray(objValue)) {
87
+ return false;
88
+ }
89
+
90
+ const arrayElementType = targetType[0];
91
+
92
+ for (const element of objValue) {
93
+ if (typeof arrayElementType === "object") {
94
+ if (
95
+ !validType(element, arrayElementType as TypeMapping) ||
96
+ Object.keys(element).length !==
97
+ Object.keys(arrayElementType as TypeMapping).length
98
+ ) {
99
+ return false;
100
+ }
101
+ } else {
102
+ if (
103
+ typeof element !== getTypeName(arrayElementType as TypeDescriptor)
104
+ ) {
105
+ return false;
106
+ }
107
+ }
108
+ }
109
+ } else if (typeof targetType === "object") {
110
+ if (
111
+ typeof objValue !== "object" ||
112
+ Array.isArray(objValue) ||
113
+ !validType(objValue, targetType as TypeMapping) ||
114
+ Object.keys(objValue).length !==
115
+ Object.keys(targetType as TypeMapping).length
116
+ ) {
117
+ return false;
118
+ }
119
+ } else {
120
+ if (typeof objValue !== getTypeName(targetType as TypeDescriptor)) {
121
+ return false;
122
+ }
123
+ }
124
+ }
125
+
126
+ return true;
127
+ }
128
+
129
+ export default function validType(
130
+ obj: any,
131
+ typeMapping: TypeMapping | TypeDescriptor,
132
+ strict: boolean = false
133
+ ): boolean {
134
+ if (getType(typeMapping) == "function") {
135
+ const typeT = typeMapping.toString().split(" ")[1].trimEnd();
136
+ if (typeT == "Boolean()") {
137
+ return getType(obj) == "boolean";
138
+ } else if (typeT == "Number()") {
139
+ return getType(obj) == "number";
140
+ } else if (typeT == "String()") {
141
+ return getType(obj) == "string";
142
+ }
143
+ } else {
144
+ return strict
145
+ ? validTypeObjStrict(obj, typeMapping as any)
146
+ : validTypeObj(obj, typeMapping as any);
147
+ }
148
+ return false;
149
+ }
@@ -0,0 +1,96 @@
1
+ type BasicTypeDescriptor =
2
+ | StringConstructor
3
+ | NumberConstructor
4
+ | BooleanConstructor
5
+ | ObjectConstructor;
6
+ type TypeDescriptor = BasicTypeDescriptor | Array<any>;
7
+
8
+ interface TypeMapping {
9
+ [key: string]:
10
+ | TypeDescriptor
11
+ | TypeMapping
12
+ | Array<TypeDescriptor | TypeMapping>;
13
+ }
14
+
15
+ function getTypeName(descriptor: TypeDescriptor): string {
16
+ if (Array.isArray(descriptor)) {
17
+ return "array";
18
+ } else {
19
+ return (descriptor as BasicTypeDescriptor).name.toLowerCase();
20
+ }
21
+ }
22
+
23
+ function validType(obj: any, typeMapping: TypeMapping): boolean {
24
+ for (const key in typeMapping) {
25
+ if (!obj.hasOwnProperty(key)) {
26
+ return false;
27
+ }
28
+
29
+ const targetType = typeMapping[key];
30
+ const objValue = obj[key];
31
+
32
+ if (Array.isArray(targetType)) {
33
+ if (!Array.isArray(objValue)) {
34
+ return false;
35
+ }
36
+
37
+ const arrayElementType = targetType[0];
38
+
39
+ for (const element of objValue) {
40
+ if (typeof arrayElementType === "object") {
41
+ if (
42
+ !validType(element, arrayElementType as TypeMapping) ||
43
+ Object.keys(element).length !==
44
+ Object.keys(arrayElementType as TypeMapping).length
45
+ ) {
46
+ return false;
47
+ }
48
+ } else {
49
+ if (
50
+ typeof element !== getTypeName(arrayElementType as TypeDescriptor)
51
+ ) {
52
+ return false;
53
+ }
54
+ }
55
+ }
56
+ } else if (typeof targetType === "object") {
57
+ if (
58
+ typeof objValue !== "object" ||
59
+ Array.isArray(objValue) ||
60
+ !validType(objValue, targetType as TypeMapping) ||
61
+ Object.keys(objValue).length !==
62
+ Object.keys(targetType as TypeMapping).length
63
+ ) {
64
+ return false;
65
+ }
66
+ } else {
67
+ if (typeof objValue !== getTypeName(targetType as TypeDescriptor)) {
68
+ return false;
69
+ }
70
+ }
71
+ }
72
+
73
+ return true;
74
+ }
75
+
76
+ // 示例
77
+ console.log(validType({ a: 1, b: "s" }, { a: Number, b: String })); // 返回true
78
+ console.log(validType({ a: [1, 2, 3], b: "s" }, { a: [Number], b: String })); // 返回true
79
+ console.log(
80
+ validType(
81
+ { a: [{ a: 1 }, { a: 1 }, { a: 1 }], b: "s" },
82
+ { a: [{ b: Number }], b: String }
83
+ )
84
+ ); // 返回true
85
+ console.log(
86
+ validType(
87
+ {
88
+ a: [
89
+ { a: "1", b: 22 },
90
+ { a: "1", b: 1 },
91
+ ],
92
+ },
93
+ { a: [{ a: String, b: Number, c: Number }] }
94
+ )
95
+ ); // 返回false
96
+ export {};
@@ -0,0 +1,98 @@
1
+ type BasicTypeDescriptor =
2
+ | typeof String
3
+ | typeof Number
4
+ | typeof Boolean
5
+ | typeof Object;
6
+ type TypeDescriptor = BasicTypeDescriptor | Array<TypeDescriptor>;
7
+
8
+ interface TypeMapping {
9
+ [key: string]: TypeDescriptor | TypeMapping;
10
+ }
11
+
12
+ function getTypeName(descriptor: TypeDescriptor): string {
13
+ if (Array.isArray(descriptor)) {
14
+ return "array";
15
+ } else {
16
+ return descriptor.name.toLowerCase();
17
+ }
18
+ }
19
+
20
+ function validType(obj: any, typeMapping: TypeMapping): boolean {
21
+ for (const key in typeMapping) {
22
+ if (!(key in obj)) {
23
+ return false;
24
+ }
25
+
26
+ const targetType = typeMapping[key];
27
+ const objValue = obj[key];
28
+
29
+ if (Array.isArray(targetType)) {
30
+ if (!Array.isArray(objValue)) {
31
+ return false;
32
+ }
33
+
34
+ const arrayElementType = targetType[0];
35
+
36
+ for (const element of objValue) {
37
+ if (typeof arrayElementType === "object") {
38
+ if (
39
+ !validType(element, arrayElementType as unknown as TypeMapping) ||
40
+ Object.keys(element).length !==
41
+ Object.keys(arrayElementType as unknown as TypeMapping).length
42
+ ) {
43
+ return false;
44
+ }
45
+ } else {
46
+ if (
47
+ typeof element !== getTypeName(arrayElementType as TypeDescriptor)
48
+ ) {
49
+ return false;
50
+ }
51
+ }
52
+ }
53
+ } else if (typeof targetType === "object") {
54
+ if (
55
+ typeof objValue !== "object" ||
56
+ Array.isArray(objValue) ||
57
+ !validType(objValue, targetType as TypeMapping) ||
58
+ Object.keys(objValue).length !==
59
+ Object.keys(targetType as TypeMapping).length
60
+ ) {
61
+ return false;
62
+ }
63
+ } else {
64
+ if (typeof objValue !== getTypeName(targetType as TypeDescriptor)) {
65
+ return false;
66
+ }
67
+ }
68
+ }
69
+
70
+ return true;
71
+ }
72
+ // 检查基本类型
73
+ console.log(
74
+ validType({ name: "Alice", age: 30 }, { name: String, age: Number })
75
+ ); // 输出 true
76
+ console.log(
77
+ validType({ name: "Alice", age: 30 }, { name: String, age: String })
78
+ ); // 输出 false
79
+
80
+ // 检查数组类型
81
+ console.log(validType({ values: [1, 2, 3] }, { values: [Number] })); // 输出 true
82
+ console.log(validType({ values: [1, "2", 3] }, { values: [Number] })); // 输出 false
83
+
84
+ // 检查嵌套类型
85
+ console.log(
86
+ validType(
87
+ { person: { name: "Alice", age: 30 } },
88
+ { person: { name: String, age: Number } }
89
+ )
90
+ ); // 输出 true
91
+ console.log(
92
+ validType(
93
+ { person: { name: "Alice", age: 30 } },
94
+ { person: { name: String, age: String } }
95
+ )
96
+ ); // 输出
97
+
98
+ export {};
@@ -1,112 +1,3 @@
1
1
  {
2
- "json.schemas": [
3
- // 接口配置规范
4
- {
5
- "fileMatch": ["api.config.json"],
6
- "schema": {
7
- "type": "object",
8
- "patternProperties": {
9
- "^[a-zA-Z1-9/]+$": {
10
- "type": "object",
11
- "properties": {
12
- "name": {
13
- "type": "string",
14
- "description": "接口名称"
15
- },
16
- "des": {
17
- "type": "string",
18
- "description": "接口描述"
19
- },
20
- "methods": {
21
- "type": "string",
22
- "enum": ["GET", "POST", "ALL"],
23
- "description": "请求方法"
24
- },
25
- "header": {
26
- "type": "array",
27
- "description": "请求头参数",
28
- "items": {
29
- "type": "object",
30
- "properties": {
31
- "name": {
32
- "type": "string",
33
- "description": "参数名称"
34
- },
35
- "type": {
36
- "type": "string",
37
- "description": "参数类型",
38
- "enum": ["string", "number", "Array", "object"]
39
- },
40
- "optional": {
41
- "type": "boolean",
42
- "description": "参数是否可空,默认不能空"
43
- },
44
- "des": {
45
- "type": "string",
46
- "description": "参数描述"
47
- }
48
- }
49
- }
50
- },
51
- "token": {
52
- "type": "boolean",
53
- "description": "是否启用token,默认关闭"
54
- },
55
- "permission": {
56
- "type": "array",
57
- "description": "该接口需要的权限",
58
- "items": {
59
- "type": "string"
60
- }
61
- },
62
- "Content-Type": {
63
- "type": "string",
64
- "description": "请求体类型"
65
- },
66
- "example": {
67
- "type": "object",
68
- "description": "请求示例"
69
- },
70
- "checkData": {
71
- "type": "array",
72
- "description": "数据效验,可直接写字段名,也可描述字段名",
73
- "items": {
74
- "anyOf": [
75
- {
76
- "type": "string"
77
- },
78
- {
79
- "type": "object",
80
- "required": ["name"],
81
- "properties": {
82
- "name": {
83
- "type": "string",
84
- "description": "参数名称"
85
- },
86
- "type": {
87
- "type": "string",
88
- "description": "参数类型",
89
- "enum": ["string", "number", "Array", "object"]
90
- },
91
- "optional": {
92
- "type": "boolean",
93
- "description": "参数是否可空,默认不能空"
94
- },
95
- "des": {
96
- "type": "string",
97
- "description": "参数描述"
98
- }
99
- }
100
- }
101
- ]
102
- }
103
- }
104
- }
105
- }
106
- },
107
- "additionalProperties": false
108
- }
109
- }
110
- ],
111
2
  "javascript.format.semicolons": "insert"
112
3
  }
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ 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.getRouteByApiSrc = exports.readApi = exports.getApp = void 0;
30
+ require("module-alias/register");
31
+ const express_1 = __importDefault(require("express"));
32
+ const cors_1 = __importDefault(require("cors"));
33
+ const path_1 = __importDefault(require("path"));
34
+ const fs = __importStar(require("fs"));
35
+ const state_1 = require("@/state");
36
+ const getLog_1 = __importDefault(require("@/utils/getLog"));
37
+ const quickSend_1 = __importDefault(require("@/utils/quickSend"));
38
+ const readApi_1 = __importDefault(require("@/utils/readApi"));
39
+ exports.readApi = readApi_1.default;
40
+ const validRouteData_1 = __importDefault(require("@/utils/validRouteData"));
41
+ const getRouteByApiSrc_1 = __importDefault(require("@/utils/getRouteByApiSrc"));
42
+ exports.getRouteByApiSrc = getRouteByApiSrc_1.default;
43
+ const validToken_1 = __importDefault(require("@/utils/validToken"));
44
+ const ansi_colors_1 = __importDefault(require("ansi-colors"));
45
+ const moment_1 = __importDefault(require("moment"));
46
+ exports.moment = moment_1.default;
47
+ require("moment/locale/zh-cn");
48
+ moment_1.default.locale("zh-cn");
49
+ // 数据库
50
+ const masmysql_1 = require("masmysql");
51
+ Object.defineProperty(exports, "sqlEnd", { enumerable: true, get: function () { return masmysql_1.sqlEnd; } });
52
+ Object.defineProperty(exports, "sqlInit", { enumerable: true, get: function () { return masmysql_1.sqlInit; } });
53
+ Object.defineProperty(exports, "sqlQuery", { enumerable: true, get: function () { return masmysql_1.sqlQuery; } });
54
+ Object.defineProperty(exports, "checkSqlKey", { enumerable: true, get: function () { return masmysql_1.checkSqlKey; } });
55
+ Object.defineProperty(exports, "createSqlForm", { enumerable: true, get: function () { return masmysql_1.createSqlForm; } });
56
+ Object.defineProperty(exports, "massql", { enumerable: true, get: function () { return masmysql_1.massql; } });
57
+ Object.defineProperty(exports, "sqlState", { enumerable: true, get: function () { return masmysql_1.state; } });
58
+ Object.defineProperty(exports, "getsqlFormDataType", { enumerable: true, get: function () { return masmysql_1.getsqlFormDataType; } });
59
+ Object.defineProperty(exports, "getsqlFormData", { enumerable: true, get: function () { return masmysql_1.getsqlFormData; } });
60
+ const apiDoc_1 = __importDefault(require("./utils/apiDoc"));
61
+ const meaToken_1 = require("./utils/meaToken");
62
+ Object.defineProperty(exports, "createToken", { enumerable: true, get: function () { return meaToken_1.createToken; } });
63
+ Object.defineProperty(exports, "validToken", { enumerable: true, get: function () { return meaToken_1.validToken; } });
64
+ // =================================================================
65
+ function getApp(DIRNAME, beforeMounted) {
66
+ // 路径和配置放入state
67
+ state_1.state.DIRNAME = DIRNAME;
68
+ const config = require(path_1.default.join(DIRNAME, "/config/config.ts")).default;
69
+ const Router = require(path_1.default.join(DIRNAME, "/config/api.config.ts")).default;
70
+ const apiCN = require(path_1.default.join(DIRNAME, "/config/api.key.ts")).default;
71
+ state_1.state.config = config;
72
+ state_1.state.Router = Router;
73
+ state_1.state.apiCN = apiCN;
74
+ // new express
75
+ const app = (0, express_1.default)();
76
+ // 是否允许跨域
77
+ config.cors && app.use((0, cors_1.default)());
78
+ if (beforeMounted) {
79
+ beforeMounted(app);
80
+ }
81
+ // 初始化app
82
+ app.use(express_1.default.urlencoded({ extended: true }));
83
+ app.use(express_1.default.json());
84
+ // 自定义中间件来处理纯文本和JSON数据
85
+ // 错误处理中间件
86
+ app.use((err, req, res, next) => {
87
+ if (err.type == "entity.parse.failed") {
88
+ req.body._str = err.body;
89
+ }
90
+ next();
91
+ });
92
+ //快速return
93
+ app.use(quickSend_1.default);
94
+ // 日志
95
+ if (config.logs.open) {
96
+ app.use(require("express-ip")().getIpInfoMiddleware);
97
+ app.use(getLog_1.default);
98
+ }
99
+ // 权限管理
100
+ if (config.token.open) {
101
+ app.use(validToken_1.default);
102
+ }
103
+ // 数据效验
104
+ app.use(validRouteData_1.default);
105
+ if (config.apidoc) {
106
+ app.use("/apidoc", express_1.default.static(path_1.default.join(__dirname, "/apiDoc")));
107
+ (0, apiDoc_1.default)(app);
108
+ }
109
+ // 遍历api路由
110
+ for (const item in Router) {
111
+ let isExist = fs.existsSync(path_1.default.join(DIRNAME, "src", item) + ".ts");
112
+ if (!isExist) {
113
+ console.info(ansi_colors_1.default.red(`${item} not found`));
114
+ continue;
115
+ }
116
+ const apiFunc = require(path_1.default.join(DIRNAME, "src", item)).default;
117
+ if (!apiFunc) {
118
+ console.info(ansi_colors_1.default.yellow(`${item} 没有导出函数,已跳过..`));
119
+ }
120
+ else {
121
+ apiFunc && app[Router[item].methods](item, apiFunc);
122
+ }
123
+ }
124
+ // 运行debug
125
+ setTimeout(() => {
126
+ try {
127
+ require(path_1.default.join(DIRNAME + "/src/debug"));
128
+ }
129
+ catch (error) {
130
+ console.info(ansi_colors_1.default.red("debug.js报错!!!"));
131
+ console.info(error);
132
+ }
133
+ }, config.debugTime || 1000);
134
+ return app;
135
+ }
136
+ exports.getApp = getApp;