1.0.1 • Published 3 years ago

egg-pandoras v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Pandora & EGG JS

  • npm i egg-pandoras --save

Pandora 套件说明

  • 添加 Dto/Dao AOP 切面
  • 添加 Swagger JSDOC 注释生成文档
  • 添加 router 路由修饰器
  • 后续迭代更多功能

基本使用

  • 启动 Pandora 套件
  • app/router.ts
import { Application } from 'egg';
import { bootstrap } from 'egg-pandoras';

export default (app: Application) => {
    bootstrap(app);
};
  • swagger 通过 注释生成 可 配合 AOP 切面使用
  • AOP 切面 自动把 validator-class 转成 jsonschema
  • 这里注意一下 swagger 中使用 AOP 切面直接备注 切面名即可
  • app/controller/home.ts
import { Controller } from 'egg';
import { createWriteStream } from 'fs';
import { isArray } from 'lodash';
import { join } from 'path';
import pump from 'pump';
import { RequestMapping, RequestMethod, RestController } from 'egg-pandoras';
import { HomeDataDto } from '../dto/home';

/**
 * @controller home
 */
@RestController
export default class extends Controller {

    /**
     * @summary 创建资源
     * @router POST /home/index/{id}/{uid}
     * @request path string id ID
     * @request path string uid UID
     * @request query string test 测试
     * @request query string test1 测试1
     * @request formdata file file 文件 false
     * @request formdata file file1 文件1 false
     * @request body string name 名字
     * @request body string age 年龄
     * @consumes multipart/form-data
     * @apikey
     */
    @RequestMapping({ path: 'index/:id/:uid', methods: [RequestMethod.POST] })
    public async index() {
        console.log('path', this.ctx.params);
        console.log('query', this.ctx.request.query);
        console.log('header', this.ctx.request.headers);
        // console.log('body', this.ctx.request.body);
        const parts = this.ctx.multipart();
        const body: { [x: string]: string } = {};
        let stream;
        while ((stream = await parts()) != null) {
            if (isArray(stream)) {
                body[stream[0]] = stream[1];
            } else if (stream.filename) {
                const filename = stream.filename.toLowerCase();
                const target = join(this.config.baseDir, 'app/public', filename);
                const writeStream = createWriteStream(target);
                await pump(stream, writeStream);
            }
        }
        console.log('body', body);
        const { ctx } = this;
        console.log(await ctx.vaildAOP(HomeDataDto, {}));

        // ctx.body = await ctx.service.test.sayHi('egg');
        ctx.body = await ctx.service.test.test();
    }
}
import {
    Contains, IsInt, Length, IsEmail, IsFQDN, Dto,
    IsDate, Min, Max, ValidateNested, IsString, IsEmpty,
} from 'egg-pandoras';

export class Test1 extends Dto {

    @Length(10, 20)
    public title!: string;

    @Contains('hello')
    public text!: string;

    @IsInt()
    @Min(0)
    @Max(10)
    public rating!: number;

    @IsEmail()
    public email!: string;

    @IsFQDN()
    public site!: string;

    @IsDate()
    public createDate!: Date;
}

export class HomeDataDto extends Dto {

    @IsString()
    public error!: string;
    @IsInt()
    public errno!: number;

    @ValidateNested()
    @IsEmpty()
    public data?: Test1;
}
  • logic 层 做数据验证 这里对用 controller 的 文件名 和目录结构
  • app/logic/user.ts
import { Logic } from 'egg-pandoras';

export default class extends Logic {
    public async add() {
        await this.ctx.vaildAOP('dto 或者 dao 或者 使用 egg-validate 插件',this.ctx.body);
        // true 通过 false 将不会执行到 action
        return true;
    }
}