Rewritten and improved fork of class-transformer
npm install @yoolabs/class-transformer> IMPORTANT NOTE!
>
> This repository is a fork of the original class-transformer package.
> It adds tweaks of
>
> - class-transformer-global-storage (exports metadata storage, so that metadata is accessible)
> - nestjs class-transformer (currently only updates, no funtionality changes or fixes)
> - yoolabs class-transformer (custom functionality documented below)
This fork adds custom functionality to class transformer.
Note: To release to npm, run continuous-deployment-workflow!
- ES2022 class constructor behavior fix, see https://github.com/typestack/class-transformer/issues/1216
- Structure Decorator enhancement: Fill reflectedType as well to allow 3rd party libs to work correctly.
- Structure Decorator enhancement: Support Map/Set being passed in plain value (plain data only supported Array+Object until now)
- Restructure code and fix many lint and possibly edge case runtime errors
- Structure Decorator - like @Type() but with greater control and stricter transformation
Its ES6 and Typescript era. Nowadays you are working with classes and constructor objects more than ever.
Class-transformer allows you to transform plain object to some instance of class and versa.
Also it allows to serialize / deserialize object based on criteria.
This tool is super useful on both frontend and backend.
* yoolabs class-transformer
* 1.0.3
* 1.0.2
* 1.0.1
* 1.0
* 0.7
* Main Introduction
* Table of contents
* What is class-transformer⬆
* Installation⬆
* Node.js⬆
* Browser⬆
* Methods⬆
* plainToInstance⬆
* plainToClassFromExist⬆
* instanceToPlain⬆
* instanceToInstance⬆
* serialize⬆
* deserialize and deserializeArray⬆
* Enforcing type-safe instance⬆
* Working with nested objects⬆
* Providing more than one type option⬆
* Structure Decorator
* Exposing getters and method return values⬆
* Exposing properties with different names⬆
* Skipping specific properties⬆
* Skipping depend of operation⬆
* Skipping all properties of the class⬆
* Exposing/Excluding properties of subobjects
* Skipping private properties, or some prefixed properties⬆
* Using groups to control excluded properties⬆
* Using versioning to control exposed and excluded properties⬆
* Сonverting date strings into Date objects⬆
* Working with arrays⬆
* Additional data transformation⬆
* Basic usage⬆
* Advanced usage⬆
* Other decorators⬆
* Working with generics⬆
* Implicit type conversion⬆
* How does it handle circular references?⬆
* Example with Angular2⬆
* Samples⬆
* Release notes⬆
In JavaScript there are two types of objects:
- plain (literal) objects
- class (constructor) objects
Plain objects are objects that are instances of Object class.
Sometimes they are called literal objects, when created via {} notation.
Class objects are instances of classes with own defined constructor, properties and methods.
Usually you define them via class notation.
So, what is the problem?
Sometimes you want to transform plain javascript object to the ES6 classes you have.
For example, if you are loading a json from your backend, some api or from a json file,
and after you JSON.parse it you have a plain javascript object, not instance of class you have.
For example you have a list of users in your users.json that you are loading:
``json`
[
{
"id": 1,
"firstName": "Johny",
"lastName": "Cage",
"age": 27
},
{
"id": 2,
"firstName": "Ismoil",
"lastName": "Somoni",
"age": 50
},
{
"id": 3,
"firstName": "Luke",
"lastName": "Dacascos",
"age": 12
}
]
And you have a User class:
`typescript
export class User {
id: number;
firstName: string;
lastName: string;
age: number;
getName() {
return this.firstName + ' ' + this.lastName;
}
isAdult() {
return this.age > 36 && this.age < 60;
}
}
`
You are assuming that you are downloading users of type User from users.json file and may want to write
following code:
`typescript`
fetch('users.json').then((users: User[]) => {
// you can use users here, and type hinting also will be available to you,
// but users are not actually instances of User class
// this means that you can't use methods of User class
});
In this code you can use users[0].id, you can also use users[0].firstName and users[0].lastName.users[0].getName()
However you cannot use or users[0].isAdult() because "users" actually isusers: User[]
array of plain javascript objects, not instances of User object.
You actually lied to compiler when you said that its .
So what to do? How to make a users array of instances of User objects instead of plain javascript objects?
Solution is to create new instances of User object and manually copy all properties to new objects.
But things may go wrong very fast once you have a more complex object hierarchy.
Alternatives? Yes, you can use class-transformer. Purpose of this library is to help you to map your plain javascript
objects to the instances of classes you have.
This library also great for models exposed in your APIs,
because it provides a great tooling to control what your models are exposing in your API.
Here is an example how it will look like:
`typescript`
fetch('users.json').then((users: Object[]) => {
const realUsers = plainToInstance(User, users);
// now each user in realUsers is an instance of User class
});
Now you can use users[0].getName() and users[0].isAdult() methods.
1. Install module:
npm install class-transformer --save
2. reflect-metadata shim is required, install it too:
npm install reflect-metadata --save
and make sure to import it in a global place, like app.ts:
`typescript`
import 'reflect-metadata';
3. ES6 features are used, if you are using old version of node.js you may need to install es6-shim:
npm install es6-shim --save
and import it in a global place like app.ts:
`typescript`
import 'es6-shim';
1. Install module:
npm install class-transformer --save
2. reflect-metadata shim is required, install it too:
npm install reflect-metadata --save
add