@thi.ng/router 4.0.0 → 4.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.
- package/CHANGELOG.md +7 -1
- package/README.md +1 -1
- package/api.d.ts +9 -20
- package/html.js +2 -2
- package/package.json +3 -3
- package/router.js +9 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-03-
|
|
3
|
+
- **Last updated**: 2024-03-13T15:51:27Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
### [4.0.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/router@4.0.1) (2024-03-13)
|
|
13
|
+
|
|
14
|
+
#### ♻️ Refactoring
|
|
15
|
+
|
|
16
|
+
- update/simplify AugmentedRoute handling ([37ae88b](https://github.com/thi-ng/umbrella/commit/37ae88b))
|
|
17
|
+
|
|
12
18
|
# [4.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/router@4.0.0) (2024-03-13)
|
|
13
19
|
|
|
14
20
|
#### 🛑 Breaking changes
|
package/README.md
CHANGED
package/api.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type { EVENT_ALL, Fn, IObjectOf } from "@thi.ng/api";
|
|
|
11
11
|
* {@link RouterOpts.default}).
|
|
12
12
|
*
|
|
13
13
|
* The optional `ctx` is an arbitrary user provided context value given to
|
|
14
|
-
* {@link
|
|
14
|
+
* {@link Router.route} (e.g. the original request object).
|
|
15
15
|
*/
|
|
16
16
|
export type RouteAuthenticator<T = any> = (match: RouteMatch, route: AugmentedRoute, ctx?: T) => RouteMatch | undefined;
|
|
17
17
|
/**
|
|
@@ -38,9 +38,9 @@ export interface RouteParamValidator {
|
|
|
38
38
|
export interface Route {
|
|
39
39
|
/**
|
|
40
40
|
* Unique ID for this route. This value will be returned as part of a
|
|
41
|
-
* {@link RouteMatch} resulting from {@link
|
|
42
|
-
* to look up routes in {@link
|
|
43
|
-
* {@link
|
|
41
|
+
* {@link RouteMatch} resulting from {@link Router.route} and also used
|
|
42
|
+
* to look up routes in {@link Router.routeForID} and
|
|
43
|
+
* {@link Router.format}.
|
|
44
44
|
*/
|
|
45
45
|
id: string;
|
|
46
46
|
/**
|
|
@@ -81,20 +81,9 @@ export interface Route {
|
|
|
81
81
|
* processed.
|
|
82
82
|
*/
|
|
83
83
|
auth?: boolean;
|
|
84
|
-
/**
|
|
85
|
-
* Reserved property.
|
|
86
|
-
*
|
|
87
|
-
* @internal used by {@link AugmentedRoute}
|
|
88
|
-
*/
|
|
89
|
-
rest?: never;
|
|
90
|
-
/**
|
|
91
|
-
* Reserved property.
|
|
92
|
-
*
|
|
93
|
-
* @internal used by {@link AugmentedRoute}
|
|
94
|
-
*/
|
|
95
|
-
params?: never;
|
|
96
84
|
}
|
|
97
|
-
export interface AugmentedRoute
|
|
85
|
+
export interface AugmentedRoute {
|
|
86
|
+
spec: Route;
|
|
98
87
|
match: string[];
|
|
99
88
|
params?: Record<number, string>;
|
|
100
89
|
rest: number;
|
|
@@ -129,7 +118,7 @@ export interface RouteMatch {
|
|
|
129
118
|
redirect?: true;
|
|
130
119
|
}
|
|
131
120
|
/**
|
|
132
|
-
* Configuration object for {@link
|
|
121
|
+
* Configuration object for {@link Router} and {@link HTMLRouter}
|
|
133
122
|
* instances.
|
|
134
123
|
*/
|
|
135
124
|
export interface RouterOpts<T = any> {
|
|
@@ -166,8 +155,8 @@ export interface RouterOpts<T = any> {
|
|
|
166
155
|
separator?: string;
|
|
167
156
|
/**
|
|
168
157
|
* Route prefix. Default: `/`. All routes to be parsed by
|
|
169
|
-
* {@link
|
|
170
|
-
* returned by {@link
|
|
158
|
+
* {@link Router.route} are assumed to have this prefix. All routes
|
|
159
|
+
* returned by {@link Router.format} will include this prefix.
|
|
171
160
|
*
|
|
172
161
|
* @remarks
|
|
173
162
|
* If given, the prefix MUST end with {@link RouterOpts.separator}.
|
package/html.js
CHANGED
|
@@ -18,7 +18,7 @@ class HTMLRouter extends Router {
|
|
|
18
18
|
}
|
|
19
19
|
if (this.opts.initial) {
|
|
20
20
|
const route = this.routeForID(this.opts.initial);
|
|
21
|
-
this.route(this.format({ id: route.id }));
|
|
21
|
+
this.route(this.format({ id: route.spec.id }));
|
|
22
22
|
} else {
|
|
23
23
|
this.route(this.useFragment ? location.hash : location.pathname);
|
|
24
24
|
}
|
|
@@ -80,7 +80,7 @@ class HTMLRouter extends Router {
|
|
|
80
80
|
handleRouteFailure() {
|
|
81
81
|
this.ignoreHashChange = true;
|
|
82
82
|
location.hash = this.format({
|
|
83
|
-
id: this.routeForID(this.opts.default).id
|
|
83
|
+
id: this.routeForID(this.opts.default).spec.id
|
|
84
84
|
});
|
|
85
85
|
this.ignoreHashChange = false;
|
|
86
86
|
return true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/router",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "Generic trie-based router with support for wildcards, route param validation/coercion, auth",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@thi.ng/api": "^8.9.30",
|
|
40
40
|
"@thi.ng/checks": "^3.5.3",
|
|
41
41
|
"@thi.ng/equiv": "^2.1.52",
|
|
42
|
-
"@thi.ng/errors": "^2.5.
|
|
42
|
+
"@thi.ng/errors": "^2.5.1",
|
|
43
43
|
"tslib": "^2.6.2"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
@@ -98,5 +98,5 @@
|
|
|
98
98
|
],
|
|
99
99
|
"year": 2014
|
|
100
100
|
},
|
|
101
|
-
"gitHead": "
|
|
101
|
+
"gitHead": "bc0f3cb07d6f1cab6dbdc5ff57428f5484e711bb\n"
|
|
102
102
|
}
|
package/router.js
CHANGED
|
@@ -62,7 +62,7 @@ let Router = class {
|
|
|
62
62
|
start() {
|
|
63
63
|
if (this.opts.initial) {
|
|
64
64
|
const route = this.routeForID(this.opts.initial);
|
|
65
|
-
this.current = { id: route.id, params: {} };
|
|
65
|
+
this.current = { id: route.spec.id, params: {} };
|
|
66
66
|
this.notify({ id: EVENT_ROUTE_CHANGED, value: this.current });
|
|
67
67
|
}
|
|
68
68
|
}
|
|
@@ -71,7 +71,7 @@ let Router = class {
|
|
|
71
71
|
try {
|
|
72
72
|
const route = this.augmentRoute(r);
|
|
73
73
|
this.routes.set(route.match, route);
|
|
74
|
-
this.index[
|
|
74
|
+
this.index[r.id] = route;
|
|
75
75
|
} catch (e) {
|
|
76
76
|
illegalArgs(
|
|
77
77
|
`error in route "${r.id}": ${e.origMessage}`
|
|
@@ -104,7 +104,7 @@ let Router = class {
|
|
|
104
104
|
return;
|
|
105
105
|
}
|
|
106
106
|
const route = this.routeForID(this.opts.default);
|
|
107
|
-
match = { id: route.id, redirect: true };
|
|
107
|
+
match = { id: route.spec.id, redirect: true };
|
|
108
108
|
}
|
|
109
109
|
if (!equiv(match, this.current)) {
|
|
110
110
|
this.current = match;
|
|
@@ -157,7 +157,7 @@ let Router = class {
|
|
|
157
157
|
const existing = this.routes.get(match);
|
|
158
158
|
if (existing) {
|
|
159
159
|
illegalArgs(
|
|
160
|
-
`duplicate route: ${match} (id: ${route.id}, conflicts with: ${existing.id})`
|
|
160
|
+
`duplicate route: ${match} (id: ${route.id}, conflicts with: ${existing.spec.id})`
|
|
161
161
|
);
|
|
162
162
|
}
|
|
163
163
|
let hasParams = false;
|
|
@@ -169,7 +169,7 @@ let Router = class {
|
|
|
169
169
|
return acc;
|
|
170
170
|
}, {});
|
|
171
171
|
return {
|
|
172
|
-
|
|
172
|
+
spec: route,
|
|
173
173
|
match,
|
|
174
174
|
params: hasParams ? params : void 0,
|
|
175
175
|
rest: match.indexOf("+")
|
|
@@ -187,16 +187,17 @@ let Router = class {
|
|
|
187
187
|
{}
|
|
188
188
|
);
|
|
189
189
|
}
|
|
190
|
-
|
|
190
|
+
const validator = route.spec.validate;
|
|
191
|
+
if (validator && !this.validateRouteParams(params, validator)) {
|
|
191
192
|
return;
|
|
192
193
|
}
|
|
193
194
|
const rest = route.rest >= 0 ? curr.slice(route.rest) : void 0;
|
|
194
195
|
let match = {
|
|
195
|
-
id: route.id,
|
|
196
|
+
id: route.spec.id,
|
|
196
197
|
params,
|
|
197
198
|
rest
|
|
198
199
|
};
|
|
199
|
-
if (route.auth) {
|
|
200
|
+
if (route.spec.auth) {
|
|
200
201
|
match = this.opts.authenticator(match, route, ctx);
|
|
201
202
|
if (match && !this.index[match.id]) {
|
|
202
203
|
illegalState(
|