@kitbag/router 0.20.12 → 0.22.0

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 (72) hide show
  1. package/README.md +38 -87
  2. package/dist/components/routerLink.d.ts +5 -5
  3. package/dist/compositions/useLink.d.ts +2 -2
  4. package/dist/devtools/createRouterDevtools.d.ts +13 -0
  5. package/dist/devtools/filters.d.ts +12 -0
  6. package/dist/devtools/getDevtoolsLabel.d.ts +13 -0
  7. package/dist/devtools/types.d.ts +10 -0
  8. package/dist/errors/contextAbortError.d.ts +2 -2
  9. package/dist/errors/contextPushError.d.ts +2 -2
  10. package/dist/errors/contextRejectionError.d.ts +2 -2
  11. package/dist/errors/invalidRouteRedirectError.d.ts +11 -0
  12. package/dist/errors/multipleRouteRedirectsError.d.ts +13 -0
  13. package/dist/keys.d.ts +1 -3
  14. package/dist/kitbag-router.js +4927 -1741
  15. package/dist/kitbag-router.umd.cjs +3 -3
  16. package/dist/main.d.ts +1 -17
  17. package/dist/models/hooks.d.ts +12 -0
  18. package/dist/services/createComponentHooks.d.ts +7 -7
  19. package/dist/services/createExternalRoute.d.ts +4 -2
  20. package/dist/services/createPropStore.d.ts +2 -2
  21. package/dist/services/createRoute.d.ts +3 -1
  22. package/dist/services/createRouteHooks.d.ts +12 -10
  23. package/dist/services/createRouteRedirects.d.ts +10 -0
  24. package/dist/services/createRouterAssets.d.ts +5 -5
  25. package/dist/services/createRouterCallbackContext.d.ts +8 -6
  26. package/dist/services/createRouterHooks.d.ts +13 -14
  27. package/dist/services/createRouterPlugin.d.ts +2 -2
  28. package/dist/services/getGlobalHooksForRouter.d.ts +2 -3
  29. package/dist/services/getGlobalRouteHooks.d.ts +3 -3
  30. package/dist/services/getRouteHooks.d.ts +3 -3
  31. package/dist/services/hooks.d.ts +2 -2
  32. package/dist/services/insertBaseRoute.d.ts +2 -2
  33. package/dist/services/routeMatchRules.d.ts +0 -1
  34. package/dist/services/routeMatchScore.d.ts +1 -1
  35. package/dist/services/routeRegex.d.ts +1 -0
  36. package/dist/services/urlAssembly.d.ts +2 -2
  37. package/dist/services/urlCombine.d.ts +2 -2
  38. package/dist/services/urlCreator.d.ts +2 -2
  39. package/dist/services/urlParser.d.ts +1 -1
  40. package/dist/types/callbackContext.d.ts +15 -0
  41. package/dist/types/createRouteOptions.d.ts +6 -13
  42. package/dist/types/hooks.d.ts +85 -137
  43. package/dist/types/props.d.ts +8 -8
  44. package/dist/types/redirects.d.ts +40 -0
  45. package/dist/types/register.d.ts +1 -41
  46. package/dist/types/rejection.d.ts +6 -2
  47. package/dist/types/resolved.d.ts +14 -4
  48. package/dist/types/route.d.ts +3 -4
  49. package/dist/types/routeContext.d.ts +11 -5
  50. package/dist/types/router.d.ts +16 -11
  51. package/dist/types/routerAbort.d.ts +1 -0
  52. package/dist/types/routerLink.d.ts +3 -3
  53. package/dist/types/routerPlugin.d.ts +17 -41
  54. package/dist/types/routerPush.d.ts +2 -2
  55. package/dist/types/routerReject.d.ts +2 -2
  56. package/dist/types/routerReplace.d.ts +2 -2
  57. package/dist/types/{url.d.ts → urlString.d.ts} +3 -3
  58. package/dist/types/useLink.d.ts +2 -2
  59. package/dist/utilities/checkDuplicateParams.spec.d.ts +1 -0
  60. package/dist/utilities/index.d.ts +1 -1
  61. package/dist/utilities/isNamedRoute.d.ts +4 -0
  62. package/dist/utilities/testHelpers.d.ts +722 -22
  63. package/package.json +10 -8
  64. package/dist/errors/missingRouteContextError.d.ts +0 -10
  65. package/dist/models/RouteHooks.d.ts +0 -12
  66. package/dist/models/RouterRouteHooks.d.ts +0 -10
  67. package/dist/services/createCallbackContext.d.ts +0 -44
  68. package/dist/services/getRouteHooksDeprecated.d.ts +0 -10
  69. package/dist/utilities/checkMissingContext.d.ts +0 -2
  70. /package/dist/{utilities/checkDuplicateKeys.spec.d.ts → services/createRouteRedirects.spec.d.ts} +0 -0
  71. /package/dist/{utilities/checkMissingContext.spec copy.d.ts → types/redirects.spec-d.d.ts} +0 -0
  72. /package/dist/utilities/{checkDuplicateKeys.d.ts → checkDuplicateParams.d.ts} +0 -0
