@ktjs/router 0.14.10 → 0.20.0
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/dist/index.d.ts +1 -3
- package/dist/index.iife.js +25 -11
- package/dist/index.legacy.js +49 -40
- package/dist/index.mjs +25 -11
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { KTHTMLElement } from '@ktjs/core';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Guard level that determines which guards to apply during navigation
|
|
5
3
|
* - there are global and route-level guards
|
|
@@ -163,7 +161,7 @@ interface RouteMatch {
|
|
|
163
161
|
*/
|
|
164
162
|
declare function KTRouter({ router }: {
|
|
165
163
|
router: Router;
|
|
166
|
-
}):
|
|
164
|
+
}): JSX.Element;
|
|
167
165
|
|
|
168
166
|
/**
|
|
169
167
|
* Create a new router instance
|
package/dist/index.iife.js
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
var __ktjs_router__ = (function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
// Cached native methods for performance optimization
|
|
5
|
+
|
|
6
|
+
// Error handling utilities
|
|
7
|
+
const $throw = (message) => {
|
|
8
|
+
throw new Error('@ktjs/shared: ' + message);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// String manipulation utilities
|
|
4
12
|
/**
|
|
5
|
-
* Default
|
|
13
|
+
* Default empty function
|
|
14
|
+
*/
|
|
15
|
+
const $emptyFn = (() => true);
|
|
16
|
+
const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwnPropertyDescriptor(HTMLButtonElement.prototype, 'disabled');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Normalize path by joining parts and ensuring leading slash
|
|
6
20
|
*/
|
|
7
|
-
const fn = (() => true);
|
|
8
|
-
const throws = (m) => {
|
|
9
|
-
throw new Error(`@ktjs/router: ${m}`);
|
|
10
|
-
};
|
|
11
21
|
const normalizePath = (...paths) => {
|
|
12
22
|
const p = paths
|
|
13
23
|
.map((p) => p.split('/'))
|
|
@@ -78,6 +88,10 @@ var __ktjs_router__ = (function (exports) {
|
|
|
78
88
|
return params;
|
|
79
89
|
};
|
|
80
90
|
|
|
91
|
+
// Shared utilities and cached native methods for kt.js framework
|
|
92
|
+
// Re-export all utilities
|
|
93
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.0' });
|
|
94
|
+
|
|
81
95
|
/**
|
|
82
96
|
* Route matcher for finding matching routes and extracting params
|
|
83
97
|
*/
|
|
@@ -87,7 +101,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
87
101
|
const route = routes[i];
|
|
88
102
|
if (route.name !== undefined) {
|
|
89
103
|
if (route.name in nameMap) {
|
|
90
|
-
|
|
104
|
+
$throw(`Duplicate route name detected: '${route.name}'`);
|
|
91
105
|
}
|
|
92
106
|
nameMap[route.name] = route;
|
|
93
107
|
}
|
|
@@ -162,7 +176,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
162
176
|
/**
|
|
163
177
|
* Create a new router instance
|
|
164
178
|
*/
|
|
165
|
-
const createRouter = ({ beforeEach =
|
|
179
|
+
const createRouter = ({ beforeEach = $emptyFn, afterEach = $emptyFn, onNotFound = $emptyFn, onError = $emptyFn, prefix = '', routes: rawRoutes, }) => {
|
|
166
180
|
// # private values
|
|
167
181
|
const routes = [];
|
|
168
182
|
const history = [];
|
|
@@ -175,8 +189,8 @@ var __ktjs_router__ = (function (exports) {
|
|
|
175
189
|
path: prefix + path,
|
|
176
190
|
name: route.name ?? '',
|
|
177
191
|
meta: route.meta ?? {},
|
|
178
|
-
beforeEnter: route.beforeEnter ??
|
|
179
|
-
after: route.after ??
|
|
192
|
+
beforeEnter: route.beforeEnter ?? $emptyFn,
|
|
193
|
+
after: route.after ?? $emptyFn,
|
|
180
194
|
children: route.children ? normalize(route.children, path) : [],
|
|
181
195
|
component: route.component,
|
|
182
196
|
};
|
|
@@ -229,7 +243,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
229
243
|
if (options.name) {
|
|
230
244
|
targetRoute = findByName(options.name);
|
|
231
245
|
if (!targetRoute) {
|
|
232
|
-
|
|
246
|
+
$throw(`Route not found: ${options.name}`);
|
|
233
247
|
}
|
|
234
248
|
targetPath = targetRoute.path;
|
|
235
249
|
}
|
|
@@ -238,7 +252,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
238
252
|
targetRoute = match(targetPath)?.route;
|
|
239
253
|
}
|
|
240
254
|
else {
|
|
241
|
-
|
|
255
|
+
$throw(`Either path or name must be provided`);
|
|
242
256
|
}
|
|
243
257
|
// Substitute params
|
|
244
258
|
if (options.params) {
|
package/dist/index.legacy.js
CHANGED
|
@@ -82,20 +82,26 @@ var __ktjs_router__ = (function (exports) {
|
|
|
82
82
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
83
83
|
};
|
|
84
84
|
|
|
85
|
+
// Cached native methods for performance optimization
|
|
86
|
+
|
|
87
|
+
// Error handling utilities
|
|
88
|
+
const $throw = (message) => {
|
|
89
|
+
throw new Error('@ktjs/shared: ' + message);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// String manipulation utilities
|
|
85
93
|
/**
|
|
86
|
-
* Default
|
|
94
|
+
* Default empty function
|
|
87
95
|
*/
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
var p = paths
|
|
98
|
-
.map(function (p) { return p.split('/'); })
|
|
96
|
+
const $emptyFn = (() => true);
|
|
97
|
+
const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwnPropertyDescriptor(HTMLButtonElement.prototype, 'disabled');
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Normalize path by joining parts and ensuring leading slash
|
|
101
|
+
*/
|
|
102
|
+
const normalizePath = (...paths) => {
|
|
103
|
+
const p = paths
|
|
104
|
+
.map((p) => p.split('/'))
|
|
99
105
|
.flat()
|
|
100
106
|
.filter(Boolean);
|
|
101
107
|
return '/' + p.join('/');
|
|
@@ -103,15 +109,14 @@ var __ktjs_router__ = (function (exports) {
|
|
|
103
109
|
/**
|
|
104
110
|
* Parse query string into object
|
|
105
111
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
112
|
+
const parseQuery = (queryString) => {
|
|
113
|
+
const query = {};
|
|
108
114
|
if (!queryString || queryString === '?') {
|
|
109
115
|
return query;
|
|
110
116
|
}
|
|
111
|
-
|
|
112
|
-
for (
|
|
113
|
-
|
|
114
|
-
var _a = param.split('='), key = _a[0], value = _a[1];
|
|
117
|
+
const params = queryString.replace(/^\?/, '').split('&');
|
|
118
|
+
for (const param of params) {
|
|
119
|
+
const [key, value] = param.split('=');
|
|
115
120
|
if (key) {
|
|
116
121
|
query[decodeURIComponent(key)] = value ? decodeURIComponent(value) : '';
|
|
117
122
|
}
|
|
@@ -121,21 +126,21 @@ var __ktjs_router__ = (function (exports) {
|
|
|
121
126
|
/**
|
|
122
127
|
* Build query string from object
|
|
123
128
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
129
|
+
const buildQuery = (query) => {
|
|
130
|
+
const keys = Object.keys(query);
|
|
126
131
|
if (keys.length === 0)
|
|
127
132
|
return '';
|
|
128
|
-
|
|
129
|
-
return
|
|
133
|
+
const params = keys.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`).join('&');
|
|
134
|
+
return `?${params}`;
|
|
130
135
|
};
|
|
131
136
|
/**
|
|
132
137
|
* Substitute params into path pattern
|
|
133
138
|
* @example '/user/:id' + {id: '123'} => '/user/123'
|
|
134
139
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
for (
|
|
138
|
-
result = result.replace(
|
|
140
|
+
const emplaceParams = (path, params) => {
|
|
141
|
+
let result = path;
|
|
142
|
+
for (const key in params) {
|
|
143
|
+
result = result.replace(`:${key}`, params[key]);
|
|
139
144
|
}
|
|
140
145
|
return result;
|
|
141
146
|
};
|
|
@@ -143,18 +148,18 @@ var __ktjs_router__ = (function (exports) {
|
|
|
143
148
|
* Extract dynamic params from path using pattern
|
|
144
149
|
* @example pattern: '/user/:id', path: '/user/123' => {id: '123'}
|
|
145
150
|
*/
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
151
|
+
const extractParams = (pattern, path) => {
|
|
152
|
+
const params = {};
|
|
153
|
+
const patternParts = pattern.split('/');
|
|
154
|
+
const pathParts = path.split('/');
|
|
150
155
|
if (patternParts.length !== pathParts.length) {
|
|
151
156
|
return null;
|
|
152
157
|
}
|
|
153
|
-
for (
|
|
154
|
-
|
|
155
|
-
|
|
158
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
159
|
+
const patternPart = patternParts[i];
|
|
160
|
+
const pathPart = pathParts[i];
|
|
156
161
|
if (patternPart.startsWith(':')) {
|
|
157
|
-
|
|
162
|
+
const paramName = patternPart.slice(1);
|
|
158
163
|
params[paramName] = pathPart;
|
|
159
164
|
}
|
|
160
165
|
else if (patternPart !== pathPart) {
|
|
@@ -164,6 +169,10 @@ var __ktjs_router__ = (function (exports) {
|
|
|
164
169
|
return params;
|
|
165
170
|
};
|
|
166
171
|
|
|
172
|
+
// Shared utilities and cached native methods for kt.js framework
|
|
173
|
+
// Re-export all utilities
|
|
174
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.0' });
|
|
175
|
+
|
|
167
176
|
/**
|
|
168
177
|
* Route matcher for finding matching routes and extracting params
|
|
169
178
|
*/
|
|
@@ -173,7 +182,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
173
182
|
var route = routes[i];
|
|
174
183
|
if (route.name !== undefined) {
|
|
175
184
|
if (route.name in nameMap) {
|
|
176
|
-
|
|
185
|
+
$throw("Duplicate route name detected: '".concat(route.name, "'"));
|
|
177
186
|
}
|
|
178
187
|
nameMap[route.name] = route;
|
|
179
188
|
}
|
|
@@ -253,7 +262,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
253
262
|
* Create a new router instance
|
|
254
263
|
*/
|
|
255
264
|
var createRouter = function (_a) {
|
|
256
|
-
var _b = _a.beforeEach, beforeEach = _b === void 0 ?
|
|
265
|
+
var _b = _a.beforeEach, beforeEach = _b === void 0 ? $emptyFn : _b, _c = _a.afterEach, afterEach = _c === void 0 ? $emptyFn : _c, _d = _a.onNotFound, onNotFound = _d === void 0 ? $emptyFn : _d, _e = _a.onError, onError = _e === void 0 ? $emptyFn : _e, _f = _a.prefix, prefix = _f === void 0 ? '' : _f, rawRoutes = _a.routes;
|
|
257
266
|
// # private values
|
|
258
267
|
var routes = [];
|
|
259
268
|
var history = [];
|
|
@@ -268,8 +277,8 @@ var __ktjs_router__ = (function (exports) {
|
|
|
268
277
|
path: prefix + path,
|
|
269
278
|
name: (_a = route.name) !== null && _a !== void 0 ? _a : '',
|
|
270
279
|
meta: (_b = route.meta) !== null && _b !== void 0 ? _b : {},
|
|
271
|
-
beforeEnter: (_c = route.beforeEnter) !== null && _c !== void 0 ? _c :
|
|
272
|
-
after: (_d = route.after) !== null && _d !== void 0 ? _d :
|
|
280
|
+
beforeEnter: (_c = route.beforeEnter) !== null && _c !== void 0 ? _c : $emptyFn,
|
|
281
|
+
after: (_d = route.after) !== null && _d !== void 0 ? _d : $emptyFn,
|
|
273
282
|
children: route.children ? normalize(route.children, path) : [],
|
|
274
283
|
component: route.component,
|
|
275
284
|
};
|
|
@@ -335,7 +344,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
335
344
|
if (options.name) {
|
|
336
345
|
targetRoute = findByName(options.name);
|
|
337
346
|
if (!targetRoute) {
|
|
338
|
-
|
|
347
|
+
$throw("Route not found: ".concat(options.name));
|
|
339
348
|
}
|
|
340
349
|
targetPath = targetRoute.path;
|
|
341
350
|
}
|
|
@@ -344,7 +353,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
344
353
|
targetRoute = (_a = match(targetPath)) === null || _a === void 0 ? void 0 : _a.route;
|
|
345
354
|
}
|
|
346
355
|
else {
|
|
347
|
-
|
|
356
|
+
$throw("Either path or name must be provided");
|
|
348
357
|
}
|
|
349
358
|
// Substitute params
|
|
350
359
|
if (options.params) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
|
+
// Cached native methods for performance optimization
|
|
2
|
+
|
|
3
|
+
// Error handling utilities
|
|
4
|
+
const $throw = (message) => {
|
|
5
|
+
throw new Error('@ktjs/shared: ' + message);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// String manipulation utilities
|
|
1
9
|
/**
|
|
2
|
-
* Default
|
|
10
|
+
* Default empty function
|
|
11
|
+
*/
|
|
12
|
+
const $emptyFn = (() => true);
|
|
13
|
+
const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwnPropertyDescriptor(HTMLButtonElement.prototype, 'disabled');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Normalize path by joining parts and ensuring leading slash
|
|
3
17
|
*/
|
|
4
|
-
const fn = (() => true);
|
|
5
|
-
const throws = (m) => {
|
|
6
|
-
throw new Error(`@ktjs/router: ${m}`);
|
|
7
|
-
};
|
|
8
18
|
const normalizePath = (...paths) => {
|
|
9
19
|
const p = paths
|
|
10
20
|
.map((p) => p.split('/'))
|
|
@@ -75,6 +85,10 @@ const extractParams = (pattern, path) => {
|
|
|
75
85
|
return params;
|
|
76
86
|
};
|
|
77
87
|
|
|
88
|
+
// Shared utilities and cached native methods for kt.js framework
|
|
89
|
+
// Re-export all utilities
|
|
90
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.20.0' });
|
|
91
|
+
|
|
78
92
|
/**
|
|
79
93
|
* Route matcher for finding matching routes and extracting params
|
|
80
94
|
*/
|
|
@@ -84,7 +98,7 @@ const createMatcher = (routes) => {
|
|
|
84
98
|
const route = routes[i];
|
|
85
99
|
if (route.name !== undefined) {
|
|
86
100
|
if (route.name in nameMap) {
|
|
87
|
-
|
|
101
|
+
$throw(`Duplicate route name detected: '${route.name}'`);
|
|
88
102
|
}
|
|
89
103
|
nameMap[route.name] = route;
|
|
90
104
|
}
|
|
@@ -159,7 +173,7 @@ function KTRouter({ router }) {
|
|
|
159
173
|
/**
|
|
160
174
|
* Create a new router instance
|
|
161
175
|
*/
|
|
162
|
-
const createRouter = ({ beforeEach =
|
|
176
|
+
const createRouter = ({ beforeEach = $emptyFn, afterEach = $emptyFn, onNotFound = $emptyFn, onError = $emptyFn, prefix = '', routes: rawRoutes, }) => {
|
|
163
177
|
// # private values
|
|
164
178
|
const routes = [];
|
|
165
179
|
const history = [];
|
|
@@ -172,8 +186,8 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
172
186
|
path: prefix + path,
|
|
173
187
|
name: route.name ?? '',
|
|
174
188
|
meta: route.meta ?? {},
|
|
175
|
-
beforeEnter: route.beforeEnter ??
|
|
176
|
-
after: route.after ??
|
|
189
|
+
beforeEnter: route.beforeEnter ?? $emptyFn,
|
|
190
|
+
after: route.after ?? $emptyFn,
|
|
177
191
|
children: route.children ? normalize(route.children, path) : [],
|
|
178
192
|
component: route.component,
|
|
179
193
|
};
|
|
@@ -226,7 +240,7 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
226
240
|
if (options.name) {
|
|
227
241
|
targetRoute = findByName(options.name);
|
|
228
242
|
if (!targetRoute) {
|
|
229
|
-
|
|
243
|
+
$throw(`Route not found: ${options.name}`);
|
|
230
244
|
}
|
|
231
245
|
targetPath = targetRoute.path;
|
|
232
246
|
}
|
|
@@ -235,7 +249,7 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
235
249
|
targetRoute = match(targetPath)?.route;
|
|
236
250
|
}
|
|
237
251
|
else {
|
|
238
|
-
|
|
252
|
+
$throw(`Either path or name must be provided`);
|
|
239
253
|
}
|
|
240
254
|
// Substitute params
|
|
241
255
|
if (options.params) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ktjs/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "Router for kt.js - client-side routing with navigation guards",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
"directory": "packages/router"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@ktjs/core": "0.
|
|
34
|
+
"@ktjs/core": "0.20.0",
|
|
35
|
+
"@ktjs/shared": "0.20.0"
|
|
35
36
|
},
|
|
36
37
|
"scripts": {
|
|
37
38
|
"build": "rollup -c rollup.config.mjs",
|