assets-route-plugin
配合astroboy框架使用,查看更多:Astroboy
CHANGE LOGS
1.0.1
1.0.0-rc.26
1.0.0-rc.25
1.0.0-rc.24
- 新增@CustomRoute,代替@Index和@API提供高可定制的路由能力
- @Index和@API新增一个可选参数,提供部分可定制能力
- 拓宽@Router的能力,支持定制url模板
- 拓宽createRouter,提供一个新的重载,支持开启调试模式
- 兼容以前的版本
1.0.0-rc.23
- 支持无前缀的Router控制器(提供空字符串即可)
1.0.0-rc.22
- 新增装饰器@NoAuthorize,用于关闭单条Route的鉴权
- 拓宽了@Router的参数,简化配置
- 支持使用@Router修改api前缀
1.0.0-rc.17
- 支持单路由重定义business服务
- 支持多服务依赖注入能力
1.0.0-rc.16
- 增加Router/Route集成鉴权处理@Authorize
1.0.0-rc.15
- 增加PUT/POST/DELETE方法query参数的获取,在服务的第二个参数位置接收
- 优化了默认参数提取的工厂方法和config配置支持
1.0.0-rc.14
- 修复astroboy没有默认路由实现的问题
- 内置提供了默认的参数获取函数和body处理函数
- 支持在astroboy的config里面配置参数修改router行为
1.0.0-rc.12
- 增加路由方法校验:检查business的方法实现,未实现的情况下服务启动过程会报错
1. 安装
yarn add assets-route-plugin --save
# or
npm install assets-route-plugin --save
2. 构建Business层
services/demo/BusinessService.ts
import { BaseClass } from "astroboy";
class BusinessService extends BaseClass {
queryParams(query: any){
return { ...query };
}
queryParams2(query: any){
return { ...query };
}
changeData(postData: any) {
return { ...postData };
}
changeData2(postData: any, query: any) {
return {
postData,
query
};
}
testB(post: any, query: any) {
return {
post,
query
};
}
}
export = BusinessService;
3. 定义router
controllers/demo/DemoController.ts
import { Controller } from "astroboy";
import BusinessService from "../services/demo/BusinessService.ts";
import AnotherService from "...xxxx";
import ThirdService from "....xxxxxxxx";
import { Router, Service, Index, API, Metadata, RouteMethod, Inject } from "assets-route-plugin";
@Router("demo")
@Service(BusinessService)
class DemoController extends Controller {
private readonly business!: BusinessService;
@Inject()
private readonly service03!: ThirdService;
@Index(["index", "", "*"])
public async getIndexHtml(){
this.ctx.render("index.html");
}
@API("GET", "query")
public queryParams!: RouteMethod;
@API("GET", "query2")
@Metadata("路由名字")
public queryParams02!: RouteMethod;
@API("POST", "change")
public async changeData(){
const postData = this.ctx.body;
const result = await this.business.changeData(postData);
this.ctx.json(0, "success", result);
}
@API("POST", "change2")
public changeData2!: RouteMethod;
@API("POST", "change2")
@Service(AnotherService)
public changeData3!: RouteMethod;
}
4. 生成router规则
router/demo.ts
import DEMO from "../controllers/demo/DemoController";
import { createRouter } from "assets-route-plugin";
export = createRouter(DEMO, "demo.DemoController", "/section01/section02");
5. 最终生成路由
[
'GET',
[
'/section01/section02/demo/index',
'/section01/section02/demo',
'/section01/section02/demo/*'
],
'demo.DemoController',
'getIndexHtml'
],
[
'GET',
'/section01/section02/api/demo/query',
'demo.DemoController',
'queryParams'
],
[
'路由名字',
'GET',
'/section01/section02/api/demo/query2',
'demo.DemoController',
'queryParams02'
]
[
'POST',
'/section01/section02/api/demo/change',
'demo.DemoController',
'changeData'
],
[
'POST',
'/section01/section02/api/demo/change2',
'demo.DemoController',
'changeData2'
]
```changeData2'
]
6. 路由集成鉴权
npm:^1.0.0-rc.16
支持路由(Router/Route)级别集成权限处理
- 编写鉴权服务(如有必要)
services/demo/auth.ts
import { BaseClass } from "astroboy";
class AuthService extends BaseClass {
sleep(time = 500) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async checkIsLogin() {
await this.sleep(10);
return true;
}
async checkIsAdmin() {
await this.sleep(10);
if (this.ctx.header["auth"] === "admin") return true;
return false
}
async checkIsSuperAdmin() {
await this.sleep(10);
if (this.ctx.header["auth"] === "s_a") return true;
return false
}
}
export = AuthService;
- 编写鉴权工厂函数(如有必要),构建鉴权组件
import { AuthGuard } from 'assets-route-plugin';
const authFac: (auth?: "admin" | "s_a") => AuthGuard = (auth) => {
return async (ctx: AstroboyContext) => {
if (auth === "admin")
return await new AuthService(ctx).checkIsAdmin();
else if (auth === "s_a")
return await new AuthService(ctx).checkIsSuperAdmin();
else
return await new AuthService(ctx).checkIsLogin();
}
};
const admin = [authFac("admin")];
const s_a = [authFac("s_a")];
const ad_sa = [...admin, ...s_a];
const meta = { error: new Error("鉴权失败") };
- 在路由上使用
@Router("demo")
@Service(DemoService)
@Authorize(ad_sa, meta)
class DemoController extends BaseClass {
private business!: DemoService;
@Index(["", "*"])
public async getIndexHtml() {
this.ctx.render("demo/index.html")
}
@API("GET", "testA")
public testA!: RouteMethod;
@API("POST", "testB")
@Authorize(admin, scope_meta)
public testB!: RouteMethod;
@API("GET", "testC")
@NoAuthorize()
public testC!: RouteMethod;
}