@rdyl/node-koa-controller 0.0.9 → 0.1.1
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/CHANGELOG.md +1 -1
- package/dist/controller/index.js +45 -6
- package/dist/controller/meta.js +35 -31
- package/dist/controller/utils.js +21 -0
- package/package.json +2 -1
- package/types/controller/meta.d.ts +14 -11
- package/types/controller/utils.d.ts +1 -0
package/CHANGELOG.md
CHANGED
package/dist/controller/index.js
CHANGED
|
@@ -81,6 +81,8 @@ var koa_router_1 = __importDefault(require("koa-router"));
|
|
|
81
81
|
var koa_static_1 = __importDefault(require("koa-static"));
|
|
82
82
|
var fs_1 = __importDefault(require("fs"));
|
|
83
83
|
var path_1 = require("path");
|
|
84
|
+
var chalk_1 = __importDefault(require("chalk"));
|
|
85
|
+
var utils_1 = require("./utils");
|
|
84
86
|
exports.ASTCache = {
|
|
85
87
|
prefix: "",
|
|
86
88
|
routes: [],
|
|
@@ -98,7 +100,7 @@ function RegisterASTCacheRoutes(router) {
|
|
|
98
100
|
exports.ASTCache.whitelist.push(path);
|
|
99
101
|
}
|
|
100
102
|
// @ts-ignore
|
|
101
|
-
router[method.toLowerCase()](path, function (_ctx) { return __awaiter(_this, void 0, void 0, function () {
|
|
103
|
+
router[method.toLowerCase()](path, function (_ctx, next) { return __awaiter(_this, void 0, void 0, function () {
|
|
102
104
|
var instance, _a;
|
|
103
105
|
return __generator(this, function (_b) {
|
|
104
106
|
switch (_b.label) {
|
|
@@ -122,12 +124,12 @@ function RegisterASTCacheRoutes(router) {
|
|
|
122
124
|
}
|
|
123
125
|
function useController(app, opts) {
|
|
124
126
|
return __awaiter(this, void 0, void 0, function () {
|
|
125
|
-
var root, _a, prefix, router, rootDir, initAST;
|
|
127
|
+
var root, _a, prefix, _b, logs, router, rootDir, initAST;
|
|
126
128
|
var _this = this;
|
|
127
|
-
return __generator(this, function (
|
|
128
|
-
switch (
|
|
129
|
+
return __generator(this, function (_c) {
|
|
130
|
+
switch (_c.label) {
|
|
129
131
|
case 0:
|
|
130
|
-
root = opts.root, _a = opts.prefix, prefix = _a === void 0 ? "" : _a;
|
|
132
|
+
root = opts.root, _a = opts.prefix, prefix = _a === void 0 ? "" : _a, _b = opts.logs, logs = _b === void 0 ? true : _b;
|
|
131
133
|
exports.ASTCache.prefix = prefix;
|
|
132
134
|
router = new koa_router_1.default();
|
|
133
135
|
rootDir = (0, path_1.resolve)(process.cwd(), root);
|
|
@@ -177,7 +179,7 @@ function useController(app, opts) {
|
|
|
177
179
|
}); };
|
|
178
180
|
return [4 /*yield*/, initAST(rootDir)];
|
|
179
181
|
case 1:
|
|
180
|
-
|
|
182
|
+
_c.sent();
|
|
181
183
|
// 注册路由
|
|
182
184
|
RegisterASTCacheRoutes(router);
|
|
183
185
|
if (opts.static) {
|
|
@@ -186,6 +188,43 @@ function useController(app, opts) {
|
|
|
186
188
|
}));
|
|
187
189
|
exports.ASTCache.whitelist.unshift(new RegExp("/".concat(opts.static)));
|
|
188
190
|
}
|
|
191
|
+
if (logs) {
|
|
192
|
+
app.use(function (ctx, next) { return __awaiter(_this, void 0, void 0, function () {
|
|
193
|
+
var start, duration, status_1, method, statusColor, dateTime;
|
|
194
|
+
return __generator(this, function (_a) {
|
|
195
|
+
switch (_a.label) {
|
|
196
|
+
case 0:
|
|
197
|
+
start = Date.now();
|
|
198
|
+
_a.label = 1;
|
|
199
|
+
case 1:
|
|
200
|
+
_a.trys.push([1, , 3, 4]);
|
|
201
|
+
return [4 /*yield*/, next()];
|
|
202
|
+
case 2:
|
|
203
|
+
_a.sent();
|
|
204
|
+
return [3 /*break*/, 4];
|
|
205
|
+
case 3:
|
|
206
|
+
duration = Date.now() - start;
|
|
207
|
+
status_1 = ctx.status;
|
|
208
|
+
method = ctx.method.padEnd(7);
|
|
209
|
+
statusColor = void 0;
|
|
210
|
+
if (status_1 >= 500)
|
|
211
|
+
statusColor = chalk_1.default.red;
|
|
212
|
+
else if (status_1 >= 400)
|
|
213
|
+
statusColor = chalk_1.default.yellow;
|
|
214
|
+
else if (status_1 >= 300)
|
|
215
|
+
statusColor = chalk_1.default.cyan;
|
|
216
|
+
else if (status_1 >= 200)
|
|
217
|
+
statusColor = chalk_1.default.green;
|
|
218
|
+
else
|
|
219
|
+
statusColor = chalk_1.default.gray;
|
|
220
|
+
dateTime = (0, utils_1.formatDateTime)();
|
|
221
|
+
console.log("- [".concat(dateTime, "] ").concat(chalk_1.default.magenta(method), " ").concat(ctx.url, " ").concat(statusColor(status_1), " ").concat(chalk_1.default.gray("".concat(duration, "ms"))));
|
|
222
|
+
return [7 /*endfinally*/];
|
|
223
|
+
case 4: return [2 /*return*/];
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}); });
|
|
227
|
+
}
|
|
189
228
|
app.use(router.routes()).use(router.allowedMethods());
|
|
190
229
|
return [2 /*return*/, Promise.resolve({
|
|
191
230
|
whitelist: exports.ASTCache.whitelist,
|
package/dist/controller/meta.js
CHANGED
|
@@ -49,19 +49,7 @@ var HttpMethod;
|
|
|
49
49
|
})(HttpMethod || (exports.HttpMethod = HttpMethod = {}));
|
|
50
50
|
var BaseController = /** @class */ (function () {
|
|
51
51
|
function BaseController(_ctx) {
|
|
52
|
-
var _this = this;
|
|
53
52
|
this._ctx = _ctx;
|
|
54
|
-
this.getPagedQuery = function (def) {
|
|
55
|
-
if (def === void 0) { def = {}; }
|
|
56
|
-
var query = _this.query;
|
|
57
|
-
var p = Number(query.page);
|
|
58
|
-
var s = Number(query.pageSize);
|
|
59
|
-
var _a = def.page, dPage = _a === void 0 ? 1 : _a, _b = def.pageSize, dPageSize = _b === void 0 ? 20 : _b;
|
|
60
|
-
return {
|
|
61
|
-
page: isNaN(p) ? dPage : p,
|
|
62
|
-
pageSize: isNaN(s) ? dPageSize : s,
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
53
|
}
|
|
66
54
|
Object.defineProperty(BaseController.prototype, "pathname", {
|
|
67
55
|
get: function () {
|
|
@@ -72,29 +60,42 @@ var BaseController = /** @class */ (function () {
|
|
|
72
60
|
});
|
|
73
61
|
Object.defineProperty(BaseController.prototype, "query", {
|
|
74
62
|
get: function () {
|
|
75
|
-
|
|
63
|
+
var values = this._ctx.query;
|
|
64
|
+
return {
|
|
65
|
+
values: values,
|
|
66
|
+
getPaged: function (def) {
|
|
67
|
+
if (def === void 0) { def = {}; }
|
|
68
|
+
var p = Number(values.page);
|
|
69
|
+
var s = Number(values.pageSize);
|
|
70
|
+
var _a = def.page, dPage = _a === void 0 ? 1 : _a, _b = def.pageSize, dPageSize = _b === void 0 ? 20 : _b;
|
|
71
|
+
return {
|
|
72
|
+
page: isNaN(p) ? dPage : p,
|
|
73
|
+
pageSize: isNaN(s) ? dPageSize : s,
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
getNumber: function (k) {
|
|
77
|
+
var n = values[k];
|
|
78
|
+
if (!n)
|
|
79
|
+
return null;
|
|
80
|
+
var value = Number(n);
|
|
81
|
+
if (isNaN(value))
|
|
82
|
+
return null;
|
|
83
|
+
return value;
|
|
84
|
+
},
|
|
85
|
+
getBool: function (k) {
|
|
86
|
+
var b = values[k];
|
|
87
|
+
if (!b)
|
|
88
|
+
return null;
|
|
89
|
+
if (b === "1" || b === "true" || b === "TRUE") {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
76
95
|
},
|
|
77
96
|
enumerable: false,
|
|
78
97
|
configurable: true
|
|
79
98
|
});
|
|
80
|
-
BaseController.prototype.getNumberQuery = function (k) {
|
|
81
|
-
var n = this.query[k];
|
|
82
|
-
if (!n)
|
|
83
|
-
return null;
|
|
84
|
-
var value = Number(n);
|
|
85
|
-
if (isNaN(value))
|
|
86
|
-
return null;
|
|
87
|
-
return value;
|
|
88
|
-
};
|
|
89
|
-
BaseController.prototype.getBoolQuery = function (k) {
|
|
90
|
-
var b = this.query[k];
|
|
91
|
-
if (!b)
|
|
92
|
-
return null;
|
|
93
|
-
if (b === "1" || b === "true" || b === "TRUE") {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
return false;
|
|
97
|
-
};
|
|
98
99
|
Object.defineProperty(BaseController.prototype, "params", {
|
|
99
100
|
get: function () {
|
|
100
101
|
return this._ctx.params || {};
|
|
@@ -123,6 +124,7 @@ var BaseController = /** @class */ (function () {
|
|
|
123
124
|
var _ctx = this._ctx;
|
|
124
125
|
var sendJson = function (body) {
|
|
125
126
|
_ctx.response.set("Content-Type", "application/json;charset=utf-8");
|
|
127
|
+
_ctx.response.status = body.code;
|
|
126
128
|
_ctx.body = body;
|
|
127
129
|
};
|
|
128
130
|
return {
|
|
@@ -130,9 +132,11 @@ var BaseController = /** @class */ (function () {
|
|
|
130
132
|
sendJson({ data: data, code: 200, msg: "ok" });
|
|
131
133
|
},
|
|
132
134
|
okCreated: function (data, msg) {
|
|
135
|
+
if (msg === void 0) { msg = "ok created"; }
|
|
133
136
|
sendJson({ code: 201, data: data, msg: msg });
|
|
134
137
|
},
|
|
135
138
|
okAccept: function (data, msg) {
|
|
139
|
+
if (msg === void 0) { msg = "ok accept"; }
|
|
136
140
|
sendJson({ code: 203, data: data, msg: msg });
|
|
137
141
|
},
|
|
138
142
|
parameterError: function (msg) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatDateTime = formatDateTime;
|
|
4
|
+
function formatDateTime(date, format) {
|
|
5
|
+
if (date === void 0) { date = new Date(); }
|
|
6
|
+
if (format === void 0) { format = "YYYY-MM-DD HH:mm:ss"; }
|
|
7
|
+
var pad = function (num) { return num.toString().padStart(2, "0"); };
|
|
8
|
+
var year = date.getFullYear();
|
|
9
|
+
var month = pad(date.getMonth() + 1);
|
|
10
|
+
var day = pad(date.getDate());
|
|
11
|
+
var hours = pad(date.getHours());
|
|
12
|
+
var minutes = pad(date.getMinutes());
|
|
13
|
+
var seconds = pad(date.getSeconds());
|
|
14
|
+
return format
|
|
15
|
+
.replace("YYYY", year.toString())
|
|
16
|
+
.replace("MM", month)
|
|
17
|
+
.replace("DD", day)
|
|
18
|
+
.replace("HH", hours)
|
|
19
|
+
.replace("mm", minutes)
|
|
20
|
+
.replace("ss", seconds);
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rdyl/node-koa-controller",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"@types/koa": "^2.15.0",
|
|
22
22
|
"@types/koa-router": "^7.4.8",
|
|
23
23
|
"@types/koa-static": "^4.0.4",
|
|
24
|
+
"chalk": "^5.4.1",
|
|
24
25
|
"koa": "^2.15.3",
|
|
25
26
|
"koa-router": "^13.0.1",
|
|
26
27
|
"koa-static": "^5.0.0"
|
|
@@ -11,16 +11,18 @@ export declare class BaseController {
|
|
|
11
11
|
_ctx: ParameterizedContext<any, IRouterParamContext<any, {}>, any>;
|
|
12
12
|
constructor(_ctx: ParameterizedContext<any, IRouterParamContext<any, {}>, any>);
|
|
13
13
|
get pathname(): string;
|
|
14
|
-
get query():
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
get query(): {
|
|
15
|
+
values: import("querystring").ParsedUrlQuery;
|
|
16
|
+
getPaged(def?: Partial<{
|
|
17
|
+
page: number;
|
|
18
|
+
pageSize: number;
|
|
19
|
+
}>): {
|
|
20
|
+
page: number;
|
|
21
|
+
pageSize: number;
|
|
22
|
+
};
|
|
23
|
+
getNumber(k: string): number | null;
|
|
24
|
+
getBool(k: string): boolean | null;
|
|
21
25
|
};
|
|
22
|
-
getNumberQuery(k: string): number | null;
|
|
23
|
-
getBoolQuery(k: string): boolean | null;
|
|
24
26
|
get params(): Record<string, string>;
|
|
25
27
|
get body(): any;
|
|
26
28
|
files(): Promise<any>;
|
|
@@ -42,14 +44,15 @@ export declare const Delete: (url: string, isPublic?: boolean) => (target: any,
|
|
|
42
44
|
export declare const Put: (url: string, isPublic?: boolean) => (target: any, name: string) => void;
|
|
43
45
|
export declare const Patch: (url: string, isPublic?: boolean) => (target: any, name: string) => void;
|
|
44
46
|
export interface ResData<T> {
|
|
45
|
-
msg: string;
|
|
46
47
|
code: number;
|
|
47
|
-
|
|
48
|
+
msg: string;
|
|
49
|
+
data?: T | null;
|
|
48
50
|
}
|
|
49
51
|
export interface UseProps {
|
|
50
52
|
root: string;
|
|
51
53
|
prefix?: string;
|
|
52
54
|
static?: string;
|
|
55
|
+
logs?: boolean;
|
|
53
56
|
}
|
|
54
57
|
export interface ASTCacheHandler {
|
|
55
58
|
url: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatDateTime(date?: Date, format?: string): string;
|