@@ -12,6 +12,11 @@ import { ObjectKernel } from '@objectstack/runtime';
1212import { InMemoryDriver } from '@objectstack/driver-memory' ;
1313import { http , HttpResponse } from 'msw' ;
1414
15+ /** Minimal shape of the stack config used to enrich protocol responses. */
16+ interface StackConfig {
17+ objects ?: Array < { name : string ; listViews ?: Record < string , unknown > } > ;
18+ }
19+
1520/**
1621 * Create MSW request handlers for a given base URL.
1722 *
@@ -20,13 +25,13 @@ import { http, HttpResponse } from 'msw';
2025 * @param driver - InMemoryDriver for direct data access
2126 * @param appConfig - Original stack config (used to enrich protocol responses with listViews)
2227 */
23- export function createHandlers ( baseUrl : string , kernel : ObjectKernel , driver : InMemoryDriver , appConfig ?: any ) {
28+ export function createHandlers ( baseUrl : string , kernel : ObjectKernel , driver : InMemoryDriver , appConfig ?: StackConfig ) {
2429 const protocol = kernel . getService ( 'protocol' ) as any ;
2530
2631 // Build a lookup of listViews by object name from the original config.
2732 // The runtime protocol strips listViews from object metadata, so we
2833 // re-attach them here to ensure the console can resolve named views.
29- const listViewsByObject : Record < string , Record < string , any > > = { } ;
34+ const listViewsByObject : Record < string , Record < string , unknown > > = { } ;
3035 if ( appConfig ?. objects ) {
3136 for ( const obj of appConfig . objects ) {
3237 if ( obj . listViews && Object . keys ( obj . listViews ) . length > 0 ) {
@@ -35,6 +40,12 @@ export function createHandlers(baseUrl: string, kernel: ObjectKernel, driver: In
3540 }
3641 }
3742
43+ /** Merge listViews from config into a single object metadata item. */
44+ function enrichObject ( obj : any ) : any {
45+ const views = listViewsByObject [ obj . name ] ;
46+ return views ? { ...obj , listViews : { ...( obj . listViews || { } ) , ...views } } : obj ;
47+ }
48+
3849 // Determine whether we're in a browser (relative paths, wildcard prefix)
3950 // or in Node.js tests (absolute URLs)
4051 const isBrowser = ! baseUrl . startsWith ( 'http' ) ;
@@ -71,22 +82,15 @@ export function createHandlers(baseUrl: string, kernel: ObjectKernel, driver: In
7182 const response = await protocol . getMetaItems ( { type : metadataType } ) ;
7283 // Enrich object metadata with listViews from stack config
7384 if ( ( metadataType === 'object' || metadataType === 'objects' ) && response ?. items ) {
74- response . items = response . items . map ( ( obj : any ) => {
75- const views = listViewsByObject [ obj . name ] ;
76- return views ? { ...obj , listViews : { ...( obj . listViews || { } ) , ...views } } : obj ;
77- } ) ;
85+ response . items = response . items . map ( enrichObject ) ;
7886 }
7987 return HttpResponse . json ( response , { status : 200 } ) ;
8088 } ) ,
8189 http . get ( `${ prefix } ${ baseUrl } /metadata/:type` , async ( { params } ) => {
8290 const metadataType = params . type as string ;
8391 const response = await protocol . getMetaItems ( { type : metadataType } ) ;
84- // Enrich object metadata with listViews from stack config
8592 if ( ( metadataType === 'object' || metadataType === 'objects' ) && response ?. items ) {
86- response . items = response . items . map ( ( obj : any ) => {
87- const views = listViewsByObject [ obj . name ] ;
88- return views ? { ...obj , listViews : { ...( obj . listViews || { } ) , ...views } } : obj ;
89- } ) ;
93+ response . items = response . items . map ( enrichObject ) ;
9094 }
9195 return HttpResponse . json ( response , { status : 200 } ) ;
9296 } ) ,
@@ -99,12 +103,8 @@ export function createHandlers(baseUrl: string, kernel: ObjectKernel, driver: In
99103 name : params . name as string
100104 } ) ;
101105 let payload = ( response && response . item ) ? response . item : response ;
102- // Enrich single object with listViews from stack config
103- if ( ( params . type === 'object' || params . type === 'objects' ) && payload && payload . name ) {
104- const views = listViewsByObject [ payload . name ] ;
105- if ( views ) {
106- payload = { ...payload , listViews : { ...( payload . listViews || { } ) , ...views } } ;
107- }
106+ if ( ( params . type === 'object' || params . type === 'objects' ) && payload ?. name ) {
107+ payload = enrichObject ( payload ) ;
108108 }
109109 return HttpResponse . json ( payload || { error : 'Not found' } , { status : payload ? 200 : 404 } ) ;
110110 } catch ( e ) {
@@ -118,12 +118,8 @@ export function createHandlers(baseUrl: string, kernel: ObjectKernel, driver: In
118118 name : params . name as string
119119 } ) ;
120120 let payload = ( response && response . item ) ? response . item : response ;
121- // Enrich single object with listViews from stack config
122- if ( ( params . type === 'object' || params . type === 'objects' ) && payload && payload . name ) {
123- const views = listViewsByObject [ payload . name ] ;
124- if ( views ) {
125- payload = { ...payload , listViews : { ...( payload . listViews || { } ) , ...views } } ;
126- }
121+ if ( ( params . type === 'object' || params . type === 'objects' ) && payload ?. name ) {
122+ payload = enrichObject ( payload ) ;
127123 }
128124 return HttpResponse . json ( payload || { error : 'Not found' } , { status : payload ? 200 : 404 } ) ;
129125 } catch ( e ) {
0 commit comments