Skip to content

Commit 7682489

Browse files
committed
Add robust handlers for metadata and data requests in MSW setup
1 parent 953d614 commit 7682489

1 file changed

Lines changed: 1 addition & 89 deletions

File tree

.storybook/msw-browser.ts

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { ObjectQLPlugin } from '@objectstack/objectql';
1010
import { InMemoryDriver } from '@objectstack/driver-memory';
1111
import { MSWPlugin } from '@objectstack/plugin-msw';
1212
import { config as crmConfig } from '@object-ui/example-crm';
13-
import { http, HttpResponse } from 'msw';
1413

1514
let kernel: ObjectKernel | null = null;
1615
let mswPlugin: MSWPlugin | null = null;
@@ -42,97 +41,10 @@ export async function startMockServer() {
4241
console.error('❌ CRM Config is missing! Skipping AppPlugin.');
4342
}
4443

45-
46-
// Create handlers manually, similar to console app tests
47-
const protocol = kernel.getService('protocol') as any;
48-
49-
// We override mswPlugin handlers or just add ours.
50-
// Since we are not using startServer(), we need to pass handlers to msw-storybook-addon via context
51-
// But this function returns kernel.
52-
// The msw-browser handles customHandlers inside MSWPlugin initialization?
53-
// No, we passed them in constructor.
54-
55-
// Let's redefine handlers to be robust
56-
const manualHandlers = [
57-
// Discovery endpoint
58-
http.get('/api/v1', async () => {
59-
const response = await protocol.getDiscovery();
60-
return HttpResponse.json(response);
61-
}),
62-
http.get('/api/v1/', async () => {
63-
const response = await protocol.getDiscovery();
64-
return HttpResponse.json(response);
65-
}),
66-
67-
// Metadata endpoints
68-
http.get('/api/v1/meta/object/:objectName', async ({ params }) => {
69-
console.log('[MSW] Get Meta:', params.objectName);
70-
try {
71-
const response = await protocol.getMetaItem({
72-
type: 'object',
73-
name: params.objectName as string
74-
});
75-
const payload = (response && response.item) ? response.item : response;
76-
return HttpResponse.json(payload || { error: 'Not found' }, { status: payload ? 200 : 404 });
77-
} catch (e) {
78-
return HttpResponse.json({ error: String(e) }, { status: 500 });
79-
}
80-
}),
81-
82-
// Data List
83-
http.get('/api/v1/data/:objectName', async ({ params, request }) => {
84-
console.log('[MSW] Find:', params.objectName);
85-
try {
86-
const url = new URL(request.url);
87-
const query: any = {};
88-
// Simple query parsing if needed, but for now just pass to driver
89-
const records = await driver.find(params.objectName as string, {});
90-
91-
// Wrap if protocol expects it (OData style often matches { value: [] })
92-
// But Protocol service might do this.
93-
// If we bypass protocol and go to driver, we get raw array.
94-
// Client expects { value: [] } usually?
95-
// Let's check console mock: It returns { value: response }
96-
return HttpResponse.json({ value: records }, { status: 200 });
97-
} catch (e) {
98-
return HttpResponse.json({ error: String(e) }, { status: 500 });
99-
}
100-
}),
101-
102-
// Data Detail
103-
http.get('/api/v1/data/:objectName/:id', async ({ params }) => {
104-
console.log('[MSW] FindOne:', params.objectName, params.id);
105-
try {
106-
// For InMemoryDriver, findOne might need exact ID match.
107-
// Console mock used driver.find with where clause.
108-
// Let's use findOne if available or scan.
109-
const record = await driver.findOne(params.objectName as string, params.id as string);
110-
if (record) return HttpResponse.json(record);
111-
112-
// Fallback to find
113-
const records = await driver.find(params.objectName as string, {});
114-
const found = records.find((r: any) => r._id === params.id || r.id === params.id);
115-
return HttpResponse.json(found || { error: 'Not Found' }, { status: found ? 200 : 404 });
116-
} catch (e) {
117-
return HttpResponse.json({ error: String(e) }, { status: 500 });
118-
}
119-
}),
120-
http.get('/api/bootstrap', async () => {
121-
const contacts = await driver.find('contact', { object: 'contact' });
122-
const stats = { revenue: 125000, leads: 45, deals: 12 };
123-
return HttpResponse.json({
124-
user: { name: "Demo User", role: "admin" },
125-
stats,
126-
contacts: contacts || []
127-
});
128-
})
129-
];
130-
13144
mswPlugin = new MSWPlugin({
13245
enableBrowser: false,
13346
baseUrl: '/api/v1',
134-
logRequests: true,
135-
customHandlers: manualHandlers
47+
logRequests: true
13648
});
13749

13850
kernel.use(mswPlugin);

0 commit comments

Comments
 (0)