index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. createRouter,
  4. createWebHistory,
  5. type NavigationGuard,
  6. type NavigationHookAfter,
  7. type Router,
  8. type RouteRecordRaw,
  9. } from 'vue-router'
  10. import { useApplicationStore } from '#shared/stores/application.ts'
  11. import type { RouteRecordMeta } from '#shared/types/router.ts'
  12. import { errorAfterGuard } from './error.ts'
  13. import headerTitleGuard from './guards/after/headerTitle.ts'
  14. import authenticationGuard from './guards/before/authentication.ts'
  15. import permissionGuard from './guards/before/permission.ts'
  16. import { initializeWalker } from './walker.ts'
  17. import type { App } from 'vue'
  18. declare module 'vue-router' {
  19. // eslint-disable-next-line @typescript-eslint/no-empty-object-type
  20. interface RouteMeta extends RouteRecordMeta {}
  21. }
  22. export default function initializeRouter(
  23. app: App,
  24. routes: Array<RouteRecordRaw>,
  25. beforeGuards?: NavigationGuard[],
  26. afterGuards?: NavigationHookAfter[],
  27. historyBase?: string,
  28. ): Router {
  29. const router: Router = createRouter({
  30. history: createWebHistory(historyBase),
  31. routes,
  32. })
  33. const removeInitializer = router.beforeResolve(() => {
  34. const { setInitialized } = useApplicationStore()
  35. setInitialized()
  36. removeInitializer()
  37. })
  38. router.beforeEach(authenticationGuard)
  39. router.beforeEach(permissionGuard)
  40. beforeGuards?.forEach((guard) => router.beforeEach(guard))
  41. router.afterEach(headerTitleGuard)
  42. router.afterEach(errorAfterGuard)
  43. afterGuards?.forEach((guard) => router.afterEach(guard))
  44. app.use(router)
  45. initializeWalker(app, router)
  46. return router
  47. }