@rdyl/node-koa-controller 0.1.2 → 0.1.4
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 -18
- package/dist/controller/meta.js +1 -0
- package/package.json +2 -1
- package/types/controller/meta.d.ts +8 -1
- package/types/type.d.ts +1 -0
package/CHANGELOG.md
CHANGED
package/dist/controller/index.js
CHANGED
|
@@ -79,43 +79,59 @@ exports.ASTCache = void 0;
|
|
|
79
79
|
exports.useController = useController;
|
|
80
80
|
var koa_router_1 = __importDefault(require("koa-router"));
|
|
81
81
|
var koa_static_1 = __importDefault(require("koa-static"));
|
|
82
|
+
var koa_body_1 = require("koa-body");
|
|
82
83
|
var fs_1 = __importDefault(require("fs"));
|
|
83
|
-
var path_1 = require("path");
|
|
84
84
|
var chalk_1 = __importDefault(require("chalk"));
|
|
85
|
+
var path_1 = require("path");
|
|
85
86
|
var utils_1 = require("./utils");
|
|
86
87
|
exports.ASTCache = {
|
|
87
88
|
prefix: "",
|
|
88
89
|
routes: [],
|
|
89
90
|
whitelist: [],
|
|
90
91
|
};
|
|
91
|
-
function RegisterASTCacheRoutes(router) {
|
|
92
|
+
function RegisterASTCacheRoutes(router, useJwt) {
|
|
92
93
|
var _this = this;
|
|
93
94
|
exports.ASTCache.routes.forEach(function (route) {
|
|
94
95
|
var handlers = route.handlers, prefix = route.prefix, Instance = route.Instance;
|
|
95
96
|
handlers.forEach(function (handler) {
|
|
96
97
|
var method = handler.method, url = handler.url, name = handler.name;
|
|
97
98
|
var path = (0, path_1.join)("/" + exports.ASTCache.prefix, prefix, url);
|
|
98
|
-
var isPublic = route.isPublic ||
|
|
99
|
+
var isPublic = route.isPublic || handler.isPublic;
|
|
99
100
|
if (isPublic) {
|
|
100
101
|
exports.ASTCache.whitelist.push(path);
|
|
101
102
|
}
|
|
102
103
|
// @ts-ignore
|
|
103
|
-
router[method.toLowerCase()](path, function (_ctx
|
|
104
|
-
var instance, _a;
|
|
104
|
+
router[method.toLowerCase()](path, function (_ctx) { return __awaiter(_this, void 0, void 0, function () {
|
|
105
|
+
var instance, token, jwt, _a;
|
|
105
106
|
return __generator(this, function (_b) {
|
|
106
107
|
switch (_b.label) {
|
|
107
108
|
case 0:
|
|
108
|
-
_b.trys.push([0,
|
|
109
|
+
_b.trys.push([0, 4, , 5]);
|
|
109
110
|
instance = new Instance(_ctx);
|
|
110
|
-
return [
|
|
111
|
+
if (!useJwt) return [3 /*break*/, 2];
|
|
112
|
+
token = _ctx.request.headers[useJwt.name] || "";
|
|
113
|
+
return [4 /*yield*/, useJwt.get(token)];
|
|
111
114
|
case 1:
|
|
115
|
+
jwt = _b.sent();
|
|
116
|
+
if (!jwt && !isPublic) {
|
|
117
|
+
return [2 /*return*/, instance.status.unauthorized()];
|
|
118
|
+
}
|
|
119
|
+
instance.jwt = jwt;
|
|
120
|
+
_b.label = 2;
|
|
121
|
+
case 2: return [4 /*yield*/, Promise.resolve(instance[name]())];
|
|
122
|
+
case 3:
|
|
112
123
|
_b.sent();
|
|
113
|
-
return [3 /*break*/,
|
|
114
|
-
case
|
|
124
|
+
return [3 /*break*/, 5];
|
|
125
|
+
case 4:
|
|
115
126
|
_a = _b.sent();
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
127
|
+
_ctx.response.set("Content-Type", "application/json;charset=utf-8");
|
|
128
|
+
_ctx.response.status = 500;
|
|
129
|
+
_ctx.body = {
|
|
130
|
+
code: 500,
|
|
131
|
+
msg: "未知错误",
|
|
132
|
+
};
|
|
133
|
+
return [3 /*break*/, 5];
|
|
134
|
+
case 5: return [2 /*return*/];
|
|
119
135
|
}
|
|
120
136
|
});
|
|
121
137
|
}); });
|
|
@@ -124,12 +140,12 @@ function RegisterASTCacheRoutes(router) {
|
|
|
124
140
|
}
|
|
125
141
|
function useController(app, opts) {
|
|
126
142
|
return __awaiter(this, void 0, void 0, function () {
|
|
127
|
-
var root, _a, prefix, _b, logs, router, rootDir, initAST;
|
|
143
|
+
var root, _a, prefix, _b, logs, formidable, useJwt, router, rootDir, initAST;
|
|
128
144
|
var _this = this;
|
|
129
145
|
return __generator(this, function (_c) {
|
|
130
146
|
switch (_c.label) {
|
|
131
147
|
case 0:
|
|
132
|
-
root = opts.root, _a = opts.prefix, prefix = _a === void 0 ? "" : _a, _b = opts.logs, logs = _b === void 0 ? true : _b;
|
|
148
|
+
root = opts.root, _a = opts.prefix, prefix = _a === void 0 ? "" : _a, _b = opts.logs, logs = _b === void 0 ? true : _b, formidable = opts.formidable, useJwt = opts.useJwt;
|
|
133
149
|
exports.ASTCache.prefix = prefix;
|
|
134
150
|
router = new koa_router_1.default();
|
|
135
151
|
rootDir = (0, path_1.resolve)(process.cwd(), root);
|
|
@@ -181,16 +197,20 @@ function useController(app, opts) {
|
|
|
181
197
|
case 1:
|
|
182
198
|
_c.sent();
|
|
183
199
|
// 注册路由
|
|
184
|
-
RegisterASTCacheRoutes(router);
|
|
200
|
+
RegisterASTCacheRoutes(router, useJwt);
|
|
185
201
|
if (opts.static) {
|
|
186
202
|
app.use((0, koa_static_1.default)(process.cwd(), {
|
|
187
203
|
root: opts.static,
|
|
188
204
|
}));
|
|
189
205
|
exports.ASTCache.whitelist.unshift(new RegExp("/".concat(opts.static)));
|
|
190
206
|
}
|
|
207
|
+
app.use((0, koa_body_1.koaBody)({
|
|
208
|
+
multipart: true,
|
|
209
|
+
formidable: formidable,
|
|
210
|
+
}));
|
|
191
211
|
if (logs) {
|
|
192
212
|
app.use(function (ctx, next) { return __awaiter(_this, void 0, void 0, function () {
|
|
193
|
-
var start, duration, status_1, method, statusColor, dateTime;
|
|
213
|
+
var start, duration, status_1, method, statusColor, dateTime, dText;
|
|
194
214
|
return __generator(this, function (_a) {
|
|
195
215
|
switch (_a.label) {
|
|
196
216
|
case 0:
|
|
@@ -205,7 +225,7 @@ function useController(app, opts) {
|
|
|
205
225
|
case 3:
|
|
206
226
|
duration = Date.now() - start;
|
|
207
227
|
status_1 = ctx.status;
|
|
208
|
-
method = ctx.method
|
|
228
|
+
method = ctx.method;
|
|
209
229
|
statusColor = void 0;
|
|
210
230
|
if (status_1 >= 500)
|
|
211
231
|
statusColor = chalk_1.default.red;
|
|
@@ -218,7 +238,14 @@ function useController(app, opts) {
|
|
|
218
238
|
else
|
|
219
239
|
statusColor = chalk_1.default.gray;
|
|
220
240
|
dateTime = (0, utils_1.formatDateTime)();
|
|
221
|
-
|
|
241
|
+
dText = chalk_1.default.gray("".concat(duration, "ms"));
|
|
242
|
+
if (duration > 5000) {
|
|
243
|
+
dText = chalk_1.default.red("".concat(duration, "ms"));
|
|
244
|
+
}
|
|
245
|
+
else if (duration > 1000) {
|
|
246
|
+
dText = chalk_1.default.yellow("".concat(duration, "ms"));
|
|
247
|
+
}
|
|
248
|
+
console.log("- [".concat(dateTime, " | ").concat(dText, "] ").concat(chalk_1.default.magenta(method), " ").concat(ctx.url, " ").concat(statusColor(status_1)));
|
|
222
249
|
return [7 /*endfinally*/];
|
|
223
250
|
case 4: return [2 /*return*/];
|
|
224
251
|
}
|
package/dist/controller/meta.js
CHANGED
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.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"@types/koa-static": "^4.0.4",
|
|
24
24
|
"chalk": "^5.4.1",
|
|
25
25
|
"koa": "^2.15.3",
|
|
26
|
+
"koa-body": "^6.0.1",
|
|
26
27
|
"koa-router": "^13.0.1",
|
|
27
28
|
"koa-static": "^5.0.0"
|
|
28
29
|
},
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ParameterizedContext } from "koa";
|
|
2
2
|
import { IRouterParamContext } from "koa-router";
|
|
3
|
+
import { ExtendedFormidableOptions } from "koa-body";
|
|
3
4
|
export declare enum HttpMethod {
|
|
4
5
|
GET = "GET",
|
|
5
6
|
POST = "POST",
|
|
@@ -9,6 +10,7 @@ export declare enum HttpMethod {
|
|
|
9
10
|
}
|
|
10
11
|
export declare class BaseController {
|
|
11
12
|
_ctx: ParameterizedContext<any, IRouterParamContext<any, {}>, any>;
|
|
13
|
+
jwt: UseJwtValue;
|
|
12
14
|
constructor(_ctx: ParameterizedContext<any, IRouterParamContext<any, {}>, any>);
|
|
13
15
|
get pathname(): string;
|
|
14
16
|
get query(): {
|
|
@@ -25,7 +27,7 @@ export declare class BaseController {
|
|
|
25
27
|
};
|
|
26
28
|
get params(): Record<string, string>;
|
|
27
29
|
get body(): any;
|
|
28
|
-
files(): Promise<
|
|
30
|
+
files(): Promise<import("formidable").Files | undefined>;
|
|
29
31
|
get status(): {
|
|
30
32
|
ok(data?: unknown): void;
|
|
31
33
|
okCreated(data: unknown, msg?: string): void;
|
|
@@ -53,6 +55,11 @@ export interface UseProps {
|
|
|
53
55
|
prefix?: string;
|
|
54
56
|
static?: string;
|
|
55
57
|
logs?: boolean;
|
|
58
|
+
formidable?: ExtendedFormidableOptions;
|
|
59
|
+
useJwt?: {
|
|
60
|
+
name: string;
|
|
61
|
+
get(n: string): Promise<UseJwtValue>;
|
|
62
|
+
};
|
|
56
63
|
}
|
|
57
64
|
export interface ASTCacheHandler {
|
|
58
65
|
url: string;
|
package/types/type.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
type UseJwtValue = Record<string, unknown>;
|