Skip to content

Commit 1520ece

Browse files
docs: add documentation for Tokens
1 parent 8bfaf5c commit 1520ece

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

docs/README.md

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ Container.set([
8383

8484
## TypeScript Advanced Usage Examples
8585

86-
- [Services with token name](#services-with-token-name)
8786
- [Using factory function to create service](#using-factory-function-to-create-service)
8887
- [Using factory class to create service](#using-factory-class-to-create-service)
8988
- [Problem with circular references](#problem-with-circular-references)
@@ -93,45 +92,6 @@ Container.set([
9392
- [Using multiple containers and scoped containers](#using-multiple-containers-and-scoped-containers)
9493
- [Remove registered services or reset container state](#remove-registered-services-or-reset-container-state)
9594

96-
### Services with token name
97-
98-
You can use a services with a `Token` instead of name or target class.
99-
In this case you can use type safe interface-based services.
100-
101-
```typescript
102-
import { Container, Service, Inject, Token } from 'typedi';
103-
104-
export interface Factory {
105-
create(): void;
106-
}
107-
108-
export const FactoryService = new Token<Factory>();
109-
110-
@Service(FactoryService)
111-
export class BeanFactory implements Factory {
112-
create() {}
113-
}
114-
115-
@Service()
116-
export class CoffeeMaker {
117-
private factory: Factory;
118-
119-
constructor(@Inject(type => FactoryService) factory: Factory) {
120-
this.factory = factory;
121-
}
122-
123-
make() {
124-
this.factory.create();
125-
}
126-
}
127-
128-
let coffeeMaker = Container.get(CoffeeMaker);
129-
coffeeMaker.make();
130-
131-
let factory = Container.get(FactoryService); // factory is instance of Factory
132-
factory.create();
133-
```
134-
13595
### Using factory function to create service
13696

13797
You can create your services with the container using factory functions.

docs/typescript/06-service-tokens.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
11
# Service Tokens
2+
3+
Service tokens are unique identifiers what provides a type-safe access to a value stored in a `Conatiner`.
4+
5+
```ts
6+
import 'reflect-metadata';
7+
import { Container, Token } from 'typedi';
8+
9+
export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');
10+
11+
Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');
12+
13+
/**
14+
* Somewhere else in the application after the JWT_SECRET_TOKEN is
15+
* imported in can be used to request the secret from the Container.
16+
*
17+
* This value is type-safe also because the Token is typed.
18+
*/
19+
const JWT_SECRET = Container.get(JWT_SECRET_TOKEN);
20+
```
21+
22+
## Injecting service tokens
23+
24+
They can be used with the `@Inject()` decorator to overwrite the inferred type of the property of argument.
25+
26+
```ts
27+
import 'reflect-metadata';
28+
import { Container, Token, Inject, Service } from 'typedi';
29+
30+
export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');
31+
32+
Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');
33+
34+
@Service()
35+
class Example {
36+
@Inject(JWT_SECRET_TOKEN)
37+
myProp: string;
38+
}
39+
40+
const instance = Container.get(Example);
41+
// The instance.myProp property has the value assigned for the Token
42+
```
43+
44+
## Tokens with same name
45+
46+
Two token **with the same name are different tokens**. The name is only used to help the developer identify the tokens
47+
during debugging and development. (It's included in error the messages.)
48+
49+
```ts
50+
import 'reflect-metadata';
51+
import { Container, Token } from 'typedi';
52+
53+
const tokenA = new Token('TOKEN');
54+
const tokenB = new Token('TOKEN');
55+
56+
Container.set(tokenA, 'value-A');
57+
Container.set(tokenB, 'value-B');
58+
59+
const tokenValueA = Container.get(tokenA);
60+
// tokenValueA is "value-A"
61+
const tokenValueB = Container.get(tokenB);
62+
// tokenValueB is "value-B"
63+
64+
console.log(tokenValueA === tokenValueB);
65+
// returns false, as Tokens are always unique
66+
```
67+
68+
### Difference between Token and string identifier
69+
70+
They both achieve the same goal, however it's recommended to use `Tokens` as they are type-safe and cannot be mistyped,
71+
while a mistyped string identifier will silently return `undefined` as value by default.

0 commit comments

Comments
 (0)