One Typed API Interface, Two Adapters: Developing React Against a Mock or Laravel

If your React components call fetch directly, you cannot demo the app without a server and you cannot test it without mocking the network. A typed interface with swappable adapters solves both. Here is the structure from my TaskFlow project.

7 min readBy Saad Ali KhanOct 2026
One Typed API Interface, Two Adapters: Developing React Against a Mock or Laravel

The problem with calling fetch in components

When components talk to the network directly, three things get hard: running the UI without a backend (for demos, design work or CI), testing it without mocking fetch everywhere, and keeping the front end's idea of the API in sync with the real one.

Step 1: define the surface once

Describe everything the UI needs from the server as one TypeScript interface. The components only ever import this interface:

export interface Api {
  readonly mode: "http" | "mock";
  auth: {
    login(input: LoginInput): Promise<AuthResult>;
    logout(): Promise<void>;
    me(): Promise<User>;
  };
  projects: {
    list(): Promise<Project[]>;
    create(input: ProjectInput): Promise<Project>;
    remove(id: number): Promise<void>;
  };
  tasks: {
    list(projectId: number): Promise<Task[]>;
    create(projectId: number, input: TaskInput): Promise<Task>;
    move(id: number, input: MoveInput): Promise<Task>;
  };
}

Step 2: two implementations

The HTTP adapter talks to the real Laravel API and turns its uniform error shape into a typed ApiError. The mock adapter keeps everything in memory (and in localStorage for a demo), simulates latency, and enforces the same rules: 401 when not signed in, 422 with field errors for bad input, 404 for missing records.

Step 3: choose at build time

A build-time environment variable picks the adapter. Because it is a direct comparison, the bundler removes the one you did not select:

export const api: Api =
  import.meta.env.VITE_API_MODE === "mock"
    ? createMockAdapter()
    : createHttpAdapter({ baseUrl: import.meta.env.VITE_API_URL ?? "http://localhost:8000/api/v1" });

That is how the same React code produces a static demo (mock adapter, no server needed) and a production build (HTTP adapter, real Laravel API).

Step 4: one contract test suite, run against every adapter

The risk with a mock is that it drifts from the real thing. To limit that, write the behaviour you expect once, as a function that takes an adapter factory, and run it against each implementation:

export function runApiContract(name: string, factory: () => Api) {
  describe(`Api contract: ${name}`, () => {
    it("rejects unauthenticated calls with 401", async () => {
      expect((await rejects(api.projects.list())).status).toBe(401);
    });
    // login, validation errors, ownership, move/reorder behaviour ...
  });
}

runApiContract("mockAdapter", () => createMockAdapter());

The same file can be pointed at the HTTP adapter against a running test server. In this project the HTTP adapter has its own tests with a stubbed fetch, and I only smoke-tested it by hand against a live Laravel server. I would not claim more than that.

What you gain

  • A deployable demo with no backend. Useful for a portfolio, a design review or a quick stakeholder preview.
  • Fast, deterministic UI tests. No network, no flaky timing.
  • A written contract. The interface and its shared tests document what the API promises.
  • Room to change backends. Swapping Laravel for another service means writing one adapter.

What it costs

You maintain a mock. Keep it small, keep it honest about errors, and make the contract tests the thing that stops it drifting. Do not let the mock become a second implementation of your business rules; if it needs complex logic, that logic probably belongs behind the real API.

The complete project, a Laravel 12 API plus the React app, is in the TaskFlow case study, with a live demo running on the mock adapter.

Original article written by Saad Ali Khan. Code samples are illustrative and free to adapt in your own projects.

Want this done right in your project?

Send a short description and I'll reply by email with questions or a suggested approach.