@viewfly/router 3.0.0-alpha.8 → 3.0.0

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/link.d.ts CHANGED
@@ -5,7 +5,7 @@ export interface LinkProps extends Props {
5
5
  active?: string;
6
6
  exact?: boolean;
7
7
  queryParams?: QueryParams;
8
- fragment?: string;
8
+ hash?: string;
9
9
  tag?: string;
10
10
  [key: string]: any;
11
11
  }
@@ -1,3 +1,4 @@
1
1
  export * from './navigator';
2
2
  export * from './router';
3
+ export * from './routes';
3
4
  export * from './url-parser';
@@ -10,24 +10,28 @@ export declare abstract class Navigator {
10
10
  protected constructor(baseUrl: string);
11
11
  abstract onUrlChanged: Observable<void>;
12
12
  abstract get pathname(): string;
13
- abstract to(pathName: string, relative: Router, queryParams?: QueryParams, fragment?: string): boolean;
14
- abstract replace(pathName: string, relative: Router, queryParams?: QueryParams, fragment?: string): boolean;
15
- abstract join(pathName: string, relative: Router, queryParams?: QueryParams, fragment?: string): string;
13
+ abstract to(pathName: string, relative: Router, queryParams?: QueryParams, hash?: string | null): boolean;
14
+ abstract replace(pathName: string, relative: Router, queryParams?: QueryParams, hash?: string | null): boolean;
15
+ abstract join(pathName: string, relative: Router, queryParams?: QueryParams, hash?: string | null): string;
16
16
  abstract back(): void;
17
17
  abstract forward(): void;
18
18
  abstract go(offset: number): void;
19
+ /** 当前一次由 Router 触发的地址变更已被对应页面接受 */
20
+ abstract confirmNavigation(): void;
21
+ /** 当前一次由 Router 触发的地址变更被页面守卫拒绝,需要回滚地址栏 */
22
+ abstract cancelNavigation(): void;
19
23
  abstract destroy(): void;
20
24
  }
21
25
  export interface UrlFormatParams {
22
26
  queryParams?: QueryParams;
23
- fragment?: string;
27
+ hash?: string | null;
24
28
  }
25
29
  export declare function formatUrl(pathname: string, urlFormatParams: UrlFormatParams): string;
26
30
  export declare function formatQueryParams(queryParams: QueryParams): string;
27
31
  export interface NavigatorParams {
28
32
  pathname: string;
29
33
  queryParams: QueryParams;
30
- fragment: string | null;
34
+ hash: string | null;
31
35
  }
