#How to initialise the middleware while applying?

4 messages · Page 1 of 1 (latest)

runic bobcat
#
@Module({
  imports: [PassportModule.register({ session: false })],
  providers: [ClerkStrategy, ApiKeyStrategy],
})
export class AuthModule implements NestModule {
  constructor(private readonly configService: ConfigService) {}

  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(new SvixWebhookVerifyMiddleware(this.configService.getOrThrow('AUTH_MODULE_WEBHOOK_SECRET')))
      .forRoutes({ method: RequestMethod.POST, path: '/auth/webhook', version: 'v1' });
  }
}

The above code fails with following error

src/auth/auth.module.ts:17:14 - error TS2345: Argument of type 'SvixWebhookVerifyMiddleware' is not assignable to parameter of type 'Function | Type<any>'.

17       .apply(new SvixWebhookVerifyMiddleware(this.configService.getOrThrow('AUTH_MODULE_WEBHOOK_SECRET')))
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
young zodiac
#

you know that nestjs middlewares can be injectable as well, right?

#

so you could move that this.configService.getOrThrow('AUTH_MODULE_WEBHOOK_SECRET')) logic to the middleware class
But that is not the only way to solve this

#

another way would be passing the SvixWebhookVerifyMiddleware#use method instead of an instance of SvixWebhookVerifyMiddleware to .apply