@rdyl/node-koa-controller 0.1.3 → 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 CHANGED
@@ -1,4 +1,4 @@
1
- ## 0.1.3 (2025-04-07)
1
+ ## 0.1.4 (2025-04-10)
2
2
 
3
3
 
4
4
 
@@ -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 || 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, next) { return __awaiter(_this, void 0, void 0, function () {
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, 2, , 3]);
109
+ _b.trys.push([0, 4, , 5]);
109
110
  instance = new Instance(_ctx);
110
- return [4 /*yield*/, Promise.resolve(instance[name]())];
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*/, 3];
114
- case 2:
124
+ return [3 /*break*/, 5];
125
+ case 4:
115
126
  _a = _b.sent();
116
- console.log(path + ":配置错误!");
117
- return [3 /*break*/, 3];
118
- case 3: return [2 /*return*/];
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,13 +197,17 @@ 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
213
  var start, duration, status_1, method, statusColor, dateTime, dText;
@@ -50,6 +50,7 @@ var HttpMethod;
50
50
  var BaseController = /** @class */ (function () {
51
51
  function BaseController(_ctx) {
52
52
  this._ctx = _ctx;
53
+ this.jwt = {};
53
54
  }
54
55
  Object.defineProperty(BaseController.prototype, "pathname", {
55
56
  get: function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdyl/node-koa-controller",
3
- "version": "0.1.3",
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<any>;
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;
@@ -0,0 +1 @@
1
+ type UseJwtValue = Record<string, unknown>;