ui5-lib-guard-router 1.0.1

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.
Files changed (33) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +382 -0
  3. package/dist/index.d.ts +27 -0
  4. package/dist/resources/ui5/guard/router/.library +17 -0
  5. package/dist/resources/ui5/guard/router/Router-dbg.js +431 -0
  6. package/dist/resources/ui5/guard/router/Router-dbg.js.map +1 -0
  7. package/dist/resources/ui5/guard/router/Router.d.ts +28 -0
  8. package/dist/resources/ui5/guard/router/Router.d.ts.map +1 -0
  9. package/dist/resources/ui5/guard/router/Router.js +2 -0
  10. package/dist/resources/ui5/guard/router/Router.js.map +1 -0
  11. package/dist/resources/ui5/guard/router/library-dbg.js +17 -0
  12. package/dist/resources/ui5/guard/router/library-dbg.js.map +1 -0
  13. package/dist/resources/ui5/guard/router/library-preload.js +10 -0
  14. package/dist/resources/ui5/guard/router/library-preload.js.map +1 -0
  15. package/dist/resources/ui5/guard/router/library.d.ts +7 -0
  16. package/dist/resources/ui5/guard/router/library.d.ts.map +1 -0
  17. package/dist/resources/ui5/guard/router/library.js +2 -0
  18. package/dist/resources/ui5/guard/router/library.js.map +1 -0
  19. package/dist/resources/ui5/guard/router/manifest.json +33 -0
  20. package/dist/resources/ui5/guard/router/types-dbg.js +2 -0
  21. package/dist/resources/ui5/guard/router/types-dbg.js.map +1 -0
  22. package/dist/resources/ui5/guard/router/types.d.ts +118 -0
  23. package/dist/resources/ui5/guard/router/types.d.ts.map +1 -0
  24. package/dist/resources/ui5/guard/router/types.js +2 -0
  25. package/dist/resources/ui5/guard/router/types.js.map +1 -0
  26. package/package.json +52 -0
  27. package/src/.library +17 -0
  28. package/src/Router.ts +540 -0
  29. package/src/library.ts +17 -0
  30. package/src/manifest.json +33 -0
  31. package/src/types.ts +136 -0
  32. package/tsconfig.json +13 -0
  33. package/ui5.yaml +24 -0
