Skip to content

Commit 425e7da

Browse files
committed
Add @objectstack/plugin-msw and @objectstack/runtime dependencies; refactor browser mock server setup
1 parent 4576865 commit 425e7da

File tree

4 files changed

+125
-241
lines changed

4 files changed

+125
-241
lines changed

examples/msw-react-crud/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"dependencies": {
1313
"@objectstack/client": "workspace:*",
1414
"@objectstack/driver-memory": "workspace:*",
15+
"@objectstack/plugin-msw": "^0.3.3",
16+
"@objectstack/runtime": "^0.3.3",
1517
"@objectstack/spec": "workspace:*",
1618
"react": "^18.3.1",
1719
"react-dom": "^18.3.1"
Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,85 @@
11
/**
2-
* MSW Browser Worker Setup
2+
* MSW Browser Worker Setup via ObjectStack Service
33
*
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.
66
*/
77

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';
1011
import appConfig from '../../objectstack.config';
1112

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+
2015
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;
4517

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)...');
5319

54-
// Create handlers from config
55-
const handlers = createMockHandlers('/api/v1', metadata);
20+
const driver = new InMemoryDriver();
5621

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+
]);
6236

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();
6538

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;
6784
}
6885

examples/msw-react-crud/src/mocks/createMockHandlers.ts

Lines changed: 0 additions & 187 deletions
This file was deleted.

0 commit comments

Comments
 (0)