Routing
API
// da @pzeta/mfe-contracts/helpers
function sortRoutes(routes: MFERoute[]): MFERoute[]
// da @pzeta/mfe-contracts/utils
function stripShellPrefix(path: string): string
function buildSettingsFullPath(scope: string, routePath: string): string
Utility per normalizzare e ordinare path di routing tra shell e MFE in modalità app/settings.
sortRoutes
Ordina un array di route per meta.order ascendente (route senza order → in fondo, valore 999).
import { sortRoutes } from '@pzeta/mfe-contracts/helpers'
const routes: MFERoute[] = [
{ path: '/c', label: 'C', meta: { order: 2 } },
{ path: '/a', label: 'A' }, // no order → 999
{ path: '/b', label: 'B', meta: { order: 1 } },
]
const sorted = sortRoutes(routes)
// [
// { path: '/b', label: 'B', meta: { order: 1 } },
// { path: '/c', label: 'C', meta: { order: 2 } },
// { path: '/a', label: 'A' },
// ]
Ritorna un nuovo array (non muta l'input).
buildMenuFromManifest e buildSettingsMenuFromManifests ordinano internamente con meta.order ?? 99. Usa sortRoutes quando hai bisogno di ordinare al di fuori del flusso menu (es. tab bar, accordion, lista flat).stripShellPrefix
Rimuove il prefisso /settings da un path della shell.
import { stripShellPrefix } from '@pzeta/mfe-contracts/utils'
stripShellPrefix('/settings/node-user-auth/identity/users')
// → '/node-user-auth/identity/users'
stripShellPrefix('/settings') // → '/'
stripShellPrefix('/autisti/lista') // → '/autisti/lista' (invariato)
stripShellPrefix('') // → ''
Quando usarlo
Quando un MFE è montato in modalità settings dalla shell (path completo /settings/{scope}/...), il MFEContext.currentRoute o window.location.pathname ha il prefisso /settings. Le route interne del MFE però non lo conoscono: prima di matchare l'URL corrente con le proprie route, il MFE deve normalizzarlo.
// Dentro un MFE che potrebbe essere montato in 'app' o 'settings'
import { stripShellPrefix } from '@pzeta/mfe-contracts/utils'
const ctx = useMfeContext()
const mfeRelativePath = computed(() =>
stripShellPrefix(ctx.currentRoute ?? '/'),
)
// Ora puoi matchare contro le route interne del MFE
const isUsers = mfeRelativePath.value.startsWith('/node-user-auth/identity/users')
buildSettingsFullPath
Costruisce il path shell completo per una route settings di un MFE. Inverso di stripShellPrefix.
import { buildSettingsFullPath } from '@pzeta/mfe-contracts/utils'
// Caso 1: route già prefissata con lo scope
buildSettingsFullPath('node-user-auth', '/node-user-auth/identity/users')
// → '/settings/node-user-auth/identity/users'
// Caso 2: route relativa al basePath
buildSettingsFullPath('user-profiling', '/autorizzazioni/ruoli')
// → '/settings/user-profiling/autorizzazioni/ruoli'
// Caso 3: solo lo scope
buildSettingsFullPath('crm-module', '/crm-module')
// → '/settings/crm-module'
Comportamento
- Normalizza
routePathaggiungendo/iniziale se mancante - Se
cleanPath === '/{scope}'ocleanPath.startsWith('/{scope}/')→ ritorna/settings{cleanPath} - Altrimenti → ritorna
/settings/{scope}{cleanPath}
Gestisce trasparentemente entrambe le convenzioni di manifest:
scope | routePath | Output |
|---|---|---|
crm | /crm/dashboard | /settings/crm/dashboard (già prefissato → no duplicazione) |
crm | /dashboard | /settings/crm/dashboard (relative → prepend scope) |
crm | dashboard | /settings/crm/dashboard (relative + missing leading /) |
crm | /crm | /settings/crm (solo scope) |
Usato internamente
buildSettingsMenuFromManifests chiama buildSettingsFullPath(manifest.name, route.path) per ogni MFERoute nella sezione settings, popolando MenuItem.path con il path shell completo. Vedi Menu builders.
Pattern: navigazione coordinata tra modalità
Un MFE che vuole linkare un'altra propria route deve sapere in quale modalità è montato:
import { stripShellPrefix } from '@pzeta/mfe-contracts/utils'
const ctx = useMfeContext()
// Path interno del MFE (es. "/dashboard")
const internalPath = '/dashboard'
// Se il MFE è in modalità settings, il path completo è /settings/{scope}/dashboard
const isSettingsMode = (ctx.currentRoute ?? '').startsWith('/settings/')
const fullPath = isSettingsMode
? `/settings/${manifestName}${internalPath}` // o buildSettingsFullPath
: `${ctx.basePath}${internalPath}`
ctx.onNavigate(fullPath)
Tipi correlati
MFERoute— input disortRoutesbuildMenuFromManifestbuildSettingsMenuFromManifests