@ngrdt/router 0.0.97 → 0.0.99

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/package.json CHANGED
@@ -1,24 +1,24 @@
1
1
  {
2
2
  "name": "@ngrdt/router",
3
- "version": "0.0.97",
3
+ "version": "0.0.99",
4
4
  "peerDependencies": {
5
- "@angular/common": ">=20.0.0",
6
- "@angular/core": ">=20.0.0",
7
- "@angular/router": ">=20.0.0",
5
+ "@angular/common": ">=21.0.0",
6
+ "@angular/core": ">=21.0.0",
7
+ "@angular/router": ">=21.0.0",
8
8
  "rxjs": ">=7.0.0",
9
- "@ngrdt/utils": "^0.0.97",
10
- "@ngrdt/core": "^0.0.97",
11
- "@ngrdt/button": "^0.0.97"
9
+ "@ngrdt/utils": "^0.0.99",
10
+ "@ngrdt/core": "^0.0.99",
11
+ "@ngrdt/button": "^0.0.99"
12
12
  },
13
13
  "sideEffects": false,
14
14
  "module": "fesm2022/ngrdt-router.mjs",
15
- "typings": "index.d.ts",
15
+ "typings": "types/ngrdt-router.d.ts",
16
16
  "exports": {
17
17
  "./package.json": {
18
18
  "default": "./package.json"
19
19
  },
20
20
  ".": {
21
- "types": "./index.d.ts",
21
+ "types": "./types/ngrdt-router.d.ts",
22
22
  "default": "./fesm2022/ngrdt-router.mjs"
23
23
  }
24
24
  },
@@ -4,7 +4,20 @@ import * as i1 from '@angular/router';
4
4
  import { LoadChildrenCallback, Route, ResolveFn, CanActivateFn, CanDeactivateFn, CanActivateChildFn, RunGuardsAndResolvers, Data, Params, NavigationEnd, IsActiveMatchOptions, Router } from '@angular/router';
5
5
  import * as _ngrdt_router from '@ngrdt/router';
6
6
  import { Observable } from 'rxjs';
7
+ import { RdtGuardedContainer } from '@ngrdt/core';
7
8
 
9
+ /**
10
+ * Bridges an `RdtRoute` to Angular's `Route` config.
11
+ * Obtained via `RdtRoute.toAngularRoute()`. Use the fluent API to attach
12
+ * components, guards, resolvers, and providers, then call `build()` to produce
13
+ * the Angular `Route` object.
14
+ *
15
+ * `build()` validates that children defined on the `RdtRoute` match the children
16
+ * passed via `withChildren()` — a mismatch throws at startup, catching misconfiguration early.
17
+ *
18
+ * When a route has both a component and children, `build()` automatically restructures
19
+ * the output into Angular's required wrapper format (parent with empty-path child for the component).
20
+ */
8
21
  declare class RdtAngularRoute<T extends object> {
9
22
  private route;
10
23
  private children;
@@ -103,6 +116,22 @@ declare class RdtRouteBase<T extends object> {
103
116
  get children(): RdtRoute<any>[];
104
117
  }
105
118
 
119
+ /**
120
+ * Fluent builder for creating `RdtRoute` instances.
121
+ * Routes are defined once in a central file and referenced everywhere by object, eliminating string-based path typos.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * const USER_DETAIL = new RdtRouteBuilder<{ userId: number }>()
126
+ * .withName('User Detail')
127
+ * .withPath(':userId')
128
+ * .withParam('userId', 'number')
129
+ * .withCanBeEntered((route, params) => params.params.userId !== 0)
130
+ * .build();
131
+ * ```
132
+ *
133
+ * @typeParam T - Shape of the route's parameters. Enforced at compile time when navigating or creating URLs.
134
+ */
106
135
  declare class RdtRouteBuilder<T extends object = any> extends RdtRouteBase<T> {
107
136
  get canBeEntered(): RdtCanBeEnteredFn<T>;
108
137
  get orderedParams(): string[];
@@ -150,14 +179,14 @@ declare class RdtRouteBuilder<T extends object = any> extends RdtRouteBase<T> {
150
179
  setData(data: Data): this;
151
180
  /**
152
181
  * Defines parameter type and lets framework parse it.
153
- * @param paramName
154
- * @param type
182
+ * @param paramName Name of the parameter in the path.
183
+ * @param type 'string', 'number', or an array of allowed string values (enum).
155
184
  */
156
- withParam(paramName: keyof T, type: 'string' | 'number'): this;
185
+ withParam(paramName: keyof T, type: 'string' | 'number' | string[]): this;
157
186
  /**
158
187
  * @deprecated Use withParam() instead.
159
188
  */
160
- setParam(paramName: keyof T, type: 'string' | 'number'): this;
189
+ setParam(paramName: keyof T, type: 'string' | 'number' | string[]): this;
161
190
  /**
162
191
  * Sets name to display in breadcrumb, etc.
163
192
  */
@@ -177,6 +206,19 @@ declare class RdtRouteBuilder<T extends object = any> extends RdtRouteBase<T> {
177
206
  build(): RdtRoute<T>;
178
207
  }
179
208
 
209
+ /**
210
+ * Immutable, type-safe route definition. Created via `RdtRouteBuilder.build()`.
211
+ *
212
+ * Each instance represents a single route in the hierarchy and holds its path pattern,
213
+ * parameter types, parent reference, and access guard. Use `withStaticParams()`,
214
+ * `withQueryParams()`, or `withStateParams()` to create derived instances with
215
+ * pre-filled values (the original is never mutated).
216
+ *
217
+ * To bridge to Angular's router, call `toAngularRoute()` to get an `RdtAngularRoute`
218
+ * builder that produces an Angular `Route` config object.
219
+ *
220
+ * @typeParam T - Shape of this route's own parameters.
221
+ */
180
222
  declare class RdtRoute<T extends object = any> extends RdtRouteBase<T> {
181
223
  private _absoluteRegex?;
182
224
  protected _staticParams: Partial<T>;
@@ -264,23 +306,49 @@ declare class RdtRoute<T extends object = any> extends RdtRouteBase<T> {
264
306
  * this route and its parents (NOT children).
265
307
  */
266
308
  clone(): RdtRoute<T>;
309
+ private static getGroup;
267
310
  private setRegex;
268
311
  static fromBuilder<T extends object>(builder: RdtRouteBuilder<T>): RdtRoute<T>;
269
312
  private static readonly groups;
270
313
  }
271
314
 
315
+ /**
316
+ * Defines how a route parameter is matched and parsed.
317
+ * - `'string'` — matches any non-slash character (`[^/]+`), value is URI-decoded.
318
+ * - `'number'` — matches digits only (`\d+`), value is parsed to a number.
319
+ * - `string[]` — enum: matches only the listed values exactly. Invalid values are rejected at both URL creation and parsing time.
320
+ */
321
+ type ParamType = 'string' | 'number' | string[];
272
322
  type ParamTypeMap<T> = {
273
- [key in keyof T]?: 'string' | 'number';
323
+ [key in keyof T]?: ParamType;
274
324
  };
325
+ /**
326
+ * Maps between a database/model field name and the URL parameter name.
327
+ * Used with `RdtRoute.withParamMappings()` when the URL param (e.g. `:id`)
328
+ * differs from the property name on the data object (e.g. `userId`).
329
+ */
275
330
  interface ParamMapping {
331
+ /** Parameter name as it appears in the URL path (e.g. `'id'` for `:id`). */
276
332
  urlName: string;
333
+ /** Property name on the data object (e.g. `'userId'`). */
277
334
  tableName: string;
278
335
  }
279
336
  interface StaticRouteParams {
280
337
  [absolutePath: string]: object;
281
338
  }
282
339
  type RdtRedirectReturnType = string | void | undefined;
340
+ /**
341
+ * Function invoked when a route's `canBeEntered` guard returns false.
342
+ * Return a URL string to redirect to, or `undefined` to block navigation without redirect.
343
+ * Runs in injection context — you can call `inject()` inside.
344
+ */
283
345
  type RdtRedirectFn = (currentPath: string, targetPath: string, targetRoute: RdtRoute) => RdtRedirectReturnType;
346
+ /**
347
+ * Type-safe container for route parameters keyed by route instance.
348
+ * Stores params for multiple routes in a hierarchy (e.g. parent + child params from a single URL).
349
+ * Parameters are keyed internally by absolute path, so two different `RdtRoute` instances
350
+ * with the same path share the same slot.
351
+ */
284
352
  declare class RdtParameters {
285
353
  private params;
286
354
  constructor(params?: StaticRouteParams);
@@ -291,11 +359,26 @@ declare class RdtParameters {
291
359
  [Symbol.iterator](): Generator<(string | object)[], void, unknown>;
292
360
  }
293
361
  type LoadComponentCallback = Route['loadComponent'];
362
+ /**
363
+ * Synchronous guard function that determines whether a route can be entered.
364
+ * Used by `[rdtRouterLink]` and `RdtMenu` to hide/disable links, and by the
365
+ * built-in `canActivate` guard to block navigation.
366
+ * Runs in injection context when an `Injector` is available — you can call `inject()` inside.
367
+ *
368
+ * The entire parent chain is evaluated: if any ancestor's guard returns `false`, the route is blocked.
369
+ */
294
370
  type RdtCanBeEnteredFn<T extends object> = (route: RdtRoute<T>, params: RdtCombinedRouteParams<T>) => boolean;
371
+ /**
372
+ * Combined parameters available to guard functions and URL parsing results.
373
+ */
295
374
  interface RdtCombinedRouteParams<T extends object> {
375
+ /** Parameters extracted for the specific route being evaluated. */
296
376
  params: Partial<T>;
377
+ /** Parameters for all routes in the hierarchy (parent + child), keyed by route. */
297
378
  route: RdtParameters;
379
+ /** Query string parameters. */
298
380
  query: Params;
381
+ /** `history.state` parameters. */
299
382
  state: Params;
300
383
  }
301
384
 
@@ -305,20 +388,25 @@ interface RdtCombinedRouteParams<T extends object> {
305
388
  */
306
389
  declare const RDT_CANNOT_BE_ENTERED_PROVIDER: InjectionToken<RdtRedirectFn | RdtRedirectReturnType>;
307
390
 
308
- declare const RDT_CONFIRM_DATA_LOSS_SERVICE: InjectionToken<RdtConfirmDataLossService>;
309
- declare abstract class RdtConfirmDataLossService {
310
- abstract confirmDataLoss(): Observable<boolean>;
311
- }
312
- declare class RdtConfirmDataLossServiceAlert extends RdtConfirmDataLossService {
313
- confirmDataLoss(): Observable<boolean>;
314
- }
315
-
391
+ /**
392
+ * Additional options for `RdtRouterService.navigate()`.
393
+ * These take priority over any params set on the route via `withQueryParams()` / `withStateParams()`.
394
+ */
316
395
  interface RdtNavigateExtras {
396
+ /** Data passed via `history.state`. */
317
397
  state?: Params;
398
+ /** Query string parameters appended to the URL. */
318
399
  query?: Params;
400
+ /** Window target. Use `'_blank'` to open in a new tab. Defaults to `'_self'`. */
319
401
  target?: '_blank' | '_self' | '_parent' | '_top';
402
+ /** Replace the current history entry instead of pushing a new one. */
320
403
  replaceUrl?: boolean;
321
404
  }
405
+ /**
406
+ * Central navigation service for type-safe routing.
407
+ * Wraps Angular's `Router` with `RdtRoute`-based navigation, URL parsing, and history tracking.
408
+ * Requires `RDT_ROUTES_PROVIDER` to be configured with all application routes.
409
+ */
322
410
  declare class RdtRouterService {
323
411
  readonly allRoutes: RdtRoute<any>[] | null;
324
412
  readonly baseHref: string;
@@ -370,7 +458,29 @@ declare class RdtRouterService {
370
458
  static ɵprov: i0.ɵɵInjectableDeclaration<RdtRouterService>;
371
459
  }
372
460
 
461
+ /**
462
+ * Injection token holding all application routes as a flat array.
463
+ * Used internally by `RdtRouterService` to match URLs and enable type-safe navigation.
464
+ * Prefer using `provideRdtRoutes()` instead of providing this token directly.
465
+ */
373
466
  declare const RDT_ROUTES_PROVIDER: InjectionToken<RdtRoute<any>[]>;
467
+ /**
468
+ * Provides all application routes to `RdtRouterService`.
469
+ * Accepts either a route module object (`import * as routes from './routes'`)
470
+ * or a plain array of `RdtRoute` instances.
471
+ *
472
+ * @example
473
+ * ```ts
474
+ * import * as ALL_ROUTES from './rdt-routes';
475
+ *
476
+ * export const appConfig: ApplicationConfig = {
477
+ * providers: [
478
+ * provideRdtRoutes(ALL_ROUTES),
479
+ * ],
480
+ * };
481
+ * ```
482
+ */
483
+ declare function provideRdtRoutes(routes: Record<string, unknown> | RdtRoute[]): EnvironmentProviders;
374
484
 
375
485
  declare enum RdtNavigationSource {
376
486
  BREADCRUMB = "breadcrumb"
@@ -447,6 +557,15 @@ declare class RdtRouterLinkDirective<T extends object> {
447
557
  static ɵdir: i0.ɵɵDirectiveDeclaration<RdtRouterLinkDirective<any>, "[rdtRouterLink]", never, { "routeInput": { "alias": "rdtRouterLink"; "required": true; "isSignal": true; }; "target": { "alias": "target"; "required": false; "isSignal": true; }; "params": { "alias": "params"; "required": false; "isSignal": true; }; "queryParams": { "alias": "queryParams"; "required": false; "isSignal": true; }; "stateParams": { "alias": "stateParams"; "required": false; "isSignal": true; }; "disabled": { "alias": "rdtDisabled"; "required": false; "isSignal": true; }; }, {}, never, never, true, [{ directive: typeof i1.RouterLink; inputs: { "target": "target"; "replaceUrl": "replaceUrl"; }; outputs: {}; }]>;
448
558
  }
449
559
 
560
+ /**
561
+ * Ensures that `preventDataLossGuardFn` is applied to every route at runtime,
562
+ * even if it was not explicitly added in the route definition.
563
+ * Call `ensureGlobalGuards()` once during app initialization.
564
+ *
565
+ * Also patches the browser back button behavior: replaces the URL back to the
566
+ * previous value during guard checks so the address bar doesn't flash the new URL
567
+ * before the guard potentially rejects it.
568
+ */
450
569
  declare class GlobalRouteGuardService {
451
570
  private router;
452
571
  constructor(router: Router);
@@ -456,7 +575,12 @@ declare class GlobalRouteGuardService {
456
575
  static ɵprov: i0.ɵɵInjectableDeclaration<GlobalRouteGuardService>;
457
576
  }
458
577
 
459
- declare const preventDataLossGuardFn: CanDeactivateFn<any>;
578
+ /**
579
+ * `CanDeactivate` guard that blocks navigation when there are unsaved changes.
580
+ * The routed component must implement `RdtGuardedContainer` from `@ngrdt/core`.
581
+ * Add to routes via `RdtAngularRoute.addCanDeactivate(preventDataLossGuardFn)`.
582
+ */
583
+ declare const preventDataLossGuardFn: CanDeactivateFn<RdtGuardedContainer>;
460
584
 
461
- export { GlobalRouteGuardService, PERMISSION_DISABLED_KEY, RDT_CANNOT_BE_ENTERED_PROVIDER, RDT_CONFIRM_DATA_LOSS_SERVICE, RDT_ROUTES_PROVIDER, RdtAngularRoute, RdtAnyRouteActiveDirective, RdtBackLinkDirective, RdtConfirmDataLossService, RdtConfirmDataLossServiceAlert, RdtNavigationSource, RdtParameters, RdtRoute, RdtRouteBuilder, RdtRouterLinkDirective, RdtRouterService, preventDataLossGuardFn };
585
+ export { GlobalRouteGuardService, PERMISSION_DISABLED_KEY, RDT_CANNOT_BE_ENTERED_PROVIDER, RDT_ROUTES_PROVIDER, RdtAngularRoute, RdtAnyRouteActiveDirective, RdtBackLinkDirective, RdtNavigationSource, RdtParameters, RdtRoute, RdtRouteBuilder, RdtRouterLinkDirective, RdtRouterService, preventDataLossGuardFn, provideRdtRoutes };
462
586
  export type { RdtCanBeEnteredFn, RdtNavigateExtras };