@kitbag/router 0.20.11 → 0.21.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 (51) hide show
  1. package/README.md +29 -87
  2. package/dist/components/routerLink.d.ts +3 -3
  3. package/dist/errors/contextAbortError.d.ts +2 -2
  4. package/dist/errors/contextPushError.d.ts +2 -2
  5. package/dist/errors/contextRejectionError.d.ts +2 -2
  6. package/dist/keys.d.ts +1 -3
  7. package/dist/kitbag-router.js +1780 -1847
  8. package/dist/kitbag-router.umd.cjs +3 -3
  9. package/dist/main.d.ts +0 -16
  10. package/dist/models/hooks.d.ts +10 -0
  11. package/dist/services/createComponentHooks.d.ts +7 -7
  12. package/dist/services/createExternalRoute.d.ts +3 -2
  13. package/dist/services/createHooksFactory.d.ts +15 -0
  14. package/dist/services/createPropStore.d.ts +2 -2
  15. package/dist/services/createRoute.d.ts +2 -1
  16. package/dist/services/createRouterAssets.d.ts +5 -5
  17. package/dist/services/createRouterCallbackContext.d.ts +8 -6
  18. package/dist/services/createRouterHooks.d.ts +14 -14
  19. package/dist/services/createRouterPlugin.d.ts +2 -2
  20. package/dist/services/createUrlParts.d.ts +13 -0
  21. package/dist/services/getGlobalHooksForRouter.d.ts +2 -3
  22. package/dist/services/getGlobalRouteHooks.d.ts +3 -3
  23. package/dist/services/getRouteHooks.d.ts +3 -3
  24. package/dist/services/hooks.d.ts +2 -2
  25. package/dist/services/urlCombine.d.ts +2 -1
  26. package/dist/services/urlCreator.d.ts +2 -1
  27. package/dist/services/urlParser.d.ts +1 -1
  28. package/dist/types/callbackContext.d.ts +15 -0
  29. package/dist/types/createRouteOptions.d.ts +6 -13
  30. package/dist/types/hooks.d.ts +56 -136
  31. package/dist/types/params.d.ts +11 -5
  32. package/dist/types/props.d.ts +8 -8
  33. package/dist/types/register.d.ts +1 -41
  34. package/dist/types/rejection.d.ts +6 -2
  35. package/dist/types/resolved.d.ts +2 -2
  36. package/dist/types/route.d.ts +5 -4
  37. package/dist/types/routeContext.d.ts +11 -5
  38. package/dist/types/router.d.ts +11 -11
  39. package/dist/types/routerAbort.d.ts +1 -0
  40. package/dist/types/routerPlugin.d.ts +17 -41
  41. package/dist/types/routerReject.d.ts +2 -2
  42. package/dist/types/url.d.ts +0 -9
  43. package/dist/types/urlParts.d.ts +53 -0
  44. package/dist/utilities/makeOptional.d.ts +1 -0
  45. package/dist/utilities/testHelpers.d.ts +279 -11
  46. package/package.json +4 -2
  47. package/dist/models/RouteHooks.d.ts +0 -12
  48. package/dist/models/RouterRouteHooks.d.ts +0 -10
  49. package/dist/services/createCallbackContext.d.ts +0 -44
  50. package/dist/services/createRouteHooks.d.ts +0 -15
  51. package/dist/services/getRouteHooksDeprecated.d.ts +0 -10
