Skip to content

Commit 0496c64

Browse files
committed
feat: implement mock server setup with ObjectStack and update package dependencies
1 parent fbb2209 commit 0496c64

13 files changed

Lines changed: 212 additions & 242 deletions

examples/crm-app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"tailwind-merge": "^3.4.0"
3131
},
3232
"devDependencies": {
33+
"@objectstack/driver-memory": "^0.6.1",
34+
"@objectstack/objectql": "^0.6.1",
3335
"@objectstack/plugin-msw": "^0.6.1",
3436
"@tailwindcss/postcss": "^4.1.18",
3537
"@tailwindcss/vite": "^4.1.18",

examples/crm-app/src/main.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import ReactDOM from 'react-dom/client';
33
import App from './App';
44
import './index.css';
55

6+
import { startMockServer } from './mocks/runtime';
7+
68
async function enableMocking() {
79
if (process.env.NODE_ENV !== 'development') {
810
return;
911
}
10-
const { worker } = await import('./mocks/browser');
11-
return worker.start({
12-
onUnhandledRequest: 'bypass',
13-
});
12+
return startMockServer();
1413
}
1514

1615
enableMocking().then(() => {

examples/crm-app/src/mocks/MemoryProtocol.ts

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

examples/crm-app/src/mocks/browser.new.ts

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

examples/crm-app/src/mocks/browser.ts

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

examples/crm-app/src/mocks/handlers.ts

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

examples/crm-app/src/mocks/protocol.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime';
2+
import { ObjectQLPlugin } from '@objectstack/objectql';
3+
import { InMemoryDriver } from '@objectstack/driver-memory';
4+
import { MSWPlugin } from '@objectstack/plugin-msw';
5+
import appConfig from '../objectstack.config';
6+
import { mockData } from '../data';
7+
import { http, HttpResponse } from 'msw';
8+
9+
let kernel: ObjectKernel | null = null;
10+
11+
export async function startMockServer() {
12+
if (kernel) return kernel;
13+
14+
console.log('[MSW] Starting ObjectStack Runtime (Browser Mode)...');
15+
16+
const driver = new InMemoryDriver();
17+
18+
// Create kernel with MiniKernel architecture
19+
kernel = new ObjectKernel();
20+
21+
kernel
22+
// Register ObjectQL engine
23+
.use(new ObjectQLPlugin())
24+
// Register the driver
25+
.use(new DriverPlugin(driver, 'memory'))
26+
// Load app config as a plugin
27+
.use(new AppPlugin(appConfig))
28+
// MSW Plugin (intercepts network requests)
29+
.use(new MSWPlugin({
30+
enableBrowser: true,
31+
baseUrl: '/api/v1',
32+
logRequests: true,
33+
customHandlers: [
34+
// Custom handlers that are not part of standard CRUD
35+
http.get('/api/bootstrap', async () => {
36+
// We can use ObjectStackServer helper which proxies to the kernel if initialized
37+
// Or better, use the kernel instance we have right here.
38+
// But MSWPlugin might not expose the kernel globally to the handler context easily without closure
39+
// So we use the closure 'kernel' variable.
40+
41+
// However, ObjectQL usually requires a 'session'.
42+
const session = { userId: 'current', isSpaceAdmin: true }; // Mock session
43+
44+
try {
45+
const objectql = kernel!.getService<any>('objectql');
46+
const user = (await objectql.getObject('user').findOne('current', {}, session)) || {};
47+
const contacts = await objectql.getObject('contact').find({}, session);
48+
const opportunities = await objectql.getObject('opportunity').find({}, session);
49+
const stats = { revenue: 125000, leads: 45, deals: 12 };
50+
51+
return HttpResponse.json({
52+
user,
53+
stats,
54+
contacts,
55+
opportunities
56+
});
57+
} catch (e) {
58+
console.error(e);
59+
return new HttpResponse(null, { status: 500 });
60+
}
61+
})
62+
]
63+
}));
64+
65+
await kernel.bootstrap();
66+
67+
// Seed Data
68+
await seedData(kernel);
69+
70+
return kernel;
71+
}
72+
73+
// Helper to seed data into the in-memory driver
74+
async function seedData(kernel: ObjectKernel) {
75+
const session = { userId: 'system' };
76+
const objectql = kernel.getService<any>('objectql');
77+
78+
// Seed User
79+
if (mockData.user) {
80+
// ObjectQL insert usually needs specific structure, but we try simplest first
81+
await objectql.getObject('user').insert({ ...mockData.user, id: 'current', _id: 'current' }, session);
82+
}
83+
84+
// Seed Contacts
85+
if (mockData.contacts) {
86+
for (const contact of mockData.contacts) {
87+
await objectql.getObject('contact').insert({ ...contact, _id: contact.id }, session);
88+
}
89+
}
90+
91+
// Seed Opportunities
92+
if (mockData.opportunities) {
93+
for (const opp of mockData.opportunities) {
94+
await objectql.getObject('opportunity').insert({ ...opp, _id: opp.id }, session);
95+
}
96+
}
97+
98+
// Seed Accounts
99+
const accounts = [
100+
{ id: '1', name: 'Acme Corp', industry: 'Technology' },
101+
{ id: '2', name: 'TechStart Inc', industry: 'Startup' },
102+
{ id: '3', name: 'Global Solutions', industry: 'Consulting' }
103+
];
104+
105+
for (const acc of accounts) {
106+
await objectql.getObject('account').insert({ ...acc, _id: acc.id }, session);
107+
}
108+
109+
console.log('[MockServer] Data seeded successfully');
110+
}
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
import { beforeAll, afterEach, afterAll } from 'vitest';
2-
import { setupServer } from 'msw/node';
31
import '@testing-library/jest-dom';
4-
import { handlers, seedData } from './handlers';
5-
6-
// Initialize mock data
7-
seedData().catch(console.error);
8-
9-
export const server = setupServer(...handlers);
10-
11-
beforeAll(() => server.listen());
12-
afterEach(() => server.resetHandlers());
13-
afterAll(() => server.close());

0 commit comments

Comments
 (0)