Skip to content

Commit 3bc6fb3

Browse files
committed
docs(recipes): Add JS code examples to ALS
1 parent bbd24d8 commit 3bc6fb3

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

content/recipes/async-local-storage.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,43 @@ export class AppModule implements NestModule {
6363
.forRoutes('*');
6464
}
6565
}
66+
@@switch
67+
@Module({
68+
imports: [AlsModule]
69+
providers: [CatService],
70+
controllers: [CatController],
71+
})
72+
@Dependencies(AsyncLocalStorage)
73+
export class AppModule {
74+
constructor(als) {
75+
// inject the AsyncLocalStorage in the module constructor,
76+
this.als = als
77+
}
78+
79+
configure(consumer) {
80+
// bind the middleware,
81+
consumer
82+
.apply((req, res, next) => {
83+
// populate the store with some default values
84+
// based on the request,
85+
const store = {
86+
userId: req.headers['x-user-id'],
87+
};
88+
// and and pass the "next" function as callback
89+
// to the "als.run" method together with the store.
90+
als.run(store, () => next());
91+
})
92+
// and register it for all routes (in case of Fastify use '(.*)')
93+
.forRoutes('*');
94+
}
95+
}
6696
```
6797

6898
3. Now, anywhere within the lifecycle of a request, we can access the local store instance.
6999

70100
```ts
71101
@@filename(cat.service)
102+
@Injectable()
72103
export class CatService {
73104
constructor(
74105
// We can inject the provided ALS instance.
@@ -83,6 +114,23 @@ export class CatService {
83114
return this.catRepository.getForUser(userId);
84115
}
85116
}
117+
@@switch
118+
@Injectable()
119+
@Dependencies(AsyncLocalStorage, CatRepository)
120+
export class CatService {
121+
constructor(als, catRepository) {
122+
// We can inject the provided ALS instance.
123+
this.als = als
124+
this.catRepository = catRepository
125+
}
126+
127+
getCatForUser() {
128+
// The "getStore" method will always return the
129+
// store instance associated with the given request.
130+
const userId = this.als.getStore()["userId"] as number;
131+
return this.catRepository.getForUser(userId);
132+
}
133+
}
86134
```
87135

88136
4. That's it. Now we have a way to share request related state without needing to inject the whole `REQUEST` object.
@@ -139,6 +187,7 @@ export class AppModule {}
139187

140188
```ts
141189
@@filename(cat.service)
190+
@Injectable()
142191
export class CatService {
143192
constructor(
144193
// We can inject the provided ClsService instance,
@@ -152,6 +201,22 @@ export class CatService {
152201
return this.catRepository.getForUser(userId);
153202
}
154203
}
204+
@@switch
205+
@Injectable()
206+
@Dependencies(AsyncLocalStorage, CatRepository)
207+
export class CatService {
208+
constructor(als, catRepository) {
209+
// We can inject the provided ClsService instance,
210+
this.als = als
211+
this.catRepository = catRepository
212+
}
213+
214+
getCatForUser() {
215+
// and use the "get" method to retrieve any stored value.
216+
const userId = this.cls.get('userId');
217+
return this.catRepository.getForUser(userId);
218+
}
219+
}
155220
```
156221

157222
3. To get strong typing of the store values managed by the `ClsService` (and also get auto-suggestions of the string keys), we can use an optional type parameter `ClsService<MyClsStore>` when injecting it.

0 commit comments

Comments
 (0)