@rdyl/node-koa-controller 0.3.1 → 0.3.3
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/cache.js +1 -0
- package/dist/index.js +1 -3
- package/dist/meta.js +8 -4
- package/dist/utils.js +26 -41
- package/package.json +1 -1
- package/types/cache.d.ts +1 -0
- package/types/utils.d.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cache.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -85,9 +85,7 @@ function useApiController(app, opts) {
|
|
|
85
85
|
multipart: true,
|
|
86
86
|
formidable: formidable,
|
|
87
87
|
}));
|
|
88
|
-
|
|
89
|
-
app.use(utils_1.RunningLogsHandler);
|
|
90
|
-
}
|
|
88
|
+
cache_1.default.logs = logs;
|
|
91
89
|
app.use(router.routes()).use(router.allowedMethods());
|
|
92
90
|
ip = (0, utils_1.getIPAdress)();
|
|
93
91
|
return [2 /*return*/, {
|
package/dist/meta.js
CHANGED
|
@@ -42,6 +42,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
42
42
|
exports.Patch = exports.Put = exports.Delete = exports.Post = exports.Get = exports.BaseController = exports.HttpMethod = void 0;
|
|
43
43
|
exports.Controller = Controller;
|
|
44
44
|
var cache_1 = __importDefault(require("./cache"));
|
|
45
|
+
var utils_1 = require("./utils");
|
|
45
46
|
var HttpMethod;
|
|
46
47
|
(function (HttpMethod) {
|
|
47
48
|
HttpMethod["GET"] = "GET";
|
|
@@ -128,11 +129,14 @@ var BaseController = /** @class */ (function () {
|
|
|
128
129
|
};
|
|
129
130
|
Object.defineProperty(BaseController.prototype, "status", {
|
|
130
131
|
get: function () {
|
|
131
|
-
var
|
|
132
|
+
var _this = this;
|
|
132
133
|
var sendJson = function (body) {
|
|
133
|
-
_ctx.response.set("Content-Type", "application/json;charset=utf-8");
|
|
134
|
-
_ctx.response.status = body.code;
|
|
135
|
-
_ctx.body = body;
|
|
134
|
+
_this._ctx.response.set("Content-Type", "application/json;charset=utf-8");
|
|
135
|
+
_this._ctx.response.status = body.code;
|
|
136
|
+
_this._ctx.body = body;
|
|
137
|
+
if (cache_1.default.logs) {
|
|
138
|
+
(0, utils_1.LogsHandler)(_this._ctx);
|
|
139
|
+
}
|
|
136
140
|
};
|
|
137
141
|
return {
|
|
138
142
|
ok: function (data) {
|
package/dist/utils.js
CHANGED
|
@@ -41,7 +41,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
42
|
exports.formatDateTime = formatDateTime;
|
|
43
43
|
exports.getIPAdress = getIPAdress;
|
|
44
|
-
exports.
|
|
44
|
+
exports.LogsHandler = LogsHandler;
|
|
45
45
|
exports.ServerLaunchLogs = ServerLaunchLogs;
|
|
46
46
|
var os_1 = __importDefault(require("os"));
|
|
47
47
|
var path_1 = require("path");
|
|
@@ -82,49 +82,34 @@ function getIPAdress() {
|
|
|
82
82
|
}
|
|
83
83
|
return "127.0.0.1";
|
|
84
84
|
}
|
|
85
|
-
function
|
|
85
|
+
function LogsHandler(ctx) {
|
|
86
86
|
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
-
var start, duration,
|
|
87
|
+
var start, duration, status, method, statusColor, dateTime, dText;
|
|
88
88
|
return __generator(this, function (_a) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
else
|
|
108
|
-
statusColor = chalk_1.default.gray;
|
|
109
|
-
dateTime = formatDateTime();
|
|
110
|
-
dText = chalk_1.default.gray("".concat(duration, "ms"));
|
|
111
|
-
if (duration > 5000) {
|
|
112
|
-
dText = chalk_1.default.red("".concat(duration, "ms"));
|
|
113
|
-
}
|
|
114
|
-
else if (duration > 1000) {
|
|
115
|
-
dText = chalk_1.default.yellow("".concat(duration, "ms"));
|
|
116
|
-
}
|
|
117
|
-
console.log("- [".concat(dateTime, " | ").concat(dText, "] ").concat(chalk_1.default.magenta(method), " ").concat(ctx.url, " ").concat(statusColor(status_1)));
|
|
118
|
-
return [4 /*yield*/, next()];
|
|
119
|
-
case 2:
|
|
120
|
-
_a.sent();
|
|
121
|
-
return [3 /*break*/, 4];
|
|
122
|
-
case 3:
|
|
123
|
-
err_1 = _a.sent();
|
|
124
|
-
console.log(err_1);
|
|
125
|
-
return [3 /*break*/, 4];
|
|
126
|
-
case 4: return [2 /*return*/];
|
|
89
|
+
start = Date.now();
|
|
90
|
+
duration = Date.now() - start;
|
|
91
|
+
status = ctx.response.status;
|
|
92
|
+
method = ctx.method;
|
|
93
|
+
if (status >= 500)
|
|
94
|
+
statusColor = chalk_1.default.red;
|
|
95
|
+
else if (status >= 400)
|
|
96
|
+
statusColor = chalk_1.default.yellow;
|
|
97
|
+
else if (status >= 300)
|
|
98
|
+
statusColor = chalk_1.default.cyan;
|
|
99
|
+
else if (status >= 200)
|
|
100
|
+
statusColor = chalk_1.default.green;
|
|
101
|
+
else
|
|
102
|
+
statusColor = chalk_1.default.gray;
|
|
103
|
+
dateTime = formatDateTime();
|
|
104
|
+
dText = chalk_1.default.gray("".concat(duration, "ms"));
|
|
105
|
+
if (duration > 5000) {
|
|
106
|
+
dText = chalk_1.default.red("".concat(duration, "ms"));
|
|
127
107
|
}
|
|
108
|
+
else if (duration > 1000) {
|
|
109
|
+
dText = chalk_1.default.yellow("".concat(duration, "ms"));
|
|
110
|
+
}
|
|
111
|
+
console.log("- [".concat(dateTime, " | ").concat(dText, "] ").concat(chalk_1.default.magenta(method), " ").concat(ctx.url, " ").concat(statusColor(status)));
|
|
112
|
+
return [2 /*return*/];
|
|
128
113
|
});
|
|
129
114
|
});
|
|
130
115
|
}
|
package/package.json
CHANGED
package/types/cache.d.ts
CHANGED
package/types/utils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { KoaCtx } from "./meta";
|
|
2
2
|
export declare function formatDateTime(date?: Date, format?: string): string;
|
|
3
3
|
export declare function getIPAdress(): string;
|
|
4
|
-
export declare function
|
|
4
|
+
export declare function LogsHandler(ctx: KoaCtx): Promise<void>;
|
|
5
5
|
export declare function ServerLaunchLogs(config: {
|
|
6
6
|
port: number;
|
|
7
7
|
prefix: string;
|