@fuse-autotech/nest-timeout
v2.1.0
Published
This package enables to use a custom timeout alongside with the global timeout interceptor.
Downloads
26,488
Readme
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 aRequestTimeoutException
if no other timeout is defined for the endpointisEnabled
- Determine if the interceptor is enabled. Defaults totrue
. 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) // Applied for all methods unless decorated
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats';
}
@Get(':id')
@Timeout(30000) // Overrides controller timeout
findOne(@Param('id') id: string ): string {
return `This action returns a #${id} cat`;
}
@Post()
@Timeout(0) // Disables timeout for the method
create(): string {
return 'This action adds a new cat';
}
}
License
Nest timeout is MIT licensed.