@esmx/router 3.0.0-rc.17 → 3.0.0-rc.19

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 (155) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +70 -0
  3. package/README.zh-CN.md +70 -0
  4. package/dist/error.d.ts +23 -0
  5. package/dist/error.mjs +61 -0
  6. package/dist/increment-id.d.ts +7 -0
  7. package/dist/increment-id.mjs +11 -0
  8. package/dist/index.d.ts +5 -3
  9. package/dist/index.mjs +14 -3
  10. package/dist/index.test.mjs +8 -0
  11. package/dist/location.d.ts +15 -0
  12. package/dist/location.mjs +53 -0
  13. package/dist/location.test.d.ts +8 -0
  14. package/dist/location.test.mjs +370 -0
  15. package/dist/matcher.d.ts +3 -0
  16. package/dist/matcher.mjs +44 -0
  17. package/dist/matcher.test.mjs +1492 -0
  18. package/dist/micro-app.d.ts +18 -0
  19. package/dist/micro-app.dom.test.d.ts +1 -0
  20. package/dist/micro-app.dom.test.mjs +532 -0
  21. package/dist/micro-app.mjs +80 -0
  22. package/dist/navigation.d.ts +43 -0
  23. package/dist/navigation.mjs +143 -0
  24. package/dist/navigation.test.d.ts +1 -0
  25. package/dist/navigation.test.mjs +681 -0
  26. package/dist/options.d.ts +4 -0
  27. package/dist/options.mjs +88 -0
  28. package/dist/route-task.d.ts +40 -0
  29. package/dist/route-task.mjs +75 -0
  30. package/dist/route-task.test.d.ts +1 -0
  31. package/dist/route-task.test.mjs +673 -0
  32. package/dist/route-transition.d.ts +53 -0
  33. package/dist/route-transition.mjs +307 -0
  34. package/dist/route-transition.test.d.ts +1 -0
  35. package/dist/route-transition.test.mjs +146 -0
  36. package/dist/route.d.ts +72 -0
  37. package/dist/route.mjs +194 -0
  38. package/dist/route.test.d.ts +1 -0
  39. package/dist/route.test.mjs +1664 -0
  40. package/dist/router-back.test.d.ts +1 -0
  41. package/dist/router-back.test.mjs +361 -0
  42. package/dist/router-forward.test.d.ts +1 -0
  43. package/dist/router-forward.test.mjs +376 -0
  44. package/dist/router-go.test.d.ts +1 -0
  45. package/dist/router-go.test.mjs +73 -0
  46. package/dist/router-guards-cleanup.test.d.ts +1 -0
  47. package/dist/router-guards-cleanup.test.mjs +437 -0
  48. package/dist/router-link.d.ts +10 -0
  49. package/dist/router-link.mjs +126 -0
  50. package/dist/router-push.test.d.ts +1 -0
  51. package/dist/router-push.test.mjs +115 -0
  52. package/dist/router-replace.test.d.ts +1 -0
  53. package/dist/router-replace.test.mjs +114 -0
  54. package/dist/router-resolve.test.d.ts +1 -0
  55. package/dist/router-resolve.test.mjs +393 -0
  56. package/dist/router-restart-app.dom.test.d.ts +1 -0
  57. package/dist/router-restart-app.dom.test.mjs +616 -0
  58. package/dist/router-window-navigation.test.d.ts +1 -0
  59. package/dist/router-window-navigation.test.mjs +359 -0
  60. package/dist/router.d.ts +109 -102
  61. package/dist/router.mjs +260 -361
  62. package/dist/types.d.ts +246 -0
  63. package/dist/types.mjs +18 -0
  64. package/dist/util.d.ts +26 -0
  65. package/dist/util.mjs +53 -0
  66. package/dist/util.test.d.ts +1 -0
  67. package/dist/util.test.mjs +1020 -0
  68. package/package.json +10 -13
  69. package/src/error.ts +84 -0
  70. package/src/increment-id.ts +12 -0
  71. package/src/index.test.ts +9 -0
  72. package/src/index.ts +54 -3
  73. package/src/location.test.ts +406 -0
  74. package/src/location.ts +96 -0
  75. package/src/matcher.test.ts +1685 -0
  76. package/src/matcher.ts +59 -0
  77. package/src/micro-app.dom.test.ts +708 -0
  78. package/src/micro-app.ts +101 -0
  79. package/src/navigation.test.ts +858 -0
  80. package/src/navigation.ts +195 -0
  81. package/src/options.ts +131 -0
  82. package/src/route-task.test.ts +901 -0
  83. package/src/route-task.ts +105 -0
  84. package/src/route-transition.test.ts +178 -0
  85. package/src/route-transition.ts +425 -0
  86. package/src/route.test.ts +2014 -0
  87. package/src/route.ts +308 -0
  88. package/src/router-back.test.ts +487 -0
  89. package/src/router-forward.test.ts +506 -0
  90. package/src/router-go.test.ts +91 -0
  91. package/src/router-guards-cleanup.test.ts +595 -0
  92. package/src/router-link.ts +235 -0
  93. package/src/router-push.test.ts +140 -0
  94. package/src/router-replace.test.ts +139 -0
  95. package/src/router-resolve.test.ts +475 -0
  96. package/src/router-restart-app.dom.test.ts +783 -0
  97. package/src/router-window-navigation.test.ts +457 -0
  98. package/src/router.ts +289 -470
  99. package/src/types.ts +341 -0
  100. package/src/util.test.ts +1262 -0
  101. package/src/util.ts +116 -0
  102. package/dist/history/abstract.d.ts +0 -29
  103. package/dist/history/abstract.mjs +0 -107
  104. package/dist/history/base.d.ts +0 -79
  105. package/dist/history/base.mjs +0 -275
  106. package/dist/history/html.d.ts +0 -22
  107. package/dist/history/html.mjs +0 -183
  108. package/dist/history/index.d.ts +0 -7
  109. package/dist/history/index.mjs +0 -16
  110. package/dist/matcher/create-matcher.d.ts +0 -5
  111. package/dist/matcher/create-matcher.mjs +0 -218
  112. package/dist/matcher/create-matcher.spec.mjs +0 -0
  113. package/dist/matcher/index.d.ts +0 -1
  114. package/dist/matcher/index.mjs +0 -1
  115. package/dist/task-pipe/index.d.ts +0 -1
  116. package/dist/task-pipe/index.mjs +0 -1
  117. package/dist/task-pipe/task.d.ts +0 -30
  118. package/dist/task-pipe/task.mjs +0 -66
  119. package/dist/utils/bom.d.ts +0 -5
  120. package/dist/utils/bom.mjs +0 -10
  121. package/dist/utils/encoding.d.ts +0 -48
  122. package/dist/utils/encoding.mjs +0 -44
  123. package/dist/utils/guards.d.ts +0 -9
  124. package/dist/utils/guards.mjs +0 -12
  125. package/dist/utils/index.d.ts +0 -7
  126. package/dist/utils/index.mjs +0 -27
  127. package/dist/utils/path.d.ts +0 -60
  128. package/dist/utils/path.mjs +0 -281
  129. package/dist/utils/path.spec.mjs +0 -27
  130. package/dist/utils/scroll.d.ts +0 -25
  131. package/dist/utils/scroll.mjs +0 -59
  132. package/dist/utils/utils.d.ts +0 -16
  133. package/dist/utils/utils.mjs +0 -11
  134. package/dist/utils/warn.d.ts +0 -2
  135. package/dist/utils/warn.mjs +0 -12
  136. package/src/history/abstract.ts +0 -149
  137. package/src/history/base.ts +0 -408
  138. package/src/history/html.ts +0 -228
  139. package/src/history/index.ts +0 -20
  140. package/src/matcher/create-matcher.spec.ts +0 -3
  141. package/src/matcher/create-matcher.ts +0 -293
  142. package/src/matcher/index.ts +0 -1
  143. package/src/task-pipe/index.ts +0 -1
  144. package/src/task-pipe/task.ts +0 -97
  145. package/src/utils/bom.ts +0 -14
  146. package/src/utils/encoding.ts +0 -153
  147. package/src/utils/guards.ts +0 -25
  148. package/src/utils/index.ts +0 -27
  149. package/src/utils/path.spec.ts +0 -32
  150. package/src/utils/path.ts +0 -417
  151. package/src/utils/scroll.ts +0 -120
  152. package/src/utils/utils.ts +0 -30
  153. package/src/utils/warn.ts +0 -13
  154. /package/dist/{matcher/create-matcher.spec.d.ts → index.test.d.ts} +0 -0
  155. /package/dist/{utils/path.spec.d.ts → matcher.test.d.ts} +0 -0
