mobx-route 0.0.73 → 0.0.75
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/core/route/route.d.ts +14 -14
- package/core/route/route.d.ts.map +1 -1
- package/core/route/route.js +41 -33
- package/core/route/route.test.js +21 -21
- package/core/route/route.types.d.ts +19 -12
- package/core/route/route.types.d.ts.map +1 -1
- package/package.json +3 -3
- package/view-model/route-view-model.d.ts +1 -1
- package/view-model/route-view-model.d.ts.map +1 -1
package/core/route/route.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { History, IQueryParams } from 'mobx-location-history';
|
|
2
2
|
import { ParamData, TokenData } from 'path-to-regexp';
|
|
3
|
-
import { AllPropertiesOptional, AnyObject, Maybe } from 'yummies/utils/types';
|
|
4
|
-
import { AnyRoute,
|
|
3
|
+
import { AllPropertiesOptional, AnyObject, Maybe, MaybePromise } from 'yummies/utils/types';
|
|
4
|
+
import { AnyRoute, BeforeEnterFeedback, PreparedNavigationData, ExtractPathParams, RouteConfiguration, ParsedPathData, RouteNavigateParams } from './route.types.js';
|
|
5
5
|
/**
|
|
6
6
|
* Class for creating path based route.
|
|
7
7
|
*
|
|
8
8
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html)
|
|
9
9
|
*/
|
|
10
|
-
export declare class Route<TPath extends string, TParentRoute extends Route<any, any> | null = null>
|
|
10
|
+
export declare class Route<TPath extends string, TParams extends AnyObject = ExtractPathParams<TPath>, TParentRoute extends Route<any, any, any> | null = null> {
|
|
11
11
|
path: TPath;
|
|
12
|
-
protected config: RouteConfiguration<TPath, TParentRoute>;
|
|
12
|
+
protected config: RouteConfiguration<TPath, TParams, TParentRoute>;
|
|
13
13
|
protected abortController: AbortController;
|
|
14
14
|
protected history: History;
|
|
15
15
|
parent: TParentRoute;
|
|
@@ -30,9 +30,9 @@ export declare class Route<TPath extends string, TParentRoute extends Route<any,
|
|
|
30
30
|
*/
|
|
31
31
|
isHash: boolean;
|
|
32
32
|
children: AnyRoute[];
|
|
33
|
-
constructor(path: TPath, config?: RouteConfiguration<TPath, TParentRoute>);
|
|
33
|
+
constructor(path: TPath, config?: RouteConfiguration<TPath, TParams, TParentRoute>);
|
|
34
34
|
protected get baseUrl(): string | undefined;
|
|
35
|
-
protected get
|
|
35
|
+
protected get parsedPathData(): ParsedPathData<TPath> | null;
|
|
36
36
|
/**
|
|
37
37
|
* Matched path segment for current URL.
|
|
38
38
|
*
|
|
@@ -44,7 +44,7 @@ export declare class Route<TPath extends string, TParentRoute extends Route<any,
|
|
|
44
44
|
*
|
|
45
45
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#params-parsedpathparams-null)
|
|
46
46
|
*/
|
|
47
|
-
get params():
|
|
47
|
+
get params(): TParams | null;
|
|
48
48
|
/**
|
|
49
49
|
* Defines the "open" state for this route.
|
|
50
50
|
*
|
|
@@ -56,7 +56,7 @@ export declare class Route<TPath extends string, TParentRoute extends Route<any,
|
|
|
56
56
|
*
|
|
57
57
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#extend-path-config-route)
|
|
58
58
|
*/
|
|
59
|
-
extend<TExtendPath extends string
|
|
59
|
+
extend<TExtendPath extends string, TParams extends AnyObject = ExtractPathParams<`${TPath}${TExtendPath}`>>(path: TExtendPath, config?: Omit<RouteConfiguration<`${TPath}${TExtendPath}`, TParams, any>, 'parent'>): Route<`${TPath}${TExtendPath}`, TParams, this>;
|
|
60
60
|
addChildren(...routes: AnyRoute[]): void;
|
|
61
61
|
removeChildren(...routes: AnyRoute[]): void;
|
|
62
62
|
/**
|
|
@@ -73,7 +73,7 @@ export declare class Route<TPath extends string, TParentRoute extends Route<any,
|
|
|
73
73
|
open(...args: AllPropertiesOptional<ExtractPathParams<TPath>> extends true ? [
|
|
74
74
|
params?: ExtractPathParams<TPath> | null | undefined,
|
|
75
75
|
navigateParams?: RouteNavigateParams
|
|
76
|
-
] : [params: ExtractPathParams<TPath>, navigateParams?: RouteNavigateParams]):
|
|
76
|
+
] : [params: ExtractPathParams<TPath>, navigateParams?: RouteNavigateParams]): Promise<void>;
|
|
77
77
|
open(...args: AllPropertiesOptional<ExtractPathParams<TPath>> extends true ? [
|
|
78
78
|
params?: ExtractPathParams<TPath> | null | undefined,
|
|
79
79
|
replace?: RouteNavigateParams['replace'],
|
|
@@ -82,11 +82,11 @@ export declare class Route<TPath extends string, TParentRoute extends Route<any,
|
|
|
82
82
|
params: ExtractPathParams<TPath>,
|
|
83
83
|
replace?: RouteNavigateParams['replace'],
|
|
84
84
|
query?: RouteNavigateParams['query']
|
|
85
|
-
]):
|
|
86
|
-
open(url: string, navigateParams?: RouteNavigateParams):
|
|
87
|
-
open(url: string, replace?: RouteNavigateParams['replace'], query?: RouteNavigateParams['query']):
|
|
88
|
-
protected beforeOpen(openData:
|
|
89
|
-
|
|
85
|
+
]): Promise<void>;
|
|
86
|
+
open(url: string, navigateParams?: RouteNavigateParams): Promise<void>;
|
|
87
|
+
open(url: string, replace?: RouteNavigateParams['replace'], query?: RouteNavigateParams['query']): Promise<void>;
|
|
88
|
+
protected beforeOpen(openData: PreparedNavigationData): MaybePromise<BeforeEnterFeedback>;
|
|
89
|
+
protected afterClose(): true | void;
|
|
90
90
|
protected get tokenData(): TokenData;
|
|
91
91
|
destroy(): void;
|
|
92
92
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../src/core/route/route.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../src/core/route/route.ts"],"names":[],"mappings":"AAUA,OAAO,EAEL,OAAO,EACP,YAAY,EACb,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAkB,SAAS,EAAS,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EACL,qBAAqB,EACrB,SAAS,EACT,KAAK,EACL,YAAY,EACb,MAAM,qBAAqB,CAAC;AAI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C,KAAK,CAAC,CAAC,SAAS,IAAI,GACjE;QACE,MAAM,CAAC,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,IAAI,GAAG,SAAS;QACpD,OAAO,CAAC,EAAE,mBAAmB,CAAC,SAAS,CAAC;QACxC,KAAK,CAAC,EAAE,mBAAmB,CAAC,OAAO,CAAC;KACrC,GACD;QACE,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC;QAChC,OAAO,CAAC,EAAE,mBAAmB,CAAC,SAAS,CAAC;QACxC,KAAK,CAAC,EAAE,mBAAmB,CAAC,OAAO,CAAC;KACrC,GACJ,OAAO,CAAC,IAAI,CAAC;IAChB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,cAAc,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IACtE,IAAI,CACF,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,mBAAmB,CAAC,SAAS,CAAC,EACxC,KAAK,CAAC,EAAE,mBAAmB,CAAC,OAAO,CAAC,GACnC,OAAO,CAAC,IAAI,CAAC;IAoDhB,SAAS,CAAC,UAAU,CAClB,QAAQ,EAAE,sBAAsB,GAC/B,YAAY,CAAC,mBAAmB,CAAC;IAQpC,SAAS,CAAC,UAAU;IAQpB,SAAS,KAAK,SAAS,cAKtB;IAED,OAAO;CAGR"}
|
package/core/route/route.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable prefer-const */
|
|
2
2
|
import { LinkedAbortController } from 'linked-abort-controller';
|
|
3
|
-
import { action, computed, makeObservable, observable, reaction } from 'mobx';
|
|
3
|
+
import { action, computed, makeObservable, observable, reaction, when, } from 'mobx';
|
|
4
4
|
import { buildSearchString, } from 'mobx-location-history';
|
|
5
5
|
import { compile, match, parse } from 'path-to-regexp';
|
|
6
6
|
import { routeConfig } from '../config/config.js';
|
|
@@ -9,7 +9,7 @@ import { routeConfig } from '../config/config.js';
|
|
|
9
9
|
*
|
|
10
10
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html)
|
|
11
11
|
*/
|
|
12
|
-
export class Route
|
|
12
|
+
export class Route {
|
|
13
13
|
path;
|
|
14
14
|
config;
|
|
15
15
|
abortController;
|
|
@@ -33,7 +33,6 @@ export class Route extends String {
|
|
|
33
33
|
isHash;
|
|
34
34
|
children = [];
|
|
35
35
|
constructor(path, config = {}) {
|
|
36
|
-
super(path);
|
|
37
36
|
this.path = path;
|
|
38
37
|
this.config = config;
|
|
39
38
|
this.abortController = new LinkedAbortController(config.abortSignal);
|
|
@@ -53,6 +52,9 @@ export class Route extends String {
|
|
|
53
52
|
action(this, 'addChildren');
|
|
54
53
|
action(this, 'removeChildren');
|
|
55
54
|
makeObservable(this);
|
|
55
|
+
if (this.config.afterClose) {
|
|
56
|
+
when(() => !this.isOpened, this.config.afterClose);
|
|
57
|
+
}
|
|
56
58
|
if (config.onOpen || config.onClose) {
|
|
57
59
|
let firstReactionCall = true;
|
|
58
60
|
reaction(() => this.isOpened, (isOpened) => {
|
|
@@ -64,7 +66,7 @@ export class Route extends String {
|
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
68
|
if (isOpened) {
|
|
67
|
-
config.onOpen?.(this.
|
|
69
|
+
config.onOpen?.(this.parsedPathData, this);
|
|
68
70
|
}
|
|
69
71
|
else {
|
|
70
72
|
config.onClose?.();
|
|
@@ -79,7 +81,7 @@ export class Route extends String {
|
|
|
79
81
|
const baseUrl = this.config.baseUrl ?? routeConfig.get().baseUrl;
|
|
80
82
|
return baseUrl?.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
|
81
83
|
}
|
|
82
|
-
get
|
|
84
|
+
get parsedPathData() {
|
|
83
85
|
let pathnameToCheck;
|
|
84
86
|
if (this.isHash) {
|
|
85
87
|
pathnameToCheck = this.history.location.hash.slice(1);
|
|
@@ -110,7 +112,7 @@ export class Route extends String {
|
|
|
110
112
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#currentpath-parsedpathname-null)
|
|
111
113
|
*/
|
|
112
114
|
get currentPath() {
|
|
113
|
-
return this.
|
|
115
|
+
return this.parsedPathData?.path ?? null;
|
|
114
116
|
}
|
|
115
117
|
/**
|
|
116
118
|
* Current parsed path parameters.
|
|
@@ -118,7 +120,20 @@ export class Route extends String {
|
|
|
118
120
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#params-parsedpathparams-null)
|
|
119
121
|
*/
|
|
120
122
|
get params() {
|
|
121
|
-
|
|
123
|
+
if (!this.parsedPathData?.params) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
let params = this.parsedPathData?.params ?? null;
|
|
127
|
+
if (this.config.params) {
|
|
128
|
+
const result = this.config.params(this.parsedPathData.params);
|
|
129
|
+
if (result) {
|
|
130
|
+
params = result;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return params;
|
|
122
137
|
}
|
|
123
138
|
/**
|
|
124
139
|
* Defines the "open" state for this route.
|
|
@@ -126,10 +141,10 @@ export class Route extends String {
|
|
|
126
141
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopened-boolean)
|
|
127
142
|
*/
|
|
128
143
|
get isOpened() {
|
|
129
|
-
if (this.
|
|
144
|
+
if (this.parsedPathData === null) {
|
|
130
145
|
return false;
|
|
131
146
|
}
|
|
132
|
-
return !this.config.checkOpened || this.config.checkOpened(this.
|
|
147
|
+
return (!this.config.checkOpened || this.config.checkOpened(this.parsedPathData));
|
|
133
148
|
}
|
|
134
149
|
/**
|
|
135
150
|
* Allows to create child route based on this route with merging this route path and extending path.
|
|
@@ -138,7 +153,7 @@ export class Route extends String {
|
|
|
138
153
|
*/
|
|
139
154
|
extend(path, config) {
|
|
140
155
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
141
|
-
const { index, ...configFromCurrentRoute } = this.config;
|
|
156
|
+
const { index, params, ...configFromCurrentRoute } = this.config;
|
|
142
157
|
const extendedChild = new Route(`${this.path}${path}`, {
|
|
143
158
|
...configFromCurrentRoute,
|
|
144
159
|
...config,
|
|
@@ -186,7 +201,7 @@ export class Route extends String {
|
|
|
186
201
|
*
|
|
187
202
|
* [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#open-args)
|
|
188
203
|
*/
|
|
189
|
-
open(...args) {
|
|
204
|
+
async open(...args) {
|
|
190
205
|
let { replace, state: rawState, query, } = typeof args[1] === 'boolean' || args.length > 2
|
|
191
206
|
? { replace: args[1], query: args[2] }
|
|
192
207
|
: (args[1] ?? {});
|
|
@@ -200,19 +215,25 @@ export class Route extends String {
|
|
|
200
215
|
url = this.createUrl(args[0], query);
|
|
201
216
|
}
|
|
202
217
|
let state = rawState ?? null;
|
|
203
|
-
const
|
|
218
|
+
const navigationData = {
|
|
204
219
|
url,
|
|
205
220
|
params: params,
|
|
206
221
|
replace,
|
|
207
222
|
state,
|
|
208
223
|
query,
|
|
209
224
|
};
|
|
210
|
-
const
|
|
211
|
-
if (
|
|
212
|
-
return
|
|
225
|
+
const feedback = await this.beforeOpen(navigationData);
|
|
226
|
+
if (feedback === false) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (typeof feedback === 'object') {
|
|
230
|
+
Object.assign(navigationData, feedback);
|
|
231
|
+
}
|
|
232
|
+
if (replace) {
|
|
233
|
+
this.history.replace(url, state);
|
|
213
234
|
}
|
|
214
235
|
else {
|
|
215
|
-
this.
|
|
236
|
+
this.history.push(url, state);
|
|
216
237
|
}
|
|
217
238
|
}
|
|
218
239
|
beforeOpen(openData) {
|
|
@@ -221,24 +242,11 @@ export class Route extends String {
|
|
|
221
242
|
}
|
|
222
243
|
return true;
|
|
223
244
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
let state = openData.state;
|
|
228
|
-
if (checkResult === false) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (typeof checkResult === 'object') {
|
|
232
|
-
url = checkResult.url;
|
|
233
|
-
replace = checkResult.replace ?? openData.replace;
|
|
234
|
-
state = checkResult.state ?? openData.state;
|
|
235
|
-
}
|
|
236
|
-
if (replace) {
|
|
237
|
-
this.history.replace(url, state);
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
this.history.push(url, state);
|
|
245
|
+
afterClose() {
|
|
246
|
+
if (this.config.afterClose) {
|
|
247
|
+
return this.config.afterClose();
|
|
241
248
|
}
|
|
249
|
+
return true;
|
|
242
250
|
}
|
|
243
251
|
get tokenData() {
|
|
244
252
|
if (!this._tokenData) {
|
package/core/route/route.test.js
CHANGED
|
@@ -29,18 +29,18 @@ describe('route', () => {
|
|
|
29
29
|
beforeEach(() => {
|
|
30
30
|
history.clearMocks();
|
|
31
31
|
});
|
|
32
|
-
it('empty string', () => {
|
|
32
|
+
it('empty string', async () => {
|
|
33
33
|
const route = new Route('');
|
|
34
34
|
expect(route.isOpened).toBe(true);
|
|
35
35
|
});
|
|
36
|
-
it('/test', () => {
|
|
36
|
+
it('/test', async () => {
|
|
37
37
|
const route = new Route('/test');
|
|
38
|
-
route.open();
|
|
38
|
+
await route.open();
|
|
39
39
|
expect(history.spies.push).toBeCalledWith('/test', null);
|
|
40
40
|
});
|
|
41
|
-
it('/test/:id/:bar{/:bar3}', () => {
|
|
41
|
+
it('/test/:id/:bar{/:bar3}', async () => {
|
|
42
42
|
const route = new Route('/test/:id/:bar{/:bar3}');
|
|
43
|
-
route.open({
|
|
43
|
+
await route.open({
|
|
44
44
|
id: 1,
|
|
45
45
|
bar: 'barg',
|
|
46
46
|
});
|
|
@@ -48,34 +48,34 @@ describe('route', () => {
|
|
|
48
48
|
expectTypeOf(route.open).toBeFunction();
|
|
49
49
|
expectTypeOf(route.open).parameter(0).toEqualTypeOf();
|
|
50
50
|
});
|
|
51
|
-
it('/test/*splat', () => {
|
|
51
|
+
it('/test/*splat', async () => {
|
|
52
52
|
const route = new Route('/test/*splat');
|
|
53
|
-
route.open({
|
|
53
|
+
await route.open({
|
|
54
54
|
splat: [1, 2, 3],
|
|
55
55
|
});
|
|
56
56
|
expect(history.spies.push).toBeCalledWith('/test/1/2/3', null);
|
|
57
57
|
});
|
|
58
|
-
it('/users{/:id}/delete', () => {
|
|
58
|
+
it('/users{/:id}/delete', async () => {
|
|
59
59
|
const route = new Route('/users{/:id}/delete');
|
|
60
|
-
route.open({
|
|
60
|
+
await route.open({
|
|
61
61
|
id: 1,
|
|
62
62
|
});
|
|
63
63
|
expect(history.spies.push).toBeCalledWith('/users/1/delete', null);
|
|
64
64
|
history.clearMocks();
|
|
65
|
-
route.open();
|
|
65
|
+
await route.open();
|
|
66
66
|
expect(history.spies.push).toBeCalledWith('/users/delete', null);
|
|
67
67
|
history.clearMocks();
|
|
68
68
|
const childRoute = route.extend('/push/:id1{/:bar}');
|
|
69
|
-
childRoute.open({
|
|
69
|
+
await childRoute.open({
|
|
70
70
|
id1: 1,
|
|
71
71
|
bar: 2,
|
|
72
72
|
id: 3,
|
|
73
73
|
});
|
|
74
74
|
expect(history.spies.push).toBeCalledWith('/users/3/delete/push/1/2', null);
|
|
75
75
|
});
|
|
76
|
-
it('/posts{/:slug}/*rest', () => {
|
|
76
|
+
it('/posts{/:slug}/*rest', async () => {
|
|
77
77
|
const route = new Route('/posts{/:slug}/*rest');
|
|
78
|
-
route.open({
|
|
78
|
+
await route.open({
|
|
79
79
|
slug: true,
|
|
80
80
|
rest: [1, 2, 3, 'bar'],
|
|
81
81
|
});
|
|
@@ -94,9 +94,9 @@ describe('route', () => {
|
|
|
94
94
|
},
|
|
95
95
|
});
|
|
96
96
|
});
|
|
97
|
-
it('/test/:id/:bar + baseUrl + query params', () => {
|
|
97
|
+
it('/test/:id/:bar + baseUrl + query params', async () => {
|
|
98
98
|
const route = new Route('/test/:id/:bar', { baseUrl: '/mobx-view-model' });
|
|
99
|
-
route.open({
|
|
99
|
+
await route.open({
|
|
100
100
|
id: 1,
|
|
101
101
|
bar: 'barg',
|
|
102
102
|
}, {
|
|
@@ -105,9 +105,9 @@ describe('route', () => {
|
|
|
105
105
|
expect(history.spies.push).toBeCalledWith('/mobx-view-model/test/1/barg?a=1', null);
|
|
106
106
|
expect(route.isOpened).toBe(true);
|
|
107
107
|
});
|
|
108
|
-
it('/test/:id/:bar + baseUrl + query params + (query params tests)', () => {
|
|
108
|
+
it('/test/:id/:bar + baseUrl + query params + (query params tests)', async () => {
|
|
109
109
|
const route = new Route('/test/:id/:bar', { baseUrl: '/mobx-view-model' });
|
|
110
|
-
route.open({
|
|
110
|
+
await route.open({
|
|
111
111
|
id: 1,
|
|
112
112
|
bar: 'barg',
|
|
113
113
|
}, {
|
|
@@ -117,7 +117,7 @@ describe('route', () => {
|
|
|
117
117
|
expect(location.search).toBe('?a=3&b=1%2C2%2C3');
|
|
118
118
|
expect(route.query.data).toEqual({ a: '3', b: '1,2,3' });
|
|
119
119
|
});
|
|
120
|
-
it('hierarchy test', () => {
|
|
120
|
+
it('hierarchy test', async () => {
|
|
121
121
|
const routes = {
|
|
122
122
|
private: new RouteGroup({
|
|
123
123
|
index: new Route('/', { index: true }),
|
|
@@ -145,7 +145,7 @@ describe('route', () => {
|
|
|
145
145
|
expect(routes.private.isOpened).toBe(true);
|
|
146
146
|
expect(routes.private.routes.matrices.isOpened).toBe(true);
|
|
147
147
|
expect(routes.private.routes.techreview.isOpened).toBe(false);
|
|
148
|
-
routes.private.routes.index.open();
|
|
148
|
+
await routes.private.routes.index.open();
|
|
149
149
|
expect(routes.private.isOpened).toBe(true);
|
|
150
150
|
expect(routes.private.routes.index.isOpened).toBe(true);
|
|
151
151
|
expect(routes.private.routes.techreview.isOpened).toBe(false);
|
|
@@ -153,7 +153,7 @@ describe('route', () => {
|
|
|
153
153
|
expect(location.href).toBe('http://localhost:3000/');
|
|
154
154
|
history.clearMocks();
|
|
155
155
|
});
|
|
156
|
-
it('test with root paths (/, "")', () => {
|
|
156
|
+
it('test with root paths (/, "")', async () => {
|
|
157
157
|
const routes = {
|
|
158
158
|
home: new Route('/'),
|
|
159
159
|
root: new Route(''),
|
|
@@ -169,7 +169,7 @@ describe('route', () => {
|
|
|
169
169
|
expect(routes.root.isOpened).toBe(true);
|
|
170
170
|
expect(routes.projects.routes.index.isOpened).toBe(false);
|
|
171
171
|
});
|
|
172
|
-
it('parent test', () => {
|
|
172
|
+
it('parent test', async () => {
|
|
173
173
|
history.push('/a/b/c', null);
|
|
174
174
|
const routeA = new Route('/a');
|
|
175
175
|
const routeB = routeA.extend('/b');
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { ParseOptions } from 'path-to-regexp';
|
|
2
|
-
import { AnyObject } from 'yummies/utils/types';
|
|
2
|
+
import { AnyObject, MaybePromise } from 'yummies/utils/types';
|
|
3
3
|
import { RouteGlobalConfig } from '../config/config.types.js';
|
|
4
4
|
import type { Route } from './route.js';
|
|
5
|
-
export type
|
|
5
|
+
export type PreparedNavigationData<TParams extends AnyObject = AnyObject> = {
|
|
6
6
|
state?: any;
|
|
7
7
|
/**
|
|
8
8
|
* path parameters
|
|
9
9
|
*
|
|
10
10
|
* can be received from extended routes
|
|
11
11
|
*/
|
|
12
|
-
params?:
|
|
12
|
+
params?: TParams;
|
|
13
13
|
url: string;
|
|
14
14
|
replace?: boolean;
|
|
15
15
|
query?: AnyObject;
|
|
@@ -17,14 +17,19 @@ export type OpenData = {
|
|
|
17
17
|
/**
|
|
18
18
|
* Returning `false` means ignore navigation
|
|
19
19
|
*/
|
|
20
|
-
export type
|
|
20
|
+
export type BeforeEnterFeedback = void | boolean | {
|
|
21
21
|
url: string;
|
|
22
22
|
state?: any;
|
|
23
23
|
replace?: boolean;
|
|
24
24
|
};
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
export type AfterLeaveFeedback = void | boolean | {
|
|
26
|
+
url: string;
|
|
27
|
+
state?: any;
|
|
28
|
+
replace?: boolean;
|
|
29
|
+
};
|
|
30
|
+
export type BeforeOpenHandler<TParams extends AnyObject = AnyObject> = (preparedNavigationData: PreparedNavigationData<TParams>) => MaybePromise<BeforeEnterFeedback>;
|
|
31
|
+
export type AfterCloseHandler = () => void;
|
|
32
|
+
export interface RouteConfiguration<TPath extends string, TParams extends AnyObject = ParsedPathParams<TPath>, TParentRoute extends AnyRoute | null = null> extends Partial<RouteGlobalConfig> {
|
|
28
33
|
abortSignal?: AbortSignal;
|
|
29
34
|
index?: boolean;
|
|
30
35
|
hash?: boolean;
|
|
@@ -32,12 +37,14 @@ export interface RouteConfiguration<TPath extends string, TParentRoute extends A
|
|
|
32
37
|
parseOptions?: ParseOptions;
|
|
33
38
|
parent?: TParentRoute;
|
|
34
39
|
children?: AnyRoute[];
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
params?: (params: ExtractPathParams<TPath>) => TParams | null | false;
|
|
41
|
+
checkOpened?: (parsedPathData: ParsedPathData<NoInfer<TPath>>) => boolean;
|
|
42
|
+
beforeOpen?: BeforeOpenHandler<NoInfer<TParams>>;
|
|
43
|
+
afterClose?: AfterCloseHandler;
|
|
44
|
+
onOpen?: (data: ParsedPathData<NoInfer<TPath>>, route: Route<NoInfer<TPath>, NoInfer<TParams>, NoInfer<TParentRoute>>) => void;
|
|
38
45
|
onClose?: () => void;
|
|
39
46
|
}
|
|
40
|
-
export type AnyRoute = Route<string, any>;
|
|
47
|
+
export type AnyRoute = Route<string, AnyObject, any>;
|
|
41
48
|
export type PathParam = string | number | boolean | null;
|
|
42
49
|
export type PathParsedParam = string;
|
|
43
50
|
type Simplify<T> = T extends infer U ? {
|
|
@@ -62,7 +69,7 @@ export interface RouteNavigateParams {
|
|
|
62
69
|
state?: any;
|
|
63
70
|
query?: AnyObject;
|
|
64
71
|
}
|
|
65
|
-
export interface
|
|
72
|
+
export interface ParsedPathData<TPath extends string> {
|
|
66
73
|
path: string;
|
|
67
74
|
params: ParsedPathParams<TPath>;
|
|
68
75
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.types.d.ts","sourceRoot":"","sources":["../../../src/core/route/route.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"route.types.d.ts","sourceRoot":"","sources":["../../../src/core/route/route.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAE9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAE9D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,MAAM,sBAAsB,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,IAAI;IAC1E,KAAK,CAAC,EAAE,GAAG,CAAC;IAEZ;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB,GAAG,EAAE,MAAM,CAAC;IAEZ,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAC3B,IAAI,GACJ,OAAO,GACP;IACE,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEN,MAAM,MAAM,kBAAkB,GAC1B,IAAI,GACJ,OAAO,GACP;IACE,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEN,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,IAAI,CACrE,sBAAsB,EAAE,sBAAsB,CAAC,OAAO,CAAC,KACpD,YAAY,CAAC,mBAAmB,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AK,IAAI,MAAM,KAAK,EAAE,GAC1C,iBAAiB,CAAC,KAAK,CAAC,GAAG,iBAAiB,CAAC,KAAK,CAAC,GACnD,IAAI,SAAS,IAAI,MAAM,KAAK,GAAG,GAC7B;KAAG,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,SAAS;CAAE,GAC5B,IAAI,SAAS,IAAI,MAAM,KAAK,EAAE,GAC5B;KAAG,CAAC,IAAI,KAAK,GAAG,SAAS;CAAE,GAC3B,IAAI,SAAS,IAAI,MAAM,QAAQ,EAAE,GAC/B;KAAG,CAAC,IAAI,QAAQ,GAAG,SAAS,EAAE;CAAE,GAEhC,EAAE,CACf,CAAC;AAEF,MAAM,WAAW,mBAAmB;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED,MAAM,WAAW,cAAc,CAAC,KAAK,SAAS,MAAM;IAClD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;CACjC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mobx-route",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.75",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"mobx",
|
|
6
6
|
"react",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"mobx-location-history": "^6.1.0",
|
|
27
27
|
"path-to-regexp": "^8.2.0",
|
|
28
28
|
"react-simple-loadable": "^2.1.0",
|
|
29
|
-
"yummies": "^3.2.
|
|
29
|
+
"yummies": "^3.2.10"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"@types/react": "^18.3.20",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"commitfmt": "^1.0.0",
|
|
59
59
|
"eslint": "^8.57.1",
|
|
60
60
|
"js2me-eslint-config": "^1.0.7",
|
|
61
|
-
"js2me-exports-post-build-script": "^
|
|
61
|
+
"js2me-exports-post-build-script": "^3.0.2",
|
|
62
62
|
"jsdom": "^26.1.0",
|
|
63
63
|
"lefthook": "^1.11.13",
|
|
64
64
|
"nodemon": "^3.1.9",
|
|
@@ -6,7 +6,7 @@ export declare abstract class RouteViewModel<TRoute extends AnyRouteEntity = Any
|
|
|
6
6
|
constructor(params: ViewModelParams<any, any>);
|
|
7
7
|
get payload(): TRoute extends Route<string, any> ? ParsedPathParams<TRoute['path']> : TRoute extends VirtualRoute<infer Params> ? Params : EmptyObject;
|
|
8
8
|
get query(): import("mobx-location-history").IQueryParams;
|
|
9
|
-
get pathParams(): TRoute extends Route<string, any> ? ParsedPathParams<TRoute["path"]> : TRoute extends VirtualRoute<infer Params extends AnyObject | EmptyObject> ? Params : EmptyObject;
|
|
9
|
+
get pathParams(): TRoute extends Route<string, any, null> ? ParsedPathParams<TRoute["path"]> : TRoute extends VirtualRoute<infer Params extends AnyObject | EmptyObject> ? Params : EmptyObject;
|
|
10
10
|
get isMounted(): boolean;
|
|
11
11
|
}
|
|
12
12
|
//# sourceMappingURL=route-view-model.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route-view-model.d.ts","sourceRoot":"","sources":["../../src/view-model/route-view-model.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EAEL,KAAK,EACL,gBAAgB,EAChB,cAAc,EACd,YAAY,EACb,MAAM,kBAAkB,CAAC;AAE1B,8BAAsB,cAAc,CAClC,MAAM,SAAS,cAAc,GAAG,cAAc,CAC9C,SAAQ,aAAa,CAAC,SAAS,CAAC;IAChC,QAAQ,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;gBAEpB,MAAM,EAAE,e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