index.ts 1.3 KB

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