nest transformer Expose Exclude 练习

使用

在entity文件中给需要暴露的属性添加@Expose() @Exclude()

例一

You can expose what your getter or method return by setting a @Expose() decorator to those getters or methods
通过为getter或方法设置@expose()修饰符,可以公开getter或方法返回的内容

import { Expose } from "class-transformer"

export class User {
	id: number;
	firstName: string;
	lastName: string;
	password: string;
	
	@Expose()
	get name() {
		return this.firstName + " " + this.lastName;
	}

	@Expose()
	getFullName() {
		return this.firstName + " " + this.lastName;
	}
}
例二

Exposing properties with different names
If you want to expose some of properties with a different name, you can do it by specifying a name option to @Expose decorator
如果要使用不同的名称公开某些属性,可以通过向@Expose decorator指定一个name选项来实现

import { Expose } from "class-transformer"

export class User {
	@Expose({ name: "uid"}) // 使用uid导出id字段
	id: number;

	firstName: string;
	lastName: string;
	
	@Expose({ name: "secretKey" })
	password: string;

	@Expose({ name: "fullName" })
	getFullName() {
		return this.firstName + " " + this.lastName;
	}
}
例三

Skipping specific properties
Sometimes you want to skip some properties during transformation. This can be done using @Exclude decorator
有时您想在转换过程中跳过一些属性。 这可以使用@Exclude装饰器完成

impoprt { Exclude } from "class-transformer"

export class User {
	id: number;

	email: string;

	@Exclude() // 不返回password字段
	password: string;
}
例四

Skipping depend of operation
You can control on what operation you will exclude a property. Use toClassOnly or toPlainOnly options
您可以控制要排除属性的操作。 使用toClassOnly或toPlainOnly选项

import { Exclude } from "class-transformer"

export class User {
	id: number;
	email: string;
	@Exclude({ toPlainOnly: true }) // 只有在转换为普通对象时 排除password字段  / toClassOnly 只有在转为类时
	password: string

}
例五

Skipping all properties of the class
You can skip all properties of the class, and expose only those are needed explicitly
您可以跳过该类的所有属性,而只公开那些明确需要的属性

import { Exclude, Expose } from "class-transformer"

@Exclude() // 跳过整个类
export class User {
	@Expose() // 暴露id
	id: number;
	
	@Expose() // 暴露email
	email: string;

	password: string;
}
例六

Skipping private properties, or some prefixed properties
If you name your private properties with a prefix, lets say with _, then you can exclude such properties from transformation too
如果用前缀命名私有属性,用_表示,那么您也可以从转换中排除此类属性

import { classToPlain } from "class-transformer"

let photo = classToPlain(photo, { excludePrefixes: ["_"]})

// dto -----------------------------------
import { Expose, classToPlain } from "class-transformer"

export class User {
	id: number;
	private _firstName: string;
	private _lastName: string;
	_password: string;

	setName(firstName: string, lastName: string) {
		this._firstName = firstName
		this._lastName = lastName 
	}
	
	@Expose()
	get name() {
		return this._firstName + ' ' + this._lastName;
	}
}

// use --------------------------

const user = new User()
user.id = 1
user.setName("Johny", "Cage")
user._password = "123456"

const plainUser = classToPlain(user, { excludePrefixes: ["_"] })
// { id: 1, name: "Johny Cage" }
例七

Using groups to control excluded properties
You can use groups to control what data will be exposed and what will not be
您可以使用组来控制哪些数据将公开,哪些数据将不会公开

import { Exclude, Expose, classToPlain } from "class-transformer"

export class User {
	id: number;
	name: string;

	@Expose({ groups: ["user", "admin"] }) // 这意味着该数据将仅向用户和管理员公开
	email: string;

	@Expose({ groups: ["user"] }) // 这意味着该数据将仅向用户公开
	password: string;
}

// -------------------------
const user1 = classToPlain(user, { groups: ["user"] }) 
// {id: xxx, name: "xxx", email: "xxx", password: xxx}
const user2 = classToPlain(user, { groups: ["admin"] }) 
// {id: xxx, name: "xxx", email: "xxx"}
例八

Using versioning to control exposed and excluded properties
If you are building an API that has different versions, class-transformer has extremely useful tools for that. You can control which properties of your model should be exposed or excluded in what version. Example
如果要构建具有不同版本的API,则class-transformer具有非常有用的工具。 您可以控制应在哪个版本中公开或排除模型的哪些属性

import { Exclude, Expose, classToPlain } from "class-transformer"

export class User {
	id: number;
	name: string;

	@Expose({ since: 0.7, until: 1 }) // 在版本为0.7-1的时候暴露
	email: string;

	@Expose({ since: 2.1 }) // 2.1 以后的版本都暴露
	password: string;
}

let user1 = classToPlain(user, { version: 0.5 }) 
// {id: xxx, name: "xxx"}
let user2 = classToPlain(user, { version: 0.8 })
// {id: xxx, name: "xxx", email: "xxx"}
let user3 = classToPlain(user, { version: 1.5 })
// {id: xxx, name: "xxx"}
let user4 = classToPlain(user, { version: 2.5 })
// {id: xxx, name: "xxx", password: "xxxx"}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值