Skip to content

Commit

Permalink
merge: release 0.4.0 (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNameProvided authored Feb 14, 2021
2 parents 16a0506 + a101300 commit ab093a3
Show file tree
Hide file tree
Showing 17 changed files with 1,655 additions and 2,326 deletions.
4 changes: 3 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ updates:
versioning-strategy: increase
commit-message:
prefix: build
include: scope
include: scope
ignore:
- dependency-name: "husky"
20 changes: 16 additions & 4 deletions .github/workflows/auto-approve-dependabot-workflow.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
name: Auto approve PRs
name: Dependabot auto-merge
on:
pull_request
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
steps:
- uses: hmarr/auto-approve[email protected]
if: github.actor == 'dependabot[bot]'
- name: 'Auto approve PR by Dependabot'
uses: hmarr/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
github-token: "${{ secrets.TYPESTACK_BOT_TOKEN }}"
- name: 'Comment merge command'
uses: actions/github-script@v3
with:
github-token: ${{secrets.TYPESTACK_BOT_TOKEN }}
script: |
await github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '@dependabot squash and merge'
})
42 changes: 41 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,45 @@

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

### [0.3.2][v0.3.2] - 2021-01-14
### [0.4.0][v0.4.0] [BREAKING CHANGE] - 2021-02-14

#### Breaking Changes

See the breaking changes from `0.3.2` release. It was accidentally released as patch version.

#### Added

- add option to ignore unset properties
- `group` information is exposed in the `@Transform` handler
- transformation options are exposed in the `@Transform` handler

#### Fixed

- fixed TypeError when `discriminator.subTypes` is not defined

#### Changed

- various dev-dependencies has been updated

### [0.3.2][v0.3.2] [BREAKING CHANGE] - 2021-01-14

#### Breaking Changes

**Signature change for `@Transform` decorator**
From this version the `@Transform` decorator receives the transformation parameters in a a wrapper object. You need to
destructure the values you are interested in.

Old way:

```ts
@Transform((value, obj, type) => /* Do some stuff with value here. */)
```

New way with wrapper object:

```ts
@Transform(({ value, key, obj, type }) => /* Do some stuff with value here. */)
```

#### Added

Expand Down Expand Up @@ -161,6 +199,8 @@ This version has introduced a breaking-change when this library is used with cla
- Library has changed its name from `serializer.ts` to `constructor-utils`.
- Added `constructor-utils` namespace.

