All files / libs/kernel/api/src/lib/interceptors timeout.interceptor.ts

0% Statements 0/93
0% Branches 0/1
0% Functions 0/1
0% Lines 0/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                                                                                                                                                                           
import { type IncomingMessage } from 'node:http';

import {
  Injectable,
  RequestTimeoutException,
  type CallHandler,
  type ExecutionContext,
  type NestInterceptor,
} from '@nestjs/common';
import { throwError, TimeoutError, type Observable } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';
import { match, P } from 'ts-pattern';

enum HttpMethod {
  DELETE = 'DELETE',
  GET = 'GET',
  PATCH = 'PATCH',
  POST = 'POST',
  PUT = 'PUT',
}

/**
 * This is the default nestjs timeout.
 */
const REGULAR_TIMEOUT = 20_000;

const EXTENDED_TIMEOUT = REGULAR_TIMEOUT * 2;

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  public intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
    const incomingMessage = context.switchToHttp().getRequest<IncomingMessage>();

    const { url, method } = incomingMessage as { url: string; method: HttpMethod };

    const routeTimeout = match({ url, method })
      .with(
        {
          url: P.when((url) => url.startsWith('/api/companies')),
          method: P.when((method) => [HttpMethod.DELETE, HttpMethod.POST].includes(method)),
        },
        () => 80_000,
      )
      .with(
        {
          url: P.when((url) => url.startsWith('/api/connectors')),
          method: P.when((method) => method === HttpMethod.GET),
        },
        () => EXTENDED_TIMEOUT,
      )
      .with(
        {
          url: P.when((url) => url.startsWith('/api/custom-reports') && url.includes('/records')),
          method: P.when((method) => method === HttpMethod.POST),
        },
        () => EXTENDED_TIMEOUT,
      )
      .with(
        {
          url: P.when((url) => url.startsWith('/api/objects')),
          method: P.when((method) => method === HttpMethod.GET),
        },
        () => EXTENDED_TIMEOUT,
      )
      .with(
        {
          url: P.when((url) => url.startsWith('/assistant')),
          method: P.any,
        },
        () => 120_000,
      )
      .with(
        {
          // Plan agreements could be slow due to googleapis, most of the work is done async on the worker
          // but a few request could take longer than expected when manipulating using the API
          url: P.when((url) => url.startsWith('/api/plan-agreements')),
          method: P.when((method) => [HttpMethod.GET, HttpMethod.PATCH, HttpMethod.POST].includes(method)),
        },
        () => 60_000,
      )
      .otherwise(() => REGULAR_TIMEOUT);

    return next.handle().pipe(
      timeout(routeTimeout),
      catchError((err: Error) => {
        if (err instanceof TimeoutError) {
          return throwError(() => new RequestTimeoutException('Request takes longer than expected...'));
        }
        return throwError(() => err);
      }),
    );
  }
}