@metreeca/http - v0.4.1
    Preparing search index...

    Function mock

    • Creates a middleware answering exchanges with locally computed responses.

      Stands in for the network, in place of the standard function createFetch would otherwise delegate to: each exchange is handed to serve as a Request and answered with the response it returns once delay milliseconds have elapsed, so that loading states, timeouts and error handling are exercised as they would be against a live server. A failure reported by serve reaches the caller as a transport failure would.

      An exchange aborted by its signal before it is answered is rejected with the abort reason, as the standard function does, and serve is not consulted.

      Ignores the Fetch implementation it wraps: middlewares declared before it process exchanges as usual, while middlewares declared after it are never reached, so this middleware is to be declared last in a chain.

      Parameters

      • options: { serve?: (request: Request) => Promise<Response>; delay?: number } = {}

        Mock options

        • Optionalserve?: (request: Request) => Promise<Response>

          The function computing the response to each exchange; defaults to a 200 OK response carrying an empty JSON object

        • Optionaldelay?: number

          The number of milliseconds to wait before answering each exchange; defaults to 0

      Returns Middleware

      A Middleware answering every exchange with the response computed by serve, ignoring the Fetch implementation it wraps

      RangeError If delay is not a non-negative number of milliseconds within the range setTimeout() handles