Mocky Balboa API Reference
    Preparing search index...

    Route passed as an argument to the handler callback on Client.route

    Index

    Constructors

    Accessors

    Methods

    • When you want to execute the request from the client process and send the response back to the server

      Parameters

      Returns Promise<FulfillRouteResponse>

      Execute the request as is from the client and send the response back to the server

      client.route("**/api", (route) => {
      return route.continue();
      })

      Override the request headers and send the response back to the server

      client.route("**/api", (route) => {
      return route.continue({ headers: { "X-Override": "true" } });
      })
    • Fetch the request using the fetch API on the client and return the response

      Parameters

      Returns Promise<Response>

      when attempting to override the request URL with a different protocol

      when a response cannot be resolved after FetchOptions.maxRetries

      When you want to modify the real API response at runtime before sending it back to the server

      client.route("**/api", (route) => {
      const response = await route.fetch();

      // Read the response body as JSON
      const data = await response.json();

      // Update the title
      data.title = "Modified Title";

      // Modify the response body with the modified data
      const modifiedResponse = await route.modifyResponse(response, { body: JSON.stringify(data) });

      // Send the modified response back to the server
      return route.fulfill({ response: modifiedResponse });
      })
    • When fulfilling a route

      Parameters

      Returns FulfillRouteResponse

      Responding with a plain text response of "Hello World"

      client.route("**/api", (route) => {
      return route.fulfill({
      response: new Response("Hello World"),
      });
      })

      Building a response from status, headers, and body

      client.route("**/api", (route) => {
      return route.fulfill({
      headers: { "Content-Type": "application/json" },
      status: 200,
      body: JSON.stringify({ message: "Hello World" }),
      });
      })
    • When you want the server to process the request as is over the network, i.e. ignore the request and do not mock/modify it in any way

      Returns PassthroughRouteResponse

      This is the default fallback behaviour when there are no route handlers found for a given request URL.

      Explicitly pass through the request to the server

      client.route("**/api", (route) => {
      return route.passthrough();
      })