Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  101] [ 5]  / answers: 1 / hits: 7087  / 2 Years ago, thu, may 19, 2022, 12:00:00

I just started to learn about NestJS and I am wondering how could I manipulate response timeout for particular endpoints?


I could do it on a server level like:


  const server = await app.listen(...);
server.setTimeout(1800000)

or on endpoint, which looks bad:


  @Post('/test')
public async import(...props, @Res() res: Response): Promise<string> {
res.setTimeout(1800000)
}

But how could I do that on controller or method level?
I have tried to increase timeout on endpoint using interceptors like:


import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, take, timeout } from 'rxjs/operators';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {

return next.handle().pipe(
timeout(1800000),
catchError(err => {
if (err instanceof TimeoutError) {
return throwError(() => new RequestTimeoutException());
}
return throwError(() => err);
}),
);
};
};

And applying it on endpoint like:


  @Post('/test')
@UseInterceptors(TimeoutInterceptor)
public async import(...props, @Res() res: Response): Promise<string> {
long running code...
}

Although interceptor is triggered so I am able to log something
the timeout does not seems to work at all :/


More From » node.js

 Answers
11
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
constructor(
private readonly reflector: Reflector,
) {}

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const response = context.switchToHttp().getResponse();
const timeout = this.reflector.get<number>('request-timeout', context.getHandler()) || 60000;
response.setTimeout(timeout )

return next.handle();
};
};

import { applyDecorators, SetMetadata, UseInterceptors } from '@nestjs/common';


const SetTimeout = (timeout: number) => SetMetadata('request-timeout', timeout);

export function SetRequestTimeout(timeout: number = 600000) {
return applyDecorators(
SetTimeout(timeout),
UseInterceptors(TimeoutInterceptor),
);
}

You might have to play a bit with providers (add the interceptor to it)
But now you might only use @SetRequestTimeout() or @SetRequestTimeout(10000) for convenience.


[#135] Monday, May 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janayr

Total Points: 80
Total Questions: 80
Total Answers: 114

Location: Venezuela
Member since Sat, Aug 22, 2020
4 Years ago
janayr questions
Wed, Dec 29, 21, 00:00, 2 Years ago
Sun, Oct 31, 21, 00:00, 3 Years ago
Tue, Feb 4, 20, 00:00, 4 Years ago
;