@tinkoff/router 0.7.8 → 0.7.9

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.
@@ -19,6 +19,7 @@ class AbstractRouter {
19
19
  guards;
20
20
  hooks;
21
21
  syncHooks;
22
+ internalHooks;
22
23
  navigateHook;
23
24
  updateHook;
24
25
  runNavigateHook;
@@ -98,6 +99,16 @@ class AbstractRouter {
98
99
  this.blockHook.tapPromise('router', async (_, { navigation }) => {
99
100
  await this.onBlock?.(navigation);
100
101
  });
102
+ this.internalHooks = {
103
+ 'router:resolve-route': this.hooksFactory.createSync('router:resolve-route'),
104
+ 'router:resolve-url': this.hooksFactory.createSync('router:resolve-url'),
105
+ };
106
+ this.internalHooks['router:resolve-route'].tap('router', (_, args) => {
107
+ return this._resolveRoute(...args);
108
+ });
109
+ this.internalHooks['router:resolve-url'].tap('router', (_, navigation) => {
110
+ return this._resolveUrl(navigation);
111
+ });
101
112
  this.plugins.forEach((plugin) => {
102
113
  plugin.apply(this);
103
114
  });
@@ -311,50 +322,11 @@ class AbstractRouter {
311
322
  }
312
323
  return normalized;
313
324
  }
314
- resolveUrl({ url, query = {}, params, preserveQuery, hash }) {
315
- const currentRoute = this.getCurrentRoute();
316
- const currentUrl = this.getCurrentUrl();
317
- const resultUrl = url ? rawResolveUrl(currentUrl?.href ?? '', url) : rawParse(currentUrl.href);
318
- let { pathname } = resultUrl;
319
- if (params) {
320
- if (url) {
321
- pathname = makePath(resultUrl.pathname, params);
322
- }
323
- else if (currentRoute) {
324
- pathname = makePath(currentRoute.path, { ...currentRoute.params, ...params });
325
- }
326
- }
327
- if (isSameHost(resultUrl)) {
328
- pathname = this.normalizePathname(pathname);
329
- }
330
- return convertRawUrl(rawAssignUrl(resultUrl, {
331
- pathname,
332
- search: url ? resultUrl.search : '',
333
- query: {
334
- ...(preserveQuery ? this.getCurrentUrl().query : {}),
335
- ...query,
336
- },
337
- hash: hash ?? resultUrl.hash,
338
- }));
325
+ resolveUrl(navigation) {
326
+ return this.internalHooks['router:resolve-url'].call(navigation);
339
327
  }
340
- resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
341
- let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
342
- if (wildcard && !route && url) {
343
- // if ordinary route not found look for a wildcard route
344
- route = this.tree?.getWildcard(url.pathname);
345
- }
346
- if (!route) {
347
- return;
348
- }
349
- // if condition is true route data not changed, so no need to create new reference for route object
350
- if (!params && navigateState === route.navigateState) {
351
- return route;
352
- }
353
- return {
354
- ...route,
355
- params: { ...route.params, ...params },
356
- navigateState,
357
- };
328
+ resolveRoute(...args) {
329
+ return this.internalHooks['router:resolve-route'].call(args);
358
330
  }
359
331
  async runGuards(navigation) {
360
332
  logger.debug({
@@ -460,6 +432,51 @@ class AbstractRouter {
460
432
  uuid() {
461
433
  return this.currentUuid++;
462
434
  }
435
+ _resolveUrl({ url, query = {}, params, preserveQuery, hash }) {
436
+ const currentRoute = this.getCurrentRoute();
437
+ const currentUrl = this.getCurrentUrl();
438
+ const resultUrl = url ? rawResolveUrl(currentUrl?.href ?? '', url) : rawParse(currentUrl.href);
439
+ let { pathname } = resultUrl;
440
+ if (params) {
441
+ if (url) {
442
+ pathname = makePath(resultUrl.pathname, params);
443
+ }
444
+ else if (currentRoute) {
445
+ pathname = makePath(currentRoute.path, { ...currentRoute.params, ...params });
446
+ }
447
+ }
448
+ if (isSameHost(resultUrl)) {
449
+ pathname = this.normalizePathname(pathname);
450
+ }
451
+ return convertRawUrl(rawAssignUrl(resultUrl, {
452
+ pathname,
453
+ search: url ? resultUrl.search : '',
454
+ query: {
455
+ ...(preserveQuery ? this.getCurrentUrl().query : {}),
456
+ ...query,
457
+ },
458
+ hash: hash ?? resultUrl.hash,
459
+ }));
460
+ }
461
+ _resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
462
+ let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
463
+ if (wildcard && !route && url) {
464
+ // if ordinary route not found look for a wildcard route
465
+ route = this.tree?.getWildcard(url.pathname);
466
+ }
467
+ if (!route) {
468
+ return;
469
+ }
470
+ // if condition is true route data not changed, so no need to create new reference for route object
471
+ if (!params && navigateState === route.navigateState) {
472
+ return route;
473
+ }
474
+ return {
475
+ ...route,
476
+ params: { ...route.params, ...params },
477
+ navigateState,
478
+ };
479
+ }
463
480
  }
464
481
 
465
482
  export { AbstractRouter };
@@ -1,7 +1,7 @@
1
1
  import type { Url } from '@tinkoff/url';
2
2
  import type { AsyncParallelTapableHookInstance, AsyncTapableHookInstance, SyncTapableHookInstance } from '@tinkoff/hook-runner';
3
3
  import { TapableHooks } from '@tinkoff/hook-runner';
4
- import { Route, NavigateOptions, UpdateCurrentRouteOptions, Navigation, NavigationGuard, NavigationHook, NavigationSyncHook, HookName, Params, SyncHookName, HistoryOptions, RouterPlugin, BackNavigationType } from '../types';
4
+ import { Route, NavigateOptions, UpdateCurrentRouteOptions, Navigation, NavigationGuard, NavigationHook, NavigationSyncHook, HookName, Params, SyncHookName, HistoryOptions, RouterPlugin, BackNavigationType, NavigationRoute } from '../types';
5
5
  import type { History } from '../history/base';
6
6
  import type { RouteTree } from '../tree/tree';
7
7
  export interface Options {
@@ -48,6 +48,10 @@ export declare abstract class AbstractRouter {
48
48
  readonly syncHooks: Map<SyncHookName, SyncTapableHookInstance<{
49
49
  navigation: Navigation;
50
50
  }>>;
51
+ readonly internalHooks: {
52
+ 'router:resolve-route': SyncTapableHookInstance<Parameters<AbstractRouter['_resolveRoute']>, NavigationRoute | undefined>;
53
+ 'router:resolve-url': SyncTapableHookInstance<NavigateOptions, Url>;
54
+ };
51
55
  readonly navigateHook: AsyncTapableHookInstance<{
52
56
  navigateOptions: NavigateOptions | string;
53
57
  }>;
@@ -80,9 +84,9 @@ export declare abstract class AbstractRouter {
80
84
  protected onNotFound?: NavigationHook;
81
85
  protected onBlock?: NavigationHook;
82
86
  start(): Promise<void>;
83
- getCurrentRoute(): import("../types").NavigationRoute;
87
+ getCurrentRoute(): NavigationRoute;
84
88
  getCurrentUrl(): Url;
85
- getLastRoute(): import("../types").NavigationRoute;
89
+ getLastRoute(): NavigationRoute;
86
90
  getLastUrl(): Url;
87
91
  protected commitNavigation(navigation: Navigation): void;
88
92
  updateCurrentRoute(updateRouteOptions: UpdateCurrentRouteOptions): Promise<void>;
@@ -92,7 +96,7 @@ export declare abstract class AbstractRouter {
92
96
  protected internalNavigate(navigateOptions: NavigateOptions, { history, redirect }: InternalOptions): Promise<void>;
93
97
  protected runNavigate(navigation: Navigation): Promise<void>;
94
98
  protected run(navigation: Navigation): Promise<void>;
95
- resolve(resolveOptions: NavigateOptions | string, options?: Parameters<AbstractRouter['resolveRoute']>[1]): import("../types").NavigationRoute;
99
+ resolve(resolveOptions: NavigateOptions | string, options?: Parameters<AbstractRouter['resolveRoute']>[1]): NavigationRoute;
96
100
  back(options?: HistoryOptions): Promise<void>;
97
101
  forward(): Promise<void>;
98
102
  go(to: number, options?: HistoryOptions): Promise<void>;
@@ -106,14 +110,8 @@ export declare abstract class AbstractRouter {
106
110
  protected block(navigation: Navigation): Promise<void>;
107
111
  cancel(_?: Navigation): Navigation | void;
108
112
  protected normalizePathname(pathname?: string): string;
109
- protected resolveUrl({ url, query, params, preserveQuery, hash }: NavigateOptions): Url;
110
- protected resolveRoute({ url, params, navigateState }: {
111
- url?: Url;
112
- params?: Params;
113
- navigateState?: any;
114
- }, { wildcard }?: {
115
- wildcard?: boolean;
116
- }): import("../types").NavigationRoute;
113
+ protected resolveUrl(navigation: NavigateOptions): Url;
114
+ protected resolveRoute(...args: Parameters<AbstractRouter['_resolveRoute']>): NavigationRoute;
117
115
  protected runGuards(navigation: Navigation): Promise<void>;
118
116
  registerGuard(guard: NavigationGuard): import("@tinkoff/hook-runner/lib/types").UntapCallback;
119
117
  protected runSyncHooks(hookName: SyncHookName, navigation: Navigation): void;
@@ -121,6 +119,14 @@ export declare abstract class AbstractRouter {
121
119
  protected runHooks(hookName: HookName, navigation: Navigation): Promise<void>;
122
120
  registerHook(hookName: HookName, hook: NavigationHook): import("@tinkoff/hook-runner/lib/types").UntapCallback;
123
121
  private uuid;
122
+ protected _resolveUrl({ url, query, params, preserveQuery, hash }: NavigateOptions): Url;
123
+ protected _resolveRoute({ url, params, navigateState }: {
124
+ url?: Url;
125
+ params?: Params;
126
+ navigateState?: any;
127
+ }, { wildcard }?: {
128
+ wildcard?: boolean;
129
+ }): NavigationRoute;
124
130
  }
125
131
  export {};
126
132
  //# sourceMappingURL=abstract.d.ts.map
@@ -19,6 +19,7 @@ class AbstractRouter {
19
19
  guards;
20
20
  hooks;
21
21
  syncHooks;
22
+ internalHooks;
22
23
  navigateHook;
23
24
  updateHook;
24
25
  runNavigateHook;
@@ -98,6 +99,16 @@ class AbstractRouter {
98
99
  this.blockHook.tapPromise('router', async (_, { navigation }) => {
99
100
  await this.onBlock?.(navigation);
100
101
  });
102
+ this.internalHooks = {
103
+ 'router:resolve-route': this.hooksFactory.createSync('router:resolve-route'),
104
+ 'router:resolve-url': this.hooksFactory.createSync('router:resolve-url'),
105
+ };
106
+ this.internalHooks['router:resolve-route'].tap('router', (_, args) => {
107
+ return this._resolveRoute(...args);
108
+ });
109
+ this.internalHooks['router:resolve-url'].tap('router', (_, navigation) => {
110
+ return this._resolveUrl(navigation);
111
+ });
101
112
  this.plugins.forEach((plugin) => {
102
113
  plugin.apply(this);
103
114
  });
@@ -311,50 +322,11 @@ class AbstractRouter {
311
322
  }
312
323
  return normalized;
313
324
  }
314
- resolveUrl({ url, query = {}, params, preserveQuery, hash }) {
315
- const currentRoute = this.getCurrentRoute();
316
- const currentUrl = this.getCurrentUrl();
317
- const resultUrl = url ? rawResolveUrl(currentUrl?.href ?? '', url) : rawParse(currentUrl.href);
318
- let { pathname } = resultUrl;
319
- if (params) {
320
- if (url) {
321
- pathname = makePath(resultUrl.pathname, params);
322
- }
323
- else if (currentRoute) {
324
- pathname = makePath(currentRoute.path, { ...currentRoute.params, ...params });
325
- }
326
- }
327
- if (isSameHost(resultUrl)) {
328
- pathname = this.normalizePathname(pathname);
329
- }
330
- return convertRawUrl(rawAssignUrl(resultUrl, {
331
- pathname,
332
- search: url ? resultUrl.search : '',
333
- query: {
334
- ...(preserveQuery ? this.getCurrentUrl().query : {}),
335
- ...query,
336
- },
337
- hash: hash ?? resultUrl.hash,
338
- }));
325
+ resolveUrl(navigation) {
326
+ return this.internalHooks['router:resolve-url'].call(navigation);
339
327
  }
340
- resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
341
- let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
342
- if (wildcard && !route && url) {
343
- // if ordinary route not found look for a wildcard route
344
- route = this.tree?.getWildcard(url.pathname);
345
- }
346
- if (!route) {
347
- return;
348
- }
349
- // if condition is true route data not changed, so no need to create new reference for route object
350
- if (!params && navigateState === route.navigateState) {
351
- return route;
352
- }
353
- return {
354
- ...route,
355
- params: { ...route.params, ...params },
356
- navigateState,
357
- };
328
+ resolveRoute(...args) {
329
+ return this.internalHooks['router:resolve-route'].call(args);
358
330
  }
359
331
  async runGuards(navigation) {
360
332
  logger.debug({
@@ -460,6 +432,51 @@ class AbstractRouter {
460
432
  uuid() {
461
433
  return this.currentUuid++;
462
434
  }
435
+ _resolveUrl({ url, query = {}, params, preserveQuery, hash }) {
436
+ const currentRoute = this.getCurrentRoute();
437
+ const currentUrl = this.getCurrentUrl();
438
+ const resultUrl = url ? rawResolveUrl(currentUrl?.href ?? '', url) : rawParse(currentUrl.href);
439
+ let { pathname } = resultUrl;
440
+ if (params) {
441
+ if (url) {
442
+ pathname = makePath(resultUrl.pathname, params);
443
+ }
444
+ else if (currentRoute) {
445
+ pathname = makePath(currentRoute.path, { ...currentRoute.params, ...params });
446
+ }
447
+ }
448
+ if (isSameHost(resultUrl)) {
449
+ pathname = this.normalizePathname(pathname);
450
+ }
451
+ return convertRawUrl(rawAssignUrl(resultUrl, {
452
+ pathname,
453
+ search: url ? resultUrl.search : '',
454
+ query: {
455
+ ...(preserveQuery ? this.getCurrentUrl().query : {}),
456
+ ...query,
457
+ },
458
+ hash: hash ?? resultUrl.hash,
459
+ }));
460
+ }
461
+ _resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
462
+ let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
463
+ if (wildcard && !route && url) {
464
+ // if ordinary route not found look for a wildcard route
465
+ route = this.tree?.getWildcard(url.pathname);
466
+ }
467
+ if (!route) {
468
+ return;
469
+ }
470
+ // if condition is true route data not changed, so no need to create new reference for route object
471
+ if (!params && navigateState === route.navigateState) {
472
+ return route;
473
+ }
474
+ return {
475
+ ...route,
476
+ params: { ...route.params, ...params },
477
+ navigateState,
478
+ };
479
+ }
463
480
  }
464
481
 
465
482
  export { AbstractRouter };
@@ -28,6 +28,7 @@ class AbstractRouter {
28
28
  guards;
29
29
  hooks;
30
30
  syncHooks;
31
+ internalHooks;
31
32
  navigateHook;
32
33
  updateHook;
33
34
  runNavigateHook;
@@ -107,6 +108,16 @@ class AbstractRouter {
107
108
  this.blockHook.tapPromise('router', async (_, { navigation }) => {
108
109
  await this.onBlock?.(navigation);
109
110
  });
111
+ this.internalHooks = {
112
+ 'router:resolve-route': this.hooksFactory.createSync('router:resolve-route'),
113
+ 'router:resolve-url': this.hooksFactory.createSync('router:resolve-url'),
114
+ };
115
+ this.internalHooks['router:resolve-route'].tap('router', (_, args) => {
116
+ return this._resolveRoute(...args);
117
+ });
118
+ this.internalHooks['router:resolve-url'].tap('router', (_, navigation) => {
119
+ return this._resolveUrl(navigation);
120
+ });
110
121
  this.plugins.forEach((plugin) => {
111
122
  plugin.apply(this);
112
123
  });
@@ -320,50 +331,11 @@ class AbstractRouter {
320
331
  }
321
332
  return normalized;
322
333
  }
323
- resolveUrl({ url: url$1, query = {}, params, preserveQuery, hash }) {
324
- const currentRoute = this.getCurrentRoute();
325
- const currentUrl = this.getCurrentUrl();
326
- const resultUrl = url$1 ? url.rawResolveUrl(currentUrl?.href ?? '', url$1) : url.rawParse(currentUrl.href);
327
- let { pathname } = resultUrl;
328
- if (params) {
329
- if (url$1) {
330
- pathname = utils$1.makePath(resultUrl.pathname, params);
331
- }
332
- else if (currentRoute) {
333
- pathname = utils$1.makePath(currentRoute.path, { ...currentRoute.params, ...params });
334
- }
335
- }
336
- if (utils.isSameHost(resultUrl)) {
337
- pathname = this.normalizePathname(pathname);
338
- }
339
- return url.convertRawUrl(url.rawAssignUrl(resultUrl, {
340
- pathname,
341
- search: url$1 ? resultUrl.search : '',
342
- query: {
343
- ...(preserveQuery ? this.getCurrentUrl().query : {}),
344
- ...query,
345
- },
346
- hash: hash ?? resultUrl.hash,
347
- }));
334
+ resolveUrl(navigation) {
335
+ return this.internalHooks['router:resolve-url'].call(navigation);
348
336
  }
349
- resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
350
- let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
351
- if (wildcard && !route && url) {
352
- // if ordinary route not found look for a wildcard route
353
- route = this.tree?.getWildcard(url.pathname);
354
- }
355
- if (!route) {
356
- return;
357
- }
358
- // if condition is true route data not changed, so no need to create new reference for route object
359
- if (!params && navigateState === route.navigateState) {
360
- return route;
361
- }
362
- return {
363
- ...route,
364
- params: { ...route.params, ...params },
365
- navigateState,
366
- };
337
+ resolveRoute(...args) {
338
+ return this.internalHooks['router:resolve-route'].call(args);
367
339
  }
368
340
  async runGuards(navigation) {
369
341
  logger.logger.debug({
@@ -469,6 +441,51 @@ class AbstractRouter {
469
441
  uuid() {
470
442
  return this.currentUuid++;
471
443
  }
444
+ _resolveUrl({ url: url$1, query = {}, params, preserveQuery, hash }) {
445
+ const currentRoute = this.getCurrentRoute();
446
+ const currentUrl = this.getCurrentUrl();
447
+ const resultUrl = url$1 ? url.rawResolveUrl(currentUrl?.href ?? '', url$1) : url.rawParse(currentUrl.href);
448
+ let { pathname } = resultUrl;
449
+ if (params) {
450
+ if (url$1) {
451
+ pathname = utils$1.makePath(resultUrl.pathname, params);
452
+ }
453
+ else if (currentRoute) {
454
+ pathname = utils$1.makePath(currentRoute.path, { ...currentRoute.params, ...params });
455
+ }
456
+ }
457
+ if (utils.isSameHost(resultUrl)) {
458
+ pathname = this.normalizePathname(pathname);
459
+ }
460
+ return url.convertRawUrl(url.rawAssignUrl(resultUrl, {
461
+ pathname,
462
+ search: url$1 ? resultUrl.search : '',
463
+ query: {
464
+ ...(preserveQuery ? this.getCurrentUrl().query : {}),
465
+ ...query,
466
+ },
467
+ hash: hash ?? resultUrl.hash,
468
+ }));
469
+ }
470
+ _resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
471
+ let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
472
+ if (wildcard && !route && url) {
473
+ // if ordinary route not found look for a wildcard route
474
+ route = this.tree?.getWildcard(url.pathname);
475
+ }
476
+ if (!route) {
477
+ return;
478
+ }
479
+ // if condition is true route data not changed, so no need to create new reference for route object
480
+ if (!params && navigateState === route.navigateState) {
481
+ return route;
482
+ }
483
+ return {
484
+ ...route,
485
+ params: { ...route.params, ...params },
486
+ navigateState,
487
+ };
488
+ }
472
489
  }
473
490
 
474
491
  exports.AbstractRouter = AbstractRouter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkoff/router",
3
- "version": "0.7.8",
3
+ "version": "0.7.9",
4
4
  "description": "router",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -28,7 +28,7 @@
28
28
  "use-sync-external-store": "^1.4.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@tramvai/core": "7.2.3",
31
+ "@tramvai/core": "7.3.0",
32
32
  "react": ">=16.14.0",
33
33
  "tslib": "^2.4.0"
34
34
  },