@rdyl/node-koa-controller 0.0.4 → 0.0.6

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.0.4 (2025-03-28)
1
+ ## 0.0.6 (2025-04-02)
2
2
 
3
3
 
4
4
 
@@ -78,6 +78,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
78
78
  exports.ASTCache = void 0;
79
79
  exports.useController = useController;
80
80
  var koa_router_1 = __importDefault(require("koa-router"));
81
+ var koa_static_1 = __importDefault(require("koa-static"));
81
82
  var fs_1 = __importDefault(require("fs"));
82
83
  var path_1 = require("path");
83
84
  exports.ASTCache = {
@@ -86,6 +87,7 @@ exports.ASTCache = {
86
87
  whitelist: [],
87
88
  };
88
89
  function RegisterASTCacheRoutes(router) {
90
+ var _this = this;
89
91
  exports.ASTCache.routes.forEach(function (route) {
90
92
  var handlers = route.handlers, prefix = route.prefix, Instance = route.Instance;
91
93
  handlers.forEach(function (handler) {
@@ -96,26 +98,36 @@ function RegisterASTCacheRoutes(router) {
96
98
  exports.ASTCache.whitelist.push(path);
97
99
  }
98
100
  // @ts-ignore
99
- router[method.toLowerCase()](path, function (_ctx) {
100
- try {
101
- var instance = new Instance(_ctx);
102
- instance[name]();
103
- }
104
- catch (_a) {
105
- console.log(path + ":配置错误!");
106
- }
107
- });
101
+ router[method.toLowerCase()](path, function (_ctx) { return __awaiter(_this, void 0, void 0, function () {
102
+ var instance, _a;
103
+ return __generator(this, function (_b) {
104
+ switch (_b.label) {
105
+ case 0:
106
+ _b.trys.push([0, 2, , 3]);
107
+ instance = new Instance(_ctx);
108
+ return [4 /*yield*/, instance[name]()];
109
+ case 1:
110
+ _b.sent();
111
+ return [3 /*break*/, 3];
112
+ case 2:
113
+ _a = _b.sent();
114
+ console.log(path + ":配置错误!");
115
+ return [3 /*break*/, 3];
116
+ case 3: return [2 /*return*/];
117
+ }
118
+ });
119
+ }); });
108
120
  });
109
121
  });
110
122
  }
111
- function useController(app_1, _a) {
112
- return __awaiter(this, arguments, void 0, function (app, _b) {
113
- var router, rootDir, initAST;
123
+ function useController(app, opts) {
124
+ return __awaiter(this, void 0, void 0, function () {
125
+ var root, _a, prefix, router, rootDir, initAST;
114
126
  var _this = this;
115
- var root = _b.root, _c = _b.prefix, prefix = _c === void 0 ? "" : _c;
116
- return __generator(this, function (_d) {
117
- switch (_d.label) {
127
+ return __generator(this, function (_b) {
128
+ switch (_b.label) {
118
129
  case 0:
130
+ root = opts.root, _a = opts.prefix, prefix = _a === void 0 ? "" : _a;
119
131
  exports.ASTCache.prefix = prefix;
120
132
  router = new koa_router_1.default();
121
133
  rootDir = (0, path_1.resolve)(process.cwd(), root);
@@ -165,9 +177,15 @@ function useController(app_1, _a) {
165
177
  }); };
166
178
  return [4 /*yield*/, initAST(rootDir)];
167
179
  case 1:
168
- _d.sent();
180
+ _b.sent();
169
181
  // 注册路由
170
182
  RegisterASTCacheRoutes(router);
183
+ if (opts.static) {
184
+ app.use((0, koa_static_1.default)(process.cwd(), {
185
+ root: opts.static,
186
+ }));
187
+ exports.ASTCache.whitelist.unshift(new RegExp("/".concat(opts.static)));
188
+ }
171
189
  app.use(router.routes()).use(router.allowedMethods());
172
190
  return [2 /*return*/, Promise.resolve({
173
191
  whitelist: exports.ASTCache.whitelist,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdyl/node-koa-controller",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -20,15 +20,17 @@
20
20
  "dependencies": {
21
21
  "@types/koa": "^2.15.0",
22
22
  "@types/koa-router": "^7.4.8",
23
+ "@types/koa-static": "^4.0.4",
23
24
  "koa": "^2.15.3",
24
- "koa-router": "^13.0.1"
25
+ "koa-router": "^13.0.1",
26
+ "koa-static": "^5.0.0"
25
27
  },
26
28
  "devDependencies": {
27
- "@types/node": "^22.9.0",
28
- "copyfiles": "^2.4.1",
29
- "typescript": "^5.6.3",
30
29
  "@commitlint/cli": "^19.6.1",
31
30
  "@commitlint/config-conventional": "^19.6.0",
32
- "conventional-changelog-cli": "^5.0.0"
31
+ "@types/node": "^22.9.0",
32
+ "conventional-changelog-cli": "^5.0.0",
33
+ "copyfiles": "^2.4.1",
34
+ "typescript": "^5.6.3"
33
35
  }
34
- }
36
+ }
@@ -1,7 +1,7 @@
1
1
  import Koa from "koa";
2
- import { ASTCacheProp, InitOpts } from "./meta";
2
+ import { ASTCacheProp, UseProps } from "./meta";
3
3
  export declare const ASTCache: ASTCacheProp;
4
- export declare function useController(app: Koa, { root, prefix }: InitOpts): Promise<{
5
- whitelist: string[];
4
+ export declare function useController(app: Koa, opts: UseProps): Promise<{
5
+ whitelist: (string | RegExp)[];
6
6
  }>;
7
7
  export * from "./meta";
@@ -37,9 +37,10 @@ export interface ResData<T> {
37
37
  code: number;
38
38
  data: T;
39
39
  }
40
- export interface InitOpts {
40
+ export interface UseProps {
41
41
  root: string;
42
42
  prefix?: string;
43
+ static?: string;
43
44
  }
44
45
  export interface ASTCacheHandler {
45
46
  url: string;
@@ -56,5 +57,5 @@ export interface ASTCacheRoute {
56
57
  export interface ASTCacheProp {
57
58
  prefix: string;
58
59
  routes: ASTCacheRoute[];
59
- whitelist: string[];
60
+ whitelist: (string | RegExp)[];
60
61
  }