@shuvi/router 1.0.0 → 1.0.2

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.
@@ -22,6 +22,10 @@ export interface TransitionOptions {
22
22
  state?: State;
23
23
  action?: Action;
24
24
  redirectedFrom?: PathRecord;
25
+ /**
26
+ * skipGuards means this route transition will be straightforwardly executed without any before guard
27
+ */
28
+ skipGuards?: boolean;
25
29
  onTransition(event: {
26
30
  location: Location;
27
31
  state: HistoryState;
@@ -32,11 +36,12 @@ export interface TransitionOptions {
32
36
  export interface PushOptions {
33
37
  state?: object | null | undefined;
34
38
  redirectedFrom?: PathRecord;
39
+ skipGuards?: boolean;
35
40
  }
36
41
  export default abstract class BaseHistory {
37
42
  action: Action;
38
43
  location: Location;
39
- doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function) => void;
44
+ doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function, skipGuards?: boolean) => void;
40
45
  protected _index: number;
41
46
  protected _blockers: import("../utils").Events<Blocker<State>>;
42
47
  protected abstract getIndexAndLocation(): [number, Location];
@@ -65,6 +70,6 @@ export default abstract class BaseHistory {
65
70
  back(): void;
66
71
  forward(): void;
67
72
  resolve(to: any, from?: any): ResolvedPath;
68
- transitionTo(to: PathRecord, { onTransition, onAbort, action, state, redirectedFrom }: TransitionOptions): void;
73
+ transitionTo(to: PathRecord, { onTransition, onAbort, action, state, redirectedFrom, skipGuards }: TransitionOptions): void;
69
74
  private _updateState;
70
75
  }
@@ -39,7 +39,7 @@ export default class BaseHistory {
39
39
  href: pathToString(toPath)
40
40
  };
41
41
  }
42
- transitionTo(to, { onTransition, onAbort, action = ACTION_PUSH, state = null, redirectedFrom }) {
42
+ transitionTo(to, { onTransition, onAbort, action = ACTION_PUSH, state = null, redirectedFrom, skipGuards }) {
43
43
  const { path } = this.resolve(to, this.location.pathname);
44
44
  const nextLocation = createLocation(path, { state, redirectedFrom });
45
45
  // check transition
@@ -53,7 +53,8 @@ export default class BaseHistory {
53
53
  onAbort,
54
54
  action,
55
55
  state,
56
- redirectedFrom
56
+ redirectedFrom,
57
+ skipGuards
57
58
  });
58
59
  }
59
60
  });
@@ -70,7 +71,7 @@ export default class BaseHistory {
70
71
  url: this.resolve(nextLocation).href
71
72
  });
72
73
  this._updateState(action);
73
- }, onAbort);
74
+ }, onAbort, skipGuards);
74
75
  }
