当前位置: 首页 > 工具软件 > Expose > 使用案例 >

nest transformer Expose Exclude 练习

丌官博文
2023-12-01

使用

在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"}
 类似资料: