You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/controllers.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -471,7 +471,7 @@ With @@Returns@@ you can document correctly your endpoint to reflect the correct
471
471
472
472
:::
473
473
474
-
> See our codesandbox example [here](https://codesandbox.io/embed/tsed-swagger-example-ripfl?fontsize=14&hidenavigation=1&theme=dark)
474
+
> See a live example of generics with Swagger documentation in our [interactive demo](https://codesandbox.io/embed/tsed-swagger-example-ripfl?fontsize=14&hidenavigation=1&theme=dark). This demo showcases how to implement and document generic types with proper Swagger/OpenAPI specifications.
Copy file name to clipboardExpand all lines: docs/docs/custom-providers.md
+57-14Lines changed: 57 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,34 @@ There are a lot of scenarios where you might want to bind something directly to
4
4
For example, any constant values, configuration objects created based on the current environment,
5
5
external libraries, or pre-calculated values that depend on few other defined providers.
6
6
7
-
Moreover, you are able to override default implementations, e.g. use different classes or make use of various test doubles (for testing purposes) when needed.
7
+
Moreover, you are able to override default implementations, e.g. use different classes or make use of various test
8
+
doubles (for testing purposes) when needed.
8
9
9
10
One essential thing that you should always keep in mind is that Ts.ED uses @@TokenProvider@@ to identify a dependency.
10
11
11
-
Usually, the auto-generated tokens are equal to classes. If you want to create a custom provider, you'd need to choose a token.
12
+
Usually, the auto-generated tokens are equal to classes. If you want to create a custom provider, you'd need to choose a
13
+
token.
12
14
Mostly, the custom tokens are represented by either plain strings or symbols.
13
15
14
16
Let's go through the available options.
15
17
16
18
Custom providers can also use [hooks](/docs/hooks.md) to handle Ts.ED lifecycle events.
17
19
20
+
For example:
21
+
22
+
```typescript
23
+
import {Injectable} from"@tsed/di";
24
+
25
+
@Injectable()
26
+
classCustomProvider {
27
+
async $onInit() {
28
+
// Initialize your custom provider
29
+
}
30
+
}
31
+
```
32
+
33
+
> There is other hooks available like `$onDestroy`, `$afterRoutesInit`, `$beforeRoutesInit`, `$onReady`, see more [here](/docs/hooks.md).
34
+
18
35
## Register Value
19
36
20
37
The `useValue` syntax is useful when it comes to either define a constant value, put external library into DI container,
@@ -35,6 +52,26 @@ In order to inject custom provider, we use the @@Inject@@ decorator. This decora
35
52
::: warning
36
53
When you declare a provider using a Symbol as a token, you must use the same Symbol to @@Inject@@ decorator.
37
54
TypeScript set Object as metadata key for the Symbol token.
55
+
56
+
```typescript
57
+
// Define the symbol once and export it
58
+
exportconst MY_SERVICE =Symbol.for("MY_SERVICE");
59
+
60
+
// Correct usage
61
+
@Injectable()
62
+
classMyService {
63
+
@Inject(MY_SERVICE)
64
+
service:MyServiceType;
65
+
}
66
+
67
+
// Incorrect usage - creates a new Symbol
68
+
@Injectable()
69
+
classMyService {
70
+
@Inject()
71
+
service:MyServiceType;
72
+
}
73
+
```
74
+
38
75
:::
39
76
40
77
## Register Factory
@@ -46,18 +83,14 @@ It means that factory may accept arguments, that DI will resolve and pass during
In this case, even if any class depends on ConfigService, Ts.ED will inject an instance of the provided class (`ProdConfigService` or `DevConfigService`) instead.
136
+
In this case, even if any class depends on ConfigService, Ts.ED will inject an instance of the provided class (
137
+
`ProdConfigService` or `DevConfigService`) instead.
0 commit comments