Skip to content

Commit

Permalink
feat(docs): rewrite router in TS (#408)
Browse files Browse the repository at this point in the history
Rewrites the router defined in the `src/router` folder in TypeScript.

In `index.(js → ts)`:
- Augments the `RouteMeta` interface provided by `vue-router` with the
  `Route` interface defined in `src/data/routes.ts` so that the
  application can access the current `Route` information through the Vue
  router's meta data without any type error.

Common benign changes:
- Removes unused method parameters
  • Loading branch information
kikuomax authored Jan 17, 2025
1 parent ec29113 commit fb9c6ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const afterEachGlobal = (vueApp, to, from) => {
import type { App } from 'vue'
import type { NavigationHookAfter } from 'vue-router'

export const afterEachGlobal = (vueApp: App, to: Parameters<NavigationHookAfter>[0]) => {
const title = to.meta.path === '/' ? to.meta.title : `${to.meta.title} | Buefy`
const url = `https://buefy.org${to.meta.path}`
const description = to.meta.subtitle.replace(/<(.|\n)*?>/g, '')
Expand All @@ -17,7 +20,7 @@ export const afterEachGlobal = (vueApp, to, from) => {

updates.forEach((item) => {
if (!item.value) return
document.querySelector(item.tag)
document.querySelector(item.tag)!
.setAttribute(item.attr || 'content', item.value)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import type { App } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { afterEachGlobal } from './guards.js'
import type { RouteComponent, RouteMeta } from 'vue-router'
import { afterEachGlobal } from './guards'
import routes from '@/data/routes'
import type { Route } from '@/data/routes'

export function createDocsRouter(vueApp) {
function route(path, component) {
declare module 'vue-router' {
interface RouteMeta extends Route {}
}

export function createDocsRouter(vueApp: App) {
function route(path: string, component: () => Promise<RouteComponent>) {
return {
path,
name: path,
meta: routes[path],
meta: routes[path] as RouteMeta,
// `component` is now a function to import the component; e.g.,
// () => import('@/pages/Home.vue')
//
Expand Down Expand Up @@ -95,11 +102,11 @@ export function createDocsRouter(vueApp) {
{
path: '/:pathMatch(.*)*',
name: 'notfound',
meta: routes.notfound,
meta: routes.notfound as RouteMeta,
component: NotFound
}
],
scrollBehavior(to, from, savedPosition) {
scrollBehavior(to) {
if (to.hash) {
return {
el: to.hash
Expand Down

0 comments on commit fb9c6ad

Please sign in to comment.