@grafctory/http - v0.9.0
    Preparing search index...

    Function monitor

    • Creates an exchange monitoring middleware.

      Wraps a Fetch implementation, logging every exchange it relays and leaving the exchange itself untouched.

      Every entry opens with the method and the target URL of the exchange. The method is stated in uppercase and the URL is clipped, so that entries stay uniform and on a single line. An exchange is logged as it is sent. It is logged once more as it settles, unless it ends with a successful response served by the origin server:

      • Request is sent: logged to logger.info, with a malformed URL logged as stated
      • Response is not ok (non-2xx status): its status and statusText are logged to logger.warn, followed by the error type its Proxy-Status field states, if any, enclosed in square brackets
      • Response is ok (2xx status) and served from a cache: its status is logged to logger.info, with a note stating that the content came from a cache, as told by the Age header field that only a cache adds to a response
      • Response is ok (2xx status) and served by the origin: nothing further is logged, as the entry stating the request was sent already covers the whole of a plain exchange with the origin server
      • Exchange fails without a response: the error message is logged to logger.warn, followed by the code or else the message of its cause, if any, and the failure is then relayed as it stands
      • Exchange is aborted by the caller: logged to logger.info and relayed as it stands

      The busy status of the client is stated to logger.busy as well. It turns true as the first exchange is relayed and false as the last one in flight settles, whether it failed or not. A waiting indicator is thus driven without the call sites keeping count. A request stating a malformed URL is never sent, so it leaves the busy status as it is.

      Every member of the logger is optional, and whatever a logger leaves out is not logged.

      Requests are inspected as they are stated, rather than normalised into Request objects, so that a body carried by the request reaches the layers below untouched.

      Tip

      With resolve declared below this middleware, failed exchanges reach it as responses stating why they failed. A malformed URL is then logged with the 400 answering it, and a refused connection with the 502 and the connection_refused error type stated in the Proxy-Status field answering it.

      Parameters

      • logger: {
            busy?: (status: boolean) => unknown;
            info?: (message: string) => unknown;
            warn?: (message: string) => unknown;
        }

        The logger exchanges are logged to, for instance the platform console

        • Optional Readonlybusy?: (status: boolean) => unknown

          Takes true as the client turns busy and false as it turns idle again

        • Optional Readonlyinfo?: (message: string) => unknown

          Takes an entry stating a sent request, a response served from a cache or an aborted exchange

        • Optional Readonlywarn?: (message: string) => unknown

          Takes an entry stating an unsuccessful response or a failed exchange

      Returns Middleware

      A Middleware logging every exchange it relays to logger