75
76
  _updateState(nextAction) {
76
77
  // update state
@@ -13,8 +13,8 @@ export default class MemoryHistory extends BaseHistory {
13
13
  private _entries;
14
14
  constructor({ initialEntries, initialIndex }?: MemoryHistoryOptions);
15
15
  setup(): void;
16
- push(to: PathRecord, { state, redirectedFrom }?: PushOptions): void;
17
- replace(to: PathRecord, { state, redirectedFrom }?: PushOptions): void;
16
+ push(to: PathRecord, { state, redirectedFrom, skipGuards }?: PushOptions): void;
17
+ replace(to: PathRecord, { state, redirectedFrom, skipGuards }?: PushOptions): void;
18
18
  go(delta: number): void;
19
19
  block(blocker: Blocker): () => void;
20
20
  resolve(to: PathRecord, from?: string): ResolvedPath;
@@ -18,21 +18,23 @@ export default class MemoryHistory extends BaseHistory {
18
18
  setup() {
19
19
  // do nothing
20
20
  }
21
- push(to, { state, redirectedFrom } = {}) {
21
+ push(to, { state, redirectedFrom, skipGuards } = {}) {
22
22
  return this.transitionTo(to, {
23
23
  state,
24
24
  redirectedFrom,
25
+ skipGuards,
25
26
  onTransition: ({ location }) => {
26
27
  this._index += 1;
27
28
  this._entries.splice(this._index, this._entries.length, location);
28
29
  }
29
30
  });
30
31
  }
31
- replace(to, { state, redirectedFrom } = {}) {
32
+ replace(to, { state, redirectedFrom, skipGuards } = {}) {
32
33
  return this.transitionTo(to, {
33
34
  state,
34
35
  action: ACTION_REPLACE,
35
36
  redirectedFrom,
37
+ skipGuards,
36
38
  onTransition: ({ location }) => {
37
39
  this._entries[this._index] = location;
38
40
  }
package/esm/router.js CHANGED
@@ -121,7 +121,7 @@ class Router {
121
121
  5. Emit change event(trigger react update)
122
122
  6. Call router.afterEach
123
123
  */
124
- _doTransition(to, onComplete, onAbort) {
124
+ _doTransition(to, onComplete, onAbort, skipGuards) {
125
125
  const nextRoute = this._getNextRoute(to);
126
126
  const current = this._current;
127
127
  const nextMatches = nextRoute.matches;
@@ -132,7 +132,9 @@ class Router {
132
132
  });
133
133
  }
134
134
  const routeContext = new Map();
135
- const queue = [].concat(this._beforeEachs.toArray(), this._beforeResolves.toArray());
135
+ const queue = skipGuards
136
+ ? []
137
+ : [].concat(this._beforeEachs.toArray(), this._beforeResolves.toArray());
136
138
  let cancel = false;
137
139
  this._cancleHandler = () => {
138
140
  cancel = true;
@@ -169,14 +171,22 @@ class Router {
169
171
  abort();
170
172
  if (typeof to === 'object') {
171
173
  if (to.replace) {
172
- this.replace(to.path);
174
+ this._history.replace(to.path, {
175
+ redirectedFrom: current,
176
+ skipGuards: to.skipGuards,
177
+ state: to.state
178
+ });
173
179
  }
174
180
  else {
175
- this.push(to.path);
181
+ this._history.push(to.path, {
182
+ redirectedFrom: current,
183
+ skipGuards: to.skipGuards,
184
+ state: to.state
185
+ });
176
186
  }
177
187
  }
178
188
  else {
179
- this.push(to);
189
+ this._history.push(to, { redirectedFrom: current });
180
190
  }
181
191
  }
182
192
  else {
@@ -16,6 +16,8 @@ export interface NavigationGuardNext {
16
16
  (location: string | {
17
17
  path?: string;
18
18
  replace?: boolean;
19
+ skipGuards?: boolean;
20
+ state?: any;
19
21
  }): void;
20
22
  (valid: boolean | undefined): void;
21
23
  (cb: NavigationGuardNextCallback): void;
@@ -22,6 +22,10 @@ export interface TransitionOptions {
22
22
  state?: State;
23
23
  action?: Action;
24
24
  redirectedFrom?: PathRecord;
25
+ /**
26
+ * skipGuards means this route transition will be straightforwardly executed without any before guard
27
+ */
28
+ skipGuards?: boolean;
25
29
  onTransition(event: {
26
30
  location: Location;
27
31
  state: HistoryState;
@@ -32,11 +36,12 @@ export interface TransitionOptions {
32
36
  export interface PushOptions {
33
37
  state?: object | null | undefined;
34
38
  redirectedFrom?: PathRecord;
39
+ skipGuards?: boolean;
35
40
  }
36
41
  export default abstract class BaseHistory {
37
42
  action: Action;
38
43
  location: Location;
39
- doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function) => void;
44
+ doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function, skipGuards?: boolean) => void;
40
45
  protected _index: number;
41
46
  protected _blockers: import("../utils").Events<Blocker<State>>;
42
47
  protected abstract getIndexAndLocation(): [number, Location];
@@ -65,6 +70,6 @@ export default abstract class BaseHistory {
65
70
  back(): void;
66
71
  forward(): void;
67
72
  resolve(to: any, from?: any): ResolvedPath;
68
- transitionTo(to: PathRecord, { onTransition, onAbort, action, state, redirectedFrom }: TransitionOptions): void;
73
+ transitionTo(to: PathRecord, { onTransition, onAbort, action, state, redirectedFrom, skipGuards }: TransitionOptions): void;
69
74
  private _updateState;
70
75
  }
@@ -42,7 +42,7 @@ class BaseHistory {
42
42
  href: (0, utils_1.pathToString)(toPath)
43
43
  };
44
44
  }
45
- transitionTo(to, { onTransition, onAbort, action = exports.ACTION_PUSH, state = null, redirectedFrom }) {
45
+ transitionTo(to, { onTransition, onAbort, action = exports.ACTION_PUSH, state = null, redirectedFrom, skipGuards }) {
46
46
  const { path } = this.resolve(to, this.location.pathname);
47
47
  const nextLocation = (0, utils_1.createLocation)(path, { state, redirectedFrom });
48
48
  // check transition
@@ -56,7 +56,8 @@ class BaseHistory {
56
56
  onAbort,
57
57
  action,
58
58
  state,
59
- redirectedFrom
59
+ redirectedFrom,
60
+ skipGuards
60
61
  });
61
62
  }
62
63
  });
@@ -73,7 +74,7 @@ class BaseHistory {
73
74
  url: this.resolve(nextLocation).href
74
75
  });
75
76
  this._updateState(action);
76
- }, onAbort);
77
+ }, onAbort, skipGuards);
77
78
  }
78
79
  _updateState(nextAction) {
79
80
  // update state
@@ -13,8 +13,8 @@ export default class MemoryHistory extends BaseHistory {
13
13
  private _entries;
14
14
  constructor({ initialEntries, initialIndex }?: MemoryHistoryOptions);
15
15
  setup(): void;
16
- push(to: PathRecord, { state, redirectedFrom }?: PushOptions): void;
17
- replace(to: PathRecord, { state, redirectedFrom }?: PushOptions): void;
16
+ push(to: PathRecord, { state, redirectedFrom, skipGuards }?: PushOptions): void;
17
+ replace(to: PathRecord, { state, redirectedFrom, skipGuards }?: PushOptions): void;
18
18
  go(delta: number): void;
19
19
  block(blocker: Blocker): () => void;
20
20
  resolve(to: PathRecord, from?: string): ResolvedPath;
@@ -20,21 +20,23 @@ class MemoryHistory extends base_1.default {
20
20
  setup() {
21
21
  // do nothing
22
22
  }
23
- push(to, { state, redirectedFrom } = {}) {
23
+ push(to, { state, redirectedFrom, skipGuards } = {}) {
24
24
  return this.transitionTo(to, {
25
25
  state,
26
26
  redirectedFrom,
27
+ skipGuards,
27
28
  onTransition: ({ location }) => {
28
29
  this._index += 1;
29
30
  this._entries.splice(this._index, this._entries.length, location);
30
31
  }
31
32
  });
32
33
  }
33
- replace(to, { state, redirectedFrom } = {}) {
34
+ replace(to, { state, redirectedFrom, skipGuards } = {}) {
34
35
  return this.transitionTo(to, {
35
36
  state,
36
37
  action: base_1.ACTION_REPLACE,
37
38
  redirectedFrom,
39
+ skipGuards,
38
40
  onTransition: ({ location }) => {
39
41
  this._entries[this._index] = location;
40
42
  }
package/lib/router.js CHANGED
@@ -124,7 +124,7 @@ class Router {
124
124
  5. Emit change event(trigger react update)
125
125
  6. Call router.afterEach
126
126
  */
127
- _doTransition(to, onComplete, onAbort) {
127
+ _doTransition(to, onComplete, onAbort, skipGuards) {
128
128
  const nextRoute = this._getNextRoute(to);
129
129
  const current = this._current;
130
130
  const nextMatches = nextRoute.matches;
@@ -135,7 +135,9 @@ class Router {
135
135
  });
136
136
  }
137
137
  const routeContext = new Map();
138
- const queue = [].concat(this._beforeEachs.toArray(), this._beforeResolves.toArray());
138
+ const queue = skipGuards
139
+ ? []
140
+ : [].concat(this._beforeEachs.toArray(), this._beforeResolves.toArray());
139
141
  let cancel = false;
140
142
  this._cancleHandler = () => {
141
143
  cancel = true;
@@ -172,14 +174,22 @@ class Router {
172
174
  abort();
173
175
  if (typeof to === 'object') {
174
176
  if (to.replace) {
175
- this.replace(to.path);
177
+ this._history.replace(to.path, {
178
+ redirectedFrom: current,
179
+ skipGuards: to.skipGuards,
180
+ state: to.state
181
+ });
176
182
  }
177
183
  else {
178
- this.push(to.path);
184
+ this._history.push(to.path, {
185
+ redirectedFrom: current,
186
+ skipGuards: to.skipGuards,
187
+ state: to.state
188
+ });
179
189
  }
180
190
  }
181
191
  else {
182
- this.push(to);
192
+ this._history.push(to, { redirectedFrom: current });
183
193
  }
184
194
  }
185
195
  else {
@@ -16,6 +16,8 @@ export interface NavigationGuardNext {
16
16
  (location: string | {
17
17
  path?: string;
18
18
  replace?: boolean;
19
+ skipGuards?: boolean;
20
+ state?: any;
19
21
  }): void;
20
22
  (valid: boolean | undefined): void;
21
23
  (cb: NavigationGuardNextCallback): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/router",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -28,7 +28,7 @@
28
28
  "node": ">= 12.0.0"
29
29
  },
30
30
  "dependencies": {
31
- "@shuvi/utils": "1.0.0",
31
+ "@shuvi/utils": "1.0.2",
32
32
  "query-string": "6.13.8"
33
33
  }
34
34
  }