This document explains how the mock API system works and how to use it effectively in development.
The mock API system simulates a backend API without requiring a real server. It provides:
The mock API is implemented through several components:
Located in plugins/mock/mockData.ts, this file contains the sample data used by mock services:
// Mock data store for demo purposes
export const mockData = {
// Users
users: [
{ id: 1, name: 'John Doe', email: 'john@example.com', role: 'admin' },
// more users...
],
// Posts
posts: [
{
id: 1,
title: 'Getting Started with Nuxt 3',
// more fields...
},
// more posts...
],
// Products
products: [
// product data...
],
// Authentication tokens (for demo)
tokens: {
'john@example.com': 'mock-token-john',
// more tokens...
}
};
Located in plugins/mock/mockService.ts and plugins/mock/mockAuthService.ts, these files implement the service interfaces using the mock data:
export const createMockCrudService = (resource: string, logger: any) => {
// Check if the resource exists in mock data
if (!mockData[resource]) {
console.warn(`Mock data for resource "${resource}" not found. Creating empty collection.`);
mockData[resource] = [];
}
// Get a reference to the mock data collection
const collection = mockData[resource];
return {
// Service methods that simulate API behavior...
async fetchAll() {
// Implementation...
},
// More methods...
};
};
The plugins/api.ts file determines whether to use mock or real services:
export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig();
const { $axios, $logger } = nuxtApp;
// Check if we should use mock services
const useMockApi = config.public.useMockApi || process.env.USE_MOCK_API === 'true';
// Create API services (real or mock)
const api = {
posts: useMockApi
? createMockCrudService('posts', $logger)
: createCrudService($axios, 'posts', $logger),
// More services...
};
// Return API for use in app
return {
provide: {
api,
useMockApi
}
};
});
Mock mode is enabled by default in the template. To toggle it:
nuxt.config.ts:
runtimeConfig: {
public: {
useMockApi: true, // Change to false to use real API
}
}
USE_MOCK_API=false npm run dev
When mock mode is enabled, you can log in with these credentials:
john@example.com, jane@example.com, or bob@example.comThe UI shows indicators when in mock mode:
To add more mock data for a new resource:
mockData in plugins/mock/mockData.ts:
```typescript
export const mockData = {
// Existing resources…// New resource categories: [ { id: 1, name: ‘Category 1’ }, { id: 2, name: ‘Category 2’ }, // Add more items… ] };
2. Register the service in `plugins/api.ts`:
```typescript
const api = {
// Existing services...
// New service
categories: useMockApi
? createMockCrudService('categories', $logger)
: createCrudService($axios, 'categories', $logger),
};
For custom mock behavior beyond the standard CRUD operations:
plugins/mock/:
```typescript
// plugins/mock/customMockService.ts
import { mockData } from ‘./mockData’;export const createCustomMockService = (logger: any) => { return { // Custom methods… async specialOperation() { try { // Custom implementation… return { /* response data */ }; } catch (error) { logger.error(‘specialOperationError’, ‘customService’); throw error; } } }; };
2. Register it in `plugins/api.ts`:
```typescript
import { createCustomMockService } from './mock/customMockService';
// In the plugin...
const api = {
// Other services...
custom: useMockApi
? createCustomMockService($logger)
: customService($axios, $logger),
};
The mock API has some limitations to be aware of: