天天看點

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"}