@zenweb/controller
Advanced tools
+42
-6
@@ -21,3 +21,3 @@ import { Middleware } from '@zenweb/core'; | ||
| * class Target { | ||
| * \@mapping(opt?) someMethod(ctx: Context) {} | ||
| * \@Mapping(opt?) someMethod(ctx: Context) {} | ||
| * } | ||
@@ -31,6 +31,6 @@ * ``` | ||
| * } | ||
| * mapping(opt?)(Target.prototype, 'someMethod', [Context]); | ||
| * Mapping(opt?)(Target.prototype, 'someMethod', [Context]); | ||
| * ``` | ||
| */ | ||
| export declare function mapping({ method, path, prefix, middleware, }?: { | ||
| export declare function Mapping({ method, path, prefix, middleware, }?: { | ||
| method?: RouterMethod | RouterMethod[]; | ||
@@ -41,2 +41,38 @@ path?: RouterPath; | ||
| }): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| /** | ||
| * GET 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 中间件 | ||
| */ | ||
| export declare function Get(path_or_middleware?: RouterPath | Middleware, ...middleware: Middleware[]): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| /** | ||
| * POST 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export declare function Post(path_or_middleware?: RouterPath | Middleware, ...middleware: Middleware[]): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| /** | ||
| * PUT 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export declare function Put(path_or_middleware?: RouterPath | Middleware, ...middleware: Middleware[]): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| /** | ||
| * PATCH 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export declare function Patch(path_or_middleware?: RouterPath | Middleware, ...middleware: Middleware[]): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| /** | ||
| * DELETE 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export declare function Delete(path_or_middleware?: RouterPath | Middleware, ...middleware: Middleware[]): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| /** | ||
| * 任何请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export declare function All(path_or_middleware?: RouterPath | Middleware, ...middleware: Middleware[]): (target: Object, propertyKey: string | symbol, descriptor_or_paramtypes?: PropertyDescriptor | any[]) => void; | ||
| export declare const controllerDecorator: { | ||
@@ -53,3 +89,3 @@ wrap: (cb?: import("decorator-make").ClassDecoratorCallback<ControllerOption> | undefined) => (target: Object) => void; | ||
| * ```ts | ||
| * \@controller(opt?) | ||
| * \@Controller(opt?) | ||
| * class Target { | ||
@@ -63,5 +99,5 @@ * } | ||
| * } | ||
| * controller(opt?)(Target); | ||
| * Controller(opt?)(Target); | ||
| * ``` | ||
| */ | ||
| export declare function controller(opt: ControllerOption): (target: Object) => void; | ||
| export declare function Controller(opt: ControllerOption): (target: Object) => void; |
+70
-6
@@ -15,3 +15,3 @@ import { makeClassDecorator, makeMethodDecorator } from 'decorator-make'; | ||
| * class Target { | ||
| * \@mapping(opt?) someMethod(ctx: Context) {} | ||
| * \@Mapping(opt?) someMethod(ctx: Context) {} | ||
| * } | ||
@@ -25,6 +25,6 @@ * ``` | ||
| * } | ||
| * mapping(opt?)(Target.prototype, 'someMethod', [Context]); | ||
| * Mapping(opt?)(Target.prototype, 'someMethod', [Context]); | ||
| * ``` | ||
| */ | ||
| export function mapping({ method, path, prefix, middleware, } = {}) { | ||
| export function Mapping({ method, path, prefix, middleware, } = {}) { | ||
| return mappingDecorator.wrap((descriptor, target, propertyKey) => { | ||
@@ -54,2 +54,66 @@ if (!path) { | ||
| } | ||
| /** | ||
| * 简单路由映射方法 | ||
| * | ||
| * 大部分情况下较为常用,提供给下方常见方法 | ||
| * | ||
| * @param method 方法 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 中间件 | ||
| */ | ||
| function SimpleMapping(method, path, ...middleware) { | ||
| if (typeof path === 'function') { | ||
| middleware.unshift(path); | ||
| path = undefined; | ||
| } | ||
| return Mapping({ method: 'GET', path, middleware }); | ||
| } | ||
| /** | ||
| * GET 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 中间件 | ||
| */ | ||
| export function Get(path_or_middleware, ...middleware) { | ||
| return SimpleMapping('GET', path_or_middleware, ...middleware); | ||
| } | ||
| /** | ||
| * POST 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export function Post(path_or_middleware, ...middleware) { | ||
| return SimpleMapping('POST', path_or_middleware, ...middleware); | ||
| } | ||
| /** | ||
| * PUT 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export function Put(path_or_middleware, ...middleware) { | ||
| return SimpleMapping('PUT', path_or_middleware, ...middleware); | ||
| } | ||
| /** | ||
| * PATCH 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export function Patch(path_or_middleware, ...middleware) { | ||
| return SimpleMapping('PATCH', path_or_middleware, ...middleware); | ||
| } | ||
| /** | ||
| * DELETE 请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export function Delete(path_or_middleware, ...middleware) { | ||
| return SimpleMapping('DELETE', path_or_middleware, ...middleware); | ||
| } | ||
| /** | ||
| * 任何请求方法路由映射 | ||
| * @param path 路径或中间件 | ||
| * @param middleware 其他中间件 | ||
| */ | ||
| export function All(path_or_middleware, ...middleware) { | ||
| return SimpleMapping('ALL', path_or_middleware, ...middleware); | ||
| } | ||
| export const controllerDecorator = makeClassDecorator(); | ||
@@ -61,3 +125,3 @@ /** | ||
| * ```ts | ||
| * \@controller(opt?) | ||
| * \@Controller(opt?) | ||
| * class Target { | ||
@@ -71,6 +135,6 @@ * } | ||
| * } | ||
| * controller(opt?)(Target); | ||
| * Controller(opt?)(Target); | ||
| * ``` | ||
| */ | ||
| export function controller(opt) { | ||
| export function Controller(opt) { | ||
| return controllerDecorator.wrap(() => { | ||
@@ -77,0 +141,0 @@ return opt; |
+5
-5
@@ -8,3 +8,3 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
| /// <reference types="@zenweb/result" /> | ||
| import { component } from '@zenweb/inject'; | ||
| import { Component } from '@zenweb/inject'; | ||
| import { controllerDecorator, mappingDecorator } from './controller.js'; | ||
@@ -16,6 +16,6 @@ import { debug } from './utils.js'; | ||
| export function registerControllerRouter(router, { option, mappingList, target }) { | ||
| debug('@controller(%o) %o', option, target); | ||
| debug('@Controller(%o) %o', option, target); | ||
| const controllerMiddlewares = option.middleware ? (Array.isArray(option.middleware) ? option.middleware : [option.middleware]) : []; | ||
| for (const item of mappingList) { | ||
| debug('@mapping(%o)', item); | ||
| debug('@Mapping(%o)', item); | ||
| router.register({ | ||
@@ -71,3 +71,3 @@ prefix: option.prefix, | ||
| debug('registerClass(%o)', target); | ||
| component('request', false)(target); | ||
| Component('request', false)(target); | ||
| const mappingList = mappingDecorator.getMethods(target.prototype); | ||
@@ -112,4 +112,4 @@ if (mappingList.length > 0) { | ||
| ControllerRegister = __decorate([ | ||
| component('singleton') | ||
| Component('singleton') | ||
| ], ControllerRegister); | ||
| export { ControllerRegister }; |
+3
-3
@@ -23,7 +23,7 @@ import { Middleware } from '@zenweb/core'; | ||
| /** | ||
| * 在控制器中的每个 \@mapping path 上添加前缀 | ||
| * 在控制器中的每个 \@Mapping path 上添加前缀 | ||
| */ | ||
| prefix?: string; | ||
| /** | ||
| * 在控制器中的每个 \@mapping 上添加前置中间件 | ||
| * 在控制器中的每个 \@Mapping 上添加前置中间件 | ||
| */ | ||
@@ -66,3 +66,3 @@ middleware?: Middleware | Middleware[]; | ||
| * - 如果文件名使用 index 则自动忽略 | ||
| * - 如果 \@controller({ prefix: '/somepath' }) 前缀为 '/' 也会忽略 | ||
| * - 如果 \@Controller({ prefix: '/somepath' }) 前缀为 '/' 也会忽略 | ||
| * @default false | ||
@@ -69,0 +69,0 @@ */ |
+4
-4
| { | ||
| "name": "@zenweb/controller", | ||
| "type": "module", | ||
| "version": "6.1.0", | ||
| "version": "6.2.0", | ||
| "description": "Zenweb Controller module", | ||
@@ -32,3 +32,3 @@ "exports": "./dist/index.js", | ||
| "@types/react": "^18.3.28", | ||
| "@zenweb/messagecode": "^5.1.1", | ||
| "@zenweb/messagecode": "^5.2.0", | ||
| "cross-env": "^7.0.3", | ||
@@ -42,4 +42,4 @@ "react": "^18.3.1", | ||
| "@zenweb/core": "^5.2.1", | ||
| "@zenweb/inject": "^5.1.0", | ||
| "@zenweb/result": "^5.0.0", | ||
| "@zenweb/inject": "^5.2.0", | ||
| "@zenweb/result": "^5.1.0", | ||
| "@zenweb/router": "^6.3.0", | ||
@@ -46,0 +46,0 @@ "decorator-make": "^1.5.0", |
+24
-24
@@ -8,6 +8,6 @@ # zenweb 控制器与路由 | ||
| ```ts | ||
| import { Context, mapping } from 'zenweb'; | ||
| import { Context, Mapping } from 'zenweb'; | ||
| export class Controller { | ||
| @mapping() | ||
| @Mapping() | ||
| index(ctx: Context) { // 如果函数名称为 index 则路径名称为 /,否则路径名称默认为函数名称 | ||
@@ -17,3 +17,3 @@ return 'Hello zenweb'; | ||
| @mapping() // 不指定 path 参数则默认使用函数名称 /path2 | ||
| @Mapping() // 不指定 path 参数则默认使用函数名称 /path2 | ||
| path2(ctx: Context) { | ||
@@ -23,3 +23,3 @@ return 'Hello path2'; | ||
| @mapping({ path: '/p3' }) // 指定 path 值为 /p3 | ||
| @Mapping({ path: '/p3' }) // 指定 path 值为 /p3 | ||
| path3(ctx: Context) { | ||
@@ -29,3 +29,3 @@ return 'Hello path3'; | ||
| @mapping({ method: 'POST' }) // 指定请求方法 | ||
| @Mapping({ method: 'POST' }) // 指定请求方法 | ||
| post(ctx: Context) { | ||
@@ -43,3 +43,3 @@ return 'Hello post'; | ||
| ```ts | ||
| import { Context, Next, mapping, controller } from 'zenweb'; | ||
| import { Context, Next, Mapping, Controller } from 'zenweb'; | ||
@@ -54,3 +54,3 @@ // 定义一个中间件处理函数 | ||
| // 方法上的中间件 | ||
| @mapping({ middleware: actionLog }) | ||
| @Mapping({ middleware: actionLog }) | ||
| simple() { | ||
@@ -62,7 +62,7 @@ return 'simple'; | ||
| // 控制器中间件,作用与所有控制器方法上 | ||
| @controller({ | ||
| @Controller({ | ||
| middleware: actionLog, | ||
| }) | ||
| export class Controller2 { | ||
| @mapping() | ||
| @Mapping() | ||
| simple() { | ||
@@ -76,3 +76,3 @@ return 'simple'; | ||
| 如果开启 `autoControllerPrefix` 功能,则会自动为控制器方法添加路径前缀,等同于自动给控制器类添加 `@controller({ prefix })` 选项 | ||
| 如果开启 `autoControllerPrefix` 功能,则会自动为控制器方法添加路径前缀,等同于自动给控制器类添加 `@Controller({ prefix })` 选项 | ||
@@ -103,12 +103,12 @@ #### 文件名规则 | ||
| class SomeController { | ||
| @mapping() // 默认映射路径为 / | ||
| @Mapping() // 默认映射路径为 / | ||
| index() {} | ||
| @mapping() // 默认映射路径为 /detail | ||
| @Mapping() // 默认映射路径为 /detail | ||
| detail() {} | ||
| @mapping() // 默认映射路径为 /detail_by_id | ||
| @Mapping() // 默认映射路径为 /detail_by_id | ||
| detail_by_id() {} | ||
| @mapping() // 默认映射路径为 /detail/:id | ||
| @Mapping() // 默认映射路径为 /detail/:id | ||
| 'detail/:id'(ctx: Context) { return ctx.params.id } | ||
@@ -124,3 +124,3 @@ } | ||
| ```ts file=src/controller/admin/_helper.ts | ||
| import { mapping, Middleware, RouterMethod, RouterPath } from 'zenweb'; | ||
| import { Mapping, Middleware, RouterMethod, RouterPath } from 'zenweb'; | ||
@@ -138,8 +138,8 @@ /** | ||
| } | ||
| s | ||
| /** | ||
| * 管理后台路径统一映射 | ||
| */ | ||
| export function adminMapping(method?: RouterMethod, path?: RouterPath, ...middleware: Middleware[]) { | ||
| return mapping({ | ||
| export function AdminMapping(method?: RouterMethod, path?: RouterPath, ...middleware: Middleware[]) { | ||
| return Mapping({ | ||
| method, | ||
@@ -154,8 +154,8 @@ prefix: '/admin', | ||
| ```ts file=src/controller/admin/index.ts | ||
| import { Context, Next, mapping, controller } from 'zenweb'; | ||
| import { adminMapping } from './_helper'; | ||
| import { Context, Next, Mapping, Controller } from 'zenweb'; | ||
| import { AdminMapping } from './_helper'; | ||
| export class IndexController { | ||
| // 等同于 @mapping({ path: '/admin/', method: 'GET', middleware: [adminRequired()] }) | ||
| @adminMapping() | ||
| // 等同于 @Mapping({ path: '/admin/', method: 'GET', middleware: [adminRequired()] }) | ||
| @AdminMapping() | ||
| index() { | ||
@@ -165,4 +165,4 @@ return 'admin index'; | ||
| // 等同于 @mapping({ path: '/admin/create_user', method: 'POST', middleware: [adminRequired()] }) | ||
| @adminMapping('POST') | ||
| // 等同于 @Mapping({ path: '/admin/create_user', method: 'POST', middleware: [adminRequired()] }) | ||
| @AdminMapping('POST') | ||
| create_user() { | ||
@@ -169,0 +169,0 @@ return 'create user'; |
22430
20.52%511
24.33%Updated
Updated