Skip to main content
LABS

Service Worker API Mocker

Stop writing custom mock backends. Define your API JSON responses here and generate a secure injection script that mocks fetch calls instantly in your browser.

Service Worker API Mocker (Local-First)

Define your mock API routes below. We will generate a secure, dependency-free Monkey-patch script that intercepts window.fetch in your app. 100% Zero-Server. No Node.js backend required.

Generated Injection Script

// Paste this in your browser console or top-level React/Next.js layout
(function() {
  const originalFetch = window.fetch;
  const mockRoutes = [
  {
    "method": "GET",
    "path": "/api/users",
    "response": "{\n  \"status\": \"success\",\n  \"data\": []\n}",
    "delay": 500
  }
];

  window.fetch = async function(url, options) {
    const method = (options?.method || 'GET').toUpperCase();
    const routeUrl = typeof url === 'string' ? url : url.url;
    
    const matchedMock = mockRoutes.find(r => 
      routeUrl.includes(r.path) && r.method === method
    );

    if (matchedMock) {
      console.log(`%c[Castov Mock Server] Intercepted ${method} ${r.path}`, 'color: #8b5cf6; font-weight: bold;');
      
      if (matchedMock.delay > 0) {
        await new Promise(res => setTimeout(res, matchedMock.delay));
      }

      return new Response(matchedMock.response, {
        status: 200,
        headers: { 'Content-Type': 'application/json' }
      });
    }

    return originalFetch.apply(this, arguments);
  };
  
  console.log("%c[Castov Mock Server] Installed and listening for fetch requests!", "color: #10b981; font-weight: bold; font-size: 14px;");
})();

Local API Mocking

Intercept fetch calls without a backend.

How to use this tool

  1. Define your routes
  2. Copy the script
  3. Paste into your browser console or layout file.

Examples

Frequently Asked Questions

Do I need a Node.js server?
No, this runs a monkey-patch on window.fetch directly in your browser.