@tinkoff/router 0.1.67 → 0.1.70
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/README.md +14 -3
- package/lib/index.browser.js +12 -4
- package/lib/index.es.js +11 -9
- package/lib/index.js +11 -9
- package/lib/router/abstract.d.ts +1 -1
- package/lib/router/server.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,8 +60,12 @@ export const myGuard: NavigationGuard = async ({ to }) => {
|
|
|
60
60
|
return false; // block this transition
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
if (to.config.redirect) {
|
|
64
|
-
return
|
|
63
|
+
if (typeof to.config.redirect === 'string') {
|
|
64
|
+
return to.config.redirect; // call a redirect to the specified page
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (typeof to.config.needAuth && !isAuth()) {
|
|
68
|
+
return { url: '/login/', code: '302' }; // redirect to page with NavigateOptions
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
// if nothing is returned, the transition will be performed as usual
|
|
@@ -83,7 +87,7 @@ The behaviour of routing depends on the result of executing guards functions and
|
|
|
83
87
|
- if all of the guards returns `undefined` than navigation will continue executing
|
|
84
88
|
- if any of the guards returns `false` than navigation is getting blocked and next action differs on server and client
|
|
85
89
|
- if any of the guards returns `string` it is considered as url to which redirect should be happen
|
|
86
|
-
- if any of the guards returns `NavigateOptions` interface, `url` property from it is considered as url to which redirect should be happen
|
|
90
|
+
- if any of the guards returns [`NavigateOptions`](#navigateoptions) interface, `url` property from it is considered as url to which redirect should be happen
|
|
87
91
|
|
|
88
92
|
### Transitions hooks
|
|
89
93
|
|
|
@@ -172,6 +176,13 @@ router.updateCurrentRoute({ query: { a: '1' } });
|
|
|
172
176
|
2. `change`
|
|
173
177
|
3. `afterUpdateCurrent`
|
|
174
178
|
|
|
179
|
+
### NavigateOptions
|
|
180
|
+
|
|
181
|
+
Object that allows to specify transition options both to [navigate](#navigate) and [updateCurrentRoute](#updatecurrentroute) transitions
|
|
182
|
+
|
|
183
|
+
- `code` - redirect code that is returned on server in case of redirects
|
|
184
|
+
- `navigateState` - any additional data that is stored with route
|
|
185
|
+
|
|
175
186
|
### Working with query
|
|
176
187
|
|
|
177
188
|
#### query option
|
package/lib/index.browser.js
CHANGED
|
@@ -220,17 +220,16 @@ class AbstractRouter {
|
|
|
220
220
|
await this.runHooks('afterUpdateCurrent', navigation);
|
|
221
221
|
}
|
|
222
222
|
async navigate(navigateOptions) {
|
|
223
|
-
return this.internalNavigate(navigateOptions, {});
|
|
223
|
+
return this.internalNavigate(typeof navigateOptions === 'string' ? { url: navigateOptions } : navigateOptions, {});
|
|
224
224
|
}
|
|
225
225
|
async internalNavigate(navigateOptions, { history }) {
|
|
226
226
|
var _a;
|
|
227
|
-
const
|
|
228
|
-
const { url, replace, params, navigateState, code } = options;
|
|
227
|
+
const { url, replace, params, navigateState, code } = navigateOptions;
|
|
229
228
|
const prevNavigation = (_a = this.currentNavigation) !== null && _a !== void 0 ? _a : this.lastNavigation;
|
|
230
229
|
if (!url && !prevNavigation) {
|
|
231
230
|
throw new Error('Navigate url should be specified and cannot be omitted for first navigation');
|
|
232
231
|
}
|
|
233
|
-
const resolvedUrl = this.resolveUrl(
|
|
232
|
+
const resolvedUrl = this.resolveUrl(navigateOptions);
|
|
234
233
|
const { to: from, url: fromUrl } = prevNavigation !== null && prevNavigation !== void 0 ? prevNavigation : {};
|
|
235
234
|
let navigation = {
|
|
236
235
|
type: 'navigate',
|
|
@@ -954,6 +953,15 @@ class Router extends ClientRouter {
|
|
|
954
953
|
if (this.currentNavigation) {
|
|
955
954
|
return this.delayNavigation(navigation);
|
|
956
955
|
}
|
|
956
|
+
// ignore previous navigations that were put in delayedNavigation as we have more fresh navigation to execute
|
|
957
|
+
if (this.delayedNavigation) {
|
|
958
|
+
logger.info({
|
|
959
|
+
event: 'delay-navigation-drop',
|
|
960
|
+
delayedNavigation: this.delayedNavigation,
|
|
961
|
+
navigation,
|
|
962
|
+
});
|
|
963
|
+
this.delayedNavigation = null;
|
|
964
|
+
}
|
|
957
965
|
return this.flattenDelayedNavigation(navigation);
|
|
958
966
|
}
|
|
959
967
|
delayNavigation(navigation) {
|
package/lib/index.es.js
CHANGED
|
@@ -220,17 +220,16 @@ class AbstractRouter {
|
|
|
220
220
|
await this.runHooks('afterUpdateCurrent', navigation);
|
|
221
221
|
}
|
|
222
222
|
async navigate(navigateOptions) {
|
|
223
|
-
return this.internalNavigate(navigateOptions, {});
|
|
223
|
+
return this.internalNavigate(typeof navigateOptions === 'string' ? { url: navigateOptions } : navigateOptions, {});
|
|
224
224
|
}
|
|
225
225
|
async internalNavigate(navigateOptions, { history }) {
|
|
226
226
|
var _a;
|
|
227
|
-
const
|
|
228
|
-
const { url, replace, params, navigateState, code } = options;
|
|
227
|
+
const { url, replace, params, navigateState, code } = navigateOptions;
|
|
229
228
|
const prevNavigation = (_a = this.currentNavigation) !== null && _a !== void 0 ? _a : this.lastNavigation;
|
|
230
229
|
if (!url && !prevNavigation) {
|
|
231
230
|
throw new Error('Navigate url should be specified and cannot be omitted for first navigation');
|
|
232
231
|
}
|
|
233
|
-
const resolvedUrl = this.resolveUrl(
|
|
232
|
+
const resolvedUrl = this.resolveUrl(navigateOptions);
|
|
234
233
|
const { to: from, url: fromUrl } = prevNavigation !== null && prevNavigation !== void 0 ? prevNavigation : {};
|
|
235
234
|
let navigation = {
|
|
236
235
|
type: 'navigate',
|
|
@@ -682,13 +681,16 @@ class Router extends AbstractRouter {
|
|
|
682
681
|
});
|
|
683
682
|
return this.lastNavigation;
|
|
684
683
|
}
|
|
685
|
-
async
|
|
686
|
-
if (this.redirectCode) {
|
|
687
|
-
return this.redirect(navigation, { url: navigation.url.path, code: this.redirectCode });
|
|
688
|
-
}
|
|
684
|
+
async internalNavigate(navigateOptions, internalOptions) {
|
|
689
685
|
// any navigation after initial should be considered as redirects
|
|
690
686
|
if (this.getCurrentRoute()) {
|
|
691
|
-
return this.redirect(
|
|
687
|
+
return this.redirect(this.lastNavigation, navigateOptions);
|
|
688
|
+
}
|
|
689
|
+
return super.internalNavigate(navigateOptions, internalOptions);
|
|
690
|
+
}
|
|
691
|
+
async run(navigation) {
|
|
692
|
+
if (this.redirectCode) {
|
|
693
|
+
return this.redirect(navigation, { url: navigation.url.href, code: this.redirectCode });
|
|
692
694
|
}
|
|
693
695
|
await super.run(navigation);
|
|
694
696
|
}
|
package/lib/index.js
CHANGED
|
@@ -236,17 +236,16 @@ class AbstractRouter {
|
|
|
236
236
|
await this.runHooks('afterUpdateCurrent', navigation);
|
|
237
237
|
}
|
|
238
238
|
async navigate(navigateOptions) {
|
|
239
|
-
return this.internalNavigate(navigateOptions, {});
|
|
239
|
+
return this.internalNavigate(typeof navigateOptions === 'string' ? { url: navigateOptions } : navigateOptions, {});
|
|
240
240
|
}
|
|
241
241
|
async internalNavigate(navigateOptions, { history }) {
|
|
242
242
|
var _a;
|
|
243
|
-
const
|
|
244
|
-
const { url, replace, params, navigateState, code } = options;
|
|
243
|
+
const { url, replace, params, navigateState, code } = navigateOptions;
|
|
245
244
|
const prevNavigation = (_a = this.currentNavigation) !== null && _a !== void 0 ? _a : this.lastNavigation;
|
|
246
245
|
if (!url && !prevNavigation) {
|
|
247
246
|
throw new Error('Navigate url should be specified and cannot be omitted for first navigation');
|
|
248
247
|
}
|
|
249
|
-
const resolvedUrl = this.resolveUrl(
|
|
248
|
+
const resolvedUrl = this.resolveUrl(navigateOptions);
|
|
250
249
|
const { to: from, url: fromUrl } = prevNavigation !== null && prevNavigation !== void 0 ? prevNavigation : {};
|
|
251
250
|
let navigation = {
|
|
252
251
|
type: 'navigate',
|
|
@@ -698,13 +697,16 @@ class Router extends AbstractRouter {
|
|
|
698
697
|
});
|
|
699
698
|
return this.lastNavigation;
|
|
700
699
|
}
|
|
701
|
-
async
|
|
702
|
-
if (this.redirectCode) {
|
|
703
|
-
return this.redirect(navigation, { url: navigation.url.path, code: this.redirectCode });
|
|
704
|
-
}
|
|
700
|
+
async internalNavigate(navigateOptions, internalOptions) {
|
|
705
701
|
// any navigation after initial should be considered as redirects
|
|
706
702
|
if (this.getCurrentRoute()) {
|
|
707
|
-
return this.redirect(
|
|
703
|
+
return this.redirect(this.lastNavigation, navigateOptions);
|
|
704
|
+
}
|
|
705
|
+
return super.internalNavigate(navigateOptions, internalOptions);
|
|
706
|
+
}
|
|
707
|
+
async run(navigation) {
|
|
708
|
+
if (this.redirectCode) {
|
|
709
|
+
return this.redirect(navigation, { url: navigation.url.href, code: this.redirectCode });
|
|
708
710
|
}
|
|
709
711
|
await super.run(navigation);
|
|
710
712
|
}
|
package/lib/router/abstract.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ export declare abstract class AbstractRouter {
|
|
|
45
45
|
protected internalUpdateCurrentRoute(updateRouteOptions: UpdateCurrentRouteOptions, { history }: InternalOptions): Promise<void>;
|
|
46
46
|
protected runUpdateCurrentRoute(navigation: Navigation): Promise<void>;
|
|
47
47
|
navigate(navigateOptions: NavigateOptions | string): Promise<void>;
|
|
48
|
-
protected internalNavigate(navigateOptions: NavigateOptions
|
|
48
|
+
protected internalNavigate(navigateOptions: NavigateOptions, { history }: InternalOptions): Promise<void>;
|
|
49
49
|
protected runNavigate(navigation: Navigation): Promise<void>;
|
|
50
50
|
protected run(navigation: Navigation): Promise<void>;
|
|
51
51
|
resolve(resolveOptions: NavigateOptions | string, options?: Parameters<AbstractRouter['resolveRoute']>[1]): import("../types").NavigationRoute;
|
package/lib/router/server.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export declare class Router extends AbstractRouter {
|
|
|
8
8
|
constructor(options: Options);
|
|
9
9
|
protected onRedirect: (navigation: Navigation) => Promise<void>;
|
|
10
10
|
dehydrate(): Promise<Navigation>;
|
|
11
|
+
protected internalNavigate(navigateOptions: NavigateOptions, internalOptions: any): Promise<void>;
|
|
11
12
|
protected run(navigation: Navigation): Promise<void>;
|
|
12
13
|
protected redirect(navigation: Navigation, target: NavigateOptions): Promise<void>;
|
|
13
14
|
protected notfound(navigation: Navigation): Promise<void>;
|