@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
47 lines • 1.77 kB
JavaScript
import { resolvedRoutes, routerDefineOptions } from "./configuration";
import { getRoute } from "./navigation/get-route";
import { Routes } from "./route-registry";
/**
* Initializes the router with the specified routes and configuration
*
* The createRouter function is the main entry point for setting up the routing system.
* It processes route definitions, registers them, and triggers the initial route resolution.
*
* @param {Array<TRoute>} routes - Array of route configurations defining the application's routing structure
* @param {TRouterOptions} [options] - Optional configuration options for customizing router behavior
* @throws {Error} Throws an error if no routes are provided and no error handler is configured
*
* @example
* createRouter([
* {
* path: '/',
* element: () => document.createTextNode('Home page'),
* target: document.getElementById('content')
* },
* {
* path: '/about',
* element: () => document.createTextNode('About page')
* }
* ], {
* prefix: '/app',
* onError: (err) => console.error('Router error:', err)
* });
*/
export function createRouter(routes, options) {
if (options) {
routerDefineOptions(options);
}
if (routes.length === 0) {
if (options === null || options === void 0 ? void 0 : options.onError) {
options.onError(new Error("No routes provided", { cause: "no-routes" }));
return;
}
throw new Error("No routes provided", { cause: "no-routes" });
}
resolvedRoutes.clear();
for (const route of Routes(routes, options === null || options === void 0 ? void 0 : options.target)) {
resolvedRoutes.set(route.id, route);
}
getRoute();
}
//# sourceMappingURL=create-router.js.map