@@ -11,61 +11,58 @@ export async function startMockServer() {
1111 if ( kernel ) return kernel ;
1212
1313 console . log ( '[MSW] Starting ObjectStack Runtime (Browser Mode)...' ) ;
14+ console . log ( '[MSW] Loaded Config:' , crmConfig ? 'Found' : 'Missing' , crmConfig ?. apps ?. length ) ;
1415
1516 const driver = new InMemoryDriver ( ) ;
16-
17- // Create kernel with MiniKernel architecture
1817 kernel = new ObjectKernel ( ) ;
19-
20- kernel
21- // Register ObjectQL engine
22- . use ( new ObjectQLPlugin ( ) )
23- // Register the driver
24- . use ( new DriverPlugin ( driver , 'memory' ) )
25- // Load app config as a plugin
26- . use ( new AppPlugin ( crmConfig ) )
27- // MSW Plugin (intercepts network requests)
28- . use ( new MSWPlugin ( {
29- enableBrowser : true ,
30- baseUrl : '/api/v1' ,
31- logRequests : true ,
32- customHandlers : [
33- // Custom handlers that are not part of standard CRUD
34- http . get ( '/api/bootstrap' , async ( ) => {
35- // We use closure 'driver' variable to bypass objectql service issues
36- try {
37- // Use IDataEngine interface directly via driver
38- // const user = (await driver.findOne('user', 'current')) || {};
18+
19+ try {
20+ kernel
21+ . use ( new ObjectQLPlugin ( ) )
22+ . use ( new DriverPlugin ( driver , 'memory' ) ) ;
23+
24+ if ( crmConfig ) {
25+ kernel . use ( new AppPlugin ( crmConfig ) ) ;
26+ } else {
27+ console . error ( '❌ CRM Config is missing! Skipping AppPlugin.' ) ;
28+ }
29+
30+ kernel . use ( new MSWPlugin ( {
31+ enableBrowser : true ,
32+ baseUrl : '/api/v1' , // Use root to match client
33+ logRequests : true ,
34+ customHandlers : [
35+ http . get ( '/api/bootstrap' , async ( ) => {
3936 const contacts = await driver . find ( 'contact' , { object : 'contact' } ) ;
40- const opportunities = await driver . find ( 'opportunity' , { object : 'opportunity' } ) ;
4137 const stats = { revenue : 125000 , leads : 45 , deals : 12 } ;
42-
4338 return HttpResponse . json ( {
44- user : { name : "Demo User" , role : "admin" } , // simple mock
39+ user : { name : "Demo User" , role : "admin" } ,
4540 stats,
46- contacts,
47- opportunities
41+ contacts : contacts || [ ]
4842 } ) ;
49- } catch ( e ) {
50- console . error ( e ) ;
51- return new HttpResponse ( null , { status : 500 } ) ;
52- }
53- } )
54- ]
43+ } )
44+ ]
5545 } ) ) ;
56-
57- await kernel . bootstrap ( ) ;
46+
47+ console . log ( '[Kernel] Bootstrapping...' ) ;
48+ await kernel . bootstrap ( ) ;
49+ console . log ( '[Kernel] Bootstrap Complete' ) ;
5850
59- // Seed Data
60- await initializeMockData ( driver ) ;
51+ // Seed Data
52+ if ( crmConfig ) {
53+ await initializeMockData ( driver ) ;
54+ }
55+ } catch ( err : any ) {
56+ console . error ( '❌ Mock Server Start Failed:' , err ) ;
57+ throw err ;
58+ }
6159
6260 return kernel ;
6361}
6462
6563// Helper to seed data into the in-memory driver
6664async function initializeMockData ( driver : InMemoryDriver ) {
67- console . log ( '[MockServer] Initializing mock data from manifest...' ) ;
68-
65+ console . log ( '[MockServer] Initializing mock data...' ) ;
6966 // @ts -ignore
7067 const manifest = crmConfig . manifest ;
7168 if ( manifest && manifest . data ) {
@@ -78,5 +75,4 @@ async function initializeMockData(driver: InMemoryDriver) {
7875 }
7976 }
8077 }
81- }
82-
78+ }
0 commit comments