@ktjs/router 0.14.9 → 0.19.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 +1268 -3
- package/dist/index.iife.js +25 -12
- package/dist/index.legacy.js +49 -41
- package/dist/index.mjs +25 -12
- package/package.json +3 -2
package/dist/index.iife.js
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
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
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Normalize path by joining parts and ensuring leading slash
|
|
6
19
|
*/
|
|
7
|
-
const fn = (() => true);
|
|
8
|
-
const throws = (m) => {
|
|
9
|
-
throw new Error(`@ktjs/router: ${m}`);
|
|
10
|
-
};
|
|
11
20
|
const normalizePath = (...paths) => {
|
|
12
21
|
const p = paths
|
|
13
22
|
.map((p) => p.split('/'))
|
|
@@ -78,6 +87,10 @@ var __ktjs_router__ = (function (exports) {
|
|
|
78
87
|
return params;
|
|
79
88
|
};
|
|
80
89
|
|
|
90
|
+
// Shared utilities and cached native methods for kt.js framework
|
|
91
|
+
// Re-export all utilities
|
|
92
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.19.0' });
|
|
93
|
+
|
|
81
94
|
/**
|
|
82
95
|
* Route matcher for finding matching routes and extracting params
|
|
83
96
|
*/
|
|
@@ -87,7 +100,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
87
100
|
const route = routes[i];
|
|
88
101
|
if (route.name !== undefined) {
|
|
89
102
|
if (route.name in nameMap) {
|
|
90
|
-
|
|
103
|
+
$throw(`Duplicate route name detected: '${route.name}'`);
|
|
91
104
|
}
|
|
92
105
|
nameMap[route.name] = route;
|
|
93
106
|
}
|
|
@@ -162,7 +175,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
162
175
|
/**
|
|
163
176
|
* Create a new router instance
|
|
164
177
|
*/
|
|
165
|
-
const createRouter = ({ beforeEach =
|
|
178
|
+
const createRouter = ({ beforeEach = $emptyFn, afterEach = $emptyFn, onNotFound = $emptyFn, onError = $emptyFn, prefix = '', routes: rawRoutes, }) => {
|
|
166
179
|
// # private values
|
|
167
180
|
const routes = [];
|
|
168
181
|
const history = [];
|
|
@@ -175,8 +188,8 @@ var __ktjs_router__ = (function (exports) {
|
|
|
175
188
|
path: prefix + path,
|
|
176
189
|
name: route.name ?? '',
|
|
177
190
|
meta: route.meta ?? {},
|
|
178
|
-
beforeEnter: route.beforeEnter ??
|
|
179
|
-
after: route.after ??
|
|
191
|
+
beforeEnter: route.beforeEnter ?? $emptyFn,
|
|
192
|
+
after: route.after ?? $emptyFn,
|
|
180
193
|
children: route.children ? normalize(route.children, path) : [],
|
|
181
194
|
component: route.component,
|
|
182
195
|
};
|
|
@@ -229,7 +242,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
229
242
|
if (options.name) {
|
|
230
243
|
targetRoute = findByName(options.name);
|
|
231
244
|
if (!targetRoute) {
|
|
232
|
-
|
|
245
|
+
$throw(`Route not found: ${options.name}`);
|
|
233
246
|
}
|
|
234
247
|
targetPath = targetRoute.path;
|
|
235
248
|
}
|
|
@@ -238,7 +251,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
238
251
|
targetRoute = match(targetPath)?.route;
|
|
239
252
|
}
|
|
240
253
|
else {
|
|
241
|
-
|
|
254
|
+
$throw(`Either path or name must be provided`);
|
|
242
255
|
}
|
|
243
256
|
// Substitute params
|
|
244
257
|
if (options.params) {
|
|
@@ -417,7 +430,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
417
430
|
window.history.forward();
|
|
418
431
|
},
|
|
419
432
|
};
|
|
420
|
-
normalize(
|
|
433
|
+
normalize(rawRoutes, '/');
|
|
421
434
|
const { findByName, match } = createMatcher(routes);
|
|
422
435
|
const currentHash = window.location.hash.slice(1);
|
|
423
436
|
if (currentHash) {
|
package/dist/index.legacy.js
CHANGED
|
@@ -82,20 +82,25 @@ 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
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Normalize path by joining parts and ensuring leading slash
|
|
100
|
+
*/
|
|
101
|
+
const normalizePath = (...paths) => {
|
|
102
|
+
const p = paths
|
|
103
|
+
.map((p) => p.split('/'))
|
|
99
104
|
.flat()
|
|
100
105
|
.filter(Boolean);
|
|
101
106
|
return '/' + p.join('/');
|
|
@@ -103,15 +108,14 @@ var __ktjs_router__ = (function (exports) {
|
|
|
103
108
|
/**
|
|
104
109
|
* Parse query string into object
|
|
105
110
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
const parseQuery = (queryString) => {
|
|
112
|
+
const query = {};
|
|
108
113
|
if (!queryString || queryString === '?') {
|
|
109
114
|
return query;
|
|
110
115
|
}
|
|
111
|
-
|
|
112
|
-
for (
|
|
113
|
-
|
|
114
|
-
var _a = param.split('='), key = _a[0], value = _a[1];
|
|
116
|
+
const params = queryString.replace(/^\?/, '').split('&');
|
|
117
|
+
for (const param of params) {
|
|
118
|
+
const [key, value] = param.split('=');
|
|
115
119
|
if (key) {
|
|
116
120
|
query[decodeURIComponent(key)] = value ? decodeURIComponent(value) : '';
|
|
117
121
|
}
|
|
@@ -121,21 +125,21 @@ var __ktjs_router__ = (function (exports) {
|
|
|
121
125
|
/**
|
|
122
126
|
* Build query string from object
|
|
123
127
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
128
|
+
const buildQuery = (query) => {
|
|
129
|
+
const keys = Object.keys(query);
|
|
126
130
|
if (keys.length === 0)
|
|
127
131
|
return '';
|
|
128
|
-
|
|
129
|
-
return
|
|
132
|
+
const params = keys.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`).join('&');
|
|
133
|
+
return `?${params}`;
|
|
130
134
|
};
|
|
131
135
|
/**
|
|
132
136
|
* Substitute params into path pattern
|
|
133
137
|
* @example '/user/:id' + {id: '123'} => '/user/123'
|
|
134
138
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
for (
|
|
138
|
-
result = result.replace(
|
|
139
|
+
const emplaceParams = (path, params) => {
|
|
140
|
+
let result = path;
|
|
141
|
+
for (const key in params) {
|
|
142
|
+
result = result.replace(`:${key}`, params[key]);
|
|
139
143
|
}
|
|
140
144
|
return result;
|
|
141
145
|
};
|
|
@@ -143,18 +147,18 @@ var __ktjs_router__ = (function (exports) {
|
|
|
143
147
|
* Extract dynamic params from path using pattern
|
|
144
148
|
* @example pattern: '/user/:id', path: '/user/123' => {id: '123'}
|
|
145
149
|
*/
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
+
const extractParams = (pattern, path) => {
|
|
151
|
+
const params = {};
|
|
152
|
+
const patternParts = pattern.split('/');
|
|
153
|
+
const pathParts = path.split('/');
|
|
150
154
|
if (patternParts.length !== pathParts.length) {
|
|
151
155
|
return null;
|
|
152
156
|
}
|
|
153
|
-
for (
|
|
154
|
-
|
|
155
|
-
|
|
157
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
158
|
+
const patternPart = patternParts[i];
|
|
159
|
+
const pathPart = pathParts[i];
|
|
156
160
|
if (patternPart.startsWith(':')) {
|
|
157
|
-
|
|
161
|
+
const paramName = patternPart.slice(1);
|
|
158
162
|
params[paramName] = pathPart;
|
|
159
163
|
}
|
|
160
164
|
else if (patternPart !== pathPart) {
|
|
@@ -164,6 +168,10 @@ var __ktjs_router__ = (function (exports) {
|
|
|
164
168
|
return params;
|
|
165
169
|
};
|
|
166
170
|
|
|
171
|
+
// Shared utilities and cached native methods for kt.js framework
|
|
172
|
+
// Re-export all utilities
|
|
173
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.19.0' });
|
|
174
|
+
|
|
167
175
|
/**
|
|
168
176
|
* Route matcher for finding matching routes and extracting params
|
|
169
177
|
*/
|
|
@@ -173,7 +181,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
173
181
|
var route = routes[i];
|
|
174
182
|
if (route.name !== undefined) {
|
|
175
183
|
if (route.name in nameMap) {
|
|
176
|
-
|
|
184
|
+
$throw("Duplicate route name detected: '".concat(route.name, "'"));
|
|
177
185
|
}
|
|
178
186
|
nameMap[route.name] = route;
|
|
179
187
|
}
|
|
@@ -253,7 +261,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
253
261
|
* Create a new router instance
|
|
254
262
|
*/
|
|
255
263
|
var createRouter = function (_a) {
|
|
256
|
-
var _b = _a.beforeEach, beforeEach = _b === void 0 ?
|
|
264
|
+
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
265
|
// # private values
|
|
258
266
|
var routes = [];
|
|
259
267
|
var history = [];
|
|
@@ -268,8 +276,8 @@ var __ktjs_router__ = (function (exports) {
|
|
|
268
276
|
path: prefix + path,
|
|
269
277
|
name: (_a = route.name) !== null && _a !== void 0 ? _a : '',
|
|
270
278
|
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 :
|
|
279
|
+
beforeEnter: (_c = route.beforeEnter) !== null && _c !== void 0 ? _c : $emptyFn,
|
|
280
|
+
after: (_d = route.after) !== null && _d !== void 0 ? _d : $emptyFn,
|
|
273
281
|
children: route.children ? normalize(route.children, path) : [],
|
|
274
282
|
component: route.component,
|
|
275
283
|
};
|
|
@@ -335,7 +343,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
335
343
|
if (options.name) {
|
|
336
344
|
targetRoute = findByName(options.name);
|
|
337
345
|
if (!targetRoute) {
|
|
338
|
-
|
|
346
|
+
$throw("Route not found: ".concat(options.name));
|
|
339
347
|
}
|
|
340
348
|
targetPath = targetRoute.path;
|
|
341
349
|
}
|
|
@@ -344,7 +352,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
344
352
|
targetRoute = (_a = match(targetPath)) === null || _a === void 0 ? void 0 : _a.route;
|
|
345
353
|
}
|
|
346
354
|
else {
|
|
347
|
-
|
|
355
|
+
$throw("Either path or name must be provided");
|
|
348
356
|
}
|
|
349
357
|
// Substitute params
|
|
350
358
|
if (options.params) {
|
|
@@ -550,7 +558,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
550
558
|
window.history.forward();
|
|
551
559
|
},
|
|
552
560
|
};
|
|
553
|
-
normalize(
|
|
561
|
+
normalize(rawRoutes, '/');
|
|
554
562
|
var _g = createMatcher(routes), findByName = _g.findByName, match = _g.match;
|
|
555
563
|
var currentHash = window.location.hash.slice(1);
|
|
556
564
|
if (currentHash) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
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
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Normalize path by joining parts and ensuring leading slash
|
|
3
16
|
*/
|
|
4
|
-
const fn = (() => true);
|
|
5
|
-
const throws = (m) => {
|
|
6
|
-
throw new Error(`@ktjs/router: ${m}`);
|
|
7
|
-
};
|
|
8
17
|
const normalizePath = (...paths) => {
|
|
9
18
|
const p = paths
|
|
10
19
|
.map((p) => p.split('/'))
|
|
@@ -75,6 +84,10 @@ const extractParams = (pattern, path) => {
|
|
|
75
84
|
return params;
|
|
76
85
|
};
|
|
77
86
|
|
|
87
|
+
// Shared utilities and cached native methods for kt.js framework
|
|
88
|
+
// Re-export all utilities
|
|
89
|
+
Object.defineProperty(window, '@ktjs/shared', { value: '0.19.0' });
|
|
90
|
+
|
|
78
91
|
/**
|
|
79
92
|
* Route matcher for finding matching routes and extracting params
|
|
80
93
|
*/
|
|
@@ -84,7 +97,7 @@ const createMatcher = (routes) => {
|
|
|
84
97
|
const route = routes[i];
|
|
85
98
|
if (route.name !== undefined) {
|
|
86
99
|
if (route.name in nameMap) {
|
|
87
|
-
|
|
100
|
+
$throw(`Duplicate route name detected: '${route.name}'`);
|
|
88
101
|
}
|
|
89
102
|
nameMap[route.name] = route;
|
|
90
103
|
}
|
|
@@ -159,7 +172,7 @@ function KTRouter({ router }) {
|
|
|
159
172
|
/**
|
|
160
173
|
* Create a new router instance
|
|
161
174
|
*/
|
|
162
|
-
const createRouter = ({ beforeEach =
|
|
175
|
+
const createRouter = ({ beforeEach = $emptyFn, afterEach = $emptyFn, onNotFound = $emptyFn, onError = $emptyFn, prefix = '', routes: rawRoutes, }) => {
|
|
163
176
|
// # private values
|
|
164
177
|
const routes = [];
|
|
165
178
|
const history = [];
|
|
@@ -172,8 +185,8 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
172
185
|
path: prefix + path,
|
|
173
186
|
name: route.name ?? '',
|
|
174
187
|
meta: route.meta ?? {},
|
|
175
|
-
beforeEnter: route.beforeEnter ??
|
|
176
|
-
after: route.after ??
|
|
188
|
+
beforeEnter: route.beforeEnter ?? $emptyFn,
|
|
189
|
+
after: route.after ?? $emptyFn,
|
|
177
190
|
children: route.children ? normalize(route.children, path) : [],
|
|
178
191
|
component: route.component,
|
|
179
192
|
};
|
|
@@ -226,7 +239,7 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
226
239
|
if (options.name) {
|
|
227
240
|
targetRoute = findByName(options.name);
|
|
228
241
|
if (!targetRoute) {
|
|
229
|
-
|
|
242
|
+
$throw(`Route not found: ${options.name}`);
|
|
230
243
|
}
|
|
231
244
|
targetPath = targetRoute.path;
|
|
232
245
|
}
|
|
@@ -235,7 +248,7 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
235
248
|
targetRoute = match(targetPath)?.route;
|
|
236
249
|
}
|
|
237
250
|
else {
|
|
238
|
-
|
|
251
|
+
$throw(`Either path or name must be provided`);
|
|
239
252
|
}
|
|
240
253
|
// Substitute params
|
|
241
254
|
if (options.params) {
|
|
@@ -414,7 +427,7 @@ const createRouter = ({ beforeEach = fn, afterEach = fn, onNotFound = fn, onErro
|
|
|
414
427
|
window.history.forward();
|
|
415
428
|
},
|
|
416
429
|
};
|
|
417
|
-
normalize(
|
|
430
|
+
normalize(rawRoutes, '/');
|
|
418
431
|
const { findByName, match } = createMatcher(routes);
|
|
419
432
|
const currentHash = window.location.hash.slice(1);
|
|
420
433
|
if (currentHash) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ktjs/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.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.19.0",
|
|
35
|
+
"@ktjs/shared": "0.19.0"
|
|
35
36
|
},
|
|
36
37
|
"scripts": {
|
|
37
38
|
"build": "rollup -c rollup.config.mjs",
|