package/README.md CHANGED
@@ -15,6 +15,8 @@ Get Started with our [documentation](https://kitbag-router.netlify.app/) or our
15
15
 
16
16
  ## Installation
17
17
 
18
+ Install Kitbag Router with your favorite package manager
19
+
18
20
  ```bash
19
21
  # bun
20
22
  bun add @kitbag/router
@@ -24,43 +26,52 @@ yarn add @kitbag/router
24
26
  npm install @kitbag/router
25
27
  ```
26
28
 
27
- ## Define Basic Routes
29
+ ## Define Routes
28
30
 
29
- Create an array of possible routes. Learn more about [defining routes](https://kitbag-router.netlify.app/core-concepts/defining-routes).
31
+ 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
32
 
31
33
  ```ts
32
- // /routes.ts
33
34
  import { createRoute } from '@kitbag/router'
34
35
 
35
36
  const Home = { template: '<div>Home</div>' }
36
37
  const About = { template: '<div>About</div>' }
37
38
 
38
- export const routes = [
39
+ const routes = [
39
40
  createRoute({ name: 'home', path: '/', component: Home }),
40
41
  createRoute({ name: 'path', path: '/about', component: About }),
41
42
  ] as const
42
43
  ```
43
44
 
44
- ## Plugin
45
+ > [!NOTE] Type Safety
46
+ > Using `as const` when defining routes is important as it ensures the types are correctly inferred.
45
47
 
46
- Create a router instance and pass it to the app as a plugin
48
+ ## Create Router
49
+
50
+ A router is created using the [`createRouter`](https://kitbag-router.netlify.app/api/functions/createRouter) utility and passing in the routes.
47
51
 
48
52
  ```ts
49
- import { createApp } from 'vue'
50
53
  import { createRouter } from '@kitbag/router'
51
- import { routes } from '/routes'
52
- import App from './App.vue'
53
54
 
54
55
  const router = createRouter(routes)
56
+ ```
57
+
58
+ ## Vue Plugin
59
+
60
+ Create a router instance and pass it to the app as a plugin
61
+
62
+ ```ts {6}
63
+ import { createApp } from 'vue'
64
+ import App from './App.vue'
65
+
55
66
  const app = createApp(App)
56
67
 
57
68
  app.use(router)
58
69
  app.mount('#app')
59
70
  ```
60
71
 
61
- ## Update Registered Router
72
+ ## Type Safety
62
73
 
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.
74
+ 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
75
 
65
76
  ```ts
66
77
  declare module '@kitbag/router' {
@@ -70,99 +81,30 @@ declare module '@kitbag/router' {
70
81
  }
71
82
  ```
72
83
 
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
- ```
84
+ 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
85
 
132
86
  ## RouterView
133
87
 
134
88
  Give your route components a place to be mounted
135
89
 
136
- ```html {4-5}
137
- <!-- App.vue -->
90
+ ```html
138
91
  <div class="app">
139
- ...
140
- <!-- matched route.component gets rendered here -->
141
92
  <router-view />
142
93
  </div>
143
94
  ```
144
95
 
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).
96
+ 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
97
 
147
98
  ## RouterLink
148
99
 
149
100
  Use RouterLink for navigating between routes.
150
101
 
151
- ```html {3-4}
102
+ ```html
152
103
  <template>
153
- ...
154
- <!-- router-link renders as <a> with href -->
155
- <router-link :to="(resolve) => resolve('home')">Go somewhere</router-link>
104
+ <router-link :to="(resolve) => resolve('home')">Home</router-link>
156
105
  </template>
157
106
  ```
158
107
 
159
- This component gives the router the power to change the URL without reloading the page.
108
+ ### Type Safety in RouterLink
160
109
 
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
110
+ 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.
@@ -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[];
@@ -26,8 +26,8 @@ export declare function createRouterLink<TRouter extends Router>(routerKey: Inje
26
26
  };
27
27
  state: import('../types/state').ExtractRouteStateParamsAsOptional<Record<string, import('../main').Param>>;
28
28
  href: Url;
29
- hooks: import('../models/RouterRouteHooks').RouterRouteHooks[];
30
- }> | ToCallback<TRouter>;
29
+ hooks: import('../models/hooks').Hooks[];
30
+ }> | Url | ToCallback<TRouter>;
31
31
  prefetch?: import('../main').PrefetchConfig;
32
32
  } & ({
33
33
  [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
@@ -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
  }
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;