Timeout Interceptor for NestJS
Description
NestJS Timeout Interceptor repository. It enables setting up a global timeout for a NestJS application, which can be overridden by a Timeout Decorator for controller classes and methods specific timeouts.
This gives the user more flexibility with a small amount of additional code. Method timeouts are preferred over class timeouts, which are preferred over global timeouts. Enjoy!
Installation
$ npm install @fuse-autotech/nest-timeout
Usage
Interceptor
Options:
defaultTimeout
- Number of milliseconds after which the interceptor throws a RequestTimeoutException
if no other timeout is defined for the endpointisEnabled
- Determine if the interceptor is enabled. Defaults to true
. Useful for debugging to avoid timeouts
import { Module } from "@nestjs/common";
import { APP_INTERCEPTOR } from "@nestjs/core";
import { TimeoutInterceptor } from "@fuse-autotech/nest-timeout";
@Module({
imports: [],
controllers: [],
providers: [{
provide: APP_INTERCEPTOR,
useValue: new TimeoutInterceptor({ defaultTimeout: 10000 })
}]
})
export class AppModule {}
@Timeout()
Decorator
Can be used both on Controllers and Controller Methods:
import { Controller, Get, Param, Post } from '@nestjs/common';
import { Timeout } from '@fuse-autotech/nest-timeout';
@Controller('cats')
@Timeout(10000)
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats';
}
@Get(':id')
@Timeout(30000)
findOne(@Param('id') id: string ): string {
return `This action returns a #${id} cat`;
}
@Post()
@Timeout(0)
create(): string {
return 'This action adds a new cat';
}
}
License
Nest timeout is MIT licensed.