Skip to content

Mock Server Implementation#4

Open
dagimene wants to merge 1 commit intowebflow:mainfrom
dagimene:mock-server
Open

Mock Server Implementation#4
dagimene wants to merge 1 commit intowebflow:mainfrom
dagimene:mock-server

Conversation

@dagimene
Copy link
Copy Markdown

@dagimene dagimene commented Mar 31, 2026

Add supports for mock server that run in the client.

API

Patterns can define routes to provide a mock API server. When a pattern with routes is loaded, Codeflow intercepts fetch calls to /api/* via a Service Worker and routes them to your handlers — no real backend needed.

Each route has:

  • method: HTTP method — 'GET', 'POST', 'PUT', or 'DELETE'
  • path: URL path starting with /api/. Supports named parameters with :param syntax (e.g. /api/items/:id)
  • handler: Async function that receives an ApiRequest and returns the response body

The ApiRequest object contains:

  • params: Named path parameters (e.g. { id: '42' } for /api/items/:id)
  • query: Query string parameters (e.g. { search: 'foo' } for /api/items?search=foo)
  • body: Parsed JSON request body (for POST/PUT requests)

Handlers run in the main thread with a simulated network delay. Applications use standard fetch('/api/...') calls as if the route handlers were running server-side.

How it works

A Service Worker captures all outgoing /api/* requests, and connects back to the main thread via a MessageChannel. With this architecture, the API implementations run in the browser and refresh in the same way as the rest of the app. Yet, all requests are made with fetch and can be inspected in the network tab.

Check the latest interview challenges to try it out.

Adds a service worker that captures all outgoing `/api/*` requests, and connects back to the main thread via a MessageChannel. That way, code running in the browser can implement the APIs. All requests can be inspected in the network tab.
Copy link
Copy Markdown
Collaborator

@kadenBeckstead kadenBeckstead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really valuable addition, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants