@zenweb/controller
Advanced tools
+3
-0
| # Changelog | ||
| ## 3.11.0 | ||
| - 新增 Core.controllerRegister | ||
| ## 3.10.0 | ||
@@ -4,0 +7,0 @@ - discoverPaths 支持使用 "./" 开头的相对路径 |
+23
-1
@@ -59,4 +59,26 @@ import { Context, Middleware, SetupHelper } from '@zenweb/core'; | ||
| * @param target 控制器 | ||
| * @returns | ||
| * - true: 完成设置路由 | ||
| * - false: 没有找到有效的 mapping 方法 | ||
| */ | ||
| export declare function addToRouter(setup: SetupHelper, target: any): void; | ||
| export declare function addToRouter(setup: SetupHelper, target: any): boolean; | ||
| /** | ||
| * 控制器注册器 | ||
| */ | ||
| export declare class ControllerRegister { | ||
| private setup; | ||
| /** | ||
| * 已注册的控制器类 | ||
| */ | ||
| private controllers; | ||
| constructor(setup: SetupHelper); | ||
| /** | ||
| * 注册控制器 | ||
| * @param target 控制器类 | ||
| * @returns | ||
| * - true 注册成功 | ||
| * - false 无效控制器 | ||
| */ | ||
| register(target: Function): boolean; | ||
| } | ||
| export {}; |
+36
-3
@@ -12,3 +12,3 @@ "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.addToRouter = exports.controller = exports.mapping = exports.Controller = void 0; | ||
| exports.ControllerRegister = exports.addToRouter = exports.controller = exports.mapping = exports.Controller = void 0; | ||
| /// <reference types="@zenweb/result" /> | ||
@@ -101,2 +101,5 @@ const core_1 = require("@zenweb/core"); | ||
| * @param target 控制器 | ||
| * @returns | ||
| * - true: 完成设置路由 | ||
| * - false: 没有找到有效的 mapping 方法 | ||
| */ | ||
@@ -108,3 +111,3 @@ function addToRouter(setup, target) { | ||
| const option = controllerDecorator.getValue(target); | ||
| setup.debug('controller option: %o', option); | ||
| setup.debug('@controller(%o)', option); | ||
| const _router = new router_1.Router(option); | ||
@@ -116,3 +119,3 @@ if (option && option.middleware) { | ||
| for (const item of mappingList) { | ||
| setup.debug('controller mapping: %o', item); | ||
| setup.debug('@mapping(%o)', item); | ||
| const middlewares = [ | ||
@@ -142,2 +145,3 @@ ...item.middleware, | ||
| setup.core.router.use(_router.routes()); | ||
| return true; | ||
| } | ||
@@ -147,3 +151,32 @@ else { | ||
| } | ||
| return false; | ||
| } | ||
| exports.addToRouter = addToRouter; | ||
| /** | ||
| * 控制器注册器 | ||
| */ | ||
| class ControllerRegister { | ||
| constructor(setup) { | ||
| this.setup = setup; | ||
| /** | ||
| * 已注册的控制器类 | ||
| */ | ||
| this.controllers = []; | ||
| } | ||
| /** | ||
| * 注册控制器 | ||
| * @param target 控制器类 | ||
| * @returns | ||
| * - true 注册成功 | ||
| * - false 无效控制器 | ||
| */ | ||
| register(target) { | ||
| this.setup.debug('class: %o', target); | ||
| if (addToRouter(this.setup, target)) { | ||
| this.controllers.push(target); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| exports.ControllerRegister = ControllerRegister; |
+9
-0
| import { SetupFunction } from '@zenweb/core'; | ||
| import { ControllerRegister } from './controller'; | ||
| export { Controller, controller, mapping } from './controller'; | ||
@@ -7,1 +8,9 @@ export interface ControllerOption { | ||
| export default function setup(opt?: ControllerOption): SetupFunction; | ||
| declare module '@zenweb/core' { | ||
| interface Core { | ||
| /** | ||
| * 控制器注册器 | ||
| */ | ||
| controllerRegister: ControllerRegister; | ||
| } | ||
| } |
+3
-2
@@ -20,2 +20,4 @@ "use strict"; | ||
| setup.assertModuleExists('inject'); | ||
| const controllerRegister = new controller_1.ControllerRegister(setup); | ||
| setup.defineCoreProperty('controllerRegister', { value: controllerRegister }); | ||
| if (option.discoverPaths && option.discoverPaths.length) { | ||
@@ -31,4 +33,3 @@ for (let p of option.discoverPaths) { | ||
| if (typeof i === 'function') { | ||
| setup.debug('class: %o', i); | ||
| (0, controller_1.addToRouter)(setup, i); | ||
| controllerRegister.register(i); | ||
| } | ||
@@ -35,0 +36,0 @@ } |
+1
-1
| { | ||
| "name": "@zenweb/controller", | ||
| "version": "3.10.0", | ||
| "version": "3.11.0", | ||
| "description": "Zenweb Controller module", | ||
@@ -5,0 +5,0 @@ "exports": "./dist/index.js", |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
12660
14.69%314
26.1%