32
36
  export interface NavigatorHooks {
33
37
  beforeEach?(currentParams: NavigatorParams, nextParams: NavigatorParams, next: () => void): void;
@@ -36,6 +40,7 @@ export interface NavigatorHooks {
36
40
  export declare class BrowserNavigator extends Navigator {
37
41
  private hooks;
38
42
  onUrlChanged: Observable<void>;
43
+ private pendingNavigation;
39
44
  /** 挂载在 location 上的路径前缀;'' 或 '/' 表示站点根,不做剥离 */
40
45
  private get basePathPrefix();
41
46
  get pathname(): string;
@@ -44,13 +49,22 @@ export declare class BrowserNavigator extends Navigator {
44
49
  private urlChangeEvent;
45
50
  private subscription;
46
51
  constructor(baseUrl: string, hooks?: NavigatorHooks);
47
- to(pathName: string, relative: Router, queryParams?: QueryParams, fragment?: string): boolean;
48
- replace(pathName: string, relative: Router, queryParams?: QueryParams, fragment?: string): boolean;
49
- join(pathname: string, relative: Router, queryParams?: QueryParams, fragment?: string): string;
52
+ to(pathName: string, relative: Router, queryParams?: QueryParams, hash?: string | null): boolean;
53
+ replace(pathName: string, relative: Router, queryParams?: QueryParams, hash?: string | null): boolean;
54
+ join(pathname: string, relative: Router, queryParams?: QueryParams, hash?: string | null): string;
50
55
  back(): void;
51
56
  forward(): void;
52
57
  go(offset: number): void;
58
+ confirmNavigation(): void;
59
+ cancelNavigation(): void;
53
60
  destroy(): void;
54
61
  private runHooks;
55
- private getUrlTree;
62
+ private stripBaseFromLocationPathname;
63
+ /**
64
+ * 按与 `history.pushState` / `replaceState` 写入会话的 URL 解析 urlTree(与传入 `join` 的相对路径一致)。
65
+ * History 更新后若仍仅从 `Location` 读路径,可能与实际会话 URL 不一致(如 Location 与 document 的同步时序、部分宿主环境)。
66
+ */
67
+ private readUrlTreeFromHistoryHref;
68
+ private readUrlTreeFromLocation;
69
+ private getCurrentUrl;
56
70
  }
@@ -0,0 +1,3 @@
1
+ /** 查询串组件:写入 URL 时 encode,从 URL 解析出对象时 decode(与常见路由约定一致)。 */
2
+ export declare function encodeQueryParamComponent(value: string): string;
3
+ export declare function decodeQueryParamComponent(value: string): string;
@@ -1,28 +1,44 @@
1
1
  import { Observable } from '@tanbo/stream';
2
- import { ComponentSetup } from '@viewfly/core';
3
- import { Navigator, NavigatorParams, QueryParams } from './navigator';
4
- export interface RouteConfig {
5
- path: string;
6
- component?: ComponentSetup;
7
- asyncComponent?: () => Promise<ComponentSetup>;
8
- beforeEach?(): boolean | Promise<boolean>;
9
- afterEach?(): void;
10
- redirectTo?: string | ((path: string) => string | NavigatorParams);
11
- }
2
+ import { Navigator, QueryParams } from './navigator';
3
+ import { Params, Route } from './routes';
12
4
  export declare class Router {
13
5
  private navigator;
14
6
  parent: Router | null;
15
7
  onRefresh: Observable<void>;
8
+ private lastResolvedParams;
9
+ /** 当前重定向链上已解析过的 path 段(规范化后);非重定向成功匹配后清空 */
10
+ private redirectTrail;
11
+ private static readonly maxRedirectHops;
12
+ /** 当前层最近一次匹配所消费的 URL 段数(默认 1,保持历史行为) */
13
+ private consumedSegments;
14
+ private routeParams;
15
+ /**
16
+ * 本次 `resolve` 命中的 `route.path` 上的动态段(供子 Router、`canActivate`、`redirectTo`)。
17
+ * 与 `params` 按层隔离(对齐 Angular):有 `parent` 时 `params` 只表示本层注入域,不由子级匹配覆盖。
18
+ */
19
+ lastResolvePathParams: Params;
16
20
  get deep(): number;
21
+ private get remainingPaths();
17
22
  get path(): string;
23
+ get params(): Params;
24
+ setParams(params: Params): void;
18
25
  private refreshEvent;
19
26
  constructor(navigator: Navigator, parent: Router | null);
20
- navigateTo(path: string, params?: QueryParams, fragment?: string | null): void;
21
- replaceTo(path: string, params?: QueryParams): void;
27
+ navigateTo(path: string, queryParams?: QueryParams, hash?: string | null): void;
28
+ replaceTo(path: string, queryParams?: QueryParams, hash?: string | null): void;
22
29
  refresh(): void;
23
- consumeConfig(routes: RouteConfig[]): RouteConfig | null;
30
+ resolve(routes: Route[]): Route | null;
24
31
  back(): void;
25
32
  forward(): void;
26
33
  go(offset: number): void;
34
+ private clearRedirectTrail;
35
+ private getNavigatorParams;
36
+ private cloneNavigatorParams;
37
+ private beginRedirectResolution;
38
+ private assertRedirectTarget;
39
+ private splitRoutePath;
40
+ /** 单段路由:`user` | `:id` | `:id?`(`?` 仅表示可选;整条 path 里 `:…?` 只允许出现在最后一段,由 matchRoutePath 校验) */
41
+ private parseRouteSegment;
42
+ private matchRoutePath;
27
43
  private matchRoute;
28
44
  }
@@ -0,0 +1,32 @@
1
+ import { ComponentSetup, InjectionToken } from '@viewfly/core';
2
+ import { NavigatorParams } from './navigator';
3
+ import { Router } from './router';
4
+ /** URL path 上由 `:param` 解析得到的键值(与查询串 `queryParams` 区分) */
5
+ export type Params = Record<string, string>;
6
+ export interface CanActivateContext {
7
+ to: NavigatorParams;
8
+ from: NavigatorParams | null;
9
+ router: Router;
10
+ params: Params;
11
+ }
12
+ export interface RedirectContext {
13
+ to: NavigatorParams;
14
+ from: NavigatorParams | null;
15
+ router: Router;
16
+ params: Params;
17
+ }
18
+ export interface NamedRouteComponent {
19
+ name: string;
20
+ component?: ComponentSetup;
21
+ asyncComponent?: () => Promise<ComponentSetup>;
22
+ }
23
+ export interface Route {
24
+ path: string;
25
+ component?: ComponentSetup;
26
+ children?: Route[] | (() => Promise<Route[]>);
27
+ namedComponents?: NamedRouteComponent[];
28
+ asyncComponent?: () => Promise<ComponentSetup>;
29
+ canActivate?(context: CanActivateContext): boolean | Promise<boolean>;
30
+ redirectTo?: string | ((context: RedirectContext) => string | NavigatorParams);
31
+ }
32
+ export declare const Routes: InjectionToken<Route[]>;
@@ -7,7 +7,7 @@ export interface UrlChildPath {
7
7
  }
8
8
  export interface UrlQuery {
9
9
  type: 'query';
10
- params: UrlQueryParams;
10
+ queryParams: UrlQueryParams;
11
11
  }
12
12
  export interface UrlQueryParams {
13
13
  [key: string]: string | string[];
@@ -1,10 +1,16 @@
1
1
  import { Application, Module } from '@viewfly/core';
2
2
  import { NavigatorHooks } from './providers/navigator';
3
+ import { Route } from './providers/routes';
4
+ export interface RouterConfig {
5
+ baseUrl?: string;
6
+ routes?: Route[];
7
+ hooks?: NavigatorHooks;
8
+ }
3
9
  export declare class RouterModule implements Module {
4
- baseUrl: string;
10
+ config: RouterConfig;
5
11
  private subscription;
6
12
  private navigator;
7
- constructor(baseUrl?: string, hooks?: NavigatorHooks);
13
+ constructor(config: RouterConfig);
8
14
  setup(app: Application): void;
9
15
  onDestroy(): void;
10
16
  }
@@ -1,6 +1,5 @@
1
1
  import { Props } from '@viewfly/core';
2
- import { RouteConfig } from './providers/_api';
3
2
  export interface RouterOutletProps extends Props {
4
- config: RouteConfig[];
3
+ name?: string;
5
4
  }
6
5
  export declare function RouterOutlet(props: RouterOutletProps): () => import("@viewfly/core").JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/router",
3
- "version": "3.0.0-alpha.8",
3
+ "version": "3.0.0",
4
4
  "description": "A routing library based on the Viewfly framework that can be run in the browser or Nodejs background.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -19,7 +19,7 @@
19
19
  "keywords": [],
20
20
  "dependencies": {
21
21
  "@tanbo/stream": "^1.2.4",
22
- "@viewfly/core": "^3.0.0-alpha.8",
22
+ "@viewfly/core": "^3.0.0",
23
23
  "url": "^0.11.4"
24
24
  },
25
25
  "devDependencies": {