1.0.3 • Published 5 years ago

egg-ts-decorator v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

egg-ts-decorator

插件安装

npm i egg-ts-decorator --save

依赖说明

依赖的 egg 版本

egg-ts-decorator 版本egg 1.x
1.x😁
0.x

依赖的插件

  • lodash

开启插件

// config/plugin.js
exports.decorators = {
  enable: true,
  package: 'egg-ts-decorator',
};

使用场景

  • 支持定义声明路由
  import { routes } from 'egg-ts-decorator';
  // 声明restful路由
  @routes.resources('users', '/api/v1/users', 'api.v1.users')
  // 声明具体路由
  @routes.route('get', '/api/v1/wm_people')
  • 支持规则校验
  import { routes } from 'egg-ts-decorator';
  @routes.route('posts', '/api/v1/users', {
    rules: [{
      // 待校验数据获取方式
      key: 'query',
      // 具体规则,详细配置参考 https://github.com/node-modules/parameter
      rule: {
        name: 'string',
        age: {type: 'int', max: 200},
        gender: ['male', 'female'],
      },
    }],
  });
  • 支持传入前置中间件,比如想针对接口进行权限校验,日志记录等等
  import { routes } from 'egg-ts-decorator';
  @routes.route('posts', '/api/v1/users', {
    extra: {
      authKey: 'create_users',
    },
  });

  // config/config.default.ts
  const before = options => {
    const authMiddleware = async (ctx: Context, next) => {
      if (options.extra && options.extra.authKey) {
        console.log(ctx, options.extra.authKey);
      }
      await next();
    };
    return [ authMiddleware ];
  };
  config = {
    loadConfig: {
      before,
    }
  }

详细配置

请到 config/config.default.js 查看详细配置项说明。

单元测试

npm run test

提问交流

请到 egg issues 异步交流。

License

MIT