package/README.md CHANGED
@@ -7,6 +7,15 @@ Type safe router for Vue.js
7
7
  [![Discord chat][discord-badge]][discord-url]
8
8
  [![Open in StackBlitz][stackblitz-badge]][stackblitz-url]
9
9
 
10
+ [npm-badge]: https://img.shields.io/npm/v/@kitbag/router.svg
11
+ [npm-url]: https://www.npmjs.org/package/@kitbag/router
12
+ [netlify-badge]: https://api.netlify.com/api/v1/badges/c12f79b8-49f9-4529-bc23-f8ffca8919a3/deploy-status
13
+ [netlify-url]: https://app.netlify.com/sites/kitbag-router/deploys
14
+ [discord-badge]: https://img.shields.io/discord/1079625926024900739?logo=discord&label=Discord
15
+ [discord-url]: https://discord.gg/zw7dpcc5HV
16
+ [stackblitz-badge]: https://developer.stackblitz.com/img/open_in_stackblitz_small.svg
17
+ [stackblitz-url]: https://stackblitz.com/~/github.com/kitbagjs/router-preview
18
+
10
19
  <img src="https://kitbag.dev/kitbag-logo.svg" width="20%" />
11
20
 
12
21
  ## Getting Started
@@ -15,6 +24,8 @@ Get Started with our [documentation](https://kitbag-router.netlify.app/) or our
15
24
 
16
25
  ## Installation
17
26
 
27
+ Install Kitbag Router with your favorite package manager
28
+
18
29
  ```bash
19
30
  # bun
20
31
  bun add @kitbag/router
@@ -24,43 +35,52 @@ yarn add @kitbag/router
24
35
  npm install @kitbag/router
25
36
  ```
26
37
 
27
- ## Define Basic Routes
38
+ ## Define Routes
28
39
 
29
- Create an array of possible routes. Learn more about [defining routes](https://kitbag-router.netlify.app/core-concepts/defining-routes).
40
+ Routes are created individually using the [`createRoute`](https://kitbag-router.netlify.app/api/functions/createRoute) utility. Learn more about [defining routes](https://kitbag-router.netlify.app/core-concepts/routes).
30
41
 
31
42
  ```ts
32
- // /routes.ts
33
43
  import { createRoute } from '@kitbag/router'
34
44
 
35
45
  const Home = { template: '<div>Home</div>' }
36
46
  const About = { template: '<div>About</div>' }
37
47
 
38
- export const routes = [
48
+ const routes = [
39
49
  createRoute({ name: 'home', path: '/', component: Home }),
40
50
  createRoute({ name: 'path', path: '/about', component: About }),
41
51
  ] as const
42
52
  ```
43
53
 
44
- ## Plugin
54
+ > [!NOTE] Type Safety
55
+ > Using `as const` when defining routes is important as it ensures the types are correctly inferred.
45
56
 
46
- Create a router instance and pass it to the app as a plugin
57
+ ## Create Router
58
+
59
+ A router is created using the [`createRouter`](https://kitbag-router.netlify.app/api/functions/createRouter) utility and passing in the routes.
47
60
 
48
61
  ```ts
49
- import { createApp } from 'vue'
50
62
  import { createRouter } from '@kitbag/router'
51
- import { routes } from '/routes'
52
- import App from './App.vue'
53
63
 
54
64
  const router = createRouter(routes)
65
+ ```
66
+
67
+ ## Vue Plugin
68
+
69
+ Create a router instance and pass it to the app as a plugin
70
+
71
+ ```ts {6}
72
+ import { createApp } from 'vue'
73
+ import App from './App.vue'
74
+
55
75
  const app = createApp(App)
56
76
 
57
77
  app.use(router)
58
78
  app.mount('#app')
59
79
  ```
60
80
 
61
- ## Update Registered Router
81
+ ## Type Safety
62
82
 
63
- This block utilizes [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) to provide the internal types to match the actual router you're using. You put this in main.ts right after you call `createRouter`, or you can export your router and put this interface inside of a `router.d.ts` file, anywhere that your tsconfig can find it.
83
+ Kitbag Router utilizes [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) to provide the internal types to match the actual router you're using.
64
84
 
65
85
  ```ts
66
86
  declare module '@kitbag/router' {
@@ -70,99 +90,30 @@ declare module '@kitbag/router' {
70
90
  }
71
91
  ```
72
92
 
73
- ## Push
74
-
75
- To navigate to another route, you can use `router.push`. This method will update the URL for the browser and also add the URL into the history so when a user uses the back button on their browser it will behave as expected.
76
-
77
- ```ts
78
- import { defineAsyncComponent } from 'vue'
79
- import { createRoute, useRouter } from '@kitbag/router'
80
-
81
- const user = createRoute({
82
- name: 'user',
83
- path: '/user',
84
- component: defineAsyncComponent(() => import('./UserPage.vue')),
85
- })
86
-
87
- const profile = createRoute({
88
- parent: user,
89
- name: 'profile',
90
- path: '/profile',
91
- component: defineAsyncComponent(() => import('./ProfilePage.vue')),
92
- })
93
-
94
- const settings = createRoute({
95
- parent: user,
96
- name: 'settings',
97
- path: '/settings',
98
- component: defineAsyncComponent(() => import('./SettingsPage.vue')),
99
- })
100
-
101
- const router = useRouter([user, profile, settings])
102
-
103
- router.push('settings')
104
- ```
105
-
106
- The push method also accepts a plain string if you know the URL you want to go to.
107
-
108
- ```ts
109
- router.push('/user/settings')
110
- router.push('https://github.com/kitbagjs/router')
111
- ```
112
-
113
- This `source` argument is type safe, expecting either a [`Url`](/api/types/Url) or a valid route [`name`](/api/types/Route#name). URL is any string that starts with "http", "https", or a forward slash "/". Additionally if using the route name, push will require params be passed in if there are any.
114
-
115
- ## Update
116
-
117
- If you only wish to change the params on the current route you can use `router.route.update`.
118
-
119
- ```ts
120
- router.route.update('myParam', 123)
121
- ```
122
-
123
- or for setting multiple params at once
124
-
125
- ```ts
126
- router.route.update({
127
- myParam: 123,
128
- tab: 'github',
129
- })
130
- ```
93
+ This means then when you import a component, composition, or hook from `@kitbag/router` it will be correctly typed. Alternatively, you can create your own typed router assets by using the [`createRouterAssets`](https://kitbag-router.netlify.app/api/functions/createRouterAssets) utility. This approach is especially useful for projects that use multiple routers.
131
94
 
132
95
  ## RouterView
133
96
 
134
97
  Give your route components a place to be mounted
135
98
 
136
- ```html {4-5}
137
- <!-- App.vue -->
99
+ ```html
138
100
  <div class="app">
139
- ...
140
- <!-- matched route.component gets rendered here -->
141
101
  <router-view />
142
102
  </div>
143
103
  ```
144
104
 
145
- This component can be mounted anywhere you want route components to be mounted. Nested routes can also have a nested `RouterView` which would be responsible for rendering any children that route may have. Read more about [nested routes](https://kitbag-router.netlify.app/core-concepts/defining-routes#nested-routes).
105
+ This component can be mounted anywhere you want route components to be mounted. Nested routes can also have a nested `RouterView` which would be responsible for rendering any children that route may have. Read more about [nested routes](https://kitbag-router.netlify.app/core-concepts/routes#parent).
146
106
 
147
107
  ## RouterLink
148
108
 
149
109
  Use RouterLink for navigating between routes.
150
110
 
151
- ```html {3-4}
111
+ ```html
152
112
  <template>
153
- ...
154
- <!-- router-link renders as <a> with href -->
155
- <router-link :to="(resolve) => resolve('home')">Go somewhere</router-link>
113
+ <router-link :to="(resolve) => resolve('home')">Home</router-link>
156
114
  </template>
157
115
  ```
158
116
 
159
- This component gives the router the power to change the URL without reloading the page.
117
+ ### Type Safety in RouterLink
160
118
 
161
- [npm-badge]: https://img.shields.io/npm/v/@kitbag/router.svg
162
- [npm-url]: https://www.npmjs.org/package/@kitbag/router
163
- [netlify-badge]: https://api.netlify.com/api/v1/badges/c12f79b8-49f9-4529-bc23-f8ffca8919a3/deploy-status
164
- [netlify-url]: https://app.netlify.com/sites/kitbag-router/deploys
165
- [discord-badge]: https://img.shields.io/discord/1079625926024900739?logo=discord&label=Discord
166
- [discord-url]: https://discord.gg/zw7dpcc5HV
167
- [stackblitz-badge]: https://developer.stackblitz.com/img/open_in_stackblitz_small.svg
168
- [stackblitz-url]: https://stackblitz.com/~/github.com/kitbagjs/router-preview
119
+ The `to` prop accepts a callback function or a [`Url`](https://kitbag-router.netlify.app/api/types/Url) string. When using a callback function, the router will provide a `resolve` function that is a type safe way to create link for your pre-defined routes.
@@ -1,4 +1,4 @@
1
- import { Url } from '../types/url';
1
+ import { UrlString } from '../types/urlString';
2
2
  import { ResolvedRoute } from '../types/resolved';
3
3
  import { EmitsOptions, InjectionKey, SlotsType, VNode } from 'vue';
4
4
  import { Router } from '../types/router';
@@ -14,7 +14,7 @@ type RouterLinkSlots = {
14
14
  }) => VNode[];
15
15
  };
16
16
  export declare function createRouterLink<TRouter extends Router>(routerKey: InjectionKey<TRouter>): import('vue').DefineSetupFnComponent<RouterLinkProps<TRouter>, EmitsOptions, SlotsType<RouterLinkSlots>, import('../main').RouterPushOptions & {
17
- to: Url | Readonly<{
17
+ to: Readonly<{
18
18
  id: string;
19
19
  matched: import('../main').CreatedRouteOptions;
20
20
  matches: import('../main').CreatedRouteOptions[];
@@ -25,9 +25,9 @@ export declare function createRouterLink<TRouter extends Router>(routerKey: Inje
25
25
  [x: string]: unknown;
26
26
  };
27
27
  state: import('../types/state').ExtractRouteStateParamsAsOptional<Record<string, import('../main').Param>>;
28
- href: Url;
29
- hooks: import('../models/RouterRouteHooks').RouterRouteHooks[];
30
- }> | ToCallback<TRouter>;
28
+ href: UrlString;
29
+ hooks: import('../models/hooks').Hooks[];
30
+ }> | UrlString | ToCallback<TRouter>;
31
31
  prefetch?: import('../main').PrefetchConfig;
32
32
  } & ({
33
33
  [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
@@ -1,14 +1,14 @@
1
1
  import { InjectionKey, MaybeRefOrGetter } from 'vue';
2
2
  import { ResolvedRoute } from '../types/resolved';
3
3
  import { RouteParamsByKey } from '../types/routeWithParams';
4
- import { Url } from '../types/url';
4
+ import { UrlString } from '../types/urlString';
5
5
  import { AllPropertiesAreOptional } from '../types/utilities';
6
6
  import { Router, RouterRouteName, RouterRoutes } from '../types/router';
7
7
  import { UseLink, UseLinkOptions } from '../types/useLink';
8
8
  type UseLinkArgs<TRouter extends Router, TSource extends RouterRouteName<TRouter>, TParams = RouteParamsByKey<RouterRoutes<TRouter>, TSource>> = AllPropertiesAreOptional<TParams> extends true ? [params?: MaybeRefOrGetter<TParams>, options?: MaybeRefOrGetter<UseLinkOptions>] : [params: MaybeRefOrGetter<TParams>, options?: MaybeRefOrGetter<UseLinkOptions>];
9
9
  type UseLinkFunction<TRouter extends Router> = {
10
10
  <TRouteKey extends RouterRouteName<TRouter>>(name: MaybeRefOrGetter<TRouteKey>, ...args: UseLinkArgs<TRouter, TRouteKey>): UseLink;
11
- (url: MaybeRefOrGetter<Url>, options?: MaybeRefOrGetter<UseLinkOptions>): UseLink;
11
+ (url: MaybeRefOrGetter<UrlString>, options?: MaybeRefOrGetter<UseLinkOptions>): UseLink;
12
12
  (resolvedRoute: MaybeRefOrGetter<ResolvedRoute | undefined>, options?: MaybeRefOrGetter<UseLinkOptions>): UseLink;
13
13
  (source: MaybeRefOrGetter<string | ResolvedRoute | undefined>, paramsOrOptions?: MaybeRefOrGetter<Record<PropertyKey, unknown> | UseLinkOptions>, maybeOptions?: MaybeRefOrGetter<UseLinkOptions>): UseLink;
14
14
  };
@@ -0,0 +1,13 @@
1
+ import { App } from 'vue';
2
+ import { Router } from '../types/router';
3
+ import { Routes } from '../types/route';
4
+ type RouterDevtoolsProps = {
5
+ router: Router;
6
+ app: App;
7
+ routes: Routes;
8
+ };
9
+ /**
10
+ * Sets up Vue DevTools integration for Kitbag Router.
11
+ */
12
+ export declare function setupRouterDevtools({ router, app, routes }: RouterDevtoolsProps): void;
13
+ export {};
@@ -0,0 +1,12 @@
1
+ import { Route } from '../types/route';
2
+ import { InspectorTreePayload } from './types';
3
+ type RouteFilterOptions = {
4
+ route: Route;
5
+ payload: InspectorTreePayload;
6
+ };
7
+ type RouteFilter = (options: RouteFilterOptions) => boolean;
8
+ /**
9
+ * Checks if a route should be shown based on multiple filters.
10
+ */
11
+ export declare const shouldShowRoute: RouteFilter;
12
+ export {};
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Generates a DevTools label with optional router instance ID.
3
+ * If the router ID is greater than 1, appends the ID to the label.
4
+ *
5
+ * @param label - Base label for the DevTools element
6
+ * @param routerId - Router instance ID (string from createUniqueIdSequence)
7
+ * @returns Label with ID appended if routerId > '1'
8
+ *
9
+ * @example
10
+ * getDevtoolsLabel('Routes', '1') // 'Routes'
11
+ * getDevtoolsLabel('Routes', '2') // 'Routes 2'
12
+ */
13
+ export declare function getDevtoolsLabel(label: string, routerId: string): string;
@@ -0,0 +1,10 @@
1
+ import { setupDevtoolsPlugin } from '@vue/devtools-api';
2
+ type ExtractAPIFromCallback = Parameters<Parameters<typeof setupDevtoolsPlugin>[1]>[0];
3
+ type ExtractInspectorTreeHandler = Parameters<ExtractAPIFromCallback['on']['getInspectorTree']>[0];
4
+ type ExtractInspectorStateHandler = Parameters<ExtractAPIFromCallback['on']['getInspectorState']>[0];
5
+ type ExtractInspectorStatePayload = Parameters<ExtractInspectorStateHandler>[0];
6
+ type InspectorTreePayload = Parameters<ExtractInspectorTreeHandler>[0];
7
+ type CustomInspectorNode = InspectorTreePayload['rootNodes'][number];
8
+ type InspectorNodeTag = NonNullable<CustomInspectorNode['tags']>[number];
9
+ type CustomInspectorState = ExtractInspectorStatePayload['state'];
10
+ export type { CustomInspectorNode, InspectorTreePayload, InspectorNodeTag, CustomInspectorState };
@@ -1,6 +1,6 @@
1
- import { CallbackAbortResponse } from '../services/createCallbackContext';
1
+ import { CallbackContextAbort } from '../types/callbackContext';
2
2
  import { ContextError } from './contextError';
3
3
  export declare class ContextAbortError extends ContextError {
4
- response: CallbackAbortResponse;
4
+ response: CallbackContextAbort;
5
5
  constructor();
6
6
  }
@@ -1,6 +1,6 @@
1
- import { CallbackPushResponse } from '../services/createCallbackContext';
1
+ import { CallbackContextPush } from '../types/callbackContext';
2
2
  import { ContextError } from './contextError';
3
3
  export declare class ContextPushError extends ContextError {
4
- response: CallbackPushResponse;
4
+ response: CallbackContextPush;
5
5
  constructor(to: unknown[]);
6
6
  }
@@ -1,6 +1,6 @@
1
- import { CallbackRejectResponse } from '../services/createCallbackContext';
1
+ import { CallbackContextReject } from '../types/callbackContext';
2
2
  import { ContextError } from './contextError';
3
3
  export declare class ContextRejectionError extends ContextError {
4
- response: CallbackRejectResponse;
4
+ response: CallbackContextReject;
5
5
  constructor(type: string);
6
6
  }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * An error thrown when a route does not support redirects.
3
+ * @group Errors
4
+ */
5
+ export declare class InvalidRouteRedirectError extends Error {
6
+ /**
7
+ * Constructs a new InvalidRouteRedirectError instance with a message indicating the problematic route redirect.
8
+ * @param routeName - The name of the route that does not support redirects.
9
+ */
10
+ constructor(routeName: string);
11
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * An error thrown when duplicate parameters are detected in a route.
3
+ * Param names must be unique. This includes params defined in a path
4
+ * parent and params defined in the query.
5
+ * @group Errors
6
+ */
7
+ export declare class MultipleRouteRedirectsError extends Error {
8
+ /**
9
+ * Constructs a new MultipleRouteRedirectsError instance with a message indicating the problematic route redirect.
10
+ * @param routeName - The name of the route that has multiple redirects.
11
+ */
12
+ constructor(routeName: string);
13
+ }
package/dist/keys.d.ts CHANGED
@@ -1,3 +1 @@
1
- import { InjectionKey } from 'vue';
2
- import { RegisteredRouter } from './types/register';
3
- export declare const routerInjectionKey: InjectionKey<RegisteredRouter>;
1
+ export declare const routerInjectionKey: unique symbol;