@@ -0,0 +1,88 @@
1
+ import { createMatcher } from "./matcher.mjs";
2
+ import { RouterMode } from "./types.mjs";
3
+ import { isBrowser } from "./util.mjs";
4
+ function getBaseUrl(options) {
5
+ let sourceUrl;
6
+ if (options.base) {
7
+ sourceUrl = options.base;
8
+ } else if (isBrowser) {
9
+ sourceUrl = location.origin;
10
+ } else if (options.req) {
11
+ const { req } = options;
12
+ const protocol = req.headers["x-forwarded-proto"] || req.headers["x-forwarded-protocol"] || (req.socket && "encrypted" in req.socket && req.socket.encrypted ? "https" : "http");
13
+ const host = req.headers["x-forwarded-host"] || req.headers.host || req.headers["x-real-ip"] || "localhost";
14
+ const port = req.headers["x-forwarded-port"];
15
+ const path = req.url || "";
16
+ sourceUrl = `${protocol}://${host}${port ? `:${port}` : ""}${path}`;
17
+ } else {
18
+ sourceUrl = "https://www.esmnext.com/";
19
+ }
20
+ let base;
21
+ try {
22
+ base = new URL(".", sourceUrl);
23
+ } catch (e) {
24
+ console.warn(
25
+ `Failed to parse base URL '${sourceUrl}', using default: https://www.esmnext.com/`
26
+ );
27
+ base = new URL("https://www.esmnext.com/");
28
+ }
29
+ base.search = base.hash = "";
30
+ return base;
31
+ }
32
+ export function parsedOptions(options = {}) {
33
+ const base = getBaseUrl(options);
34
+ const routes = Array.from(options.routes ?? []);
35
+ return Object.freeze({
36
+ rootStyle: options.rootStyle || false,
37
+ root: options.root || "",
38
+ context: options.context || {},
39
+ req: options.req || null,
40
+ res: options.res || null,
41
+ layer: options.layer || false,
42
+ zIndex: options.zIndex || 1e4,
43
+ base,
44
+ mode: isBrowser ? options.mode ?? RouterMode.history : RouterMode.memory,
45
+ routes,
46
+ apps: typeof options.apps === "function" ? options.apps : Object.assign({}, options.apps),
47
+ matcher: createMatcher(routes),
48
+ normalizeURL: options.normalizeURL ?? ((url) => url),
49
+ fallback: options.fallback ?? fallback,
50
+ handleBackBoundary: options.handleBackBoundary ?? (() => {
51
+ }),
52
+ handleLayerClose: options.handleLayerClose ?? (() => {
53
+ })
54
+ });
55
+ }
56
+ export function fallback(to, from, router) {
57
+ const href = to.url.href;
58
+ if (!isBrowser && (router == null ? void 0 : router.res)) {
59
+ let statusCode = 302;
60
+ const validRedirectCodes = [300, 301, 302, 303, 304, 307, 308];
61
+ if (to.statusCode && validRedirectCodes.includes(to.statusCode)) {
62
+ statusCode = to.statusCode;
63
+ } else if (to.statusCode) {
64
+ console.warn(
65
+ `Invalid redirect status code ${to.statusCode}, using default 302`
66
+ );
67
+ }
68
+ router.res.statusCode = statusCode;
69
+ router.res.setHeader("Location", href);
70
+ router.res.end();
71
+ return;
72
+ }
73
+ if (isBrowser) {
74
+ if (to.isPush) {
75
+ try {
76
+ const newWindow = window.open(href);
77
+ if (!newWindow) {
78
+ location.href = href;
79
+ } else {
80
+ newWindow.opener = null;
81
+ }
82
+ return newWindow;
83
+ } catch {
84
+ }
85
+ }
86
+ location.href = href;
87
+ }
88
+ }
@@ -0,0 +1,40 @@
1
+ import { Route } from './route';
2
+ import type { Router } from './router';
3
+ import type { RouteConfirmHook } from './types';
4
+ /**
5
+ * Controls the execution and cancellation of a route task.
6
+ */
7
+ export declare class RouteTaskController {
8
+ private _aborted;
9
+ /**
10
+ * Aborts the current task.
11
+ */
12
+ abort(): void;
13
+ shouldCancel(name: string): boolean;
14
+ }
15
+ export interface RouteTaskOptions {
16
+ to: Route;
17
+ from: Route | null;
18
+ tasks: RouteTask[];
19
+ router: Router;
20
+ /**
21
+ * Optional route task controller.
22
+ * Used to control the execution and cancellation of the task.
23
+ */
24
+ controller?: RouteTaskController;
25
+ }
26
+ export declare function createRouteTask(opts: RouteTaskOptions): Promise<Route>;
27
+ export declare enum RouteTaskType {
28
+ fallback = "fallback",
29
+ override = "override",
30
+ asyncComponent = "asyncComponent",
31
+ beforeEach = "beforeEach",
32
+ beforeEnter = "beforeEnter",
33
+ beforeUpdate = "beforeUpdate",
34
+ beforeLeave = "beforeLeave",
35
+ confirm = "confirm"
36
+ }
37
+ export interface RouteTask {
38
+ name: string;
39
+ task: RouteConfirmHook;
40
+ }
@@ -0,0 +1,75 @@
1
+ import {
2
+ RouteNavigationAbortedError,
3
+ RouteSelfRedirectionError,
4
+ RouteTaskCancelledError,
5
+ RouteTaskExecutionError
6
+ } from "./error.mjs";
7
+ import { Route } from "./route.mjs";
8
+ import { isUrlEqual, isValidConfirmHookResult } from "./util.mjs";
9
+ export class RouteTaskController {
10
+ _aborted = false;
11
+ /**
12
+ * Aborts the current task.
13
+ */
14
+ abort() {
15
+ this._aborted = true;
16
+ }
17
+ shouldCancel(name) {
18
+ if (this._aborted) {
19
+ return true;
20
+ }
21
+ return false;
22
+ }
23
+ }
24
+ export async function createRouteTask(opts) {
25
+ const { to, from, tasks, controller, router } = opts;
26
+ for (const task of tasks) {
27
+ if (controller == null ? void 0 : controller.shouldCancel(task.name)) {
28
+ throw new RouteTaskCancelledError(task.name, to, from);
29
+ }
30
+ let result = null;
31
+ try {
32
+ result = await task.task(to, from, router);
33
+ } catch (e) {
34
+ throw new RouteTaskExecutionError(task.name, to, from, e);
35
+ }
36
+ if (controller == null ? void 0 : controller.shouldCancel(task.name)) {
37
+ throw new RouteTaskCancelledError(task.name, to, from);
38
+ }
39
+ if (!isValidConfirmHookResult(result)) continue;
40
+ if (typeof result === "function") {
41
+ to.handle = result;
42
+ return to;
43
+ }
44
+ if (result === false) {
45
+ throw new RouteNavigationAbortedError(task.name, to, from);
46
+ }
47
+ const nextTo = new Route({
48
+ options: router.parsedOptions,
49
+ toType: to.type,
50
+ toInput: result,
51
+ from: to.url
52
+ });
53
+ if (isUrlEqual(nextTo.url, to.url)) {
54
+ throw new RouteSelfRedirectionError(to.fullPath, to, from);
55
+ }
56
+ return createRouteTask({
57
+ ...opts,
58
+ to: nextTo,
59
+ from: to,
60
+ controller
61
+ });
62
+ }
63
+ return to;
64
+ }
65
+ export var RouteTaskType = /* @__PURE__ */ ((RouteTaskType2) => {
66
+ RouteTaskType2["fallback"] = "fallback";
67
+ RouteTaskType2["override"] = "override";
68
+ RouteTaskType2["asyncComponent"] = "asyncComponent";
69
+ RouteTaskType2["beforeEach"] = "beforeEach";
70
+ RouteTaskType2["beforeEnter"] = "beforeEnter";
71
+ RouteTaskType2["beforeUpdate"] = "beforeUpdate";
72
+ RouteTaskType2["beforeLeave"] = "beforeLeave";
73
+ RouteTaskType2["confirm"] = "confirm";
74
+ return RouteTaskType2;
75
+ })(RouteTaskType || {});
@@ -0,0 +1 @@
1
+ export {};