カスタム インターセプターを使用するコントローラーがあります。
コントローラ:
@UseInterceptors(SignInterceptor)
@Get('users')
async findOne(@Query() getUserDto: GetUser) {
return await this.userService.findByUsername(getUserDto.username)
}
NestJwt のラッパーである SignService もあります。
SignService モジュール:
@Module({
imports: [
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
privateKey: configService.get('PRIVATE_KEY'),
publicKey: configService.get('PUBLIC_KEY'),
signOptions: {
expiresIn: configService.get('JWT_EXP_TIME_IN_SECONDS'),
algorithm: 'RS256',
},
}),
inject: [ConfigService],
}),
],
providers: [SignService],
exports: [SignService],
})
export class SignModule {}
そして最後に SignInterceptor:
@Injectable()
export class SignInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(map(data => this.sign(data)))
}
sign(data) {
const signed = {
...data,
_signed: 'signedContent',
}
return signed
}
}
SignService は正常に動作し、使用しています。これをインターセプターとして使用したいのですが、SignInterceptor に SignService を挿入して、それが提供する機能を使用するにはどうすればよいですか?