only code
import { Controller, Delete, Get, Post, Put, Query } from 'egg-next';
@Controller('cards')
export class CardController {
constructor() {}
@Get()
async getAll(
@Query('page') page: number = 1,
@Query('countPerPage') countPerPage: number = 20,
@Query('order') order: 'ASC' | 'DESC' = 'ASC',
@Query('orderBy') orderBy: string = 'id',
@Query('like') like: string = null,
) {
const cards = await this.service.cardService.getAll(page, countPerPage, order, orderBy, like);
this.body = cards;
}
@Get('/:name')
async get(
@Query('name') name: string,
) {
const card = await this.service.cardService.get(name);
this.body = cards;
}
@Delete('/:name')
async remove(
@Query('name') name: string
) {
await this.service.cardService.remove(name);
this.body = true
}
this.is mainly two concept:
@Delete('/:name')这样写起来非常爽啊,不用一开始一堆query的赋值,也不用一会看看router.js,一会看看controller
你们觉得呢
-1
我想说 ts only 吧
搞复杂了,原本 js 就足够简单的,现在改成这样连写个路由都看起来很费劲。
-1
不就是 TS 的装饰器么?
哈哈 前端项目里已经很习惯写 decorator 了(基于mobx的开发) 我上网上搜了下,还真有这种框架是这么写的 https://github.com/nestjs/nest 后面如果egg拥抱ts的话,应该也可以接纳这些进来
蚂蚁那边不都在用 TS 么, 等 @shepherdwind 的文章,还有 @popomore 的 tegg RFC
Query 那个不错~
InversifyJS 看起来很炫酷~ 还有这个 https://github.com/typestack/routing-controllers
@okoala 这个除了@Query叫 @QueryParam ,其他已经非常接近了。。我研究下是不是可以把这个接到egg里
如果本意是解决复杂查询,还不如试试 odata 的 query 格式,直接从 ctx 上取值,实现到 mongo 到 mysql 的语句自动生成
@jtyjty99999 看看能不能在它的基础上进行扩展~
@jaredleechn 不是用来解决复杂查询的, 1是为了让路由更加易用 2是为了让一个controller更加可读
ps basement把你暴露了。。
这些语法 JS 还不支持吧,可以先在 TS 上实践后,作为实践分享出来,再开启讨论。
我也很期待这种支持,正在考虑是否可以加入这个库 routing-controllers