@esmx/router-vue 3.0.0-rc.17 → 3.0.0-rc.19

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.
Files changed (55) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +563 -0
  3. package/README.zh-CN.md +563 -0
  4. package/dist/index.d.ts +6 -4
  5. package/dist/index.mjs +11 -4
  6. package/dist/index.test.d.ts +1 -0
  7. package/dist/index.test.mjs +206 -0
  8. package/dist/plugin.d.ts +55 -11
  9. package/dist/plugin.mjs +32 -16
  10. package/dist/plugin.test.d.ts +1 -0
  11. package/dist/plugin.test.mjs +436 -0
  12. package/dist/router-link.d.ts +202 -0
  13. package/dist/router-link.mjs +84 -0
  14. package/dist/router-link.test.d.ts +1 -0
  15. package/dist/router-link.test.mjs +456 -0
  16. package/dist/router-view.d.ts +30 -0
  17. package/dist/router-view.mjs +17 -0
  18. package/dist/router-view.test.d.ts +1 -0
  19. package/dist/router-view.test.mjs +459 -0
  20. package/dist/use.d.ts +198 -3
  21. package/dist/use.mjs +75 -9
  22. package/dist/use.test.d.ts +1 -0
  23. package/dist/use.test.mjs +461 -0
  24. package/dist/util.d.ts +7 -0
  25. package/dist/util.mjs +24 -0
  26. package/dist/util.test.d.ts +1 -0
  27. package/dist/util.test.mjs +319 -0
  28. package/dist/vue2.d.ts +13 -0
  29. package/dist/vue2.mjs +0 -0
  30. package/dist/vue3.d.ts +13 -0
  31. package/dist/vue3.mjs +0 -0
  32. package/package.json +31 -14
  33. package/src/index.test.ts +263 -0
  34. package/src/index.ts +16 -4
  35. package/src/plugin.test.ts +574 -0
  36. package/src/plugin.ts +86 -31
  37. package/src/router-link.test.ts +569 -0
  38. package/src/router-link.ts +148 -0
  39. package/src/router-view.test.ts +599 -0
  40. package/src/router-view.ts +61 -0
  41. package/src/use.test.ts +616 -0
  42. package/src/use.ts +307 -11
  43. package/src/util.test.ts +418 -0
  44. package/src/util.ts +32 -0
  45. package/src/vue2.ts +16 -0
  46. package/src/vue3.ts +15 -0
  47. package/dist/link.d.ts +0 -101
  48. package/dist/link.mjs +0 -103
  49. package/dist/symbols.d.ts +0 -3
  50. package/dist/symbols.mjs +0 -3
  51. package/dist/view.d.ts +0 -21
  52. package/dist/view.mjs +0 -75
  53. package/src/link.ts +0 -177
  54. package/src/symbols.ts +0 -8
  55. package/src/view.ts +0 -95
