Mock API – What it is, how we use it, and how to move to a real API

Documentation for Public

What is the Mock API (in simple words)?

The Mock API is an HTTP interceptor for Angular that can answer requests right inside the app, without contacting a real server. It lets us develop screens quickly with predictable data.

How it’s enabled in docs-angular

En app.config.ts la app registra el interceptor y lo configura en el arranque (solo en dev):

provideHttpClient(
  withInterceptorsFromDi(),
  withInterceptors([mockHttpInterceptor])
),

// In APP_INITIALIZER
utils.configureMockInterceptor(
  {
    enabled: !environment.production,
    fallbackToNetwork: true,
    delay: [80, 220],
    logging: { enabled: true, level: 'info', logRequests: true },
  },
  DocsMocks
);

Mocks are defined in apps/docs-angular/src/app/mocks/index.ts:

export const DocsMocks: MockDefinition = {
  'GET /api/docs/health': () => ({ status: 200, data: { ok: true } }),
  'GET /api/docs/projects': () => ({ status: 200, data: [ { id: 'public', name: 'Public Docs' } ] }),
};

Each entry follows: 'VERB /path' : (req) => MockResponse.

Does it actually save data?

Recommended usage pattern

  1. Define mocks para flujos de lectura/escritura que no dependan de persistencia real.
  2. Para persistencia real, usa un endpoint del servidor (no lo moquees) y deja fallbackToNetwork: true.
  3. Aprovecha latencias simuladas y logging para probar UX.

Complete example (docs-angular)

// apps/docs-angular/src/app/mocks/index.ts
export const DocsMocks: MockDefinition = {
  'GET /api/docs/health': () => ({ status: 200, data: { ok: true, ts: Date.now() } }),
  'POST /api/users': (req) => ({ status: 201, data: { ...req.body, id: Date.now() } }),
};

At runtime you’ll see logs like:

mock-http-interceptor.ts: Mock Request procesado: { method: 'GET', url: '/api/docs/health', matched: true }
mock-http-interceptor.ts: Mock Request procesado: { method: 'POST', url: '/api/admin/docs/save-folders-config', matched: false }

Summary

How to move from mocks to a real API

  1. Keep the same URL and method for the real endpoint (e.g., keep GET /api/items).
  2. Return the same data shape the UI already expects.
  3. Remove the corresponding entry from DocsMocks (or disable it). With fallbackToNetwork, the app will call the real server immediately.
  4. Keep logging on temporarily to verify matched: false for those endpoints.
  5. Add proper error codes at the server and verify the UI flows with real failures as well.