NetworkInterceptor

The NetworkInterceptor intercepts and mocks outgoing fetch() calls. Useful for testing, offline scenarios, and CORS avoidance.

Quick start

const container = boot();

// Static mock
container.mockFetch("/api/users", { json: [{ name: "Alice" }] });

// Dynamic handler
container.onFetch(url => {
  if (url.includes("/health")) return { json: { ok: true } };
  return null; // pass through
});

MockResponse

interface MockResponse {
  status?: number; // default: 200
  headers?: Record<string, string>;
  body?: string;
  json?: unknown; // auto-serialized, overrides body
}

Container methods

MethodDescription
mockFetch(pattern, response)Register static mock (string or RegExp)
onFetch(handler)Register dynamic handler
network.clear()Remove all rules and handlers
network.ruleCountNumber of static rules
network.handlerCountNumber of dynamic handlers

Matching order

  1. Static rules are checked first (first match wins)
  2. Dynamic handlers are checked in registration order
  3. If nothing matches, the request passes through to the real network

mockResponseToFetchResponse

Converts a MockResponse to a fetch-compatible object:

import { mockResponseToFetchResponse } from "@run0/jiki";

const resp = mockResponseToFetchResponse({ json: { ok: true } });
await resp.json(); // { ok: true }