Skip to content

Commit 7ac8e62

Browse files
committed
improved example with clearer return types
1 parent 0319991 commit 7ac8e62

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ which support all options of their `class-transformer` respective counterparts `
2828

2929
### `InjectTransform`
3030

31-
Replace your `Transform` decorator with the dependency injection enabled `InjectTransform` decorator.
32-
3331
To inject a dependencies pass an array of injection tokens to the `inject` option. They will be passed
3432
as additional arguments to your transform function, in the order they were given:
3533

@@ -75,10 +73,9 @@ export class MyDTO {
7573

7674
### `InjectType`
7775

78-
This decorator allows you to provide a dependency injection enabled type injector. Like the
79-
type transformer you can use the type injector's class body to scaffold your dependencies.
80-
81-
Its `inject` function is called with the same arguments as the `Type` function would have been.
76+
A `TypeInjector` lets you inject types similar to the `Type` decorator. Its `inject` function is
77+
called with the same arguments as the `Type` function would have been and should return the type
78+
to be used.
8279

8380
The following example illustrates how you could return different DTO types (and thereby different
8481
validation schemes when used with `class-validator`), based on a supposed client's
@@ -89,17 +86,35 @@ configuration:
8986
class ClientDtoInjector implements TypeInjector {
9087
constructor(
9188
private readonly service: ClientConfigurationService
92-
) {
93-
}
89+
) {}
9490

9591
inject(type?: TypeHelpOptions) {
9692
const client = type.object['client'] ?? 'default';
9793
const clientConfig = this.service.getClientConfiguration(client);
98-
const dto = clientConfig.getNestedDTO(type.newObject, type.property);
99-
return dto;
94+
if (clientConfig.accountType === 'named') {
95+
return NamedAccountDTO;
96+
} else if (clientConfig.accountType === 'numbered') {
97+
return NumberedAccountDTO;
98+
}
99+
return AccountDTO;
100100
}
101101
}
102102

103+
class AccountDTO {
104+
@IsString()
105+
name: string;
106+
}
107+
108+
class NamedAccountDTO extends AccountDTO {
109+
@IsString()
110+
id: string
111+
}
112+
113+
class NumberedAccountDTO extends AccountDTO {
114+
@IsNumber()
115+
id: number
116+
}
117+
103118
class OpenAccountDTO {
104119
@IsString()
105120
client: string;

0 commit comments

Comments
 (0)