@consumidor-positivo/ts-utils - v1.21.0
    Preparing search index...

    Function createRequestsTimeRecorder

    • Creates a per-request timer controller using a generated requestId stored in the request header to correlate request/response.

      Ideal usage: middlewares or interceptors, where startTimer(req) is invoked before the outbound call and getTime(req) after the response arrives.

      Flow:

      • startTimer(req): generates a requestId, injects it into the header and starts the timer.
      • getTime(req): reads the requestId from the header, stops the timer, performs cleanup and returns the elapsed time.

      Notes:

      • The timer reports elapsed time in milliseconds.
      • The entry is always removed from the internal map after stop() to avoid leaks.

      Parameters

      • precise: boolean = true

        Whether to return full precision from the timer (default: true). If false, values are rounded to 2 decimals.

      Returns { getTime(req: Request): null | number; startTimer(req: Request): void }

      • getTime: function
        • Stops the timer associated with req and returns the elapsed time. Also performs cleanup of the entry in the internal map.

          Parameters

          • req: Request

            The same Request used in startTimer.

          Returns null | number

          Elapsed time in ms; null if no timer is found.

      • startTimer: function
        • Starts the request timer and injects the generated requestId into the header.

          Parameters

          • req: Request

            The Request instance to be measured.

          Returns void