如何创建自定义插件以减少样板代码?

问题描述

有没有人对 nest.js 插件有想法,比如(用于减少样板代码

我从官网得到的文档如下

TypeScript 的元数据反射系统有几个限制,例如,无法确定类包含哪些属性或识别给定的属性是可选的还是必需的。但是,其中一些限制可以在编译时解决nest 提供了一个插件来增强 TypeScript 编译过程,以减少所需的样板代码量。

@nestJs/swagger and @nestJs/graphql

eg:-

export class createuserDto {
  @Apiproperty()
  email: string;

  @Apiproperty()
  password: string;

  @ApiProperty({ enum: RoleEnum,default: [],isArray: true })
  roles: RoleEnum[] = [];

  @ApiProperty({ required: false,default: true })
  isEnabled?: boolean = true;
}

To


export class createuserDto {
  email: string;
  password: string;
  roles: RoleEnum[] = [];
  isEnabled?: boolean = true;
}





The above is working fine and also I need to use custom annotations with custom datatypes like the following


@InputType()
export class UseRSSorting {

    @Field(type => BaseSorting,{ nullable: true })
    name: BaseSorting;

    @Field(type => BaseSorting,{ nullable: true })
    email: BaseSorting;

}

To

@InputType()
export class UseRSSorting {

    name: BaseSorting;

    email: BaseSorting;

}

Note: I have the custom datatype like the following


import { registerEnumType } from "@nestJs/graphql";

export enum BaseSorting {
    ASC = "",DESC = "DESC"
}
registerEnumType(BaseSorting,{
    name: "BaseSorting",// this one is mandatory
    description: "ascending or descending order",// this one is optional
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)