UNPKG

svelte5-spa-router

Version:

A simple, flexible, and lightweight SPA router specifically designed for Svelte 5 with runes support (dev/non-stable, not production ready)

140 lines (139 loc) 4.35 kB
/** * @param {string} path * @param {Record<string, string>} [queryParams] * @param {string} [hash] */ export function goto(path: string, queryParams?: Record<string, string>, hash?: string): void; /** * @param {string} key * @param {string} [defaultValue] * @returns {string} */ export function getQueryParam(key: string, defaultValue?: string): string; /** * @param {Record<string, string>} newParams * @param {boolean} [replace] */ export function updateQueryParams(newParams: Record<string, string>, replace?: boolean): void; export const locationStore: import("svelte/store").Writable<{ pathname: string; search: string; hash: string; }>; /** * @typedef {Object} CurrentRoute * @property {string} path * @property {any} component * @property {Record<string, string>} params * @property {Array<{component: any, params: Record<string, string>}>} branch */ /** @type {import('svelte/store').Writable<CurrentRoute>} */ export const currentRoute: import("svelte/store").Writable<CurrentRoute>; export const routeParams: import("svelte/store").Writable<{}>; export const queryParams: import("svelte/store").Writable<{}>; export const hashFragment: import("svelte/store").Writable<string>; export const router: Router; export type CurrentRoute = { path: string; component: any; params: Record<string, string>; branch: Array<{ component: any; params: Record<string, string>; }>; }; declare class Router { /** * Find route match for a given path (flat route definitions) * @param {string} path * @returns {Array<{route: object, params: object}>} */ /** * Find route match for a given path (flat route definitions) * @param {string} path * @returns {Array<{route: {path: string, component: any, beforeEnter?: (to: string, from: string) => boolean|Promise<boolean>}, params: Record<string, string>}>} */ findRouteBranch(path: string): Array<{ route: { path: string; component: any; beforeEnter?: (to: string, from: string) => boolean | Promise<boolean>; }; params: Record<string, string>; }>; /** * @type {Array<{path: string, component: any, beforeEnter?: (to: string, from: string) => boolean|Promise<boolean>}>} */ routes: Array<{ path: string; component: any; beforeEnter?: (to: string, from: string) => boolean | Promise<boolean>; }>; /** @type {any} */ fallback: any; _lastPath: string; /** * @param {string} path * @param {any} component * @param {object} [options] * @param {(to: string, from: string) => boolean|Promise<boolean>} [options.beforeEnter] */ /** * @param {string} path * @param {any} component * @param {{ beforeEnter?: (to: string, from: string) => boolean|Promise<boolean> }} [options] */ /** * Add a route (flat definition) * @param {{path: string, component: any, beforeEnter?: (to: string, from: string) => boolean|Promise<boolean>}} routeObj - Route object, must have path and component */ addRoute(routeObj: { path: string; component: any; beforeEnter?: (to: string, from: string) => boolean | Promise<boolean>; }): void; /** * Clear all routes */ clearRoutes(): void; /** * @param {any} component */ setFallback(component: any): void; /** * @param {string} fullPath * @returns {Promise<any>} */ navigate(fullPath: string): Promise<any>; /** * @returns {Promise<any>} */ navigateToCurrentUrl(): Promise<any>; /** * @param {string} url * @returns {{ pathname: string, queryParams: Record<string, string>, hash: string }} */ parseUrl(url: string): { pathname: string; queryParams: Record<string, string>; hash: string; }; /** * @param {string} path * @returns {any} */ findRoute(path: string): any; /** * @param {string} routePath * @param {string} currentPath * @returns {{ params: Record<string, string> } | null} */ matchRoute(routePath: string, currentPath: string): { params: Record<string, string>; } | null; /** * @returns {any} */ getCurrentComponent(): any; } export {};