[v0.4.0]: https://github.com/typestack/class-transformer/compare/v0.3.2...v0.4.0
[v0.3.2]: https://github.com/typestack/class-transformer/compare/v0.3.1...v0.3.2
[v0.3.1]: https://github.com/typestack/class-transformer/compare/v0.2.3...v0.3.1
[v0.2.3]: https://github.com/typestack/class-transformer/compare/v0.2.2...v0.2.3
[v0.2.2]: https://github.com/typestack/class-transformer/compare/v0.2.1...v0.2.2
Expand Down
56 changes: 29 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/typestack/class-transformer/branch/develop/graph/badge.svg)](https://codecov.io/gh/typestack/class-transformer)
[![npm version](https://badge.fury.io/js/class-transformer.svg)](https://badge.fury.io/js/class-transformer)

Its ES6 and Typescript era. Nowadays you are working with classes and constructor objects more then ever.
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.
Expand Down Expand Up @@ -131,17 +131,17 @@ So what to do? How to make a `users` array of instances of `User` objects instea
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 you plain javascript
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 example how it will look like:
Here is an example how it will look like:

```typescript
fetch('users.json').then((users: Object[]) => {
const realUsers = plainToClass(User, users);
// now each user in realUsers is instance of User class
// now each user in realUsers is an instance of User class
});
```

Expand Down Expand Up @@ -226,7 +226,7 @@ let users = plainToClass(User, userJson); // to convert user plain object a sing

### plainToClassFromExist[](#table-of-contents)

This method transforms a plain object into a instance using a already filled Object which is a instance from the target class.
This method transforms a plain object into an instance using an already filled Object which is an instance of the target class.

```typescript
const defaultUser = new User();
Expand All @@ -246,19 +246,19 @@ let photo = classToPlain(photo);

### classToClass[](#table-of-contents)

This method transforms your class object into new instance of the class object.
This maybe treated as deep clone of your objects.
This method transforms your class object into a new instance of the class object.
This may be treated as deep clone of your objects.

```typescript
import { classToClass } from 'class-transformer';
let photo = classToClass(photo);
```

You can also use a `ignoreDecorators` option in transformation options to ignore all decorators you classes is using.
You can also use an `ignoreDecorators` option in transformation options to ignore all decorators you classes is using.

### serialize[](#table-of-contents)

You can serialize your model right to the json using `serialize` method:
You can serialize your model right to json using `serialize` method:

```typescript
import { serialize } from 'class-transformer';
Expand All @@ -269,14 +269,14 @@ let photo = serialize(photo);

### deserialize and deserializeArray[](#table-of-contents)

You can deserialize your model to from a json using `deserialize` method:
You can deserialize your model from json using the `deserialize` method:

```typescript
import { deserialize } from 'class-transformer';
let photo = deserialize(Photo, photo);
```

To make deserialization to work with arrays use `deserializeArray` method:
To make deserialization work with arrays, use the `deserializeArray` method:

```typescript
import { deserializeArray } from 'class-transformer';
Expand Down Expand Up @@ -342,7 +342,7 @@ console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true })
## Working with nested objects[](#table-of-contents)

When you are trying to transform objects that have nested objects,
its required to known what type of object you are trying to transform.
it's required to known what type of object you are trying to transform.
Since Typescript does not have good reflection abilities yet,
we should implicitly specify what type of object each property contain.
This is done using `@Type` decorator.
Expand Down Expand Up @@ -374,8 +374,8 @@ let album = plainToClass(Album, albumJson);
### Providing more than one type option[](#table-of-contents)

In case the nested object can be of different types, you can provide an additional options object,
that specifies a discriminator. The discriminator option must define a `property` that holds the sub
type name for the object and the possible `subTypes`, the nested object can converted to. A sub type
that specifies a discriminator. The discriminator option must define a `property` that holds the subtype
name for the object and the possible `subTypes` that the nested object can converted to. A sub type
has a `value`, that holds the constructor of the Type and the `name`, that can match with the `property`
of the discriminator.

Expand Down Expand Up @@ -444,7 +444,7 @@ in the options to keep the discriminator property also inside your resulting cla

## Exposing getters and method return values[](#table-of-contents)

You can expose what your getter or method return by setting a `@Expose()` decorator to those getters or methods:
You can expose what your getter or method return by setting an `@Expose()` decorator to those getters or methods:

```typescript
import { Expose } from 'class-transformer';
Expand All @@ -469,8 +469,8 @@ export class User {

## Exposing properties with different names[](#table-of-contents)

If you want to expose some of properties with a different name,
you can do it by specifying a `name` option to `@Expose` decorator:
If you want to expose some of the properties with a different name,
you can do that by specifying a `name` option to `@Expose` decorator:

```typescript
import { Expose } from 'class-transformer';
Expand Down Expand Up @@ -511,7 +511,7 @@ export class User {
}
```

Now when you transform a User, `password` property will be skipped and not be included in the transformed result.
Now when you transform a User, the `password` property will be skipped and not be included in the transformed result.

## Skipping depend of operation[](#table-of-contents)

Expand All @@ -530,7 +530,7 @@ export class User {
}
```

Now `password` property will be excluded only during `classToPlain` operation. Oppositely, use `toClassOnly` option.
Now `password` property will be excluded only during `classToPlain` operation. Vice versa, use the `toClassOnly` option.

## Skipping all properties of the class[](#table-of-contents)

Expand Down Expand Up @@ -760,7 +760,7 @@ export class Photo {
id: number;

@Type(() => Date)
@Transform(value => moment(value), { toClassOnly: true })
@Transform(({ value }) => moment(value), { toClassOnly: true })
date: Moment;
}
```
Expand All @@ -773,15 +773,17 @@ it will convert a date value in your photo object to moment date.

The `@Transform` decorator is given more arguments to let you configure how you want the transformation to be done.

```
@Transform((value, obj, type) => value)
```ts
@Transform(({ value, key, obj, type }) => value)
```

| Argument | Description |
| -------- | --------------------------------------------- |
| `value` | The property value before the transformation. |
| `obj` | The transformation source object. |
| `type` | The transformation type. |
| Argument | Description |
| --------- | ------------------------------------------------------- |
| `value` | The property value before the transformation. |
| `key` | The name of the transformed property. |
| `obj` | The transformation source object. |
| `type` | The transformation type. |
| `options` | The options object passed to the transformation method. |

## Other decorators[](#table-of-contents)

Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
collectCoverageFrom: ['src/**/*.ts', '!src/**/index.ts', '!src/**/*.interface.ts'],
globals: {
'ts-jest': {
tsConfig: 'tsconfig.spec.json',
tsconfig: 'tsconfig.spec.json',
},
},
};
Loading

0 comments on commit ab093a3

Please sign in to comment.