@oiij/auto-router 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 oiij
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Use auto-Router 🚀
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@oiij/auto-router)](https://www.npmjs.com/package/@oiij/auto-router)
4
+ [![MIT-license](https://img.shields.io/npm/l/@oiij/auto-router)](https://github.com/oiij/use/blob/main/packages/auto-router/LICENSE)
5
+
6
+ ## 项目简介 📦
7
+
8
+ Use auto-Router 是一个 Vue Router 工具库,为 Vue 3 应用提供自动路由管理、Keep-Alive 管理等实用功能,帮助开发者更高效地管理应用路由。
9
+
10
+ ## 功能特点 ✨
11
+
12
+ ### 自动路由管理 🛣️
13
+
14
+ - 🔄 自动解析和排序路由(支持数字前缀排序)
15
+ - 📊 路由扁平化处理
16
+ - 🎯 支持嵌套路由元数据继承
17
+ - 📝 自动规范化路由名称
18
+
19
+ ### Keep-Alive 管理 💾
20
+
21
+ - 🚀 自动管理页面缓存
22
+ - 📦 基于路由元数据的缓存配置
23
+ - 🎨 支持动态缓存控制
24
+
25
+ ### 加载状态管理 ⏳
26
+
27
+ - 📊 自动跟踪路由加载状态
28
+ - 🔄 基于导航守卫的状态管理
29
+ - 🎯 响应式的加载状态计算属性
30
+
31
+ ### 类型安全 🔒
32
+
33
+ - 📝 完整的 TypeScript 类型定义
34
+ - 💡 提供准确的类型推断和代码提示
35
+ - 🎯 支持 Vue 3 的 Composition API 类型系统
36
+
37
+ ## 安装 📥
38
+
39
+ ### 使用 pnpm 🐱
40
+
41
+ ```bash
42
+ pnpm add @oiij/auto-router
43
+ ```
44
+
45
+ ### 使用 npm 📦
46
+
47
+ ```bash
48
+ npm install @oiij/auto-router
49
+ ```
50
+
51
+ ### 使用 yarn 🧶
52
+
53
+ ```bash
54
+ yarn add @oiij/auto-router
55
+ ```
56
+
57
+ ## 快速开始 🌟
58
+
59
+ ### 基础使用
60
+
61
+ ```vue
62
+ <script setup>
63
+ import { useAutoRouter } from '@oiij/auto-router'
64
+
65
+ const { loading, routes, currentRoutePath } = useAutoRouter()
66
+ </script>
67
+
68
+ <template>
69
+ <div>
70
+ <div v-if="loading">
71
+ 加载中...
72
+ </div>
73
+ <div v-else>
74
+ <p>当前路由: {{ currentRoutePath }}</p>
75
+ <nav>
76
+ <router-link
77
+ v-for="route in routes"
78
+ :key="route.path"
79
+ :to="route.path"
80
+ >
81
+ {{ route.meta?.title }}
82
+ </router-link>
83
+ </nav>
84
+ </div>
85
+ </div>
86
+ </template>
87
+ ```
88
+
89
+ ## 在线文档 📚
90
+
91
+ [在线文档](https://oiij-use.vercel.app/auto-router/auto-router) 📖
92
+
93
+ ## 贡献指南 🤝
94
+
95
+ 欢迎贡献代码、报告问题或提出新功能建议!
96
+
97
+ 1. Fork 本仓库 🍴
98
+ 2. 创建您的特性分支 (`git checkout -b feature/amazing-feature`) 🌿
99
+ 3. 提交您的更改 (`git commit -m 'Add some amazing feature'`) 💾
100
+ 4. 推送到分支 (`git push origin feature/amazing-feature`) 🚀
101
+ 5. 打开一个 Pull Request 📥
102
+
103
+ ## 许可证 📄
104
+
105
+ 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情 📑
106
+
107
+ ## 联系方式 📞
108
+
109
+ - GitHub: [https://github.com/oiij/use](https://github.com/oiij/use) 🌟
@@ -0,0 +1,93 @@
1
+ /* eslint-disable */
2
+ /* prettier-ignore */
3
+ // biome-ignore format: off
4
+ // biome-ignore lint: off
5
+ // @ts-nocheck
6
+ // Vue Router 路由元数据类型扩展
7
+ // 这个文件扩展了 vue-router 的 RouteMeta 接口
8
+ // 确保此文件包含在项目的 tsconfig.json 中
9
+
10
+ import 'vue-router'
11
+
12
+ // 为了确保这个文件被当作一个模块,添加至少一个 export 声明
13
+ export {}
14
+
15
+ declare module 'vue-router' {
16
+ interface RouteMeta {
17
+ /**
18
+ * 路由标题
19
+ */
20
+ title?: string
21
+
22
+ /**
23
+ * 路由描述
24
+ */
25
+ description?: string
26
+
27
+ /**
28
+ * 路由图标
29
+ */
30
+ icon?: string
31
+
32
+ /**
33
+ * 图标颜色
34
+ */
35
+ iconColor?: string
36
+
37
+ /**
38
+ * 排序权重(数字越小越靠前)
39
+ */
40
+ sort?: number
41
+
42
+ /**
43
+ * 布局组件名称
44
+ */
45
+ layout?: string
46
+
47
+ /**
48
+ * 过渡动画名称
49
+ */
50
+ transitionName?: string
51
+
52
+ /**
53
+ * 是否启用 Keep-Alive 缓存
54
+ */
55
+ keepAlive?: boolean
56
+
57
+ /**
58
+ * 是否为根路由
59
+ */
60
+ root?: boolean
61
+
62
+ /**
63
+ * 路由分组信息(用于导航菜单等)
64
+ */
65
+ group?: {
66
+ /**
67
+ * 分组标题
68
+ */
69
+ title?: string
70
+
71
+ /**
72
+ * 分组描述
73
+ */
74
+ description?: string
75
+
76
+ /**
77
+ * 分组图标
78
+ */
79
+ icon?: string
80
+
81
+ /**
82
+ * 分组图标颜色
83
+ */
84
+ iconColor?: string
85
+
86
+ /**
87
+ * 分组排序权重
88
+ */
89
+ sort?: number
90
+ }
91
+ }
92
+ }
93
+
@@ -0,0 +1,45 @@
1
+ import { AutoRouterInstance } from "./setup-auto-router.mjs";
2
+ import { App } from "vue";
3
+
4
+ //#region src/index.d.ts
5
+ /**
6
+ * 获取自动路由实例
7
+ *
8
+ * @returns 自动路由实例,包含路由配置和工具方法
9
+ *
10
+ * @example
11
+ * ```vue
12
+ * <script setup>
13
+ * import { useAutoRouter } from '@oiij/auto-router'
14
+ *
15
+ * const { routes, flattenRoutes, keepAlivePath } = useAutoRouter()
16
+ * </script>
17
+ * ```
18
+ */
19
+ declare function useAutoRouter(): AutoRouterInstance;
20
+ /**
21
+ * 创建自动路由插件
22
+ *
23
+ * 必须在 Vue Router 之后安装
24
+ *
25
+ * @returns Vue 插件对象
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { createApp } from 'vue'
30
+ * import { createRouter } from 'vue-router'
31
+ * import { createAutoRouter } from '@oiij/auto-router'
32
+ *
33
+ * const app = createApp(App)
34
+ * const router = createRouter({ ... })
35
+ *
36
+ * app.use(router)
37
+ * app.use(createAutoRouter())
38
+ * app.mount('#app')
39
+ * ```
40
+ */
41
+ declare function createAutoRouter(): {
42
+ install(app: App): void;
43
+ };
44
+ //#endregion
45
+ export { type AutoRouterInstance, createAutoRouter, useAutoRouter };
package/dist/index.mjs ADDED
@@ -0,0 +1,56 @@
1
+ import { setupAutoRouter } from "./setup-auto-router.mjs";
2
+ import { inject } from "vue";
3
+
4
+ //#region src/index.ts
5
+ const __INJECTION_KEY__ = Symbol("auto-router");
6
+ /**
7
+ * 获取自动路由实例
8
+ *
9
+ * @returns 自动路由实例,包含路由配置和工具方法
10
+ *
11
+ * @example
12
+ * ```vue
13
+ * <script setup>
14
+ * import { useAutoRouter } from '@oiij/auto-router'
15
+ *
16
+ * const { routes, flattenRoutes, keepAlivePath } = useAutoRouter()
17
+ * <\/script>
18
+ * ```
19
+ */
20
+ function useAutoRouter() {
21
+ const router = inject(__INJECTION_KEY__);
22
+ if (!router) throw new Error("Auto router is not provided. Make sure to install createAutoRouter plugin.");
23
+ return router;
24
+ }
25
+ /**
26
+ * 创建自动路由插件
27
+ *
28
+ * 必须在 Vue Router 之后安装
29
+ *
30
+ * @returns Vue 插件对象
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { createApp } from 'vue'
35
+ * import { createRouter } from 'vue-router'
36
+ * import { createAutoRouter } from '@oiij/auto-router'
37
+ *
38
+ * const app = createApp(App)
39
+ * const router = createRouter({ ... })
40
+ *
41
+ * app.use(router)
42
+ * app.use(createAutoRouter())
43
+ * app.mount('#app')
44
+ * ```
45
+ */
46
+ function createAutoRouter() {
47
+ return { install(app) {
48
+ const router = app.config.globalProperties.$router;
49
+ if (!router) throw new Error("Router instance is not found on this Vue app. This plugin should be installed after Vue Router.");
50
+ const autoRouter = setupAutoRouter(router);
51
+ app.provide(__INJECTION_KEY__, autoRouter);
52
+ } };
53
+ }
54
+
55
+ //#endregion
56
+ export { createAutoRouter, useAutoRouter };
@@ -0,0 +1,20 @@
1
+ import { EditableTreeNode } from "vue-router/unplugin";
2
+
3
+ //#region src/plugin.d.ts
4
+ /**
5
+ * 为路由添加元数据
6
+ *
7
+ * 从路由路径中提取排序编号(例如:01_home -> sort: 1)
8
+ * 并规范化路由名称(移除数字前缀)
9
+ *
10
+ * @param route - 可编辑的路由树节点
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // 路径: 01_home
15
+ * // 结果: { sort: 1, name: '/home', path: '/home' }
16
+ * ```
17
+ */
18
+ declare function appendRouterMeta(route: EditableTreeNode): void;
19
+ //#endregion
20
+ export { appendRouterMeta };
@@ -0,0 +1,28 @@
1
+ //#region src/plugin.ts
2
+ /**
3
+ * 为路由添加元数据
4
+ *
5
+ * 从路由路径中提取排序编号(例如:01_home -> sort: 1)
6
+ * 并规范化路由名称(移除数字前缀)
7
+ *
8
+ * @param route - 可编辑的路由树节点
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // 路径: 01_home
13
+ * // 结果: { sort: 1, name: '/home', path: '/home' }
14
+ * ```
15
+ */
16
+ function appendRouterMeta(route) {
17
+ const sortMatch = route.path.match(/(\d+)_/);
18
+ const sortNum = sortMatch ? Number(sortMatch[1]) : null;
19
+ if (sortNum !== null && !Number.isNaN(sortNum)) route.addToMeta({ sort: sortNum });
20
+ if (route.name) {
21
+ const newName = route.name.replace(/\d+_/g, "");
22
+ route.name = newName.startsWith("/") ? newName : `/${newName}`;
23
+ if (route.path !== "") route.path = route.name;
24
+ }
25
+ }
26
+
27
+ //#endregion
28
+ export { appendRouterMeta };
@@ -0,0 +1,39 @@
1
+ import { ComputedRef } from "vue";
2
+ import { RouteLocationNormalizedLoaded, RouteRecordRaw } from "vue-router";
3
+
4
+ //#region src/setup-auto-router.d.ts
5
+ /**
6
+ * 自动路由实例接口
7
+ */
8
+ type AutoRouterInstance = {
9
+ /**
10
+ * 加载状态
11
+ */
12
+ loading: ComputedRef<boolean>;
13
+ /**
14
+ * 原始路由配置
15
+ */
16
+ routesRaw: readonly RouteRecordRaw[];
17
+ /**
18
+ * 解析并排序后的路由配置
19
+ */
20
+ routes: RouteRecordRaw[];
21
+ /**
22
+ * 扁平化的路由列表(将所有子路由提取到一级)
23
+ */
24
+ flattenRoutes: RouteRecordRaw[];
25
+ /**
26
+ * 需要缓存的路由路径列表
27
+ */
28
+ keepAlivePath: ComputedRef<string[]>;
29
+ /**
30
+ * 当前路由
31
+ */
32
+ currentRoute: ComputedRef<RouteLocationNormalizedLoaded>;
33
+ /**
34
+ * 当前路由路径
35
+ */
36
+ currentRoutePath: ComputedRef<string>;
37
+ };
38
+ //#endregion
39
+ export { AutoRouterInstance };
@@ -0,0 +1,88 @@
1
+ import { computed, ref } from "vue";
2
+ import { cloneDeep } from "es-toolkit";
3
+ import { routes as routes$1 } from "vue-router/auto-routes";
4
+
5
+ //#region src/setup-auto-router.ts
6
+ /**
7
+ * 解析路由配置
8
+ *
9
+ * 处理以下逻辑:
10
+ * 1. 从子路由的 index 页面继承 group 元数据
11
+ * 2. 将空路径的子路由路径设置为父路由路径
12
+ * 3. 根据 sort 元数据排序路由
13
+ *
14
+ * @param routes - 原始路由配置
15
+ * @returns 解析后的路由配置
16
+ */
17
+ function parseRoutes(routes) {
18
+ return routes.map((route) => {
19
+ const indexMeta = (route.children?.find((f) => f.path === ""))?.meta?.group;
20
+ return {
21
+ ...route,
22
+ meta: {
23
+ ...route.meta,
24
+ ...indexMeta
25
+ },
26
+ children: route.children?.map((child) => ({
27
+ ...child,
28
+ path: child.path === "" ? route.path : child.path
29
+ })).toSorted((a, b) => (a.meta?.sort ?? Infinity) - (b.meta?.sort ?? Infinity))
30
+ };
31
+ }).toSorted((a, b) => (a.meta?.sort ?? Infinity) - (b.meta?.sort ?? Infinity));
32
+ }
33
+ /**
34
+ * 扁平化路由配置
35
+ *
36
+ * 将嵌套的路由结构展平为一维数组
37
+ *
38
+ * @param routes - 路由配置
39
+ * @returns 扁平化后的路由数组
40
+ */
41
+ function flattenRoutes(routes) {
42
+ return cloneDeep(routes).flatMap((route) => route.children ?? route);
43
+ }
44
+ /**
45
+ * 设置自动路由
46
+ *
47
+ * 解析路由配置,提供路由工具方法
48
+ *
49
+ * @param router - Vue Router 实例
50
+ * @returns 自动路由实例
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { setupAutoRouter } from '@oiij/auto-router'
55
+ * import { router } from './router'
56
+ *
57
+ * const autoRouter = setupAutoRouter(router)
58
+ * console.log(autoRouter.routes) // 解析后的路由
59
+ * console.log(autoRouter.flattenRoutes) // 扁平化路由
60
+ * ```
61
+ */
62
+ function setupAutoRouter(router) {
63
+ const loading = ref(false);
64
+ router.beforeEach((to, from, next) => {
65
+ loading.value = true;
66
+ next();
67
+ });
68
+ router.afterEach(() => {
69
+ loading.value = false;
70
+ });
71
+ const routes = parseRoutes(cloneDeep(routes$1));
72
+ const flattenRoutesCache = flattenRoutes(routes);
73
+ const keepAlivePath = computed(() => flattenRoutesCache.filter((f) => f.meta?.keepAlive).map((m) => m.path));
74
+ const currentRoute = computed(() => router.currentRoute.value);
75
+ const currentRoutePath = computed(() => currentRoute.value.path);
76
+ return {
77
+ loading: computed(() => loading.value),
78
+ routesRaw: routes$1,
79
+ routes,
80
+ flattenRoutes: flattenRoutesCache,
81
+ keepAlivePath,
82
+ currentRoute,
83
+ currentRoutePath
84
+ };
85
+ }
86
+
87
+ //#endregion
88
+ export { setupAutoRouter };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@oiij/auto-router",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "Vue Router utilities and composables for Vue 3",
6
+ "author": "oiij",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/oiij/use",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git@github.com:oiij/use.git"
12
+ },
13
+ "bugs": "https://github.com/oiij/use/issues",
14
+ "keywords": [
15
+ "@oiij/auto-router",
16
+ "vue-routes",
17
+ "vue3",
18
+ "composables"
19
+ ],
20
+ "sideEffects": false,
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.mts",
24
+ "import": "./dist/index.mjs"
25
+ },
26
+ "./plugin": {
27
+ "types": "./dist/plugin.d.mts",
28
+ "import": "./dist/plugin.mjs"
29
+ },
30
+ "./auto-router": {
31
+ "types": "./auto-router.d.ts"
32
+ }
33
+ },
34
+ "main": "./dist/index.mjs",
35
+ "module": "./dist/index.mjs",
36
+ "types": "./dist/index.d.mts",
37
+ "files": [
38
+ "LICENSE",
39
+ "README.md",
40
+ "auto-router.d.ts",
41
+ "dist",
42
+ "package.json"
43
+ ],
44
+ "peerDependencies": {
45
+ "vue": "^3.5.27",
46
+ "vue-router": "^5.0.2"
47
+ },
48
+ "dependencies": {
49
+ "es-toolkit": "^1.44.0"
50
+ },
51
+ "devDependencies": {
52
+ "vue": "^3.5.27",
53
+ "vue-router": "^5.0.2"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "scripts": {
59
+ "dev": "tsdown --watch",
60
+ "build": "tsc --noEmit && tsdown",
61
+ "bumpp": "bumpp --no-push"
62
+ }
63
+ }