Fetch Client API Documentation - v1.1.1
    Preparing search index...

    Class Interceptor<T>

    Generic interceptor for processing items with middleware pattern

    const interceptor = new Interceptor<string>();
    const id = interceptor.use((str) => str.toUpperCase());
    const result = await interceptor.execute('hello'); // "HELLO"
    console.log(result);
    interceptor.reject(id); // Remove the handler

    Type Parameters

    • T

      The type of item being processed

    Index

    Constructors

    Accessors

    Methods

    Constructors

    Accessors

    • get size(): number

      Get the number of registered handlers

      Returns number

      Number of handlers in the chain

      console.log(`Interceptor has ${interceptor.size} handlers`);
      
    • get isEmpty(): boolean

      Check if the interceptor has any handlers

      Returns boolean

      True if no handlers are registered, false otherwise

      if (interceptor.isEmpty) {
      console.log('No handlers registered');
      }

    Methods

    • Register a new interceptor handler

      Parameters

      • handler: (item: T) => T | Promise<T>

        Function to process the item

      Returns number

      Unique ID for the registered handler

      When handler is not a function

      const id = interceptor.use((item) => {
      console.log('Processing:', item);
      return item;
      });
    • Execute all registered handlers in sequence

      Parameters

      • item: T

        Item to process through the handler chain

      Returns Promise<T>

      Promise resolving to the processed item

      When any handler fails

      try {
      const result = await interceptor.execute(originalItem);
      console.log('Processed:', result);
      } catch (error) {
      console.error('Handler failed:', error.message);
      }
    • Clear all registered handlers

      Returns void

      interceptor.clear();
      console.log(interceptor.isEmpty); // true
    • Remove a specific handler by ID

      Parameters

      • id: number

        Handler ID to remove

      Returns void

      const id = interceptor.use(handler);
      interceptor.reject(id); // Remove the handler