@ktjs/router 0.14.0 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { KTHTMLElement } from '@ktjs/core';
2
+
1
3
  /**
2
4
  * Guard level that determines which guards to apply during navigation
3
5
  * - there are global and route-level guards
@@ -31,9 +33,11 @@ interface RawRouteConfig {
31
33
  name?: string;
32
34
  /** Optional metadata attached to the route */
33
35
  meta?: Record<string, any>;
36
+ /** Optional component to render */
37
+ component: () => HTMLElement | Promise<HTMLElement>;
34
38
  /** Route-level guard executed before entering this route. Return false to block, string/object to redirect */
35
39
  beforeEnter?: (
36
- context: RouteContext
40
+ context: RouteContext,
37
41
  ) => string | NavBaseOptions | boolean | void | Promise<string | NavBaseOptions | boolean | void>;
38
42
  /** Route-level hook executed after navigation */
39
43
  after?: (context: RouteContext) => void | Promise<void>;
@@ -95,13 +99,15 @@ interface NavOptions extends NavBaseOptions {
95
99
  * Router configuration
96
100
  */
97
101
  interface RouterConfig {
102
+ baseUrl?: string;
103
+
98
104
  /** Array of route definitions */
99
105
  routes: RawRouteConfig[];
100
106
 
101
107
  /** Global guard executed before each navigation (except silentPush). Return false to block, string/object to redirect */
102
108
  beforeEach?: (
103
109
  to: RouteContext,
104
- from: RouteContext | null
110
+ from: RouteContext | null,
105
111
  ) => string | NavBaseOptions | boolean | void | Promise<string | NavBaseOptions | boolean | void>;
106
112
 
107
113
  /** Global hook executed after each navigation */
@@ -130,6 +136,9 @@ interface Router {
130
136
  /** Navigation history */
131
137
  history: RouteContext[];
132
138
 
139
+ /** Set the router view container */
140
+ setRouterView(view: HTMLElement): void;
141
+
133
142
  /** Navigate with guards */
134
143
  push(location: string | NavOptions): boolean | Promise<boolean>;
135
144
 
@@ -152,10 +161,17 @@ interface RouteMatch {
152
161
  result: RouteConfig[];
153
162
  }
154
163
 
164
+ /**
165
+ * Create a router view container that automatically renders route components
166
+ */
167
+ declare function KTRouter({ router }: {
168
+ router: Router;
169
+ }): KTHTMLElement;
170
+
155
171
  /**
156
172
  * Create a new router instance
157
173
  */
158
174
  declare const createRouter: (config: RouterConfig) => Router;
159
175
 
160
- export { GuardLevel, createRouter };
176
+ export { GuardLevel, KTRouter, createRouter };
161
177
  export type { NavOptions, RawRouteConfig, RouteConfig, RouteContext, RouteMatch, Router, RouterConfig };
@@ -150,6 +150,15 @@ var __ktjs_router__ = (function (exports) {
150
150
  };
151
151
  };
152
152
 
153
+ /**
154
+ * Create a router view container that automatically renders route components
155
+ */
156
+ function KTRouter({ router }) {
157
+ const view = document.createElement('kt-router-view');
158
+ router.setRouterView(view);
159
+ return view;
160
+ }
161
+
153
162
  /**
154
163
  * Create a new router instance
155
164
  */
@@ -160,20 +169,23 @@ var __ktjs_router__ = (function (exports) {
160
169
  const onNotFound = config.onNotFound ?? defaultHook;
161
170
  const onError = config.onError ?? defaultHook;
162
171
  const asyncGuards = config.asyncGuards ?? true;
172
+ const baseUrl = config.baseUrl ?? '';
163
173
  // # private values
164
174
  const routes = [];
175
+ let routerView = null;
165
176
  /**
166
177
  * Normalize routes by adding default guards
167
178
  */
168
179
  const normalize = (rawRoutes, parentPath) => rawRoutes.map((route) => {
169
180
  const path = normalizePath(parentPath, route.path);
170
181
  const normalized = {
171
- path,
182
+ path: baseUrl + path,
172
183
  name: route.name ?? '',
173
184
  meta: route.meta ?? {},
174
185
  beforeEnter: route.beforeEnter ?? defaultHook,
175
186
  after: route.after ?? defaultHook,
176
187
  children: route.children ? normalize(route.children, path) : [],
188
+ component: route.component,
177
189
  };
178
190
  // directly push the normalized route to the list
179
191
  // avoid flatten them again
@@ -338,6 +350,23 @@ var __ktjs_router__ = (function (exports) {
338
350
  // Update current route
339
351
  current = to;
340
352
  history.push(to);
353
+ // Render component if routerView exists
354
+ if (routerView && to.matched.length > 0) {
355
+ const route = to.matched[to.matched.length - 1];
356
+ if (route.component) {
357
+ const element = route.component();
358
+ if (element instanceof Promise) {
359
+ element.then((el) => {
360
+ routerView.innerHTML = '';
361
+ routerView.appendChild(el);
362
+ });
363
+ }
364
+ else {
365
+ routerView.innerHTML = '';
366
+ routerView.appendChild(element);
367
+ }
368
+ }
369
+ }
341
370
  // Execute after hooks
342
371
  executeAfterHooksSync(to, history[history.length - 2] ?? null);
343
372
  return true;
@@ -377,6 +406,15 @@ var __ktjs_router__ = (function (exports) {
377
406
  }
378
407
  current = to;
379
408
  history.push(to);
409
+ // Render component if routerView exists
410
+ if (routerView && to.matched.length > 0) {
411
+ const route = to.matched[to.matched.length - 1];
412
+ if (route.component) {
413
+ const element = await route.component();
414
+ routerView.innerHTML = '';
415
+ routerView.appendChild(element);
416
+ }
417
+ }
380
418
  executeAfterHooks(to, history[history.length - 2] ?? null);
381
419
  return true;
382
420
  }
@@ -425,6 +463,9 @@ var __ktjs_router__ = (function (exports) {
425
463
  get history() {
426
464
  return history.concat();
427
465
  },
466
+ setRouterView(view) {
467
+ routerView = view;
468
+ },
428
469
  push(location) {
429
470
  const options = normalizeLocation(location);
430
471
  return navigate(options);
@@ -446,6 +487,7 @@ var __ktjs_router__ = (function (exports) {
446
487
  };
447
488
  };
448
489
 
490
+ exports.KTRouter = KTRouter;
449
491
  exports.createRouter = createRouter;
450
492
 
451
493
  return exports;
@@ -239,19 +239,31 @@ var __ktjs_router__ = (function (exports) {
239
239
  };
240
240
  };
241
241
 
242
+ /**
243
+ * Create a router view container that automatically renders route components
244
+ */
245
+ function KTRouter(_a) {
246
+ var router = _a.router;
247
+ var view = document.createElement('kt-router-view');
248
+ router.setRouterView(view);
249
+ return view;
250
+ }
251
+
242
252
  /**
243
253
  * Create a new router instance
244
254
  */
245
255
  var createRouter = function (config) {
246
- var _a, _b, _c, _d, _e;
256
+ var _a, _b, _c, _d, _e, _f;
247
257
  // # default configs
248
258
  var beforeEach = (_a = config.beforeEach) !== null && _a !== void 0 ? _a : defaultHook;
249
259
  var afterEach = (_b = config.afterEach) !== null && _b !== void 0 ? _b : defaultHook;
250
260
  var onNotFound = (_c = config.onNotFound) !== null && _c !== void 0 ? _c : defaultHook;
251
261
  var onError = (_d = config.onError) !== null && _d !== void 0 ? _d : defaultHook;
252
262
  var asyncGuards = (_e = config.asyncGuards) !== null && _e !== void 0 ? _e : true;
263
+ var baseUrl = (_f = config.baseUrl) !== null && _f !== void 0 ? _f : '';
253
264
  // # private values
254
265
  var routes = [];
266
+ var routerView = null;
255
267
  /**
256
268
  * Normalize routes by adding default guards
257
269
  */
@@ -260,12 +272,13 @@ var __ktjs_router__ = (function (exports) {
260
272
  var _a, _b, _c, _d;
261
273
  var path = normalizePath(parentPath, route.path);
262
274
  var normalized = {
263
- path: path,
275
+ path: baseUrl + path,
264
276
  name: (_a = route.name) !== null && _a !== void 0 ? _a : '',
265
277
  meta: (_b = route.meta) !== null && _b !== void 0 ? _b : {},
266
278
  beforeEnter: (_c = route.beforeEnter) !== null && _c !== void 0 ? _c : defaultHook,
267
279
  after: (_d = route.after) !== null && _d !== void 0 ? _d : defaultHook,
268
280
  children: route.children ? normalize(route.children, path) : [],
281
+ component: route.component,
269
282
  };
270
283
  // directly push the normalized route to the list
271
284
  // avoid flatten them again
@@ -275,7 +288,7 @@ var __ktjs_router__ = (function (exports) {
275
288
  };
276
289
  // Normalize routes with default guards
277
290
  normalize(config.routes, '/');
278
- var _f = createMatcher(routes), findByName = _f.findByName, match = _f.match;
291
+ var _g = createMatcher(routes), findByName = _g.findByName, match = _g.match;
279
292
  var current = null;
280
293
  var history = [];
281
294
  // # methods
@@ -445,6 +458,23 @@ var __ktjs_router__ = (function (exports) {
445
458
  // Update current route
446
459
  current = to;
447
460
  history.push(to);
461
+ // Render component if routerView exists
462
+ if (routerView && to.matched.length > 0) {
463
+ var route = to.matched[to.matched.length - 1];
464
+ if (route.component) {
465
+ var element = route.component();
466
+ if (element instanceof Promise) {
467
+ element.then(function (el) {
468
+ routerView.innerHTML = '';
469
+ routerView.appendChild(el);
470
+ });
471
+ }
472
+ else {
473
+ routerView.innerHTML = '';
474
+ routerView.appendChild(element);
475
+ }
476
+ }
477
+ }
448
478
  // Execute after hooks
449
479
  executeAfterHooksSync(to, (_a = history[history.length - 2]) !== null && _a !== void 0 ? _a : null);
450
480
  return true;
@@ -460,13 +490,13 @@ var __ktjs_router__ = (function (exports) {
460
490
  args_1[_i - 1] = arguments[_i];
461
491
  }
462
492
  return __awaiter(void 0, __spreadArray([options_1], args_1, true), void 0, function (options, redirectCount) {
463
- var prep, guardLevel, replace, to, fullPath, guardResult, url, error_2;
493
+ var prep, guardLevel, replace, to, fullPath, guardResult, url, route, element, error_2;
464
494
  var _a;
465
495
  if (redirectCount === void 0) { redirectCount = 0; }
466
496
  return __generator(this, function (_b) {
467
497
  switch (_b.label) {
468
498
  case 0:
469
- _b.trys.push([0, 2, , 3]);
499
+ _b.trys.push([0, 4, , 5]);
470
500
  // Prevent infinite redirect loop
471
501
  if (redirectCount > 10) {
472
502
  onError(new Error('Maximum redirect count exceeded'));
@@ -496,13 +526,23 @@ var __ktjs_router__ = (function (exports) {
496
526
  }
497
527
  current = to;
498
528
  history.push(to);
529
+ if (!(routerView && to.matched.length > 0)) return [3 /*break*/, 3];
530
+ route = to.matched[to.matched.length - 1];
531
+ if (!route.component) return [3 /*break*/, 3];
532
+ return [4 /*yield*/, route.component()];
533
+ case 2:
534
+ element = _b.sent();
535
+ routerView.innerHTML = '';
536
+ routerView.appendChild(element);
537
+ _b.label = 3;
538
+ case 3:
499
539
  executeAfterHooks(to, (_a = history[history.length - 2]) !== null && _a !== void 0 ? _a : null);
500
540
  return [2 /*return*/, true];
501
- case 2:
541
+ case 4:
502
542
  error_2 = _b.sent();
503
543
  onError(error_2);
504
544
  return [2 /*return*/, false];
505
- case 3: return [2 /*return*/];
545
+ case 5: return [2 /*return*/];
506
546
  }
507
547
  });
508
548
  });
@@ -559,6 +599,9 @@ var __ktjs_router__ = (function (exports) {
559
599
  get history() {
560
600
  return history.concat();
561
601
  },
602
+ setRouterView: function (view) {
603
+ routerView = view;
604
+ },
562
605
  push: function (location) {
563
606
  var options = normalizeLocation(location);
564
607
  return navigate(options);
@@ -580,6 +623,7 @@ var __ktjs_router__ = (function (exports) {
580
623
  };
581
624
  };
582
625
 
626
+ exports.KTRouter = KTRouter;
583
627
  exports.createRouter = createRouter;
584
628
 
585
629
  return exports;
package/dist/index.mjs CHANGED
@@ -147,6 +147,15 @@ const createMatcher = (routes) => {
147
147
  };
148
148
  };
149
149
 
150
+ /**
151
+ * Create a router view container that automatically renders route components
152
+ */
153
+ function KTRouter({ router }) {
154
+ const view = document.createElement('kt-router-view');
155
+ router.setRouterView(view);
156
+ return view;
157
+ }
158
+
150
159
  /**
151
160
  * Create a new router instance
152
161
  */
@@ -157,20 +166,23 @@ const createRouter = (config) => {
157
166
  const onNotFound = config.onNotFound ?? defaultHook;
158
167
  const onError = config.onError ?? defaultHook;
159
168
  const asyncGuards = config.asyncGuards ?? true;
169
+ const baseUrl = config.baseUrl ?? '';
160
170
  // # private values
161
171
  const routes = [];
172
+ let routerView = null;
162
173
  /**
163
174
  * Normalize routes by adding default guards
164
175
  */
165
176
  const normalize = (rawRoutes, parentPath) => rawRoutes.map((route) => {
166
177
  const path = normalizePath(parentPath, route.path);
167
178
  const normalized = {
168
- path,
179
+ path: baseUrl + path,
169
180
  name: route.name ?? '',
170
181
  meta: route.meta ?? {},
171
182
  beforeEnter: route.beforeEnter ?? defaultHook,
172
183
  after: route.after ?? defaultHook,
173
184
  children: route.children ? normalize(route.children, path) : [],
185
+ component: route.component,
174
186
  };
175
187
  // directly push the normalized route to the list
176
188
  // avoid flatten them again
@@ -335,6 +347,23 @@ const createRouter = (config) => {
335
347
  // Update current route
336
348
  current = to;
337
349
  history.push(to);
350
+ // Render component if routerView exists
351
+ if (routerView && to.matched.length > 0) {
352
+ const route = to.matched[to.matched.length - 1];
353
+ if (route.component) {
354
+ const element = route.component();
355
+ if (element instanceof Promise) {
356
+ element.then((el) => {
357
+ routerView.innerHTML = '';
358
+ routerView.appendChild(el);
359
+ });
360
+ }
361
+ else {
362
+ routerView.innerHTML = '';
363
+ routerView.appendChild(element);
364
+ }
365
+ }
366
+ }
338
367
  // Execute after hooks
339
368
  executeAfterHooksSync(to, history[history.length - 2] ?? null);
340
369
  return true;
@@ -374,6 +403,15 @@ const createRouter = (config) => {
374
403
  }
375
404
  current = to;
376
405
  history.push(to);
406
+ // Render component if routerView exists
407
+ if (routerView && to.matched.length > 0) {
408
+ const route = to.matched[to.matched.length - 1];
409
+ if (route.component) {
410
+ const element = await route.component();
411
+ routerView.innerHTML = '';
412
+ routerView.appendChild(element);
413
+ }
414
+ }
377
415
  executeAfterHooks(to, history[history.length - 2] ?? null);
378
416
  return true;
379
417
  }
@@ -422,6 +460,9 @@ const createRouter = (config) => {
422
460
  get history() {
423
461
  return history.concat();
424
462
  },
463
+ setRouterView(view) {
464
+ routerView = view;
465
+ },
425
466
  push(location) {
426
467
  const options = normalizeLocation(location);
427
468
  return navigate(options);
@@ -443,4 +484,4 @@ const createRouter = (config) => {
443
484
  };
444
485
  };
445
486
 
446
- export { createRouter };
487
+ export { KTRouter, createRouter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/router",
3
- "version": "0.14.0",
3
+ "version": "0.14.2",
4
4
  "description": "Router for kt.js - client-side routing with navigation guards",
5
5
  "type": "module",
6
6
  "module": "./dist/index.mjs",
@@ -31,7 +31,7 @@
31
31
  "directory": "packages/router"
32
32
  },
33
33
  "dependencies": {
34
- "@ktjs/core": "0.13.0"
34
+ "@ktjs/core": "0.14.6"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "rollup -c rollup.config.mjs",