@uxf/router 15.0.0 → 16.0.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/LICENSE +21 -0
- package/README.md +85 -12
- package/create-client-router.d.ts +12 -5
- package/create-client-router.js +13 -7
- package/create-router.d.ts +10 -4
- package/create-router.js +7 -0
- package/esm/schema/array.js +10 -0
- package/esm/schema/boolean.js +11 -0
- package/esm/schema/index.js +6 -0
- package/esm/schema/integer.js +14 -0
- package/index.d.ts +1 -0
- package/index.js +3 -0
- package/package.json +13 -16
- package/query-params-validation.d.ts +17 -0
- package/query-params-validation.js +46 -0
- package/schema/array.d.ts +8 -0
- package/schema/array.js +46 -0
- package/schema/boolean.d.ts +6 -0
- package/schema/boolean.js +47 -0
- package/{superstruct → schema}/index.d.ts +1 -1
- package/{superstruct → schema}/index.js +6 -4
- package/schema/integer.d.ts +9 -0
- package/schema/integer.js +50 -0
- package/types.d.ts +17 -3
- package/create-router.test.d.ts +0 -1
- package/create-router.test.js +0 -237
- package/helper.test.d.ts +0 -1
- package/helper.test.js +0 -89
- package/routes-check/routes-check.test.d.ts +0 -1
- package/routes-check/routes-check.test.js +0 -33
- package/sitemap-generator.test.d.ts +0 -1
- package/sitemap-generator.test.js +0 -56
- package/superstruct/array.d.ts +0 -7
- package/superstruct/array.js +0 -14
- package/superstruct/boolean.d.ts +0 -1
- package/superstruct/boolean.js +0 -8
- package/superstruct/integer.d.ts +0 -1
- package/superstruct/integer.js +0 -12
- package/utils/object-to-xml.test.d.ts +0 -1
- package/utils/object-to-xml.test.js +0 -43
- package/utils/path-specificity.test.d.ts +0 -1
- package/utils/path-specificity.test.js +0 -72
- package/utils/path-to-regex.test.d.ts +0 -1
- package/utils/path-to-regex.test.js +0 -109
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const path_specificity_1 = require("./path-specificity");
|
|
4
|
-
describe("getPathSpecificity", () => {
|
|
5
|
-
it("counts static, dynamic and catch-all segments", () => {
|
|
6
|
-
expect((0, path_specificity_1.getPathSpecificity)("/hotel/create-hotel")).toEqual({
|
|
7
|
-
catchAllSegmentCount: 0,
|
|
8
|
-
dynamicSegmentCount: 0,
|
|
9
|
-
staticSegmentCount: 2,
|
|
10
|
-
});
|
|
11
|
-
expect((0, path_specificity_1.getPathSpecificity)("/hotel/[hotel-id]/edit")).toEqual({
|
|
12
|
-
catchAllSegmentCount: 0,
|
|
13
|
-
dynamicSegmentCount: 1,
|
|
14
|
-
staticSegmentCount: 2,
|
|
15
|
-
});
|
|
16
|
-
expect((0, path_specificity_1.getPathSpecificity)("/hotel/[...rest]")).toEqual({
|
|
17
|
-
catchAllSegmentCount: 1,
|
|
18
|
-
dynamicSegmentCount: 1,
|
|
19
|
-
staticSegmentCount: 1,
|
|
20
|
-
});
|
|
21
|
-
expect((0, path_specificity_1.getPathSpecificity)("/hotel/[[...rest]]")).toEqual({
|
|
22
|
-
catchAllSegmentCount: 1,
|
|
23
|
-
dynamicSegmentCount: 1,
|
|
24
|
-
staticSegmentCount: 1,
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
it("handles the root path", () => {
|
|
28
|
-
expect((0, path_specificity_1.getPathSpecificity)("/")).toEqual({
|
|
29
|
-
catchAllSegmentCount: 0,
|
|
30
|
-
dynamicSegmentCount: 0,
|
|
31
|
-
staticSegmentCount: 0,
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
describe("comparePathSpecificity", () => {
|
|
36
|
-
const sortPaths = (paths) => [...paths].sort((a, b) => (0, path_specificity_1.comparePathSpecificity)((0, path_specificity_1.getPathSpecificity)(a), (0, path_specificity_1.getPathSpecificity)(b)));
|
|
37
|
-
it("puts a static path before a dynamic one", () => {
|
|
38
|
-
expect(sortPaths(["/hotel/[hotel-id]", "/hotel/create-hotel"])).toEqual([
|
|
39
|
-
"/hotel/create-hotel",
|
|
40
|
-
"/hotel/[hotel-id]",
|
|
41
|
-
]);
|
|
42
|
-
});
|
|
43
|
-
it("puts catch-all segments last", () => {
|
|
44
|
-
expect(sortPaths(["/hotel/[...rest]", "/hotel/[hotel-id]", "/hotel/detail"])).toEqual([
|
|
45
|
-
"/hotel/detail",
|
|
46
|
-
"/hotel/[hotel-id]",
|
|
47
|
-
"/hotel/[...rest]",
|
|
48
|
-
]);
|
|
49
|
-
});
|
|
50
|
-
it("prefers more static segments when the dynamic count is equal", () => {
|
|
51
|
-
expect(sortPaths(["/hotel/[hotel-id]/[tab]", "/hotel/[hotel-id]/edit"])).toEqual([
|
|
52
|
-
"/hotel/[hotel-id]/edit",
|
|
53
|
-
"/hotel/[hotel-id]/[tab]",
|
|
54
|
-
]);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
describe("getRouteSpecificity", () => {
|
|
58
|
-
it("accepts a plain path", () => {
|
|
59
|
-
expect((0, path_specificity_1.getRouteSpecificity)("/hotel/[hotel-id]")).toEqual({
|
|
60
|
-
catchAllSegmentCount: 0,
|
|
61
|
-
dynamicSegmentCount: 1,
|
|
62
|
-
staticSegmentCount: 1,
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
it("ranks a localized route by its most specific variant", () => {
|
|
66
|
-
expect((0, path_specificity_1.getRouteSpecificity)({ cs: "/hotel/[hotel-id]", en: "/hotel/detail" })).toEqual({
|
|
67
|
-
catchAllSegmentCount: 0,
|
|
68
|
-
dynamicSegmentCount: 0,
|
|
69
|
-
staticSegmentCount: 2,
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const path_to_regex_1 = require("./path-to-regex");
|
|
4
|
-
describe("pathToRegex", () => {
|
|
5
|
-
describe("static routes", () => {
|
|
6
|
-
it("should match exact static paths", () => {
|
|
7
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/product/detail");
|
|
8
|
-
expect(regex.test("/product/detail")).toBe(true);
|
|
9
|
-
expect(regex.test("/product/detail/extra")).toBe(false);
|
|
10
|
-
expect(regex.test("/product")).toBe(false);
|
|
11
|
-
});
|
|
12
|
-
it("should handle root path", () => {
|
|
13
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/");
|
|
14
|
-
expect(regex.test("/")).toBe(true);
|
|
15
|
-
expect(regex.test("/anything")).toBe(false);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
describe("dynamic routes [param]", () => {
|
|
19
|
-
it("should match single dynamic segment", () => {
|
|
20
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/product/[id]");
|
|
21
|
-
expect(regex.test("/product/1")).toBe(true);
|
|
22
|
-
expect(regex.test("/product/abc")).toBe(true);
|
|
23
|
-
expect(regex.test("/product/123-xyz")).toBe(true);
|
|
24
|
-
expect(regex.test("/product/")).toBe(false);
|
|
25
|
-
expect(regex.test("/product/1/extra")).toBe(false);
|
|
26
|
-
});
|
|
27
|
-
it("should match dynamic route with detail", () => {
|
|
28
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/product/[id]/detail");
|
|
29
|
-
expect(regex.test("/product/1/detail")).toBe(true);
|
|
30
|
-
expect(regex.test("/product/abc/detail")).toBe(true);
|
|
31
|
-
expect(regex.test("/product/1/detail/extra")).toBe(false);
|
|
32
|
-
expect(regex.test("/product/detail")).toBe(false);
|
|
33
|
-
});
|
|
34
|
-
it("should handle multiple dynamic segments", () => {
|
|
35
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/[category]/[id]/edit");
|
|
36
|
-
expect(regex.test("/electronics/123/edit")).toBe(true);
|
|
37
|
-
expect(regex.test("/books/abc/edit")).toBe(true);
|
|
38
|
-
expect(regex.test("/electronics/123/edit/more")).toBe(false);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
describe("catch-all routes [...slug]", () => {
|
|
42
|
-
it("should match catch-all segments", () => {
|
|
43
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/docs/[...slug]");
|
|
44
|
-
expect(regex.test("/docs/getting-started")).toBe(true);
|
|
45
|
-
expect(regex.test("/docs/api/reference")).toBe(true);
|
|
46
|
-
expect(regex.test("/docs/api/reference/auth")).toBe(true);
|
|
47
|
-
expect(regex.test("/docs")).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
it("should require at least one segment for catch-all", () => {
|
|
50
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/blog/[...slug]");
|
|
51
|
-
expect(regex.test("/blog/2023")).toBe(true);
|
|
52
|
-
expect(regex.test("/blog/2023/12/post")).toBe(true);
|
|
53
|
-
expect(regex.test("/blog/")).toBe(false);
|
|
54
|
-
expect(regex.test("/blog")).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
describe("optional catch-all routes [[...slug]]", () => {
|
|
58
|
-
it("should match optional catch-all segments", () => {
|
|
59
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/shop/[[...categories]]");
|
|
60
|
-
expect(regex.test("/shop")).toBe(true);
|
|
61
|
-
expect(regex.test("/shop/")).toBe(true);
|
|
62
|
-
expect(regex.test("/shop/electronics")).toBe(true);
|
|
63
|
-
expect(regex.test("/shop/electronics/phones")).toBe(true);
|
|
64
|
-
expect(regex.test("/shop/electronics/phones/iphone")).toBe(true);
|
|
65
|
-
});
|
|
66
|
-
it("should handle optional catch-all at root", () => {
|
|
67
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/[[...slug]]");
|
|
68
|
-
expect(regex.test("/")).toBe(true);
|
|
69
|
-
expect(regex.test("/about")).toBe(true);
|
|
70
|
-
expect(regex.test("/about/team")).toBe(true);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
describe("complex combinations", () => {
|
|
74
|
-
it("should handle dynamic + catch-all", () => {
|
|
75
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/user/[id]/posts/[...slug]");
|
|
76
|
-
expect(regex.test("/user/123/posts/my-first-post")).toBe(true);
|
|
77
|
-
expect(regex.test("/user/abc/posts/category/tech/post-1")).toBe(true);
|
|
78
|
-
expect(regex.test("/user/123/posts")).toBe(false);
|
|
79
|
-
});
|
|
80
|
-
it("should handle dynamic + optional catch-all", () => {
|
|
81
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/user/[id]/files/[[...path]]");
|
|
82
|
-
expect(regex.test("/user/123/files")).toBe(true);
|
|
83
|
-
expect(regex.test("/user/123/files/documents")).toBe(true);
|
|
84
|
-
expect(regex.test("/user/123/files/documents/pdf/report.pdf")).toBe(true);
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
describe("edge cases", () => {
|
|
88
|
-
it("should handle special regex characters in static parts", () => {
|
|
89
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/api/v1.0/users");
|
|
90
|
-
expect(regex.test("/api/v1.0/users")).toBe(true);
|
|
91
|
-
expect(regex.test("/api/v1x0/users")).toBe(false);
|
|
92
|
-
});
|
|
93
|
-
it("should handle paths with hyphens and underscores", () => {
|
|
94
|
-
const regex = (0, path_to_regex_1.pathToRegex)("/api-v1/user_profile/[user-id]");
|
|
95
|
-
expect(regex.test("/api-v1/user_profile/123-abc")).toBe(true);
|
|
96
|
-
expect(regex.test("/api-v1/user_profile/user_123")).toBe(true);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
describe("regex checks", () => {
|
|
100
|
-
const PATHS = [
|
|
101
|
-
"/",
|
|
102
|
-
"/product/[id]",
|
|
103
|
-
"/product/[id]/detail",
|
|
104
|
-
"/user/files/[...path]",
|
|
105
|
-
"/user/files/[[...path]]",
|
|
106
|
-
];
|
|
107
|
-
expect(PATHS.reduce((acc, curr) => ({ ...acc, [curr]: (0, path_to_regex_1.pathToRegex)(curr).source }), {})).toMatchSnapshot();
|
|
108
|
-
});
|
|
109
|
-
});
|