Skip to content

Commit c5b7065

Browse files
chore: resolve conflicts
2 parents 320ef47 + 9c9012f commit c5b7065

32 files changed

+14114
-2577
lines changed

.gitbook.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root: ./docs
2+
​structure:
3+
readme: pages/01-getting-started.md
4+
summary: SUMMARY.md​

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22

33
_This changelog follows the [keep a changelog][keep-a-changelog]_ format to maintain a human readable changelog.
44

5+
### [0.5.1][v0.5.1] [BREAKING CHANGE] - 2021-11-22
6+
7+
#### Changed
8+
9+
- re-added accidentally removed deprecated function names `classToPlain` and `plainToClass`
10+
11+
### [0.5.0][v0.5.0] [BREAKING CHANGE] - 2021-11-20
12+
13+
> **NOTE:** This version fixes a security vulnerability allowing denial of service attacks with a specially crafted request payload. Please update as soon as possible.
14+
15+
#### Breaking Changes
16+
17+
See the breaking changes from `0.4.1` release. It was accidentally released as patch version.
18+
19+
### [0.4.1][v0.4.1] [BREAKING CHANGE] - 2021-11-20
20+
21+
> **NOTE:** This version fixes a security vulnerability allowing denial of service attacks with a specially crafted request payload. Please update as soon as possible.
22+
23+
#### Breaking Changes
24+
25+
**Exported functions has been renamed**
26+
Some of the exported functions has been renamed to better reflect what they are doing.
27+
28+
- `classToPlain` -> `instanceToPlain`
29+
- `plainToClass` -> `plainToInstance`
30+
- `classToClass` -> `instanceToInstance`
31+
32+
#### Fixed
33+
34+
- prevent unhandled error in `plaintToclass` when union-type member is undefined
35+
- fixed a scenario when a specially crafted JS object would be parsed to Array
36+
37+
#### Changed
38+
39+
- various dev-dependencies updated
40+
541
### [0.4.0][v0.4.0] [BREAKING CHANGE] - 2021-02-14
642

743
#### Breaking Changes
@@ -199,6 +235,9 @@ This version has introduced a breaking-change when this library is used with cla
199235
- Library has changed its name from `serializer.ts` to `constructor-utils`.
200236
- Added `constructor-utils` namespace.
201237

238+
[v0.5.1]: https://github.com/typestack/class-transformer/compare/v0.5.0...v0.5.1
239+
[v0.5.0]: https://github.com/typestack/class-transformer/compare/v0.4.1...v0.5.0
240+
[v0.4.1]: https://github.com/typestack/class-transformer/compare/v0.4.0...v0.4.1
202241
[v0.4.0]: https://github.com/typestack/class-transformer/compare/v0.3.2...v0.4.0
203242
[v0.3.2]: https://github.com/typestack/class-transformer/compare/v0.3.1...v0.3.2
204243
[v0.3.1]: https://github.com/typestack/class-transformer/compare/v0.2.3...v0.3.1

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,6 @@ export class User {
675675
}
676676
```
677677

678-
Note, that dates will be converted to strings when you'll try to convert class object to plain object.
679-
680678
Same technique can be used with `Number`, `String`, `Boolean`
681679
primitive types when you want to convert your values into these types.
682680

docs/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Table of contents
2+
3+
- [Getting Started](pages/01-getting-started.md)
4+
- [Basic usage](pages/02-basis-usage.md)

docs/pages/01-getting-started.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Getting Started
2+
3+
The `class-transformer` package is a zero-dependency utility library helping you to quickly transform class instances to plain objects and vice-versa.
4+
It works well with the [`class-validator`][class-validator] library. The main features include:
5+
6+
- conditionally transforming object properties
7+
- excluding specific properties from the transformed object
8+
- exposing properties under a different name on the transformed object
9+
- supports both NodeJS and browsers
10+
- fully three-shakable
11+
- zero external dependencies
12+
13+
## Installation
14+
15+
To start using class-transformer install the required packages via NPM:
16+
17+
```bash
18+
npm install class-transformer reflect-metadata
19+
```
20+
21+
Import the `reflect-metadata` package at the **first line** of your application:
22+
23+
```ts
24+
import 'reflect-metadata';
25+
26+
// Your other imports and initialization code
27+
// comes here after you imported the reflect-metadata package!
28+
```
29+
30+
As the last step, you need to enable emitting decorator metadata in your Typescript config. Add these two lines to your `tsconfig.json` file under the `compilerOptions` key:
31+
32+
```json
33+
"emitDecoratorMetadata": true,
34+
"experimentalDecorators": true,
35+
```
36+
37+
Now you are ready to use class-transformer with Typescript!
38+
39+
## Basic Usage
40+
41+
The most basic usage is to transform a class to a plain object:
42+
43+
```ts
44+
import { Expose, Exclude, classToPlain } from 'class-transformer';
45+
46+
class User {
47+
/**
48+
* When transformed to plain the `_id` property will be remapped to `id`
49+
* in the plain object.
50+
*/
51+
@Expose({ name: 'id' })
52+
private _id: string;
53+
54+
/**
55+
* Expose the `name` property as it is in the plain object.
56+
*/
57+
@Expose()
58+
public name: string;
59+
60+
/**
61+
* Exclude the `passwordHash` so it won't be included in the plain object.
62+
*/
63+
@Exclude()
64+
public passwordHash: string;
65+
}
66+
67+
const user = getUserMagically();
68+
// contains: User { _id: '42', name: 'John Snow', passwordHash: '2f55ce082...' }
69+
70+
const plain = classToPlain(user);
71+
// contains { id: '42', name: 'John Snow' }
72+
```
73+
74+
[class-validator]: https://github.com/typestack/class-validator/

docs/pages/02-basic-usage.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Basic usage
2+
3+
There are two main exported functions what can be used for transformations:
4+
5+
- `plainToClass` - transforms a plain object to an instance of the specified class constructor
6+
- `classToPlain` - transforms a _known_ class instance to a plain object
7+
8+
Both function transforms the source object to the target via applying the metadata registered by the decorators on
9+
the class definition. The four main decorators are:
10+
11+
- `@Expose` specifies how expose the given property on the plain object
12+
- `@Exclude` marks the property as skipped, so it won't show up in the transformation
13+
- `@Transform` allows specifying a custom transformation on the property via a custom handler
14+
- `@Type` decorator explicitly sets the type of the property, during the transformation `class-transformer` will attempt
15+
to create an instance of the specified type
16+
17+
You must always decorate all your properties with an `@Expose` or `@Exclude` decorator.
18+
19+
> **NOTE:** It's important to remember `class-transformer` will call the target type with am empty constructor, so if
20+
> you are using a type what requires special setup, you need to use a `@Transform` decorator and create the instance yourself.

0 commit comments

Comments
 (0)