|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +FakeRest is a browser library that intercepts AJAX calls to mock a REST server based on JSON data. It provides adapters for MSW, fetch-mock, and Sinon.js to enable testing of JavaScript REST clients without a backend server. |
| 8 | + |
| 9 | +## Common Commands |
| 10 | + |
| 11 | +### Development |
| 12 | +```bash |
| 13 | +npm run dev # Run dev server (uses MSW by default) |
| 14 | +make run-msw # Run with MSW adapter |
| 15 | +make run-fetch-mock # Run with fetch-mock adapter |
| 16 | +make run-sinon # Run with Sinon adapter |
| 17 | +``` |
| 18 | + |
| 19 | +### Testing & Quality |
| 20 | +```bash |
| 21 | +npm test # Run tests with Vitest |
| 22 | +npm run format # Format code with Biome |
| 23 | +npm run lint # Lint code with Biome |
| 24 | +``` |
| 25 | + |
| 26 | +### Building |
| 27 | +```bash |
| 28 | +npm run build # Build both minified and non-minified versions |
| 29 | +make build # Production build via Make |
| 30 | +``` |
| 31 | + |
| 32 | +### Running Single Tests |
| 33 | +```bash |
| 34 | +npx vitest run [test-file-pattern] # Run specific test file |
| 35 | +npx vitest [test-name-pattern] # Run tests matching pattern |
| 36 | +``` |
| 37 | + |
| 38 | +## Architecture |
| 39 | + |
| 40 | +### Core Components |
| 41 | + |
| 42 | +The library has a layered architecture: |
| 43 | + |
| 44 | +1. **Adapters Layer** (`src/adapters/`) |
| 45 | + - `MswAdapter`: Integrates with MSW (Mock Service Worker) |
| 46 | + - `FetchMockAdapter`: Integrates with fetch-mock |
| 47 | + - `SinonAdapter`: Integrates with Sinon.js fake server |
| 48 | + - Each adapter normalizes requests from their respective mocking library and transforms responses back |
| 49 | + |
| 50 | +2. **Server Layer** (`SimpleRestServer`) |
| 51 | + - Implements REST semantics (GET, POST, PUT, PATCH, DELETE) |
| 52 | + - Handles routing to collections vs singles |
| 53 | + - Processes middleware chain |
| 54 | + - URL pattern: `/{collection}` or `/{collection}/{id}` or `/{single}` |
| 55 | + |
| 56 | +3. **Database Layer** (`Database`) |
| 57 | + - Manages collections (arrays of records) and singles (single objects) |
| 58 | + - Routes CRUD operations to appropriate collection/single |
| 59 | + - Handles initialization from data objects |
| 60 | + |
| 61 | +4. **Collection/Single Layer** |
| 62 | + - `Collection`: Implements filtering, sorting, pagination, and embedding for array data |
| 63 | + - `Single`: Manages single object resources (e.g., user profile, settings) |
| 64 | + - Both support embedding related resources |
| 65 | + |
| 66 | +### Request Flow |
| 67 | + |
| 68 | +``` |
| 69 | +Mocking Library (MSW/fetch-mock/Sinon) |
| 70 | + ↓ |
| 71 | +Adapter (normalizes request) |
| 72 | + ↓ |
| 73 | +SimpleRestServer.handle() |
| 74 | + ↓ |
| 75 | +Middleware chain (optional) |
| 76 | + ↓ |
| 77 | +SimpleRestServer.handleRequest() |
| 78 | + ↓ |
| 79 | +Database → Collection/Single |
| 80 | + ↓ |
| 81 | +Response (normalized) |
| 82 | + ↓ |
| 83 | +Adapter (transforms to library format) |
| 84 | + ↓ |
| 85 | +Mocking Library |
| 86 | +``` |
| 87 | + |
| 88 | +### Key Concepts |
| 89 | + |
| 90 | +- **Collections**: Array-based resources that support filtering (including `q` for full-text search, operators like `_gte`, `_lte`, `_eq`, `_neq`), sorting, range queries, and embedding |
| 91 | +- **Singles**: Single object resources (not arrays) for endpoints like `/settings` or `/me` |
| 92 | +- **Embedding**: Automatically resolve relationships by embedding related collections/singles in responses via `embed` parameter |
| 93 | +- **Middleware**: Functions that intercept requests to add authentication, validation, delays, or dynamic values |
| 94 | +- **Identifiers**: Customizable per collection (default: `id`, common alternative: `_id` for MongoDB-style APIs) |
| 95 | + |
| 96 | +### File Structure |
| 97 | + |
| 98 | +``` |
| 99 | +src/ |
| 100 | +├── adapters/ # Adapter implementations for different mocking libraries |
| 101 | +├── Collection.ts # Collection logic (filtering, sorting, pagination) |
| 102 | +├── Database.ts # Database managing collections and singles |
| 103 | +├── SimpleRestServer.ts # REST server implementation with middleware support |
| 104 | +├── Single.ts # Single object resource logic |
| 105 | +├── types.ts # TypeScript type definitions |
| 106 | +├── withDelay.ts # Middleware helper for simulating delays |
| 107 | +├── parseQueryString.ts # Query parameter parsing |
| 108 | +└── index.ts # Main entry point, exports public API |
| 109 | +``` |
| 110 | + |
| 111 | +## Code Style |
| 112 | + |
| 113 | +- **Formatter/Linter**: Biome (configured in `biome.json`) |
| 114 | + - 4-space indentation |
| 115 | + - Single quotes for strings |
| 116 | + - Explicit any types allowed (`noExplicitAny: off`) |
| 117 | +- **TypeScript**: All source files use `.ts` extension with explicit `.ts` imports |
| 118 | +- **Testing**: Vitest with happy-dom environment for DOM emulation |
0 commit comments