@zenweb/controller
Advanced tools
@@ -43,2 +43,3 @@ import { Middleware } from '@zenweb/core'; | ||
| getValue: (target: Object) => ControllerOption | undefined; | ||
| getParams: (target: Object) => unknown[]; | ||
| }; | ||
@@ -45,0 +46,0 @@ /** |
+2
-2
@@ -85,5 +85,5 @@ "use strict"; | ||
| (0, utils_1.debug)('registerClass(%o)', target); | ||
| (0, inject_1.component)('request', false)(target); | ||
| const mappingList = controller_1.mappingDecorator.getMethods(target.prototype); | ||
| if (mappingList.length > 0) { | ||
| (0, inject_1.scope)('prototype', false)(target); | ||
| const option = controller_1.controllerDecorator.getValue(target) || {}; | ||
@@ -127,3 +127,3 @@ const controllerItem = { | ||
| exports.ControllerRegister = ControllerRegister = __decorate([ | ||
| (0, inject_1.scope)('singleton') | ||
| (0, inject_1.component)('singleton') | ||
| ], ControllerRegister); |
+6
-6
| { | ||
| "name": "@zenweb/controller", | ||
| "packageManager": "yarn@4.0.2", | ||
| "version": "3.17.0", | ||
| "version": "4.0.0", | ||
| "description": "Zenweb Controller module", | ||
@@ -14,3 +14,3 @@ "exports": "./lib/index.js", | ||
| "build": "rimraf lib && tsc", | ||
| "prepublishOnly": "yarn run build", | ||
| "prepack": "yarn run build", | ||
| "dev": "cd example && cross-env DEBUG=\\* ts-node app" | ||
@@ -42,9 +42,9 @@ }, | ||
| "dependencies": { | ||
| "@zenweb/core": "^4.2.1", | ||
| "@zenweb/inject": ">=3.19.0", | ||
| "@zenweb/result": "^3.12.5", | ||
| "@zenweb/core": "^4.3.2", | ||
| "@zenweb/inject": "^4.1.1", | ||
| "@zenweb/result": "^4.0.0", | ||
| "@zenweb/router": ">=3.3.1", | ||
| "decorator-make": "^1.3.0", | ||
| "decorator-make": "^1.5.0", | ||
| "globby": "11.0.4" | ||
| } | ||
| } |
+47
-3
@@ -67,5 +67,49 @@ # zenweb 控制器与路由 | ||
| ### 统一前缀 | ||
| 例如后台管理接口中需要统一添加 /admin 作为前缀,则可以这样使用 | ||
| ### 自动路径映射规则 | ||
| 如果开启 `autoControllerPrefix` 功能,则会自动为控制器方法添加路径前缀,等同于自动给控制器类添加 `@controller({ prefix })` 选项 | ||
| #### 文件名规则 | ||
| 控制器扫描目录为 `src/controller` 下的文件结构 | ||
| | 文件名 | 映射路径 | 说明 | | ||
| | --- | --- | --- | | ||
| | index.ts | / | index 名称为特殊名称,代表首页,所以默认为根路径 `/` | | ||
| | user.ts | /user | 非 index 文件名都会被映射为路径名 | | ||
| | user/index.ts | /user | 同理即使是子目录下面的 index 文件名会当成子目录的根路径处理 | | ||
| | user/profile.ts | /user/profile | 子目录下面的文件名会当成子目录的子路径处理 | | ||
| #### prefix 的处理规则 | ||
| 映射路径会自动修改控制器的 `prefix` 配置项,但是有如下几种特殊的处理规则: | ||
| 1. `prefix` 值没有设置时直接使用自动路径值 | ||
| 2. 已经设置的 `prefix` 值如果开头为 `/`(例如: `prefix: '/admin'`)则不做任何处理(忽略) | ||
| 3. 已经设置的 `prefix` 值没有使用 `/` 开头(例如: `prefix: 'admin'`),则会在原来已经设置的结果前添加自动路径值。例如自动路径为 `/manage/some` 控制器值 `prefix: 'admin'` 则会变为 `prefix: '/manage/some/admin'` | ||
| #### 控制器内方法映射规则 | ||
| 即使没有开启 `autoControllerPrefix` 功能也不会影响控制器内方法的映射规则 | ||
| ```ts | ||
| class SomeController { | ||
| @mapping() // 默认映射路径为 / | ||
| index() {} | ||
| @mapping() // 默认映射路径为 /detail | ||
| detail() {} | ||
| @mapping() // 默认映射路径为 /detail_by_id | ||
| detail_by_id() {} | ||
| @mapping() // 默认映射路径为 /detail/:id | ||
| 'detail/:id'(ctx: Context) { return ctx.params.id } | ||
| } | ||
| ``` | ||
| ### 使用技巧 | ||
| #### 统一管理后台接口 | ||
| 例如后台管理接口中需要统一添加 /admin 作为前缀,并且需要验证管理员身份,则可以这样使用 | ||
| ```ts file=src/controller/admin/_helper.ts | ||
@@ -87,3 +131,3 @@ import { mapping, Middleware, RouterMethod, RouterPath } from 'zenweb'; | ||
| /** | ||
| * 管理后台路径映射 | ||
| * 管理后台路径统一映射 | ||
| */ | ||
@@ -90,0 +134,0 @@ export function adminMapping(method?: RouterMethod, path?: RouterPath, ...middleware: Middleware[]) { |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
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
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
20738
9.88%451
0.22%160
37.93%+ Added
+ Added
- Removed
- Removed
Updated
Updated
Updated
Updated