import { type Snippet } from 'svelte'; import type { Attachment } from 'svelte/attachments'; export type RoutePart = string | RouteParam; export declare class RouteParam { readonly name: string; constructor(name: string); } /** * A full path in the router. * Can be created from a string, or from parts. * * Examples with created parts array: * ```js * RoutePath.fromString('/users/:userId') // ['users', RouteParam('userId')] * RoutePath.fromParts(['users', new RouteParam('userId')]) // ['users', RouteParam('userId')] * RoutePath.create404() // 404 route, no parts * ``` */ export declare class RoutePath { readonly parts: readonly RoutePart[]; protected constructor(parts: readonly RoutePart[]); static fromString(path: string, is404?: boolean): RoutePath; static fromParts(parts: readonly RoutePart[]): RoutePath; static create404(layout?: readonly RoutePart[]): RoutePath; static readonly concatPaths: (base: string, ...rest: string[]) => string; /** * Normalize a path by removing leading and trailing slashes. * @param path The path to normalize. */ static readonly normalizePath: (path: string) => string; readonly matches: (path: string) => boolean; } export declare class Route404Path extends RoutePath { readonly parts: readonly RoutePart[]; constructor(parts: readonly RoutePart[]); } /** * A route in the application. This is not necessarily the route that is currently active. */ export interface ApplicationRoute { path: RoutePath; layout?: LayoutData; name?: string; parents?: string[]; component?: Snippet; } export type RouterEvents = { 'route:afterSwitch': (oldRoute: ActiveRoute | undefined, newRoute: ActiveRoute) => void; }; export type RouterOptions = Partial<{ /** * The default page title to be used when no specific title is set for a route. */ defaultTitle: string; basePath: string; }>; export declare function defaultRouterOptions(): RouterOptions; /** * A container for routes, either a router or a layout. */ export interface RouteContainer { /** * Create a route. * @param route The route to register. * @param layout The layout to register the route under. */ registerRoute(route: ApplicationRoute, layout?: LayoutData): void; /** * Create a 404 route. * @param route The route to register as a 404 route. * @param layout The layout to register the route under. */ registerRoute404(route: ApplicationRoute, layout?: LayoutData): void; /** * Remove a route * @param route The route to unregister. * @param layout The layout to unregister the route from. */ unregisterRoute(route: ApplicationRoute, layout?: LayoutData): void; /** * Check if this route container is a router. * * @returns True if this is a router, false otherwise. */ isRouter(): this is Router; } /** * A router used to switch between routes in an application. */ export interface Router extends RouteContainer { readonly options: RouterOptions; /** Params specified in the route with : syntax, like /users/:userId */ params: RouteParams; /** Query params specified after the route with ?, like /users/1234?showFullName=true */ queryParams: RouteParams; /** * Switch to a route. * @param path * @param queryParams * @param pushNewState */ switchTo(path: string, queryParams?: Record | RouteParams, pushNewState?: boolean): ActiveRoute; /** * Get the current route. */ currentRoute: ActiveRoute; getRoute(path: string): ApplicationRoute; /** * Get all routes, including 404 routes. */ getAllRoutes(): ApplicationRoute[]; createParams(path: string): RouteParams; /** * A quick refresh will not push a new state to the browser's history. * @param quick */ refresh(quick?: boolean): void; /** * Get a route parameter by name from the current route. * @param name */ getRouteParam(name: string): string; /** * Get a query parameter by name from the current route. * @param name */ getQueryParam(name: string): string | undefined; } export interface ActiveRoute { /** * The route object that is currently active. */ route: ApplicationRoute; /** * Note: this may differ from route.path if the route is a 404 route. */ path: string; /** Params specified in the route with : syntax, like /users/:userId */ params: RouteParams; /** Query params specified after the route with ?, like /users/1234?showFullName=true */ queryParams: RouteParams; } export type RouteParams = { [key: string]: string; }; export declare const ROUTER_CONTEXT_KEY = "switchboard::router"; export declare const LAYOUT_CONTEXT_KEY = "switchboard::layout"; export declare const ROUTE_NOT_FOUND_KEY = "__switchboard__404"; export type LayoutSnippet = Snippet<[Snippet]>; export interface LayoutData extends RouteContainer { path?: string; parent?: LayoutData; canonicalParent?: LayoutData; renderer: LayoutSnippet; notFoundRoute?: ApplicationRoute; joinedPath: string; } export declare function getLayout(): LayoutData | undefined; export declare function setLayoutContext(layout: LayoutData | undefined): void; export declare function getRouter(): T; export declare function getRouteParams(): RouteParams; export declare function getQueryParams(): RouteParams; export declare function setRouterContext(router: Router): void; /** * Gets the nearest route container, either a layout or the router. */ export declare function getRouteContainer(): RouteContainer; /** * Create an attachment that sets the href of an anchor element * and switches to the route when clicked. * This is useful for creating links that work with the router, and won't cause a refresh when clicked. * @param href The href to set on the anchor element. * * @see Link */ export declare const href: (href: string) => Attachment; export declare const getAllLayouts: (layout?: LayoutData) => LayoutData[]; export declare const getAllCanonicalLayouts: (layout?: LayoutData) => LayoutData[];