@@ -0,0 +1,206 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import * as RouterVueModule from "./index.mjs";
3
+ describe("index.ts - Package Entry Point", () => {
4
+ describe("Composition API Exports", () => {
5
+ it("should export useRouter function", () => {
6
+ expect(RouterVueModule.useRouter).toBeDefined();
7
+ expect(typeof RouterVueModule.useRouter).toBe("function");
8
+ });
9
+ it("should export useRoute function", () => {
10
+ expect(RouterVueModule.useRoute).toBeDefined();
11
+ expect(typeof RouterVueModule.useRoute).toBe("function");
12
+ });
13
+ it("should export useProvideRouter function", () => {
14
+ expect(RouterVueModule.useProvideRouter).toBeDefined();
15
+ expect(typeof RouterVueModule.useProvideRouter).toBe("function");
16
+ });
17
+ it("should export useLink function", () => {
18
+ expect(RouterVueModule.useLink).toBeDefined();
19
+ expect(typeof RouterVueModule.useLink).toBe("function");
20
+ });
21
+ });
22
+ describe("Options API Exports", () => {
23
+ it("should export getRouter function", () => {
24
+ expect(RouterVueModule.getRouter).toBeDefined();
25
+ expect(typeof RouterVueModule.getRouter).toBe("function");
26
+ });
27
+ it("should export getRoute function", () => {
28
+ expect(RouterVueModule.getRoute).toBeDefined();
29
+ expect(typeof RouterVueModule.getRoute).toBe("function");
30
+ });
31
+ });
32
+ describe("Component Exports", () => {
33
+ it("should export RouterLink component", () => {
34
+ expect(RouterVueModule.RouterLink).toBeDefined();
35
+ expect(typeof RouterVueModule.RouterLink).toBe("object");
36
+ expect(RouterVueModule.RouterLink.name).toBe("RouterLink");
37
+ expect(typeof RouterVueModule.RouterLink.setup).toBe("function");
38
+ });
39
+ it("should export RouterView component", () => {
40
+ expect(RouterVueModule.RouterView).toBeDefined();
41
+ expect(typeof RouterVueModule.RouterView).toBe("object");
42
+ expect(RouterVueModule.RouterView.name).toBe("RouterView");
43
+ expect(typeof RouterVueModule.RouterView.setup).toBe("function");
44
+ });
45
+ });
46
+ describe("Plugin Exports", () => {
47
+ it("should export RouterPlugin", () => {
48
+ expect(RouterVueModule.RouterPlugin).toBeDefined();
49
+ expect(typeof RouterVueModule.RouterPlugin).toBe("object");
50
+ expect(typeof RouterVueModule.RouterPlugin.install).toBe(
51
+ "function"
52
+ );
53
+ });
54
+ });
55
+ describe("Export Completeness", () => {
56
+ it("should export all expected functions and components", () => {
57
+ const expectedExports = [
58
+ // Composition API
59
+ "useRouter",
60
+ "useRoute",
61
+ "useProvideRouter",
62
+ "useLink",
63
+ // Options API
64
+ "getRouter",
65
+ "getRoute",
66
+ // Components
67
+ "RouterLink",
68
+ "RouterView",
69
+ // Plugin
70
+ "RouterPlugin"
71
+ ];
72
+ expectedExports.forEach((exportName) => {
73
+ expect(RouterVueModule).toHaveProperty(exportName);
74
+ expect(
75
+ RouterVueModule[exportName]
76
+ ).toBeDefined();
77
+ });
78
+ });
79
+ it("should not export unexpected items", () => {
80
+ const actualExports = Object.keys(RouterVueModule);
81
+ const expectedExports = [
82
+ "useRouter",
83
+ "useRoute",
84
+ "useProvideRouter",
85
+ "useLink",
86
+ "getRouter",
87
+ "getRoute",
88
+ "RouterLink",
89
+ "RouterView",
90
+ "RouterPlugin"
91
+ ];
92
+ const unexpectedExports = actualExports.filter(
93
+ (exportName) => !expectedExports.includes(exportName)
94
+ );
95
+ expect(unexpectedExports).toEqual([]);
96
+ });
97
+ });
98
+ describe("Function Signatures", () => {
99
+ it("should have correct function signatures for Composition API", () => {
100
+ expect(() => {
101
+ RouterVueModule.useRouter();
102
+ }).toThrow("useRouter() can only be called during setup()");
103
+ expect(() => {
104
+ RouterVueModule.useRoute();
105
+ }).toThrow("useRoute() can only be called during setup()");
106
+ expect(() => {
107
+ RouterVueModule.useLink({
108
+ to: "/test",
109
+ type: "push",
110
+ exact: "include"
111
+ });
112
+ }).toThrow("useRouter() can only be called during setup()");
113
+ });
114
+ it("should have correct function signatures for Options API", () => {
115
+ expect(() => {
116
+ try {
117
+ RouterVueModule.getRouter({});
118
+ } catch (error) {
119
+ expect(error.message).toContain(
120
+ "Router context not found"
121
+ );
122
+ }
123
+ }).not.toThrow();
124
+ expect(() => {
125
+ try {
126
+ RouterVueModule.getRoute({});
127
+ } catch (error) {
128
+ expect(error.message).toContain(
129
+ "Router context not found"
130
+ );
131
+ }
132
+ }).not.toThrow();
133
+ });
134
+ });
135
+ describe("Component Properties", () => {
136
+ it("should have RouterLink with correct properties", () => {
137
+ const { RouterLink } = RouterVueModule;
138
+ expect(RouterLink.name).toBe("RouterLink");
139
+ expect(RouterLink.props).toBeDefined();
140
+ expect(RouterLink.setup).toBeDefined();
141
+ expect(RouterLink.props.to).toBeDefined();
142
+ expect(RouterLink.props.to.required).toBe(true);
143
+ expect(RouterLink.props.type.default).toBe("push");
144
+ expect(RouterLink.props.exact.default).toBe("include");
145
+ expect(RouterLink.props.tag.default).toBe("a");
146
+ expect(RouterLink.props.event.default).toBe("click");
147
+ });
148
+ it("should have RouterView with correct properties", () => {
149
+ const { RouterView } = RouterVueModule;
150
+ expect(RouterView.name).toBe("RouterView");
151
+ expect(RouterView.setup).toBeDefined();
152
+ expect(RouterView.props).toBeUndefined();
153
+ });
154
+ });
155
+ describe("Plugin Interface", () => {
156
+ it("should have RouterPlugin with install method", () => {
157
+ const { RouterPlugin } = RouterVueModule;
158
+ expect(RouterPlugin.install).toBeDefined();
159
+ expect(typeof RouterPlugin.install).toBe("function");
160
+ expect(() => {
161
+ RouterPlugin.install(null);
162
+ }).toThrow();
163
+ });
164
+ });
165
+ describe("Module Structure", () => {
166
+ it("should be a proper ES module", () => {
167
+ expect(typeof RouterVueModule).toBe("object");
168
+ expect(RouterVueModule).not.toBeNull();
169
+ expect("default" in RouterVueModule).toBe(false);
170
+ });
171
+ it("should have consistent export naming", () => {
172
+ const functionExports = [
173
+ "useRouter",
174
+ "useRoute",
175
+ "useProvideRouter",
176
+ "useLink",
177
+ "getRouter",
178
+ "getRoute"
179
+ ];
180
+ functionExports.forEach((exportName) => {
181
+ expect(exportName).toMatch(/^[a-z][a-zA-Z]*$/);
182
+ });
183
+ const componentExports = [
184
+ "RouterLink",
185
+ "RouterView",
186
+ "RouterPlugin"
187
+ ];
188
+ componentExports.forEach((exportName) => {
189
+ expect(exportName).toMatch(/^[A-Z][a-zA-Z]*$/);
190
+ });
191
+ });
192
+ });
193
+ describe("TypeScript Integration", () => {
194
+ it("should provide proper TypeScript types", () => {
195
+ expect(typeof RouterVueModule.useRouter).toBe("function");
196
+ expect(typeof RouterVueModule.useRoute).toBe("function");
197
+ expect(typeof RouterVueModule.getRouter).toBe("function");
198
+ expect(typeof RouterVueModule.getRoute).toBe("function");
199
+ expect(RouterVueModule.RouterLink).toHaveProperty("name");
200
+ expect(RouterVueModule.RouterLink).toHaveProperty("props");
201
+ expect(RouterVueModule.RouterLink).toHaveProperty("setup");
202
+ expect(RouterVueModule.RouterView).toHaveProperty("name");
203
+ expect(RouterVueModule.RouterView).toHaveProperty("setup");
204
+ });
205
+ });
206
+ });
package/dist/plugin.d.ts CHANGED
@@ -1,11 +1,55 @@
1
- import type { Route, RouterInstance } from '@esmx/router';
2
- import { type App, type ShallowReactive } from 'vue';
3
- declare module '@vue/runtime-core' {
4
- interface ComponentCustomProperties {
5
- $route: ShallowReactive<Route>;
6
- $router: RouterInstance;
7
- }
8
- interface GlobalComponents {
9
- }
10
- }
11
- export declare function RouterVuePlugin(router: RouterInstance): (app: App) => void;
1
+ /**
2
+ * Vue plugin for @esmx/router integration.
3
+ * Registers RouterLink and RouterView components globally.
4
+ * Compatible with both Vue 2.7+ and Vue 3.
5
+ *
6
+ * @example Vue 3 installation
7
+ * ```typescript
8
+ * import { createApp } from 'vue';
9
+ * import { Router } from '@esmx/router';
10
+ * import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
11
+ *
12
+ * const routes = [
13
+ * { path: '/', component: Home },
14
+ * { path: '/about', component: About }
15
+ * ];
16
+ *
17
+ * const router = new Router({ routes });
18
+ * const app = createApp({
19
+ * setup() {
20
+ * useProvideRouter(router);
21
+ * }
22
+ * });
23
+ *
24
+ * app.use(RouterPlugin);
25
+ * app.mount('#app');
26
+ * ```
27
+ *
28
+ * @example Vue 2 installation
29
+ * ```typescript
30
+ * import Vue from 'vue';
31
+ * import { Router } from '@esmx/router';
32
+ * import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
33
+ *
34
+ * const routes = [
35
+ * { path: '/', component: Home },
36
+ * { path: '/about', component: About }
37
+ * ];
38
+ *
39
+ * const router = new Router({ routes });
40
+ * Vue.use(RouterPlugin);
41
+ *
42
+ * new Vue({
43
+ * setup() {
44
+ * useProvideRouter(router);
45
+ * }
46
+ * }).$mount('#app');
47
+ * ```
48
+ */
49
+ export declare const RouterPlugin: {
50
+ /**
51
+ * Install the router plugin.
52
+ * @param app Vue application instance (Vue 3) or Vue constructor (Vue 2)
53
+ */
54
+ install(app: unknown): void;
55
+ };
package/dist/plugin.mjs CHANGED
@@ -1,16 +1,32 @@
1
- import { shallowReactive, unref } from "vue";
2
- import { RouterLink } from "./link.mjs";
3
- import { routerKey, routerViewLocationKey } from "./symbols.mjs";
4
- import { RouterView } from "./view.mjs";
5
- export function RouterVuePlugin(router) {
6
- return function install(app) {
7
- const route = shallowReactive(router.route);
8
- router.route = route;
9
- app.config.globalProperties.$router = router;
10
- app.config.globalProperties.$route = router.route;
11
- app.provide(routerKey, unref(router));
12
- app.provide(routerViewLocationKey, unref(router.route));
13
- app.component("router-view", RouterView);
14
- app.component("router-link", RouterLink);
15
- };
16
- }
1
+ import { RouterLink } from "./router-link.mjs";
2
+ import { RouterView } from "./router-view.mjs";
3
+ import { getRoute, getRouter } from "./use.mjs";
4
+ import { isVue3 } from "./util.mjs";
5
+ export const RouterPlugin = {
6
+ /**
7
+ * Install the router plugin.
8
+ * @param app Vue application instance (Vue 3) or Vue constructor (Vue 2)
9
+ */
10
+ install(app) {
11
+ var _a;
12
+ const vueApp = app;
13
+ const target = ((_a = vueApp.config) == null ? void 0 : _a.globalProperties) || vueApp.prototype;
14
+ if (!target) {
15
+ throw new Error("[@esmx/router-vue] Invalid Vue app instance");
16
+ }
17
+ Object.defineProperties(target, {
18
+ $router: {
19
+ get() {
20
+ return getRouter(isVue3 ? null : this);
21
+ }
22
+ },
23
+ $route: {
24
+ get() {
25
+ return getRoute(isVue3 ? null : this);
26
+ }
27
+ }
28
+ });
29
+ vueApp.component("RouterLink", RouterLink);
30
+ vueApp.component("RouterView", RouterView);
31
+ }
32
+ };
@@ -0,0 +1 @@
1
+ export {};