mas-server 2.0.46 → 2.0.51
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/merge/validType.ts +98 -0
- package/beforebuild.js +3 -0
- package/dist/index.js +8 -47
- package/dist/utils/apiDoc.js +17 -2
- package/dist/utils/createRouter.js +51 -0
- package/dist/utils/getRouterInfo.js +34 -0
- package/dist/utils/mas-encrypt.min.js +1 -3072
- package/dist/utils/validRouteData.js +1 -1
- package/dist/utils/validToken.js +1 -1
- package/dist/utils/validType.js +0 -17
- package/package.json +50 -51
- package/src/index.ts +20 -24
- package/src/typings/index.ts +5 -5
- package/src/utils/apiDoc.ts +18 -2
- package/src/utils/createRouter.ts +22 -0
- package/src/utils/getRouterInfo.ts +28 -0
- package/src/utils/readApi.ts +0 -2
- package/src/utils/validRouteData.ts +1 -2
- package/src/utils/validToken.ts +1 -1
- package/src/utils/validType.ts +0 -17
- package/test/apiTemplate/api.temp.ts +8 -3
- package/test/config/config.ts +1 -1
- package/test/config/defaultApiConfig.ts +7 -0
- package/test/config/docNav.ts +0 -0
- package/test/main.ts +1 -0
- package/test/scripts/beforeCreated.ts +2 -41
- package/test/scripts/createApiFile.ts +0 -1
- package/test/scripts/createApis.ts +0 -48
- package/test/scripts/getSqlFormType.ts +2 -13
- package/test/src/api/example/ArrValid.ts +11 -0
- package/test/src/api/example/per.ts +34 -0
- package/test/src/api/example/tip.ts +9 -0
- package/test/src/api/example/validReqData.ts +85 -0
- package/test/src/api/index.ts +8 -1
- package/test/src/debug.ts +3 -50
- package/test/src/sqlform.json +1 -1
- package/test/test.ts +0 -12
- package/test//345/276/205/345/274/200/345/217/221.md +12 -6
- package/tsconfig-build.json +20 -0
- package/tsconfig.json +1 -3
- package/.temp/validType.ts +0 -96
- package/dist/apiDoc/assets/empty-a6de4920.js +0 -1
- package/dist/apiDoc/assets/index-be53fe8f.css +0 -6
- package/dist/apiDoc/assets/index-f9ab4187.js +0 -1427
- package/dist/apiDoc/assets/jsoneditor-icons-45c98bf0.svg +0 -749
- package/dist/apiDoc/favicon.ico +0 -0
- package/dist/apiDoc/index.html +0 -15
- package/dist/utils/getRouteByApiSrc.js +0 -54
- package/test/config/api.config.ts +0 -3
- package/test/config/api.key.ts +0 -14
- package/test/src/api/curd/student/del.ts +0 -9
- package/test/src/api/curd/student/get.ts +0 -9
- package/test/src/api/curd/student/set.ts +0 -9
- package/test/src/api/per.ts +0 -6
- package/test/src/api/person/del.ts +0 -4
- package/test/src/api/person/get.ts +0 -4
- package/test/src/api/person/set.ts +0 -4
- package/test/src/api/test.ts +0 -7
- package/test/src/api/validtest/boolArr.ts +0 -4
- package/test/typings/studentSF.ts +0 -6
- package/test/typings/test11SF.ts +0 -1
- package/test/typings/test22SF.ts +0 -1
- package/test/typings/testformSF.ts +0 -1
- /package/.temp/{logs/.keep → .keep} +0 -0
- /package/{src/utils → .temp}/getRouteByApiSrc.ts +0 -0
- /package/.temp/{a.ts → merge/a.ts} +0 -0
- /package/.temp/{as.ts → merge/as.ts} +0 -0
- /package/.temp/{b.ts → merge/b.ts} +0 -0
- /package/.temp/{i.js → merge/i.js} +0 -0
- /package/{test/typings → .temp/merge/logs}/.keep +0 -0
- /package/.temp/{test.ts → merge/test.ts} +0 -0
- /package/.temp/{validType copy.ts → merge/validType copy.ts} +0 -0
- /package/.temp/{validTypeOptimize.ts → merge/validTypeOptimize.ts} +0 -0
- /package/{test/scripts → .temp}/updateRouter.ts +0 -0
|
@@ -16,7 +16,7 @@ exports.default = (req, res, next) => {
|
|
|
16
16
|
if (routerObj.header && routerObj.header.length != 0) {
|
|
17
17
|
const headerObj = {};
|
|
18
18
|
routerObj.header.forEach((item) => {
|
|
19
|
-
headerObj[item.name] = item.type;
|
|
19
|
+
!item.optional && (headerObj[item.name] = item.type);
|
|
20
20
|
});
|
|
21
21
|
if (!(0, validType_1.default)(req.headers, headerObj)) {
|
|
22
22
|
return res.return("error headers format!", 0, 400);
|
package/dist/utils/validToken.js
CHANGED
|
@@ -9,7 +9,7 @@ exports.default = (req, res, next) => {
|
|
|
9
9
|
if ((0, masUtils_1.getType)(r === null || r === void 0 ? void 0 : r.permission) == "array") {
|
|
10
10
|
const token = req.headers[tokenConfig.headerParams];
|
|
11
11
|
if (!token) {
|
|
12
|
-
return res.return("缺少重要参数", 0);
|
|
12
|
+
return res.return("缺少重要参数", 0, 400);
|
|
13
13
|
}
|
|
14
14
|
let vt = (0, __1.validToken)({ token, permission: r.permission }, tokenConfig.pwd);
|
|
15
15
|
if (vt.status === 0) {
|
package/dist/utils/validType.js
CHANGED
|
@@ -82,20 +82,3 @@ function validType(obj, typeMapping, strict = false) {
|
|
|
82
82
|
: validTypeObj(obj, typeMapping);
|
|
83
83
|
}
|
|
84
84
|
exports.default = validType;
|
|
85
|
-
// let res = validType(
|
|
86
|
-
// { name: "2", arr: [1] },
|
|
87
|
-
// { name: String, arr: [Number] },
|
|
88
|
-
// true
|
|
89
|
-
// [
|
|
90
|
-
// { name: "s", age: "" },
|
|
91
|
-
// { names: "s", age: "" },
|
|
92
|
-
// ],
|
|
93
|
-
// { name: "11", age: 1, obj: { a: "1", b: 1 } },
|
|
94
|
-
// String,
|
|
95
|
-
// Boolean,
|
|
96
|
-
// { name: String, age: Number, obj: { a: String, b: Number } }
|
|
97
|
-
// [{ name: String }]
|
|
98
|
-
// );
|
|
99
|
-
// console.log(res);
|
|
100
|
-
// console.log(getType(null)); //"null"
|
|
101
|
-
// console.log(getType(undefined)); //"undefined"
|
package/package.json
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mas-server",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "一款基于express面向中小型项目的后端框架",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"typings": "src/index.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"testdev": "npx ts-node-dev --transpile-only ./test/scripts/run.ts --respawn",
|
|
9
|
-
"test": "npx ts-node ./test/scripts/beforeCreated.ts -end && npx ts-node ./test/scripts/run.ts",
|
|
10
|
-
"testmon": "npx nodemon ./test/scripts/run.ts",
|
|
11
|
-
"testTS": "npx nodemon ./test/test.ts",
|
|
12
|
-
"createSql": "npx ts-node ./test/scripts/createSqlForm.ts",
|
|
13
|
-
"createApi": "npx ts-node ./test/scripts/createApiFile.ts",
|
|
14
|
-
"createApis": "npx ts-node ./test/scripts/createApis.ts",
|
|
15
|
-
"init": "npx nodemon ./test/scripts/beforeCreated.ts -end",
|
|
16
|
-
"gsft": "npx nodemon ./test/scripts/getSqlFormType.ts",
|
|
17
|
-
"build": "tsc",
|
|
18
|
-
"sync": "
|
|
19
|
-
},
|
|
20
|
-
"keywords": [],
|
|
21
|
-
"author": "tingxi8087",
|
|
22
|
-
"license": "ISC",
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"@types/express": "^4.17.17",
|
|
25
|
-
"ansi-colors": "^4.1.3",
|
|
26
|
-
"axios": "^1.3.6",
|
|
27
|
-
"cors": "^2.8.5",
|
|
28
|
-
"express": "^4.18.2",
|
|
29
|
-
"express-ip": "^1.0.4",
|
|
30
|
-
"fs-extra": "^11.1.1",
|
|
31
|
-
"glob": "^10.3.1",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"ts-node": "^
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"@types/
|
|
48
|
-
"@types/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "mas-server",
|
|
3
|
+
"version": "2.0.51",
|
|
4
|
+
"description": "一款基于express面向中小型项目的后端框架",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"testdev": "npx ts-node-dev --transpile-only ./test/scripts/run.ts --respawn",
|
|
9
|
+
"test": "npx ts-node ./test/scripts/beforeCreated.ts -end && npx ts-node ./test/scripts/run.ts",
|
|
10
|
+
"testmon": "npx nodemon ./test/scripts/run.ts",
|
|
11
|
+
"testTS": "npx nodemon ./test/test.ts",
|
|
12
|
+
"createSql": "npx ts-node ./test/scripts/createSqlForm.ts",
|
|
13
|
+
"createApi": "npx ts-node ./test/scripts/createApiFile.ts",
|
|
14
|
+
"createApis": "npx ts-node ./test/scripts/createApis.ts",
|
|
15
|
+
"init": "npx nodemon ./test/scripts/beforeCreated.ts -end",
|
|
16
|
+
"gsft": "npx nodemon ./test/scripts/getSqlFormType.ts",
|
|
17
|
+
"build": "tsc --project ./tsconfig-build.json",
|
|
18
|
+
"sync": "npm run build && node ./beforebuild.js && npm publish && cnpm sync mas-server && explorer \"https://npmmirror.com/package/mas-server\""
|
|
19
|
+
},
|
|
20
|
+
"keywords": [],
|
|
21
|
+
"author": "tingxi8087",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@types/express": "^4.17.17",
|
|
25
|
+
"ansi-colors": "^4.1.3",
|
|
26
|
+
"axios": "^1.3.6",
|
|
27
|
+
"cors": "^2.8.5",
|
|
28
|
+
"express": "^4.18.2",
|
|
29
|
+
"express-ip": "^1.0.4",
|
|
30
|
+
"fs-extra": "^11.1.1",
|
|
31
|
+
"glob": "^10.3.1",
|
|
32
|
+
"masmysql": "^2.0.53",
|
|
33
|
+
"module-alias": "^2.2.2",
|
|
34
|
+
"moment": "^2.29.4",
|
|
35
|
+
"mysql": "^2.18.1",
|
|
36
|
+
"nodemon": "^2.0.22",
|
|
37
|
+
"ts-node": "^10.9.1",
|
|
38
|
+
"ts-node-dev": "^2.0.0",
|
|
39
|
+
"typescript": "^5.0.4"
|
|
40
|
+
},
|
|
41
|
+
"_moduleAliases": {
|
|
42
|
+
"@": "src/",
|
|
43
|
+
"@@": "."
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/cors": "^2.8.13",
|
|
47
|
+
"@types/express": "^4.17.17",
|
|
48
|
+
"@types/moment": "^2.13.0"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -9,9 +9,15 @@ import getLog from "./utils/getLog";
|
|
|
9
9
|
import quickSend from "./utils/quickSend";
|
|
10
10
|
import readApi from "./utils/readApi";
|
|
11
11
|
import validRouteData from "./utils/validRouteData";
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
import validTokenUse from "./utils/validToken";
|
|
14
|
-
import type {
|
|
14
|
+
import type {
|
|
15
|
+
masReq,
|
|
16
|
+
masRes,
|
|
17
|
+
apiConfigType,
|
|
18
|
+
configType,
|
|
19
|
+
apiConfigTypeItem,
|
|
20
|
+
} from "./typings";
|
|
15
21
|
import c from "ansi-colors";
|
|
16
22
|
import moment from "moment";
|
|
17
23
|
import "moment/locale/zh-cn";
|
|
@@ -32,6 +38,8 @@ import type { createSqlFormType, sqlForm } from "masmysql";
|
|
|
32
38
|
import apiDoc from "./utils/apiDoc";
|
|
33
39
|
import { createToken, validToken } from "./utils/meaToken";
|
|
34
40
|
import validType from "./utils/validType";
|
|
41
|
+
import createRouter from "./utils/createRouter";
|
|
42
|
+
import getRouterInfo from "./utils/getRouterInfo";
|
|
35
43
|
|
|
36
44
|
// =================================================================
|
|
37
45
|
export function getApp(
|
|
@@ -41,14 +49,15 @@ export function getApp(
|
|
|
41
49
|
// 路径和配置放入state
|
|
42
50
|
state.DIRNAME = DIRNAME;
|
|
43
51
|
const config = require(path.join(DIRNAME, "/config/config.ts")).default;
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
// 获取路由信息
|
|
53
|
+
getRouterInfo(
|
|
54
|
+
path.join(state.DIRNAME, "src/api"),
|
|
55
|
+
path.join(state.DIRNAME, "config/defaultApiConfig.ts")
|
|
56
|
+
);
|
|
46
57
|
state.config = config;
|
|
47
|
-
state.
|
|
48
|
-
state.apiCN = apiCN;
|
|
58
|
+
state.apiCN = [];
|
|
49
59
|
// new express
|
|
50
60
|
const app = express();
|
|
51
|
-
|
|
52
61
|
// 是否允许跨域
|
|
53
62
|
config.cors && app.use(cors());
|
|
54
63
|
if (beforeMounted) {
|
|
@@ -57,7 +66,6 @@ export function getApp(
|
|
|
57
66
|
// 初始化app
|
|
58
67
|
app.use(express.urlencoded({ extended: true }));
|
|
59
68
|
app.use(express.json());
|
|
60
|
-
// 自定义中间件来处理纯文本和JSON数据
|
|
61
69
|
// 错误处理中间件
|
|
62
70
|
app.use((err, req, res, next) => {
|
|
63
71
|
if (err.type == "entity.parse.failed") {
|
|
@@ -83,20 +91,8 @@ export function getApp(
|
|
|
83
91
|
app.use("/apidoc", express.static(path.join(__dirname, "/apiDoc")));
|
|
84
92
|
apiDoc(app);
|
|
85
93
|
}
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
let isExist = fs.existsSync(path.join(DIRNAME, "src", item) + ".ts");
|
|
89
|
-
if (!isExist) {
|
|
90
|
-
console.info(c.red(`${item} not found`));
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const apiFunc = require(path.join(DIRNAME, "src", item)).default;
|
|
94
|
-
if (!apiFunc) {
|
|
95
|
-
console.info(c.yellow(`${item} 没有导出函数,已跳过..`));
|
|
96
|
-
} else {
|
|
97
|
-
apiFunc && app[Router[item].methods](item, apiFunc);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
94
|
+
// 创建路由
|
|
95
|
+
createRouter(app);
|
|
100
96
|
// 运行debug
|
|
101
97
|
setTimeout(() => {
|
|
102
98
|
try {
|
|
@@ -110,7 +106,6 @@ export function getApp(
|
|
|
110
106
|
}
|
|
111
107
|
export {
|
|
112
108
|
readApi,
|
|
113
|
-
getRouteByApiSrc,
|
|
114
109
|
moment,
|
|
115
110
|
sqlState,
|
|
116
111
|
sqlEnd,
|
|
@@ -123,7 +118,7 @@ export {
|
|
|
123
118
|
massql,
|
|
124
119
|
validToken,
|
|
125
120
|
createToken,
|
|
126
|
-
validType
|
|
121
|
+
validType,
|
|
127
122
|
};
|
|
128
123
|
export type {
|
|
129
124
|
masReq,
|
|
@@ -132,4 +127,5 @@ export type {
|
|
|
132
127
|
configType,
|
|
133
128
|
createSqlFormType,
|
|
134
129
|
sqlForm,
|
|
130
|
+
apiConfigTypeItem,
|
|
135
131
|
};
|
package/src/typings/index.ts
CHANGED
|
@@ -14,13 +14,13 @@ export interface TypeMapping {
|
|
|
14
14
|
| Array<TypeDescriptor | TypeMapping>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
type apiConfigTypeItem = {
|
|
17
|
+
export type apiConfigTypeItem = {
|
|
18
18
|
/** 接口名称 */
|
|
19
|
-
name
|
|
19
|
+
name?: string;
|
|
20
20
|
/** 接口描述 */
|
|
21
21
|
des?: string;
|
|
22
22
|
/** 接口请求方法 */
|
|
23
|
-
methods
|
|
23
|
+
methods?: "get" | "post" | "all";
|
|
24
24
|
/** 请求头参数 */
|
|
25
25
|
header?: {
|
|
26
26
|
/** 参数名称 */
|
|
@@ -55,8 +55,8 @@ type apiConfigTypeItem = {
|
|
|
55
55
|
| {
|
|
56
56
|
/** 字段名称 */
|
|
57
57
|
name: string;
|
|
58
|
-
/**
|
|
59
|
-
type
|
|
58
|
+
/** 字段类型,不填会尝试从validFormat里取 */
|
|
59
|
+
type?: TypeMapping | TypeDescriptor;
|
|
60
60
|
/** 是否为空 */
|
|
61
61
|
optional?: boolean;
|
|
62
62
|
/** 字段描述 */
|
package/src/utils/apiDoc.ts
CHANGED
|
@@ -8,9 +8,25 @@ export default (app: Express) => {
|
|
|
8
8
|
}
|
|
9
9
|
return value;
|
|
10
10
|
});
|
|
11
|
-
|
|
11
|
+
const newRouter = JSON.parse(router);
|
|
12
|
+
// 有的描述没有类型,尝试从validFormat里取
|
|
13
|
+
for (const key in newRouter) {
|
|
14
|
+
const item = newRouter[key];
|
|
15
|
+
if (item.validFormat && item.validFormatDes) {
|
|
16
|
+
for (const validFormatDesItem of item.validFormatDes) {
|
|
17
|
+
if (
|
|
18
|
+
!validFormatDesItem.type &&
|
|
19
|
+
item.validFormat[validFormatDesItem.name]
|
|
20
|
+
) {
|
|
21
|
+
validFormatDesItem.type = item.validFormat[validFormatDesItem.name];
|
|
22
|
+
} else if (!validFormatDesItem.type) {
|
|
23
|
+
validFormatDesItem.type = "String";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
12
28
|
app.post("/apidoc/router", (req: masReq, res: masRes) => {
|
|
13
|
-
res.return(
|
|
29
|
+
res.return(newRouter);
|
|
14
30
|
});
|
|
15
31
|
app.post("/apidoc/cn", (req: masReq, res: masRes) => {
|
|
16
32
|
res.return(state.apiCN);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { state } from "@/state";
|
|
2
|
+
import getRouterInfo from "./getRouterInfo";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
import c from "ansi-colors";
|
|
6
|
+
export default function createRouter(app) {
|
|
7
|
+
const { Router, DIRNAME } = state;
|
|
8
|
+
// 遍历api路由
|
|
9
|
+
for (const item in Router) {
|
|
10
|
+
let isExist = fs.existsSync(path.join(DIRNAME, "src", item) + ".ts");
|
|
11
|
+
if (!isExist) {
|
|
12
|
+
console.info(c.red(`${item} not found`));
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const apiFunc = require(path.join(DIRNAME, "src", item)).default;
|
|
16
|
+
if (!apiFunc) {
|
|
17
|
+
console.info(c.yellow(`${item} 没有导出函数,已跳过..`));
|
|
18
|
+
} else {
|
|
19
|
+
apiFunc && app[Router[item].methods](item, apiFunc);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { state } from "@/state";
|
|
2
|
+
import readFilesRecursively from "./readApi";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import c from "ansi-colors";
|
|
6
|
+
export default function getRouterInfo(apiDir: string, defaullConfigDir) {
|
|
7
|
+
const apiConfig = {};
|
|
8
|
+
const { DIRNAME } = state;
|
|
9
|
+
const routerDir = readFilesRecursively(apiDir, "api");
|
|
10
|
+
const defaullConfig = require(defaullConfigDir).default;
|
|
11
|
+
// 遍历api路由
|
|
12
|
+
for (const item of routerDir) {
|
|
13
|
+
let isExist = fs.existsSync(path.join(DIRNAME, "src", item) + ".ts");
|
|
14
|
+
if (!isExist) {
|
|
15
|
+
console.info(c.red(`${item} not found`));
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const apiItemConfig = require(path.join(DIRNAME, "src", item)).config;
|
|
19
|
+
apiConfig[item] = apiItemConfig || defaullConfig;
|
|
20
|
+
// if (!apiFunc) {
|
|
21
|
+
// console.info(c.yellow(`${item} 没有导出函数,已跳过..`));
|
|
22
|
+
// } else {
|
|
23
|
+
// apiFunc && app[Router[item].methods](item, apiFunc);
|
|
24
|
+
// }
|
|
25
|
+
}
|
|
26
|
+
state.Router = apiConfig;
|
|
27
|
+
return apiConfig;
|
|
28
|
+
}
|
package/src/utils/readApi.ts
CHANGED
|
@@ -6,11 +6,9 @@ export default function readFilesRecursively(
|
|
|
6
6
|
routerPrefix: string = ""
|
|
7
7
|
): string[] {
|
|
8
8
|
const files: string[] = [];
|
|
9
|
-
|
|
10
9
|
fs.readdirSync(folderPath).forEach((fileName) => {
|
|
11
10
|
const filePath = path.join(folderPath, fileName);
|
|
12
11
|
const stat = fs.statSync(filePath);
|
|
13
|
-
|
|
14
12
|
if (stat.isDirectory()) {
|
|
15
13
|
const directoryPrefix = routerPrefix
|
|
16
14
|
? `${routerPrefix}/${fileName}`
|
|
@@ -15,9 +15,8 @@ export default (req: masReq, res: masRes, next) => {
|
|
|
15
15
|
if (routerObj.header && routerObj.header.length != 0) {
|
|
16
16
|
const headerObj = {};
|
|
17
17
|
routerObj.header.forEach((item) => {
|
|
18
|
-
headerObj[item.name] = item.type;
|
|
18
|
+
!item.optional && (headerObj[item.name] = item.type);
|
|
19
19
|
});
|
|
20
|
-
|
|
21
20
|
if (!validType(req.headers, headerObj)) {
|
|
22
21
|
return res.return("error headers format!", 0, 400);
|
|
23
22
|
}
|
package/src/utils/validToken.ts
CHANGED
|
@@ -8,7 +8,7 @@ export default (req: masReq, res: any, next) => {
|
|
|
8
8
|
if (getType(r?.permission) == "array") {
|
|
9
9
|
const token = req.headers[tokenConfig.headerParams];
|
|
10
10
|
if (!token) {
|
|
11
|
-
return res.return("缺少重要参数", 0);
|
|
11
|
+
return res.return("缺少重要参数", 0, 400);
|
|
12
12
|
}
|
|
13
13
|
let vt = validToken({ token, permission: r.permission }, tokenConfig.pwd);
|
|
14
14
|
if (vt.status === 0) {
|
package/src/utils/validType.ts
CHANGED
|
@@ -79,20 +79,3 @@ export default function validType(
|
|
|
79
79
|
? validTypeObjStrict(obj, typeMapping)
|
|
80
80
|
: validTypeObj(obj, typeMapping);
|
|
81
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"
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import type { masReq, masRes } from "@/index";
|
|
2
|
-
import masState from "
|
|
3
|
-
|
|
1
|
+
import type { apiConfigTypeItem, masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@/masState";
|
|
3
|
+
|
|
4
|
+
export const config: apiConfigTypeItem = {
|
|
5
|
+
name: "默认接口",
|
|
6
|
+
methods: "all",
|
|
7
|
+
};
|
|
4
8
|
export default async (req: masReq, res: masRes) => {
|
|
9
|
+
const { sql } = masState;
|
|
5
10
|
res.return("hello mas-server!");
|
|
6
11
|
};
|
package/test/config/config.ts
CHANGED
|
File without changes
|
package/test/main.ts
CHANGED
|
@@ -1,49 +1,11 @@
|
|
|
1
1
|
import "module-alias/register";
|
|
2
|
-
import {
|
|
3
|
-
checkSqlKey,
|
|
4
|
-
getRouteByApiSrc,
|
|
5
|
-
sqlInit,
|
|
6
|
-
getsqlFormData,
|
|
7
|
-
massql,
|
|
8
|
-
sqlEnd,
|
|
9
|
-
} from "@/index";
|
|
2
|
+
import { checkSqlKey, sqlInit, getsqlFormData, massql, sqlEnd } from "@/index";
|
|
10
3
|
import * as fs from "fs";
|
|
11
4
|
import * as path from "path";
|
|
12
|
-
import apiConfig from "../config/api.config";
|
|
13
|
-
import apiKey from "../config/api.key";
|
|
14
5
|
import config from "../config/config";
|
|
15
6
|
import masState from "../src/masState";
|
|
16
7
|
import c from "ansi-colors";
|
|
17
|
-
|
|
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
|
-
}
|
|
8
|
+
|
|
47
9
|
// 数据库初始化
|
|
48
10
|
(async () => {
|
|
49
11
|
// mysql初始化
|
|
@@ -57,7 +19,6 @@ if (isAdd) {
|
|
|
57
19
|
masState.sql = new massql();
|
|
58
20
|
console.info(c.greenBright("mysql数据库连接成功!"));
|
|
59
21
|
}
|
|
60
|
-
|
|
61
22
|
if (process.argv[2] == "-end") {
|
|
62
23
|
await sqlEnd();
|
|
63
24
|
console.info(c.greenBright("初始化成功!"));
|
|
@@ -16,7 +16,6 @@ import c from "ansi-colors";
|
|
|
16
16
|
// 根据数据库生成对应type,自动注入数据效验
|
|
17
17
|
(async () => {
|
|
18
18
|
let argv = process.argv.slice(2);
|
|
19
|
-
console.log(argv);
|
|
20
19
|
const formName = argv[0];
|
|
21
20
|
const wPath = argv[1];
|
|
22
21
|
// mysql初始化
|
|
@@ -57,53 +56,6 @@ import c from "ansi-colors";
|
|
|
57
56
|
path.join(__dirname, "../src/api", wPath, `/${formName}/set.ts`),
|
|
58
57
|
setStr
|
|
59
58
|
);
|
|
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
59
|
await sqlEnd();
|
|
107
|
-
// console.info(c.greenBright("获取类型成功!"));
|
|
108
60
|
}
|
|
109
61
|
})();
|
|
@@ -1,21 +1,10 @@
|
|
|
1
1
|
import "module-alias/register";
|
|
2
|
-
import {
|
|
3
|
-
checkSqlKey,
|
|
4
|
-
getRouteByApiSrc,
|
|
5
|
-
sqlInit,
|
|
6
|
-
getsqlFormData,
|
|
7
|
-
massql,
|
|
8
|
-
sqlEnd,
|
|
9
|
-
getsqlFormDataType,
|
|
10
|
-
} from "@/index";
|
|
2
|
+
import { checkSqlKey, sqlInit, sqlEnd, getsqlFormDataType } from "@/index";
|
|
11
3
|
import * as fs from "fs";
|
|
12
4
|
import * as path from "path";
|
|
13
|
-
|
|
14
|
-
import apiKey from "../config/api.key";
|
|
5
|
+
|
|
15
6
|
import config from "../config/config";
|
|
16
|
-
import masState from "../src/masState";
|
|
17
7
|
import c from "ansi-colors";
|
|
18
|
-
|
|
19
8
|
(async () => {
|
|
20
9
|
// mysql初始化
|
|
21
10
|
if (config.useSql) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { apiConfigTypeItem, masReq, masRes } from "@/index";
|
|
2
|
+
import masState from "@@/test/src/masState";
|
|
3
|
+
export const config: apiConfigTypeItem = {
|
|
4
|
+
name: "效验数组",
|
|
5
|
+
methods: "post",
|
|
6
|
+
validFormat: [Number],
|
|
7
|
+
};
|
|
8
|
+
export default async (req: masReq, res: masRes) => {
|
|
9
|
+
console.log(req.body);
|
|
10
|
+
res.return(req.body);
|
|
11
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createToken,
|
|
3
|
+
type apiConfigTypeItem,
|
|
4
|
+
type masReq,
|
|
5
|
+
type masRes,
|
|
6
|
+
} from "@/index";
|
|
7
|
+
export const config: apiConfigTypeItem = {
|
|
8
|
+
name: "权限测试接口",
|
|
9
|
+
methods: "post",
|
|
10
|
+
permission: ["admin"],
|
|
11
|
+
};
|
|
12
|
+
const token = createToken(
|
|
13
|
+
{ data: "这是一条token里的信息", permission: ["admin"] },
|
|
14
|
+
"8087"
|
|
15
|
+
);
|
|
16
|
+
// 开启这行拿到token,然后请求头里加上token即可正常访问
|
|
17
|
+
// console.log(token);
|
|
18
|
+
export default async (req: masReq, res: masRes) => {
|
|
19
|
+
console.log(res.token);
|
|
20
|
+
res.return("你是超级管理员!");
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// 也可以选择自己效验
|
|
24
|
+
// import { createToken, validToken } from "@/index";
|
|
25
|
+
// const token = createToken({
|
|
26
|
+
// time: 10,
|
|
27
|
+
// permission: ["admin", "www"],
|
|
28
|
+
// data: { a: 1, b: 2 },
|
|
29
|
+
// });
|
|
30
|
+
// setTimeout(() => {
|
|
31
|
+
// const tokenInfo = validToken({ token, permission: ["user", "admin"] });
|
|
32
|
+
// console.log(token);
|
|
33
|
+
// console.log(tokenInfo);
|
|
34
|
+
// }, 1000);
|