|
1 | 1 | /** |
2 | | - * MSW Browser Worker Setup |
| 2 | + * MSW Browser Worker Setup via ObjectStack Service |
3 | 3 | * |
4 | | - * Simplified setup using auto-generated handlers from objectstack.config.ts |
5 | | - * No runtime overhead - just pure MSW handlers generated from your config! |
| 4 | + * This creates a complete ObjectStack environment in the browser using the In-Memory Driver |
| 5 | + * and the MSW Plugin which automatically exposes the API. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { setupWorker } from 'msw/browser'; |
9 | | -import { createMockHandlers, seedData } from './createMockHandlers'; |
| 8 | +import { ObjectStackKernel } from '@objectstack/runtime'; |
| 9 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
| 10 | +import { MSWPlugin } from '@objectstack/plugin-msw'; |
10 | 11 | import appConfig from '../../objectstack.config'; |
11 | 12 |
|
12 | | -/** |
13 | | - * Start the MSW worker with auto-generated handlers |
14 | | - * |
15 | | - * This function: |
16 | | - * 1. Seeds initial data |
17 | | - * 2. Generates MSW handlers automatically from your config |
18 | | - * 3. Starts the MSW worker |
19 | | - */ |
| 13 | +let kernel: ObjectStackKernel | null = null; |
| 14 | + |
20 | 15 | export async function startMockServer() { |
21 | | - // Seed initial task data |
22 | | - seedData('task', [ |
23 | | - { |
24 | | - id: '1', |
25 | | - subject: 'Complete MSW integration example', |
26 | | - priority: 1, |
27 | | - isCompleted: false, |
28 | | - createdAt: new Date().toISOString() |
29 | | - }, |
30 | | - { |
31 | | - id: '2', |
32 | | - subject: 'Test CRUD operations with React', |
33 | | - priority: 2, |
34 | | - isCompleted: false, |
35 | | - createdAt: new Date().toISOString() |
36 | | - }, |
37 | | - { |
38 | | - id: '3', |
39 | | - subject: 'Write documentation', |
40 | | - priority: 3, |
41 | | - isCompleted: true, |
42 | | - createdAt: new Date().toISOString() |
43 | | - } |
44 | | - ]); |
| 16 | + if (kernel) return; |
45 | 17 |
|
46 | | - // Create metadata from config |
47 | | - const metadata = { |
48 | | - objects: (appConfig.objects || []).reduce((acc: any, obj: any) => { |
49 | | - acc[obj.name] = obj; |
50 | | - return acc; |
51 | | - }, {}) |
52 | | - }; |
| 18 | + console.log('[MSW] Starting ObjectStack Runtime (Browser Mode)...'); |
53 | 19 |
|
54 | | - // Create handlers from config |
55 | | - const handlers = createMockHandlers('/api/v1', metadata); |
| 20 | + const driver = new InMemoryDriver(); |
56 | 21 |
|
57 | | - // Start MSW worker |
58 | | - const worker = setupWorker(...handlers); |
59 | | - await worker.start({ |
60 | | - onUnhandledRequest: 'bypass', |
61 | | - }); |
| 22 | + kernel = new ObjectStackKernel([ |
| 23 | + // App Config |
| 24 | + appConfig, |
| 25 | + |
| 26 | + // In-Memory Database (runs in browser) |
| 27 | + driver, |
| 28 | + |
| 29 | + // MSW Plugin (intercepts network requests) |
| 30 | + new MSWPlugin({ |
| 31 | + enableBrowser: true, |
| 32 | + baseUrl: '/api/v1', |
| 33 | + logRequests: true |
| 34 | + }) |
| 35 | + ]); |
62 | 36 |
|
63 | | - console.log('[MSW] Auto-mocked API ready! All endpoints generated from objectstack.config.ts'); |
64 | | - console.log('[MSW] Objects:', Object.keys(metadata.objects)); |
| 37 | + await kernel.start(); |
65 | 38 |
|
66 | | - return worker; |
| 39 | + // Seed Data: Use the driver directly |
| 40 | + if (driver) { |
| 41 | + const tasks = [ |
| 42 | + { |
| 43 | + id: '1', |
| 44 | + subject: 'Complete MSW integration example', |
| 45 | + priority: 1, |
| 46 | + isCompleted: false, |
| 47 | + createdAt: new Date().toISOString() |
| 48 | + }, |
| 49 | + { |
| 50 | + id: '2', |
| 51 | + subject: 'Test CRUD operations with React', |
| 52 | + priority: 2, |
| 53 | + isCompleted: false, |
| 54 | + createdAt: new Date().toISOString() |
| 55 | + }, |
| 56 | + { |
| 57 | + id: '3', |
| 58 | + subject: 'Write documentation', |
| 59 | + priority: 3, |
| 60 | + isCompleted: true, |
| 61 | + createdAt: new Date().toISOString() |
| 62 | + } |
| 63 | + ]; |
| 64 | + |
| 65 | + // Ensure schema exists (Driver internal API) |
| 66 | + if (driver.syncSchema) { |
| 67 | + await driver.syncSchema('task', {}); |
| 68 | + } |
| 69 | + |
| 70 | + // Insert Data |
| 71 | + if (driver.create) { |
| 72 | + for (const task of tasks) { |
| 73 | + try { |
| 74 | + await driver.create('task', task); |
| 75 | + } catch (e) { |
| 76 | + // Ignore key conflict if seeded |
| 77 | + } |
| 78 | + } |
| 79 | + console.log('[MSW] Seeded initial data.'); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return kernel; |
67 | 84 | } |
68 | 85 |
|
0 commit comments