Skip to content

Commit 9c9d7f6

Browse files
merge: release 0.4.1 (#992)
2 parents ab093a3 + 82a9c87 commit 9c9d7f6

34 files changed

+11283
-2489
lines changed

.eslintrc.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ extends:
1212
- 'plugin:@typescript-eslint/recommended-requiring-type-checking'
1313
- 'plugin:jest/recommended'
1414
- 'prettier'
15-
- 'prettier/@typescript-eslint'
1615
rules:
1716
'@typescript-eslint/explicit-member-accessibility': off
1817
'@typescript-eslint/no-angle-bracket-type-assertion': off

.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​

.github/workflows/auto-approve-dependabot-workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Dependabot auto-merge
22
on:
3-
pull_request
3+
pull_request_target
44
jobs:
55
dependabot:
66
runs-on: ubuntu-latest

CHANGELOG.md

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

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

5+
### [0.4.1][v0.4.1] - 2021-11-20
6+
7+
> **NOTE:** This version fixes a security vulnerability allowing denial of service attacks with a specially crafted request payload. Please update as soon as possible.
8+
9+
#### Fixed
10+
11+
- prevent unhandled error in `plaintToclass` when union-type member is undefined
12+
- fixed a scenario when a specially crafted JS object would be parsed to Array
13+
14+
#### Changed
15+
16+
- various dev-dependencies updated
17+
518
### [0.4.0][v0.4.0] [BREAKING CHANGE] - 2021-02-14
619

720
#### Breaking Changes
@@ -199,6 +212,7 @@ This version has introduced a breaking-change when this library is used with cla
199212
- Library has changed its name from `serializer.ts` to `constructor-utils`.
200213
- Added `constructor-utils` namespace.
201214

215+
[v0.4.1]: https://github.com/typestack/class-transformer/compare/v0.4.0...v0.4.1
202216
[v0.4.0]: https://github.com/typestack/class-transformer/compare/v0.3.2...v0.4.0
203217
[v0.3.2]: https://github.com/typestack/class-transformer/compare/v0.3.1...v0.3.2
204218
[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
@@ -676,8 +676,6 @@ export class User {
676676
}
677677
```
678678

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

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)