import * as SegmentExtractor from '../../regexp/SegmentExtractor.mjs'; import TinyDebugger from '../tools/TinyDebugger.mjs'; export type RouterHistoryEntry = { /** * - The URL path recorded in history. */ path: string; /** * - The Unix timestamp when this path was visited. */ timestamp: number; }; export type SegExResultExtra = { /** * - Stored to allow removal by string. */ pattern?: string; }; export type AddRouteOptionsWithFunction = { /** * - A function used to process the pattern. */ callback: import('../../regexp/SegmentExtractor.mjs').SegExFunction; /** * - Stored to allow removal by string. */ pattern: string; }; export type SegExResult = import('../../regexp/SegmentExtractor.mjs').SegExResult & SegExResultExtra; export type AddRouteOptions = { /** * - Stored to allow removal by string. */ pattern: string; /** * - The search pattern (string or RegExp) used to identify dynamic segments. */ searchValue: string | RegExp; /** * - The function defining how to transform a segment into a capture group. */ replaceValue: import('../../regexp/SegmentExtractor.mjs').SegExReplacer; /** * - Optional configuration to customize error messages. */ errorConfig: import('../../regexp/SegmentExtractor.mjs').SegExErrorConfig; }; export type RouteMatch = { /** * - The matched URL path. */ path: string; /** * - The dynamic parameters extracted from the path. */ params: Record; /** * - The URL search parameters. */ query: URLSearchParams; }; export type RouteNotFoundData = { /** * - The path that failed to match. */ path: string; /** * - The URL search parameters. */ query: URLSearchParams; }; export type RouteCallback = (match: RouteMatch) => Promise | void; export type RouteNotFoundCallback = (match: RouteNotFoundData) => void; export type RouterOptions = { /** * - Whether to enable internal debug logging. */ debugMode?: boolean; /** * - Whether to enable log color support. */ useLogColors?: boolean; /** * - Whether to detect if the history navigation actually changed the URL. */ detectHistoryChange?: boolean; /** * - A custom logger object (must implement console methods). */ logger?: Partial; /** * - Callback executed whenever the route changes. */ onRouteChanged?: RouteCallback; /** * - Callback executed when no route matches. */ onRouteNotFound?: RouteNotFoundCallback; /** * - Maximum number of history entries. Use -1 for unlimited, or 0 to disable history. */ historyLimit?: number; }; export type RouteDefinition = { /** * - The extraction logic and metadata used to match the path and retrieve parameters. */ segmentExtractor: SegExResult; /** * - The function to execute on match. */ callback: RouteCallback; }; /** * Represents a single entry in the navigation history. * @typedef {Object} RouterHistoryEntry * @property {string} path - The URL path recorded in history. * @property {number} timestamp - The Unix timestamp when this path was visited. */ /** * Additional metadata stored alongside segment extraction results. * @typedef {Object} SegExResultExtra * @property {string} [pattern] - Stored to allow removal by string. */ /** * Configuration for registering a route using a function to generate the pattern. * @typedef {Object} AddRouteOptionsWithFunction * @property {import('../../regexp/SegmentExtractor.mjs').SegExFunction} callback - A function used to process the pattern. * @property {string} pattern - Stored to allow removal by string. */ /** * The result of a segment extraction combined with additional metadata. * @typedef {import('../../regexp/SegmentExtractor.mjs').SegExResult & SegExResultExtra} SegExResult */ /** * Configuration for registering a route using a custom segment extraction pattern. * @typedef {Object} AddRouteOptions * @property {string} pattern - Stored to allow removal by string. * @property {string|RegExp} searchValue - The search pattern (string or RegExp) used to identify dynamic segments. * @property {import('../../regexp/SegmentExtractor.mjs').SegExReplacer} replaceValue - The function defining how to transform a segment into a capture group. * @property {import('../../regexp/SegmentExtractor.mjs').SegExErrorConfig} errorConfig - Optional configuration to customize error messages. */ /** * Data structure containing the results of a successful route match. * @typedef {Object} RouteMatch * @property {string} path - The matched URL path. * @property {Record} params - The dynamic parameters extracted from the path. * @property {URLSearchParams} query - The URL search parameters. */ /** * Data structure containing details when no route matches the current URL. * @typedef {Object} RouteNotFoundData * @property {string} path - The path that failed to match. * @property {URLSearchParams} query - The URL search parameters. */ /** * A function executed when a route is successfully matched. * @callback RouteCallback * @param {RouteMatch} match - The object containing path, params, and query. * @returns {Promise|void} - A promise or void indicating completion. */ /** * A function executed when no route matches the current URL. * @callback RouteNotFoundCallback * @param {RouteNotFoundData} match - The object containing path and query. * @returns {void} */ /** * Configuration options for initializing the TinyRouter instance. * @typedef {Object} RouterOptions * @property {boolean} [debugMode=false] - Whether to enable internal debug logging. * @property {boolean} [useLogColors=false] - Whether to enable log color support. * @property {boolean} [detectHistoryChange=true] - Whether to detect if the history navigation actually changed the URL. * @property {Partial} [logger=console] - A custom logger object (must implement console methods). * @property {RouteCallback} [onRouteChanged] - Callback executed whenever the route changes. * @property {RouteNotFoundCallback} [onRouteNotFound] - Callback executed when no route matches. * @property {number} [historyLimit=0] - Maximum number of history entries. Use -1 for unlimited, or 0 to disable history. */ /** * A definition of a route, combining its pattern extractor and its execution callback. * @typedef {Object} RouteDefinition * @property {SegExResult} segmentExtractor - The extraction logic and metadata used to match the path and retrieve parameters. * @property {RouteCallback} callback - The function to execute on match. */ /** * A lightweight, framework-agnostic router for managing client-side navigation. */ declare class TinyRouter extends TinyDebugger { #private; /** The SegmentExtractor module. */ static SegmentExtractor: typeof SegmentExtractor; /** * @param {RouterOptions} [options={}] - Configuration options for the router. * @throws {TypeError} If options is not an object or if the logger lacks required methods. */ constructor(options?: RouterOptions); /** @returns {boolean} Returns whether the router instance has been destroyed. */ get isDestroyed(): boolean; /** @returns {number} Returns the current position in history. */ get historyIndex(): number; /** @returns {number} Returns the maximum number of history entries allowed. */ get historyLimit(): number; /** @returns {boolean} Returns whether the router detects changes in history navigation. */ get detectHistoryChange(): boolean; /** @param {boolean} value Sets whether the router detects changes in history navigation. */ set detectHistoryChange(value: boolean); /** @returns {RouteCallback} Returns the callback function for route changes. */ get onRouteChanged(): RouteCallback; /** @param {RouteCallback} value Sets the callback function for route changes. */ set onRouteChanged(value: RouteCallback); /** @returns {RouteNotFoundCallback} Returns the callback function for unmatched routes. */ get onRouteNotFound(): RouteNotFoundCallback; /** @param {RouteNotFoundCallback} value Sets the callback function for unmatched routes. */ set onRouteNotFound(value: RouteNotFoundCallback); /** @returns {boolean} Returns whether the router is currently active. */ get started(): boolean; /** * Returns the current number of registered routes. * @returns {number} */ get size(): number; /** * Returns an array of all registered path patterns. * @returns {string[]} An array of path pattern strings. */ get routes(): string[]; /** * Returns a copy of the current navigation history. * @returns {RouterHistoryEntry[]} An array of history entries. */ get history(): RouterHistoryEntry[]; /** * Imports and validates a history log to restore previous navigation state. * @param {RouterHistoryEntry[]} historyData - The array of history entries to import. * @throws {TypeError} If historyData is not an array or if any entry is invalid. */ set history(historyData: RouterHistoryEntry[]); /** * Registers a new route pattern. * @param {string | AddRouteOptions | AddRouteOptionsWithFunction} patternOrOptions - * A path pattern string (e.g., '/user/:id') OR a configuration object for custom regex. * @param {RouteCallback} callback - Function to execute when this route is matched. * @throws {TypeError} If arguments are invalid. * @throws {Error} If the route pattern is already registered. */ add(patternOrOptions: string | AddRouteOptions | AddRouteOptionsWithFunction, callback: RouteCallback): void; /** * Removes a registered route by its original pattern string. * @param {string} pathPattern - The pattern to remove. * @throws {TypeError} If pathPattern is not a string. */ remove(pathPattern: string): void; /** * Checks if a route pattern is already registered. * @param {string} pathPattern - The pattern to check. * @returns {boolean} True if the route exists, false otherwise. * @throws {TypeError} If pathPattern is not a string. */ has(pathPattern: string): boolean; /** * Navigates to a specific path programmatically. * @param {string | URL | null | undefined} path - The target path. * @param {any} [state={}] - Optional state object to associate with history. * @returns {Promise} * @throws {TypeError} If path is not a string or state is not an object. */ navigate(path: string | URL | null | undefined, state?: any): Promise; /** * Triggers the initial route resolution (for deep linking on page load). * @returns {Promise} * @throws {Error} If the router has already been started. */ start(): Promise; /** * Cleans up the router, removes event listeners, and stops the router. */ stop(): void; /** * Navigates to a specific number of steps in the browser's history. * @param {number} delta - The number of steps to move (positive for forward, negative for backward). * @returns {Promise} A promise that resolves to true if the URL changed, false otherwise. * @throws {TypeError} If delta is not an integer. */ go(delta: number): Promise; /** * Navigates to the previous entry in the browser's history stack. * @returns {Promise} A promise that resolves to true if the URL changed, false otherwise. */ back(): Promise; /** * Navigates to the next entry in the browser's history stack. * @returns {Promise} A promise that resolves to true if the URL changed, false otherwise. */ forward(): Promise; /** * Removes a specific entry from the navigation history by its index. * @param {number} index - The index of the entry to remove. * @throws {TypeError} If index is not an integer. * @throws {RangeError} If the index is out of bounds. */ removeHistoryEntry(index: number): void; /** * Clears all entries from the navigation history. */ clearHistory(): void; /** * Removes all registered routes. */ clear(): void; /** * Removes all instance data. */ clearAll(): void; /** * Destroy the instance. */ destroy(): void; } export default TinyRouter; //# sourceMappingURL=TinyRouter.d.mts.map