Skip to content

Commit 223c8ed

Browse files
fix: return a Navigation "error" inside navigate to satisfy linters who complain about throwing void
1 parent 1f640ee commit 223c8ed

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

.changeset/silent-numbers-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv-router': patch
3+
---
4+
5+
return navigation "error" inside `navigate`

src/create-router.svelte.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
stripBase,
1313
updatedLocation,
1414
} from './helpers/utils.js';
15+
import { Navigation } from './navigation.js';
1516
import { syncSearchParams } from './search-params.svelte.js';
1617

1718
/** @type {import('./index.d.ts').Routes} */
@@ -118,7 +119,7 @@ export function createRouter(r) {
118119
function navigate(path, options = {}) {
119120
if (typeof path === 'number') {
120121
globalThis.history.go(path);
121-
return;
122+
return new Navigation(`History entry: ${path}`);
122123
}
123124

124125
path = constructPath(path, options.params);
@@ -128,6 +129,7 @@ function navigate(path, options = {}) {
128129
options.hash = '#' + options.hash;
129130
}
130131
onNavigate(path, options);
132+
return new Navigation(`${path}${serializeSearch(options?.search ?? '')}${options?.hash ?? ''}`);
131133
}
132134

133135
/**
@@ -142,7 +144,7 @@ export async function onNavigate(path, options = {}) {
142144
navigationIndex++;
143145
const currentNavigationIndex = navigationIndex;
144146

145-
let matchPath = getMatchPath(path);
147+
const matchPath = getMatchPath(path);
146148
const { match, layouts, hooks, meta: newMeta, params: newParams } = matchRoute(matchPath, routes);
147149

148150
const search = parseSearch(options.search);
@@ -220,7 +222,7 @@ export async function onNavigate(path, options = {}) {
220222

221223
/** @param {string} [path] */
222224
function getMatchPath(path) {
223-
let matchPath = '';
225+
let matchPath;
224226

225227
if (path) {
226228
matchPath = path;

src/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export type IsActiveLink = Action<
106106
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
107107
export interface RouteMeta {}
108108

109+
export class Navigation extends Error {
110+
constructor(target: string);
111+
}
112+
109113
export type RouterApi<T extends Routes> = {
110114
/**
111115
* Construct a path while ensuring type safety.
@@ -139,8 +143,9 @@ export type RouterApi<T extends Routes> = {
139143
*
140144
* @param route The route to navigate to.
141145
* @param options The navigation options.
146+
* @returns {@link Navigation} For use with `throw navigate(...)` inside hooks.
142147
*/
143-
navigate<U extends Path<T>>(...args: NavigateArgs<U>): void;
148+
navigate<U extends Path<T>>(...args: NavigateArgs<U>): Navigation;
144149

145150
/**
146151
* Will return `true` if the given path is active.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { isActiveLink } from './actions.svelte.js';
22
export { createRouter } from './create-router.svelte.js';
33
export { serializeSearch } from './helpers/utils.js';
4+
export { Navigation } from './navigation.js';
45
export { default as Router } from './Router.svelte';
56
export { searchParams } from './search-params.svelte.js';

src/navigation.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class Navigation extends Error {
2+
constructor(
3+
/** @type {string} target */
4+
target,
5+
) {
6+
super(`Navigating to: ${target}`);
7+
this.name = 'Redirect';
8+
}
9+
}

0 commit comments

Comments
 (0)