UNPKG

nestjs-aborter

Version:

Automatic request cancellation and timeout handling for NestJS applications

38 lines (37 loc) 1.44 kB
import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; import { Request } from 'express'; import type { AborterOptions } from '../interfaces/aborter.interface'; import { Reflector } from '@nestjs/core'; export declare const ABORT_CONTROLLER_KEY = "abortController"; export declare const ABORT_CONTROLLER_OPTIONS = "ABORT_CONTROLLER_OPTIONS"; /** * Extended Request interface with AbortController support. */ export interface RequestWithAbortController extends Request { abortController: AbortController; abortReason?: string; effectiveTimeout?: number; } /** * Interceptor that automatically attaches an AbortController to every HTTP request. * * This enables automatic cancellation of ongoing operations when: * - The client disconnects * - A configured timeout is reached * - An error occurs during request processing */ export declare class AborterInterceptor implements NestInterceptor { private readonly reflector; private readonly options; private readonly logger; constructor(reflector: Reflector, options?: AborterOptions); intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>; private getEffectiveTimeout; private attachAbortController; private setupRequestCleanup; private createAbortObservable; private createTimeoutObservable; private shouldSkipRoute; private logAbort; }