Creating Type-safe ClsService using custom Module #70
-
|
Can someone explain to me how to use this module that explained in README.md file export class MyClsService extends ClsService<MyClsStore> {}
@Module({
imports: [ClsModule.forFeature()],
providers: [{
provide: MyClsService,
useExisting: ClsService
}],
exports: [MyClsService]
})
class MyClsModule {}I need to know after creating a custom Currently I use @Module({
imports: [
// Register the ClsModule,
ClsModule.forRoot({
middleware: {
// automatically mount the
// ClsMiddleware for all routes
mount: true,
// and use the setup method to
// provide default store values.
setup: (cls, req) => {
cls.set('userId', req.headers['x-user-id']);
},
},
}),
],
providers: [CatService],
controllers: [CatController],
})
export class AppModule {} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
You just inject You do that in addition to registering it in for-root. With this method, you're basically just aliasing If you want type safety even in the setup: (cls: MyClsService, req) => {
cls.set('userId', req.headers['x-user-id']);
}, |
Beta Was this translation helpful? Give feedback.
You just inject
MyClsServicethe same way you would injectClsService. It will be typed with yourMyClsStore. Of course,MyClsModulemust be imported in the module whereMyClsServiceis needed.You do that in addition to registering it in for-root. With this method, you're basically just aliasing
ClsServicetoMyClsServiceto get type inference, but the sameClsServiceis still used underneath.If you want type safety even in the
setupmethod, you can type it like so: