-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowser.ts
More file actions
62 lines (50 loc) · 1.82 KB
/
browser.ts
File metadata and controls
62 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* MSW Browser Example - Standalone Usage
*
* This example shows how to use MSW with ObjectStack in a browser environment.
* It matches the example from the problem statement.
*/
import { setupWorker } from 'msw/browser';
import { http, HttpResponse } from 'msw';
import { ObjectStackServer } from '@objectstack/plugin-msw';
// Mock protocol - in real usage, this would come from runtime
// For this example, we'll simulate it
const mockProtocol = {
getData: async (object: string, id: string) => {
return { id, object, name: `Mock ${object}`, status: 'active' };
},
createData: async (object: string, data: any) => {
return { id: 'new-id', ...data };
},
// Add other methods as needed
} as any;
// 1. Initialize the mock server (equivalent to ObjectStackServer.init())
ObjectStackServer.init(mockProtocol);
// 2. Define request handlers (similar to Express/Koa routes, but in Service Worker)
const handlers = [
// Intercept GET /api/user/:id
http.get('/api/user/:id', async ({ params }) => {
const { id } = params;
// Call local logic
const result = await ObjectStackServer.getUser(id as string);
// Return constructed Response
return HttpResponse.json(result.data, { status: result.status });
}),
// Intercept POST /api/user
http.post('/api/user', async ({ request }) => {
const body = await request.json();
// Call local logic
const result = await ObjectStackServer.createUser(body);
return HttpResponse.json(result.data, { status: result.status });
}),
];
// 3. Create Worker instance
export const worker = setupWorker(...handlers);
// Start the worker (typically called in your app entry point)
if (typeof window !== 'undefined') {
worker.start({
onUnhandledRequest: 'bypass',
}).then(() => {
console.log('[MSW] Mock Service Worker started');
});
}