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.
- package/.temp/a.ts +4 -0
- package/.temp/as.ts +1 -0
- package/.temp/b.ts +3 -0
- package/{src/index.js → .temp/i.js} +3 -3
- package/.temp/test.ts +2 -0
- package/.temp/validType copy.ts +149 -0
- package/.temp/validType.ts +96 -0
- package/.temp/validTypeOptimize.ts +98 -0
- package/.vscode/settings.json +0 -109
- package/dist/index.js +136 -0
- package/dist/masmysql/checkSqlkey.js +67 -0
- package/dist/masmysql/createSqlForm.js +44 -0
- package/dist/masmysql/esaysql.js +53 -0
- package/dist/masmysql/index.js +111 -0
- package/dist/masmysql/masSql.js +741 -0
- package/dist/masmysql/state.js +5 -0
- package/dist/masmysql/type.js +2 -0
- package/dist/masmysql/utils.js +12 -0
- package/dist/state.js +4 -0
- package/dist/typings/index.js +2 -0
- package/dist/utils/apiDoc.js +18 -0
- package/dist/utils/getLog.js +18 -0
- package/dist/utils/getRouteByApiSrc.js +54 -0
- package/dist/utils/masUtils.js +12 -0
- package/dist/utils/meaToken.js +57 -0
- package/dist/utils/quickSend.js +32 -0
- package/dist/utils/readApi.js +50 -0
- package/dist/utils/validRouteData.js +37 -0
- package/dist/utils/validToken.js +24 -0
- package/dist/utils/validType.js +101 -0
- package/package.json +34 -15
- package/src/apiDoc/assets/empty-907982e4.js +1 -0
- package/src/apiDoc/assets/index-b56e6bc6.js +1414 -0
- package/src/apiDoc/assets/index-bb16d834.css +6 -0
- package/src/{public/doc/img/jsoneditor-icons.256e3abc.svg → apiDoc/assets/jsoneditor-icons-45c98bf0.svg} +213 -357
- package/src/apiDoc/favicon.ico +0 -0
- package/src/apiDoc/index.html +15 -0
- package/src/index.ts +132 -0
- package/src/masmysql/checkSqlkey.ts +55 -0
- package/src/masmysql/createSqlForm.ts +37 -0
- package/src/masmysql/esaysql.ts +44 -0
- package/src/masmysql/index.ts +68 -0
- package/src/masmysql/masSql.ts +736 -0
- package/src/masmysql/state.ts +5 -0
- package/src/masmysql/type.ts +34 -0
- package/src/masmysql/utils.ts +8 -0
- package/src/state.ts +1 -0
- package/src/typings/index.ts +114 -0
- package/src/utils/apiDoc.ts +18 -0
- package/src/utils/getLog.ts +20 -0
- package/src/utils/getRouteByApiSrc.ts +55 -0
- package/src/utils/mas-encrypt.min.js +1 -0
- package/src/utils/masUtils.ts +8 -0
- package/src/utils/meaToken.ts +56 -0
- package/src/utils/quickSend.ts +19 -0
- package/src/utils/readApi.ts +30 -0
- package/src/utils/validRouteData.ts +46 -0
- package/src/utils/validToken.ts +22 -0
- package/src/utils/validType.ts +98 -0
- package/test/apiTemplate/api.temp.ts +6 -0
- package/test/apiTemplate/curd/del.ts +9 -0
- package/test/apiTemplate/curd/get.ts +9 -0
- package/test/apiTemplate/curd/set.ts +9 -0
- package/test/config/api.config.ts +3 -0
- package/test/config/api.key.ts +14 -0
- package/test/config/config.ts +30 -0
- package/test/config/createSqlFormData.ts +14 -0
- package/test/logs/.keep +0 -0
- package/test/main.ts +25 -0
- package/test/scripts/beforeCreated.ts +67 -0
- package/test/scripts/createApiFile.ts +27 -0
- package/test/scripts/createApis.ts +109 -0
- package/test/scripts/createSqlForm.ts +5 -0
- package/test/scripts/getSqlFormType.ts +34 -0
- package/test/scripts/run.ts +4 -0
- package/test/scripts/updateRouter.ts +46 -0
- package/test/src/api/curd/student/del.ts +9 -0
- package/test/src/api/curd/student/get.ts +9 -0
- package/test/src/api/curd/student/set.ts +9 -0
- package/test/src/api/index.ts +4 -0
- package/test/src/api/per.ts +6 -0
- package/test/src/api/person/del.ts +4 -0
- package/test/src/api/person/get.ts +4 -0
- package/test/src/api/person/set.ts +4 -0
- package/test/src/api/test.ts +7 -0
- package/test/src/api/validtest/boolArr.ts +4 -0
- package/test/src/debug.ts +56 -0
- package/test/src/masState.ts +4 -0
- package/test/src/sql/selectStudent.ts +7 -0
- package/test/src/sqlform.json +1 -0
- package/test/test.ts +13 -0
- package/test/typings/.keep +0 -0
- package/test/typings/studentSF.ts +6 -0
- package/test/typings/test11SF.ts +1 -0
- package/test/typings/test22SF.ts +1 -0
- package/test/typings/testformSF.ts +1 -0
- package/test//345/276/205/345/274/200/345/217/221.md +12 -0
- package/tsconfig.json +23 -0
- package/tsconfigschema.json +1263 -0
- package/src/config/api.res.json +0 -1
- package/src/config/api.tree.json +0 -1
- package/src/config/apiModules/del.module +0 -6
- package/src/config/apiModules/get.module +0 -6
- package/src/config/apiModules/set.module +0 -6
- package/src/config/config.schema.js +0 -41
- package/src/public/doc/css/app.722f60c4.css +0 -1
- package/src/public/doc/css/chunk-287b9965.80548a4b.css +0 -1
- package/src/public/doc/css/chunk-vendors.a16c4353.css +0 -1
- package/src/public/doc/favicon.ico +0 -0
- package/src/public/doc/fonts/element-icons.535877f5.woff +0 -0
- package/src/public/doc/fonts/element-icons.732389de.ttf +0 -0
- package/src/public/doc/index.html +0 -1
- package/src/public/doc/js/app.9ce10690.js +0 -2
- package/src/public/doc/js/app.9ce10690.js.map +0 -1
- package/src/public/doc/js/chunk-287b9965.77cfd27f.js +0 -32
- package/src/public/doc/js/chunk-287b9965.77cfd27f.js.map +0 -1
- package/src/public/doc/js/chunk-vendors.7c84e9a7.js +0 -35
- package/src/public/doc/js/chunk-vendors.7c84e9a7.js.map +0 -1
- package/src/public/index.html +0 -15
- package/src/state.js +0 -6
- package/src/utils/DataVerified.js +0 -35
- package/src/utils/beforeCreate.js +0 -19
- package/src/utils/createApi.js +0 -34
- package/src/utils/createApiFile.js +0 -40
- package/src/utils/createSqlForm.js +0 -7
- package/src/utils/encrypt.js +0 -1
- package/src/utils/getDir.js +0 -60
- package/src/utils/logs.js +0 -16
- package/src/utils/masDoc.js +0 -37
- package/src/utils/masRoute.js +0 -29
- package/src/utils/quickSend.js +0 -18
- package/src/utils/token.js +0 -57
- package/test/.utils/api.js +0 -4
- package/test/.utils/apis.js +0 -5
- package/test/.utils/beforeIndex.js +0 -4
- package/test/.utils/createForm.js +0 -4
- package/test/config/api.config.json +0 -7
- package/test/config/api.config.k.json +0 -1
- package/test/config/config.js +0 -22
- package/test/config/sqlform.config.js +0 -23
- package/test/index.js +0 -12
- package/test/public/1.txt +0 -1
- package/test/serverLogs/2022-10-29.log +0 -6
- package/test/serverLogs/2022-10-30.log +0 -58
- package/test/src/$form.js +0 -1
- package/test/src/api/index.js +0 -3
- package/test/src/api/test.js +0 -9
- package/test/src/api/vip/del.js +0 -6
- package/test/src/api/vip/get.js +0 -6
- package/test/src/api/vip/set.js +0 -6
- package/test/src/api/vip666.js +0 -4
- package/test/src/debug.js +0 -2
- /package/{old → .temp/logs/.keep} +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { getType } from "@/utils/masUtils";
|
|
2
|
+
|
|
3
|
+
type BasicTypeDescriptor =
|
|
4
|
+
| StringConstructor
|
|
5
|
+
| NumberConstructor
|
|
6
|
+
| BooleanConstructor
|
|
7
|
+
| ObjectConstructor;
|
|
8
|
+
type TypeDescriptor = BasicTypeDescriptor | Array<any>;
|
|
9
|
+
|
|
10
|
+
interface TypeMapping {
|
|
11
|
+
[key: string]:
|
|
12
|
+
| TypeDescriptor
|
|
13
|
+
| TypeMapping
|
|
14
|
+
| Array<TypeDescriptor | TypeMapping>;
|
|
15
|
+
}
|
|
16
|
+
const vliadBase = (obj, typeMapping) => {
|
|
17
|
+
return getType(obj) == (typeMapping as any).name.toLowerCase();
|
|
18
|
+
};
|
|
19
|
+
const validTypeObj = (obj: any, typeMapping: TypeMapping | TypeDescriptor) => {
|
|
20
|
+
if (obj === null || obj === undefined) return false;
|
|
21
|
+
let typeT = getType(typeMapping);
|
|
22
|
+
if (typeT == "function") {
|
|
23
|
+
return vliadBase(obj, typeMapping);
|
|
24
|
+
} else if (typeT == "object") {
|
|
25
|
+
if (getType(obj) !== "object") return false;
|
|
26
|
+
for (let k in typeMapping) {
|
|
27
|
+
if (obj[k] === null || obj[k] === undefined) return false;
|
|
28
|
+
if (!validType(obj[k], typeMapping[k])) return false;
|
|
29
|
+
}
|
|
30
|
+
} else if (typeT == "array") {
|
|
31
|
+
if (getType(obj) != "array") return false;
|
|
32
|
+
if (obj.length === 0) return false;
|
|
33
|
+
const typeMappingArrObj = typeMapping[0];
|
|
34
|
+
if (!typeMappingArrObj) {
|
|
35
|
+
throw new Error(`非法的效验格式 ${typeMapping} ${typeMappingArrObj}`);
|
|
36
|
+
}
|
|
37
|
+
if (getType(obj) != "array") return false;
|
|
38
|
+
for (let v of obj) {
|
|
39
|
+
if (!validTypeObj(v, typeMappingArrObj)) return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
};
|
|
44
|
+
const validTypeObjStrict = (
|
|
45
|
+
obj: any,
|
|
46
|
+
typeMapping: TypeMapping | TypeDescriptor
|
|
47
|
+
) => {
|
|
48
|
+
if (obj === null || obj === undefined) return false;
|
|
49
|
+
let typeT = getType(typeMapping);
|
|
50
|
+
if (typeT == "function") {
|
|
51
|
+
return vliadBase(obj, typeMapping);
|
|
52
|
+
} else if (typeT == "object") {
|
|
53
|
+
if (getType(obj) !== "object") return false;
|
|
54
|
+
if (Object.keys(obj).length != Object.keys(typeT).length) return false;
|
|
55
|
+
for (let k in typeMapping) {
|
|
56
|
+
if (obj[k] === null || obj[k] === undefined) return false;
|
|
57
|
+
if (!validType(obj[k], typeMapping[k])) return false;
|
|
58
|
+
}
|
|
59
|
+
} else if (typeT == "array") {
|
|
60
|
+
if (getType(obj) != "array") return false;
|
|
61
|
+
if (obj.length === 0) return false;
|
|
62
|
+
const typeMappingArrObj = typeMapping[0];
|
|
63
|
+
if (!typeMappingArrObj) {
|
|
64
|
+
throw new Error(`非法的效验格式 ${typeMapping} ${typeMappingArrObj}`);
|
|
65
|
+
}
|
|
66
|
+
if (getType(obj) != "array") return false;
|
|
67
|
+
for (let v of obj) {
|
|
68
|
+
if (!validTypeObj(v, typeMappingArrObj)) return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
export default function validType(
|
|
74
|
+
obj: any,
|
|
75
|
+
typeMapping: TypeMapping | TypeDescriptor,
|
|
76
|
+
strict: boolean = false
|
|
77
|
+
): boolean {
|
|
78
|
+
return strict
|
|
79
|
+
? validTypeObjStrict(obj, typeMapping)
|
|
80
|
+
: validTypeObj(obj, typeMapping);
|
|
81
|
+
}
|
|
82
|
+
// let res = validType(
|
|
83
|
+
// { name: "2", arr: [1] },
|
|
84
|
+
// { name: String, arr: [Number] },
|
|
85
|
+
// true
|
|
86
|
+
// [
|
|
87
|
+
// { name: "s", age: "" },
|
|
88
|
+
// { names: "s", age: "" },
|
|
89
|
+
// ],
|
|
90
|
+
// { name: "11", age: 1, obj: { a: "1", b: 1 } },
|
|
91
|
+
// String,
|
|
92
|
+
// Boolean,
|
|
93
|
+
// { name: String, age: Number, obj: { a: String, b: Number } }
|
|
94
|
+
// [{ name: String }]
|
|
95
|
+
// );
|
|
96
|
+
// console.log(res);
|
|
97
|
+
// console.log(getType(null)); //"null"
|
|
98
|
+
// console.log(getType(undefined)); //"undefined"
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
import sqlform from "@@/test/src/sqlform.json";
|
|
4
|
+
|
|
5
|
+
export default async (req: masReq, res: masRes) => {
|
|
6
|
+
const { sql } = masState;
|
|
7
|
+
const sqlRes = await sql.use(sqlform.$form).delete(req.body).go();
|
|
8
|
+
res.return(sqlRes);
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
import sqlform from "@@/test/src/sqlform.json";
|
|
4
|
+
|
|
5
|
+
export default async (req: masReq, res: masRes) => {
|
|
6
|
+
const { sql } = masState;
|
|
7
|
+
const sqlRes = await sql.use(sqlform.$form).select(req.body).paging().go();
|
|
8
|
+
res.return(sqlRes);
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
import sqlform from "@@/test/src/sqlform.json";
|
|
4
|
+
|
|
5
|
+
export default async (req: masReq, res: masRes) => {
|
|
6
|
+
const { sql } = masState;
|
|
7
|
+
const sqlRes = await sql.use(sqlform.$form).set(req.body).go();
|
|
8
|
+
res.return(sqlRes, sqlRes);
|
|
9
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { apiConfigType } from "@/index";
|
|
2
|
+
const apiConfig: apiConfigType = {"/api/curd/student/del":{"name":"删除数据","methods":"all","des":"","validFormat":{"id":Number}},"/api/curd/student/get":{"name":"得到数据","methods":"all","des":"","validFormat":null},"/api/curd/student/set":{"name":"修改或添加数据","methods":"all","des":"","validFormat":null},"/api/index":{"name":"默认接口","methods":"post","des":"一个默认接口","validFormat":{"arr":[{"name":String,"age":Number}]},"header":[{"name":"token","type":String,"des":"token密钥"}],"validFormatDes":[{"name":"arr","type":{"arr":[{"name":String,"age":Number}]},"des":"学生数组"},{"name":"arg1","type":Number,"des":"可选参数","optional":true}],"example":{"arr":[{"name":"小明","age":18}]},"resExample":{"status":1,"data":"hello mas!","msg":""}},"/api/per":{"name":"新建接口","methods":"all","des":"","validFormat":null},"/api/person/del":{"name":"新建接口","methods":"all","des":"","validFormat":{"arr":[String]}},"/api/person/get":{"name":"新建接口","methods":"all","des":""},"/api/person/set":{"name":"新建接口","methods":"all","des":""},"/api/test":{"name":"新建接口","methods":"all","des":"","validFormat":null},"/api/validtest/boolArr":{"name":"新建接口","methods":"all","des":"","validFormat":[{"name":String}]}};
|
|
3
|
+
export default apiConfig;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { configType } from "@/index";
|
|
2
|
+
export default <configType>{
|
|
3
|
+
projectName: "mas-server",
|
|
4
|
+
port: 8087,
|
|
5
|
+
apidoc: true,
|
|
6
|
+
apidocOptions: {
|
|
7
|
+
// iframe嵌套
|
|
8
|
+
baseUrl: "",
|
|
9
|
+
},
|
|
10
|
+
cors: true,
|
|
11
|
+
logs: {
|
|
12
|
+
open: true,
|
|
13
|
+
debug: false,
|
|
14
|
+
},
|
|
15
|
+
token: {
|
|
16
|
+
open: true,
|
|
17
|
+
pwd: "8087",
|
|
18
|
+
headerParams: "token",
|
|
19
|
+
},
|
|
20
|
+
useSql: true,
|
|
21
|
+
debugTime: 1000,
|
|
22
|
+
sql: {
|
|
23
|
+
host: "127.0.0.1",
|
|
24
|
+
user: "root",
|
|
25
|
+
password: "123456",
|
|
26
|
+
database: "test666",
|
|
27
|
+
// database: "test",
|
|
28
|
+
// password: "tx123456",
|
|
29
|
+
},
|
|
30
|
+
};
|
package/test/logs/.keep
ADDED
|
File without changes
|
package/test/main.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "module-alias/register";
|
|
2
|
+
import { getApp, masReq, masRes } from "@/index";
|
|
3
|
+
import masState from "./src/masState";
|
|
4
|
+
import config from "./config/config";
|
|
5
|
+
|
|
6
|
+
const app = getApp(__dirname, (app) => {
|
|
7
|
+
// 数据库连接和express服务启动是异步进行的,如果数据库连接较慢,可能会出现调用接口访问不了数据库的情况
|
|
8
|
+
app.use((req, res: masRes, next) => {
|
|
9
|
+
if (config.useSql && !masState.sql) {
|
|
10
|
+
res.return("数据库正在连接!", 0, 500);
|
|
11
|
+
} else {
|
|
12
|
+
next();
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
app.all("/", (_req, res: masRes) => {
|
|
17
|
+
res.return("default index");
|
|
18
|
+
});
|
|
19
|
+
// 404兜底
|
|
20
|
+
app.all("*", (_req: masReq, res: masRes) => {
|
|
21
|
+
res.return(404, 0, 404);
|
|
22
|
+
});
|
|
23
|
+
app.listen(8087, () => {
|
|
24
|
+
console.log("http://localhost:8087");
|
|
25
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import "module-alias/register";
|
|
2
|
+
import {
|
|
3
|
+
checkSqlKey,
|
|
4
|
+
getRouteByApiSrc,
|
|
5
|
+
sqlInit,
|
|
6
|
+
getsqlFormData,
|
|
7
|
+
massql,
|
|
8
|
+
sqlEnd,
|
|
9
|
+
} from "@/index";
|
|
10
|
+
import * as fs from "fs";
|
|
11
|
+
import * as path from "path";
|
|
12
|
+
import apiConfig from "../config/api.config";
|
|
13
|
+
import apiKey from "../config/api.key";
|
|
14
|
+
import config from "../config/config";
|
|
15
|
+
import masState from "../src/masState";
|
|
16
|
+
import c from "ansi-colors";
|
|
17
|
+
const { newApiConfig, newApiConfigArr, isAdd, newApiKey } = getRouteByApiSrc(
|
|
18
|
+
path.join(__dirname, "../src/api"),
|
|
19
|
+
apiConfig,
|
|
20
|
+
apiKey
|
|
21
|
+
);
|
|
22
|
+
// 写入api.config.js和api.key.js
|
|
23
|
+
if (isAdd) {
|
|
24
|
+
fs.writeFileSync(
|
|
25
|
+
path.join(__dirname, "../config/api.config.ts"),
|
|
26
|
+
`import { apiConfigType } from "@/index";
|
|
27
|
+
const apiConfig: apiConfigType = ${
|
|
28
|
+
// 构造器类型正则转译
|
|
29
|
+
JSON.stringify(newApiConfig, (key, value) => {
|
|
30
|
+
if (typeof value === "function") {
|
|
31
|
+
return value
|
|
32
|
+
.toString()
|
|
33
|
+
.replace(/function\s+(\w+)\(\)\s+\{.*\}/, "$1");
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}).replace(/"String"|"Number"|"Boolean"/g, (match) =>
|
|
37
|
+
match.replace(/"/g, "")
|
|
38
|
+
)
|
|
39
|
+
};
|
|
40
|
+
export default apiConfig;`
|
|
41
|
+
);
|
|
42
|
+
fs.writeFileSync(
|
|
43
|
+
path.join(__dirname, "../config/api.key.ts"),
|
|
44
|
+
"export default " + JSON.stringify(newApiKey)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
// 数据库初始化
|
|
48
|
+
(async () => {
|
|
49
|
+
// mysql初始化
|
|
50
|
+
if (config.useSql) {
|
|
51
|
+
await sqlInit(config.sql);
|
|
52
|
+
// 如果表没有update_time,create_time,is_delete三个字段,补上,放入masmysql的state中
|
|
53
|
+
await checkSqlKey();
|
|
54
|
+
// 比较入masmysql的state中的form,不一样重新生成生成sqlform,json
|
|
55
|
+
getsqlFormData(path.join(__dirname, "/../src/sqlform.json"));
|
|
56
|
+
if (process.argv[2] != "-end") {
|
|
57
|
+
masState.sql = new massql();
|
|
58
|
+
console.info(c.greenBright("mysql数据库连接成功!"));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (process.argv[2] == "-end") {
|
|
62
|
+
await sqlEnd();
|
|
63
|
+
console.info(c.greenBright("初始化成功!"));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
})();
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import c from "ansi-colors";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
const createApiFile = (DIRNAME, tempPath) => {
|
|
5
|
+
let argv = process.argv.slice(2);
|
|
6
|
+
// console.log(argv);
|
|
7
|
+
for (let dir of argv) {
|
|
8
|
+
let dirname = dir;
|
|
9
|
+
let dirpath: any = dir.split("/");
|
|
10
|
+
dirpath = dirpath.slice(0, dirpath.length - 1).join("/");
|
|
11
|
+
dir = path.join(DIRNAME, dir) + ".ts";
|
|
12
|
+
dirpath = path.join(DIRNAME, dirpath);
|
|
13
|
+
let isExist = fs.existsSync(dir);
|
|
14
|
+
if (isExist) {
|
|
15
|
+
console.info(c.yellow(`已存在!${dirname}`));
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
isExist = fs.existsSync(dirpath);
|
|
19
|
+
if (!isExist) fs.mkdirSync(dirpath, { recursive: true });
|
|
20
|
+
fs.copyFileSync(tempPath, dir);
|
|
21
|
+
console.info(c.green(`创建成功!${dirname}`));
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
createApiFile(
|
|
25
|
+
path.join(__dirname, "../src/api"),
|
|
26
|
+
path.join(__dirname, "../apiTemplate/api.temp.ts")
|
|
27
|
+
);
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import "module-alias/register";
|
|
2
|
+
import {
|
|
3
|
+
sqlInit,
|
|
4
|
+
checkSqlKey,
|
|
5
|
+
getsqlFormData,
|
|
6
|
+
massql,
|
|
7
|
+
sqlEnd,
|
|
8
|
+
getsqlFormDataType,
|
|
9
|
+
} from "@/index";
|
|
10
|
+
import config from "../config/config";
|
|
11
|
+
import masState from "../src/masState";
|
|
12
|
+
import * as fs from "fs";
|
|
13
|
+
import * as path from "path";
|
|
14
|
+
import c from "ansi-colors";
|
|
15
|
+
// import apiConfig from "../config/api.config";
|
|
16
|
+
// 根据数据库生成对应type,自动注入数据效验
|
|
17
|
+
(async () => {
|
|
18
|
+
let argv = process.argv.slice(2);
|
|
19
|
+
console.log(argv);
|
|
20
|
+
const formName = argv[0];
|
|
21
|
+
const wPath = argv[1];
|
|
22
|
+
// mysql初始化
|
|
23
|
+
if (config.useSql) {
|
|
24
|
+
await sqlInit(config.sql);
|
|
25
|
+
await checkSqlKey();
|
|
26
|
+
const formDataType = getsqlFormDataType();
|
|
27
|
+
if (!formDataType[formName]) {
|
|
28
|
+
console.info(c.redBright("该表不存在!"));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const vlidType = formDataType[formName].typeData;
|
|
32
|
+
const getStr = fs
|
|
33
|
+
.readFileSync(path.join(__dirname, "../apiTemplate/curd/get.ts"))
|
|
34
|
+
.toString()
|
|
35
|
+
.replace("$form", formName);
|
|
36
|
+
const delStr = fs
|
|
37
|
+
.readFileSync(path.join(__dirname, "../apiTemplate/curd/del.ts"))
|
|
38
|
+
.toString()
|
|
39
|
+
.replace("$form", formName);
|
|
40
|
+
const setStr = fs
|
|
41
|
+
.readFileSync(path.join(__dirname, "../apiTemplate/curd/set.ts"))
|
|
42
|
+
.toString()
|
|
43
|
+
.replace("$form", formName);
|
|
44
|
+
// console.log(getStr);
|
|
45
|
+
fs.mkdirSync(path.join(__dirname, "../src/api", wPath, `/${formName}`), {
|
|
46
|
+
recursive: true,
|
|
47
|
+
});
|
|
48
|
+
fs.writeFileSync(
|
|
49
|
+
path.join(__dirname, "../src/api", wPath, `/${formName}/get.ts`),
|
|
50
|
+
getStr
|
|
51
|
+
);
|
|
52
|
+
fs.writeFileSync(
|
|
53
|
+
path.join(__dirname, "../src/api", wPath, `/${formName}/del.ts`),
|
|
54
|
+
delStr
|
|
55
|
+
);
|
|
56
|
+
fs.writeFileSync(
|
|
57
|
+
path.join(__dirname, "../src/api", wPath, `/${formName}/set.ts`),
|
|
58
|
+
setStr
|
|
59
|
+
);
|
|
60
|
+
const dir = `/api${argv[1]}/${formName}`;
|
|
61
|
+
const apiConfig = (await import("../config/api.config")).default;
|
|
62
|
+
// console.log(apiConfig);
|
|
63
|
+
|
|
64
|
+
// console.log(apiConfig[`${dir}/del`].name);
|
|
65
|
+
// return
|
|
66
|
+
apiConfig[`${dir}/get`] = {
|
|
67
|
+
name: "新建接口",
|
|
68
|
+
methods: "all",
|
|
69
|
+
des: "",
|
|
70
|
+
validFormat: null,
|
|
71
|
+
};
|
|
72
|
+
apiConfig[`${dir}/set`] = {
|
|
73
|
+
name: "新建接口",
|
|
74
|
+
methods: "all",
|
|
75
|
+
des: "",
|
|
76
|
+
validFormat: null,
|
|
77
|
+
};
|
|
78
|
+
apiConfig[`${dir}/del`] = {
|
|
79
|
+
name: "新建接口",
|
|
80
|
+
methods: "all",
|
|
81
|
+
des: "",
|
|
82
|
+
validFormat: null,
|
|
83
|
+
};
|
|
84
|
+
apiConfig[`${dir}/get`].name = "得到数据";
|
|
85
|
+
apiConfig[`${dir}/set`].name = "修改或添加数据";
|
|
86
|
+
apiConfig[`${dir}/del`].name = "删除数据";
|
|
87
|
+
apiConfig[`${dir}/del`].validFormat = { id: Number };
|
|
88
|
+
fs.writeFileSync(
|
|
89
|
+
path.join(__dirname, "../config/api.config.ts"),
|
|
90
|
+
`import { apiConfigType } from "@/index";
|
|
91
|
+
const apiConfig: apiConfigType = ${
|
|
92
|
+
// 构造器类型正则转译
|
|
93
|
+
JSON.stringify(apiConfig, (key, value) => {
|
|
94
|
+
if (typeof value === "function") {
|
|
95
|
+
return value
|
|
96
|
+
.toString()
|
|
97
|
+
.replace(/function\s+(\w+)\(\)\s+\{.*\}/, "$1");
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
}).replace(/"String"|"Number"|"Boolean"/g, (match) =>
|
|
101
|
+
match.replace(/"/g, "")
|
|
102
|
+
)
|
|
103
|
+
};
|
|
104
|
+
export default apiConfig;`
|
|
105
|
+
);
|
|
106
|
+
await sqlEnd();
|
|
107
|
+
// console.info(c.greenBright("获取类型成功!"));
|
|
108
|
+
}
|
|
109
|
+
})();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "module-alias/register";
|
|
2
|
+
import {
|
|
3
|
+
checkSqlKey,
|
|
4
|
+
getRouteByApiSrc,
|
|
5
|
+
sqlInit,
|
|
6
|
+
getsqlFormData,
|
|
7
|
+
massql,
|
|
8
|
+
sqlEnd,
|
|
9
|
+
getsqlFormDataType,
|
|
10
|
+
} from "@/index";
|
|
11
|
+
import * as fs from "fs";
|
|
12
|
+
import * as path from "path";
|
|
13
|
+
import apiConfig from "../config/api.config";
|
|
14
|
+
import apiKey from "../config/api.key";
|
|
15
|
+
import config from "../config/config";
|
|
16
|
+
import masState from "../src/masState";
|
|
17
|
+
import c from "ansi-colors";
|
|
18
|
+
|
|
19
|
+
(async () => {
|
|
20
|
+
// mysql初始化
|
|
21
|
+
if (config.useSql) {
|
|
22
|
+
await sqlInit(config.sql);
|
|
23
|
+
await checkSqlKey();
|
|
24
|
+
const formDataType = getsqlFormDataType();
|
|
25
|
+
for (let formName in formDataType) {
|
|
26
|
+
fs.writeFileSync(
|
|
27
|
+
path.join(__dirname, `../typings/${formName}SF.ts`),
|
|
28
|
+
`export type ${formName}SF = ${formDataType[formName].typeObj}`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
await sqlEnd();
|
|
32
|
+
console.info(c.greenBright("获取类型成功!"));
|
|
33
|
+
}
|
|
34
|
+
})();
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import "module-alias/register";
|
|
2
|
+
import {
|
|
3
|
+
checkSqlKey,
|
|
4
|
+
getRouteByApiSrc,
|
|
5
|
+
sqlInit,
|
|
6
|
+
getsqlFormData,
|
|
7
|
+
massql,
|
|
8
|
+
sqlEnd,
|
|
9
|
+
} from "@/index";
|
|
10
|
+
import * as fs from "fs";
|
|
11
|
+
import * as path from "path";
|
|
12
|
+
import apiConfig from "../config/api.config";
|
|
13
|
+
import apiKey from "../config/api.key";
|
|
14
|
+
import config from "../config/config";
|
|
15
|
+
import masState from "../src/masState";
|
|
16
|
+
import c from "ansi-colors";
|
|
17
|
+
const { newApiConfig, newApiConfigArr, isAdd, newApiKey } = getRouteByApiSrc(
|
|
18
|
+
path.join(__dirname, "../src/api"),
|
|
19
|
+
apiConfig,
|
|
20
|
+
apiKey
|
|
21
|
+
);
|
|
22
|
+
// 写入api.config.js和api.key.js
|
|
23
|
+
if (isAdd) {
|
|
24
|
+
fs.writeFileSync(
|
|
25
|
+
path.join(__dirname, "../config/api.config.ts"),
|
|
26
|
+
`import { apiConfigType } from "@/index";
|
|
27
|
+
const apiConfig: apiConfigType = ${
|
|
28
|
+
// 构造器类型正则转译
|
|
29
|
+
JSON.stringify(newApiConfig, (key, value) => {
|
|
30
|
+
if (typeof value === "function") {
|
|
31
|
+
return value
|
|
32
|
+
.toString()
|
|
33
|
+
.replace(/function\s+(\w+)\(\)\s+\{.*\}/, "$1");
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}).replace(/"String"|"Number"|"Boolean"/g, (match) =>
|
|
37
|
+
match.replace(/"/g, "")
|
|
38
|
+
)
|
|
39
|
+
};
|
|
40
|
+
export default apiConfig;`
|
|
41
|
+
);
|
|
42
|
+
fs.writeFileSync(
|
|
43
|
+
path.join(__dirname, "../config/api.key.ts"),
|
|
44
|
+
"export default " + JSON.stringify(newApiKey)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
import sqlform from "@@/test/src/sqlform.json";
|
|
4
|
+
|
|
5
|
+
export default async (req: masReq, res: masRes) => {
|
|
6
|
+
const { sql } = masState;
|
|
7
|
+
const sqlRes = await sql.use(sqlform.student).delete(req.body).go();
|
|
8
|
+
res.return(sqlRes);
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
import sqlform from "@@/test/src/sqlform.json";
|
|
4
|
+
|
|
5
|
+
export default async (req: masReq, res: masRes) => {
|
|
6
|
+
const { sql } = masState;
|
|
7
|
+
const sqlRes = await sql.use(sqlform.student).select(req.body).paging().go();
|
|
8
|
+
res.return(sqlRes);
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
import sqlform from "@@/test/src/sqlform.json";
|
|
4
|
+
|
|
5
|
+
export default async (req: masReq, res: masRes) => {
|
|
6
|
+
const { sql } = masState;
|
|
7
|
+
const sqlRes = await sql.use(sqlform.student).set(req.body).go();
|
|
8
|
+
res.return(sqlRes, sqlRes);
|
|
9
|
+
};
|