export type MicroAppLocationTarget = { id : string ; container : string ; } ; export type PageConfiguration = { path : string ; apps : MicroAppLocationTarget [ ] ; } ; export type MicroAppSources = { entryJs : string ; entryStyle : string ; } ; export type MicroAppRoute = { id : string ; path : string ; } ; export type MicroAppConfig < Properties = { [ key ] : any ; } > = { selector : string ; sources : Partial < MicroAppSources > ; host : string ; name ? : string ; routes ? : MicroAppRoute [ ] ; properties ? : Properties ; } ; export type MicroAppsManifest = { pages : PageConfiguration [ ] ; apps : MicroAppConfig [ ] ; } ; export const extractRootPath = ( pathname ) : string => { if ( ! pathname || typeof pathname !== 'string' ) { throw new Error ( 'Targeted url should be a string.' ) ; } const paths = pathname . split ( '/' ) ; if ( paths . length < 2 ) { throw new Error ( 'Targeted url is not valid pathname' ) ; } return `/${ paths [ 1 ] }` ; } ; const getByRootPath = ( configuration , pathId ) => configuration . find ( ( app ) => app . selector === pathId ) ; const getAppBySelector = ( configuration , selector ) => configuration . find ( ( app ) => app . selector === selector ) ; const getRouteByFullPath = ( microAppConfig , fullPath ) => { if ( ! microAppConfig . routes ) { return undefined ; } return microAppConfig . routes . find ( ( app ) => app . path === fullPath ) ; } ; export const asMicroAppMap = ( apps ) : { [ key ] : MicroAppConfig ; } => apps . reduce ( ( result , app ) => { if ( app . selector ) { result [ app . selector ] = app ; } return result ; } , { } ) ; export const asAppPerMap = ( pages ) : { [ key ] : MicroAppLocationTarget [ ] ; } => pages . reduce ( ( result , page ) => { if ( page . path ) { result [ page . path ] = page . apps ; } return result ; } , { } ) ; export const getMicroAppConfigByPath = ( pathname , configuration ) : MicroAppConfig => { const paths = pathname . split ( '/' ) ; if ( ! paths || ! paths [ 1 ] ) { return undefined ; } const resultAfterPrimaryPath = getByRootPath ( configuration , paths [ 1 ] ) ; if ( paths . length === 2 || resultAfterPrimaryPath === undefined ) { return resultAfterPrimaryPath ; } const resultInRoutes = getRouteByFullPath ( resultAfterPrimaryPath , pathname ) ; return resultInRoutes ? resultAfterPrimaryPath : undefined ; } ; export const getAllAppsForRootPath = ( path , manifest ) => { const page = manifest . pages . find ( ( page ) => page . path === path ) ; if ( ! page || ! page . apps ) { return undefined ; } return page . apps . map ( ( locationTarget ) => locationTarget . container ) ; } ;