@rdyl/node-koa-controller 0.1.0 → 0.1.2
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
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.gray("".concat(duration, "ms")), "] ").concat(chalk_1.default.magenta(method)).concat(ctx.url, " ").concat(statusColor(status_1)));
|
|
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
|
@@ -124,6 +124,7 @@ var BaseController = /** @class */ (function () {
|
|
|
124
124
|
var _ctx = this._ctx;
|
|
125
125
|
var sendJson = function (body) {
|
|
126
126
|
_ctx.response.set("Content-Type", "application/json;charset=utf-8");
|
|
127
|
+
_ctx.response.status = body.code;
|
|
127
128
|
_ctx.body = body;
|
|
128
129
|
};
|
|
129
130
|
return {
|
|
@@ -131,9 +132,11 @@ var BaseController = /** @class */ (function () {
|
|
|
131
132
|
sendJson({ data: data, code: 200, msg: "ok" });
|
|
132
133
|
},
|
|
133
134
|
okCreated: function (data, msg) {
|
|
135
|
+
if (msg === void 0) { msg = "ok created"; }
|
|
134
136
|
sendJson({ code: 201, data: data, msg: msg });
|
|
135
137
|
},
|
|
136
138
|
okAccept: function (data, msg) {
|
|
139
|
+
if (msg === void 0) { msg = "ok accept"; }
|
|
137
140
|
sendJson({ code: 203, data: data, msg: msg });
|
|
138
141
|
},
|
|
139
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.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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"
|
|
@@ -44,14 +44,15 @@ export declare const Delete: (url: string, isPublic?: boolean) => (target: any,
|
|
|
44
44
|
export declare const Put: (url: string, isPublic?: boolean) => (target: any, name: string) => void;
|
|
45
45
|
export declare const Patch: (url: string, isPublic?: boolean) => (target: any, name: string) => void;
|
|
46
46
|
export interface ResData<T> {
|
|
47
|
-
msg: string;
|
|
48
47
|
code: number;
|
|
49
|
-
|
|
48
|
+
msg: string;
|
|
49
|
+
data?: T | null;
|
|
50
50
|
}
|
|
51
51
|
export interface UseProps {
|
|
52
52
|
root: string;
|
|
53
53
|
prefix?: string;
|
|
54
54
|
static?: string;
|
|
55
|
+
logs?: boolean;
|
|
55
56
|
}
|
|
56
57
|
export interface ASTCacheHandler {
|
|
57
58
|
url: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatDateTime(date?: Date, format?: string): string;
|