package/src/types.ts ADDED
@@ -0,0 +1,136 @@
1
+ import type MobileRouter from "sap/m/routing/Router";
2
+ import type { ComponentTargetParameters, RouteInfo } from "sap/ui/core/routing/Router";
3
+
4
+ /**
5
+ * Redirect target with route name and optional parameters.
6
+ */
7
+ export interface GuardRedirect {
8
+ /** Route name to redirect to */
9
+ route: string;
10
+ /** Optional route parameters */
11
+ parameters?: ComponentTargetParameters["parameters"];
12
+ /** Optional component target info for nested component routing */
13
+ componentTargetInfo?: Record<string, ComponentTargetParameters>;
14
+ }
15
+
16
+ /**
17
+ * Result of a guard check.
18
+ *
19
+ * Only strict `true` allows navigation. All other values (including truthy
20
+ * non-boolean values) block or redirect. This avoids accidental allow from
21
+ * falsy/truthy coercion.
22
+ *
23
+ * - `true` → allow navigation to proceed
24
+ * - `false` → block navigation (stay on current route, no history entry)
25
+ * - `string` → redirect to this route name (replaceHash, no history entry)
26
+ * - `GuardRedirect` → redirect with route name, parameters, and optional component target info
27
+ */
28
+ export type GuardResult = boolean | string | GuardRedirect;
29
+
30
+ /**
31
+ * Context passed to guard functions.
32
+ */
33
+ export interface GuardContext {
34
+ /** Target route name (empty string if no route matched) */
35
+ toRoute: string;
36
+ /** Raw hash being navigated to */
37
+ toHash: string;
38
+ /** Parsed route parameters */
39
+ toArguments: RouteInfo["arguments"];
40
+ /** Current route name (empty string on initial navigation) */
41
+ fromRoute: string;
42
+ /** Current hash */
43
+ fromHash: string;
44
+ /**
45
+ * Abort signal for this navigation. Aborted when a newer navigation
46
+ * supersedes this one or when the router is destroyed.
47
+ * Pass to `fetch()` or other cancellable APIs to avoid wasted work.
48
+ */
49
+ signal: AbortSignal;
50
+ }
51
+
52
+ /**
53
+ * A guard function - can be synchronous or asynchronous.
54
+ */
55
+ export type GuardFn = (context: GuardContext) => GuardResult | Promise<GuardResult>;
56
+
57
+ /**
58
+ * A leave guard function - can be synchronous or asynchronous.
59
+ *
60
+ * Leave guards answer the question "can I leave this route?" and return
61
+ * only a boolean. They cannot redirect — use enter guards for that.
62
+ */
63
+ export type LeaveGuardFn = (context: GuardContext) => boolean | Promise<boolean>;
64
+
65
+ /**
66
+ * Configuration object for registering enter and/or leave guards on a route.
67
+ *
68
+ * When passed to `addRouteGuard`, the object form allows registering both
69
+ * guard types in a single call. If neither `beforeEnter` nor `beforeLeave`
70
+ * is provided, an info message is logged and no guards are registered.
71
+ */
72
+ export interface RouteGuardConfig {
73
+ /** Guard that runs before entering this route */
74
+ beforeEnter?: GuardFn;
75
+ /** Guard that runs before leaving this route */
76
+ beforeLeave?: LeaveGuardFn;
77
+ }
78
+
79
+ /**
80
+ * Public instance shape of the extended Router.
81
+ *
82
+ * Extends `sap.m.routing.Router` with guard management methods.
83
+ * Use this type when casting `getRouter()` in application code.
84
+ */
85
+ export interface GuardRouter extends MobileRouter {
86
+ addGuard(guard: GuardFn): GuardRouter;
87
+ removeGuard(guard: GuardFn): GuardRouter;
88
+ addRouteGuard(routeName: string, guard: GuardFn | RouteGuardConfig): GuardRouter;
89
+ removeRouteGuard(routeName: string, guard: GuardFn | RouteGuardConfig): GuardRouter;
90
+ addLeaveGuard(routeName: string, guard: LeaveGuardFn): GuardRouter;
91
+ removeLeaveGuard(routeName: string, guard: LeaveGuardFn): GuardRouter;
92
+ }
93
+
94
+ /**
95
+ * Full internal instance shape including private state and methods.
96
+ *
97
+ * Used as the `this` type in Router method bodies to provide autocomplete
98
+ * and catch property-name typos. Not intended for external consumption.
99
+ *
100
+ * @internal
101
+ */
102
+ export interface RouterInternal extends GuardRouter {
103
+ _globalGuards: GuardFn[];
104
+ _enterGuards: Map<string, GuardFn[]>;
105
+ _leaveGuards: Map<string, LeaveGuardFn[]>;
106
+ _currentRoute: string;
107
+ _currentHash: string | null;
108
+ _pendingHash: string | null;
109
+ _redirecting: boolean;
110
+ _parseGeneration: number;
111
+ _suppressNextParse: boolean;
112
+ _abortController: AbortController | null;
113
+
114
+ _commitNavigation(hash: string, route?: string): void;
115
+ _runLeaveGuards(context: GuardContext): boolean | Promise<boolean>;
116
+ _runEnterGuards(
117
+ globalGuards: GuardFn[],
118
+ toRoute: string,
119
+ context: GuardContext,
120
+ ): GuardResult | Promise<GuardResult>;
121
+ _runRouteGuards(toRoute: string, context: GuardContext): GuardResult | Promise<GuardResult>;
122
+ _runGuards(guards: GuardFn[], context: GuardContext): GuardResult | Promise<GuardResult>;
123
+ _continueGuardsAsync(
124
+ pendingResult: Promise<GuardResult>,
125
+ guards: Array<(context: GuardContext) => GuardResult | Promise<GuardResult>>,
126
+ currentIndex: number,
127
+ context: GuardContext,
128
+ onBlock: (result: GuardResult) => GuardResult,
129
+ label: string,
130
+ isLeaveGuard: boolean,
131
+ ): Promise<GuardResult>;
132
+ _validateGuardResult(result: GuardResult): GuardResult;
133
+ _redirect(target: string | GuardRedirect): void;
134
+ _blockNavigation(): void;
135
+ _restoreHash(): void;
136
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": ".",
5
+ "baseUrl": ".",
6
+ "paths": {
7
+ "ui5/guard/router/*": ["./src/*"]
8
+ },
9
+ "types": ["@openui5/types", "@types/qunit"]
10
+ },
11
+ "include": ["src/**/*.ts", "test/**/*.ts"],
12
+ "exclude": ["test/**/wdio*.conf.ts"]
13
+ }
package/ui5.yaml ADDED
@@ -0,0 +1,24 @@
1
+ specVersion: "4.0"
2
+ metadata:
3
+ name: ui5.guard.router
4
+ type: library
5
+ framework:
6
+ name: OpenUI5
7
+ version: "1.144.0"
8
+ libraries:
9
+ - name: sap.m
10
+ - name: sap.ui.core
11
+ builder:
12
+ resources:
13
+ excludes:
14
+ - "/test-resources/**"
15
+ - "/resources/**/themes/**"
16
+ customTasks:
17
+ - name: ui5-tooling-transpile-task
18
+ afterTask: replaceVersion
19
+ configuration:
20
+ omitTSFromBuildResult: true
21
+ server:
22
+ customMiddleware:
23
+ - name: ui5-tooling-transpile-middleware
24
+ afterMiddleware: compression