22 * MSW Browser Worker Setup
33 *
44 * Simplified setup using auto-generated handlers from objectstack.config.ts
5- * All API endpoints are automatically mocked based on your data model
5+ * No runtime overhead - just pure MSW handlers generated from your config!
66 */
77
88import { setupWorker } from 'msw/browser' ;
9- import { http , HttpResponse } from 'msw' ;
10- import { ObjectStackKernel } from '@objectstack/runtime' ;
11- import { ObjectStackRuntimeProtocol } from '@objectstack/runtime' ;
9+ import { createMockHandlers , seedData } from './createMockHandlers' ;
1210import appConfig from '../../objectstack.config' ;
1311
14- let runtime : ObjectStackKernel | null = null ;
15- let protocol : ObjectStackRuntimeProtocol | null = null ;
16-
17- /**
18- * Initialize the ObjectStack runtime with your app configuration
19- */
20- async function initializeRuntime ( ) {
21- runtime = new ObjectStackKernel ( [ appConfig ] ) ;
22- await runtime . start ( ) ;
23- protocol = new ObjectStackRuntimeProtocol ( runtime ) ;
24- console . log ( '[MSW] ObjectStack runtime initialized' ) ;
25- }
26-
27- /**
28- * Generate MSW handlers automatically from the runtime protocol
29- */
30- function createHandlers ( baseUrl : string = '/api/v1' ) {
31- if ( ! protocol ) {
32- throw new Error ( 'Runtime not initialized. Call initializeRuntime() first.' ) ;
33- }
34-
35- return [
36- // Discovery endpoint
37- http . get ( `${ baseUrl } ` , ( ) => {
38- return HttpResponse . json ( protocol ! . getDiscovery ( ) ) ;
39- } ) ,
40-
41- // Meta endpoints
42- http . get ( `${ baseUrl } /meta` , ( ) => {
43- return HttpResponse . json ( protocol ! . getMetaTypes ( ) ) ;
44- } ) ,
45-
46- http . get ( `${ baseUrl } /meta/:type` , ( { params } ) => {
47- return HttpResponse . json ( protocol ! . getMetaItems ( params . type as string ) ) ;
48- } ) ,
49-
50- http . get ( `${ baseUrl } /meta/:type/:name` , ( { params } ) => {
51- try {
52- return HttpResponse . json (
53- protocol ! . getMetaItem ( params . type as string , params . name as string )
54- ) ;
55- } catch ( error ) {
56- const message = error instanceof Error ? error . message : 'Unknown error' ;
57- return HttpResponse . json ( { error : message } , { status : 404 } ) ;
58- }
59- } ) ,
60-
61- // Data endpoints
62- http . get ( `${ baseUrl } /data/:object` , async ( { params, request } ) => {
63- try {
64- const url = new URL ( request . url ) ;
65- const queryParams : Record < string , any > = { } ;
66- url . searchParams . forEach ( ( value , key ) => {
67- queryParams [ key ] = value ;
68- } ) ;
69-
70- const result = await protocol ! . findData ( params . object as string , queryParams ) ;
71- return HttpResponse . json ( result ) ;
72- } catch ( error ) {
73- const message = error instanceof Error ? error . message : 'Unknown error' ;
74- return HttpResponse . json ( { error : message } , { status : 404 } ) ;
75- }
76- } ) ,
77-
78- http . get ( `${ baseUrl } /data/:object/:id` , async ( { params } ) => {
79- try {
80- const result = await protocol ! . getData (
81- params . object as string ,
82- params . id as string
83- ) ;
84- return HttpResponse . json ( result ) ;
85- } catch ( error ) {
86- const message = error instanceof Error ? error . message : 'Unknown error' ;
87- return HttpResponse . json ( { error : message } , { status : 404 } ) ;
88- }
89- } ) ,
90-
91- http . post ( `${ baseUrl } /data/:object` , async ( { params, request } ) => {
92- try {
93- const body = await request . json ( ) ;
94- const result = await protocol ! . createData ( params . object as string , body ) ;
95- return HttpResponse . json ( result , { status : 201 } ) ;
96- } catch ( error ) {
97- const message = error instanceof Error ? error . message : 'Unknown error' ;
98- return HttpResponse . json ( { error : message } , { status : 400 } ) ;
99- }
100- } ) ,
101-
102- http . patch ( `${ baseUrl } /data/:object/:id` , async ( { params, request } ) => {
103- try {
104- const body = await request . json ( ) ;
105- const result = await protocol ! . updateData (
106- params . object as string ,
107- params . id as string ,
108- body
109- ) ;
110- return HttpResponse . json ( result ) ;
111- } catch ( error ) {
112- const message = error instanceof Error ? error . message : 'Unknown error' ;
113- return HttpResponse . json ( { error : message } , { status : 400 } ) ;
114- }
115- } ) ,
116-
117- http . delete ( `${ baseUrl } /data/:object/:id` , async ( { params } ) => {
118- try {
119- const result = await protocol ! . deleteData (
120- params . object as string ,
121- params . id as string
122- ) ;
123- return HttpResponse . json ( result ) ;
124- } catch ( error ) {
125- const message = error instanceof Error ? error . message : 'Unknown error' ;
126- return HttpResponse . json ( { error : message } , { status : 400 } ) ;
127- }
128- } ) ,
129-
130- // UI Protocol endpoint
131- http . get ( `${ baseUrl } /ui/view/:object` , ( { params, request } ) => {
132- try {
133- const url = new URL ( request . url ) ;
134- const viewType = url . searchParams . get ( 'type' ) || 'list' ;
135- const view = protocol ! . getUiView ( params . object as string , viewType as 'list' | 'form' ) ;
136- return HttpResponse . json ( view ) ;
137- } catch ( error ) {
138- const message = error instanceof Error ? error . message : 'Unknown error' ;
139- return HttpResponse . json ( { error : message } , { status : 404 } ) ;
140- }
141- } ) ,
142- ] ;
143- }
144-
14512/**
14613 * Start the MSW worker with auto-generated handlers
14714 *
14815 * This function:
149- * 1. Initializes the ObjectStack runtime with your app config
150- * 2. Generates MSW handlers automatically
16+ * 1. Seeds initial data
17+ * 2. Generates MSW handlers automatically from your config
15118 * 3. Starts the MSW worker
15219 */
15320export async function startMockServer ( ) {
154- // Initialize runtime
155- await initializeRuntime ( ) ;
156-
157- // Create handlers from runtime protocol
158- const handlers = createHandlers ( '/api/v1' ) ;
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+ ] ) ;
45+
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+ } ;
53+
54+ // Create handlers from config
55+ const handlers = createMockHandlers ( '/api/v1' , metadata ) ;
15956
16057 // Start MSW worker
16158 const worker = setupWorker ( ...handlers ) ;
@@ -164,12 +61,8 @@ export async function startMockServer() {
16461 } ) ;
16562
16663 console . log ( '[MSW] Auto-mocked API ready! All endpoints generated from objectstack.config.ts' ) ;
64+ console . log ( '[MSW] Objects:' , Object . keys ( metadata . objects ) ) ;
65+
16766 return worker ;
16867}
16968
170- /**
171- * Get the runtime instance (useful for debugging)
172- */
173- export function getRuntime ( ) {
174- return runtime ;
175- }
0 commit comments