@mpen/rerouter 0.1.9 → 0.3.1
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/README.md +80 -18
- package/cli/bin.test.ts +221 -0
- package/cli/bin.ts +342 -0
- package/cli/fixtures/bin/kitchen-sink.tsx +15 -0
- package/cli/fixtures/bin/optional.tsx +3 -0
- package/cli/fixtures/bin/pages/Home.tsx +3 -0
- package/cli/fixtures/bin/pages/KitchenSink.tsx +3 -0
- package/cli/fixtures/bin/pages/Login.tsx +3 -0
- package/cli/fixtures/bin/pages/Match.tsx +3 -0
- package/cli/fixtures/bin/pages/NotFound.tsx +3 -0
- package/cli/fixtures/bin/pages/Optional.tsx +3 -0
- package/cli/fixtures/bin/regexp-groups.tsx +11 -0
- package/cli/fixtures/bin/simple.tsx +1 -0
- package/cli/fixtures/bin/unnamed.tsx +4 -0
- package/cli/tsconfig.json +9 -0
- package/dist/acorn-k7ED_tOl.js +4968 -0
- package/dist/angular--Iqdw9UJ.js +4057 -0
- package/dist/babel-hfWAujRY.js +9878 -0
- package/dist/bin.d.ts +29 -0
- package/dist/bin.js +233 -0
- package/dist/estree-C1Zjnvlw.js +7266 -0
- package/dist/flow-BaD9LyIP.js +52912 -0
- package/dist/glimmer-CvCjW_1V.js +7541 -0
- package/dist/graphql-BdtzBuWh.js +1945 -0
- package/dist/html-DkZtUVbo.js +7137 -0
- package/dist/index.d.ts +278 -0
- package/dist/index.js +247 -0
- package/dist/markdown-Z8Vrc69e.js +6876 -0
- package/dist/meriyah-DeO4stuH.js +7590 -0
- package/dist/postcss-BmgGJ0E5.js +6777 -0
- package/dist/prettier-BT_F8kIx.js +15629 -0
- package/dist/routes-PW-bNm8e.js +135 -0
- package/dist/typescript-DtIxStjy.js +22936 -0
- package/dist/yaml-CWOPBY0q.js +5281 -0
- package/examples/App.tsx +80 -0
- package/examples/dist/BlogPost-c10d9w2p.js +1 -0
- package/examples/dist/FetchLoading-534mdrgz.js +1 -0
- package/examples/dist/FetchLoading-sbxbdkre.js +1 -0
- package/examples/dist/Home-a1258p25.js +1 -0
- package/examples/dist/KitchenSink-821mjg0h.js +1 -0
- package/examples/dist/Login-wywx6bp7.js +1 -0
- package/examples/dist/Match-1e72jm5w.js +1 -0
- package/examples/dist/NotFound-smxj24jw.js +1 -0
- package/examples/dist/SlowLoading-59xxmbfk.js +1 -0
- package/examples/dist/index-0d4kj0rv.js +2 -0
- package/examples/dist/index-3x197sbt.js +9 -0
- package/examples/dist/index-a2hkfx1n.js +9 -0
- package/examples/dist/index-d21me1mc.js +9 -0
- package/examples/dist/index-ktqdknsn.js +2 -0
- package/examples/dist/index-p53qxxzd.js +2 -0
- package/examples/dist/index.html +67 -0
- package/examples/index.html +67 -0
- package/examples/pages/BlogPost.tsx +17 -0
- package/examples/pages/FetchLoading.tsx +53 -0
- package/examples/pages/FetchLoadingItem.tsx +45 -0
- package/examples/pages/Home.tsx +3 -0
- package/examples/pages/KitchenSink.tsx +23 -0
- package/examples/pages/Login.tsx +3 -0
- package/examples/pages/Match.tsx +5 -0
- package/examples/pages/NotFound.tsx +3 -0
- package/examples/pages/SlowLoading.tsx +8 -0
- package/examples/routes.gen.ts +105 -0
- package/examples/routes.ts +40 -0
- package/examples/server/serve-dist.ts +33 -0
- package/examples/server/tsconfig.json +9 -0
- package/package.json +41 -31
- package/src/components/Link.test.tsx +139 -0
- package/src/components/Link.tsx +89 -0
- package/src/components/NavLink.test.tsx +119 -0
- package/src/components/NavLink.tsx +71 -0
- package/src/components/Router.test.tsx +183 -0
- package/src/components/Router.tsx +207 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useUrl.ts +22 -0
- package/src/index.ts +6 -0
- package/src/lib/mergeSearch.test.ts +37 -0
- package/src/lib/mergeSearch.ts +21 -0
- package/src/lib/routes.test.ts +67 -0
- package/src/lib/routes.ts +247 -0
- package/src/lib/url.ts +9 -0
- package/tsconfig.json +10 -0
- package/tsdown.config.ts +21 -0
- package/LICENSE +0 -21
- package/dist/bundle.cjs +0 -422
- package/dist/bundle.d.ts +0 -2
- package/dist/bundle.mjs +0 -420
- package/dist/dev.d.ts +0 -1
- package/dist/log.d.ts +0 -1
- package/dist/uri-template.d.ts +0 -56
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
//#region src/lib/routes.ts
|
|
3
|
+
function toUrlPattern(pattern) {
|
|
4
|
+
if (typeof pattern !== "string") return pattern;
|
|
5
|
+
return new URLPattern({ pathname: pattern });
|
|
6
|
+
}
|
|
7
|
+
function decodeRouteParams(groups) {
|
|
8
|
+
const params = {};
|
|
9
|
+
for (const [key, value] of Object.entries(groups)) if (value == null) params[key] = void 0;
|
|
10
|
+
else params[key] = decodeURIComponent(String(value));
|
|
11
|
+
return params;
|
|
12
|
+
}
|
|
13
|
+
function stripLegacyParamPattern(pattern, startIndex) {
|
|
14
|
+
let depth = 1;
|
|
15
|
+
let endIndex = startIndex + 1;
|
|
16
|
+
while (endIndex < pattern.length && depth > 0) {
|
|
17
|
+
const char = pattern[endIndex];
|
|
18
|
+
if (char === "\\") {
|
|
19
|
+
endIndex += 2;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (char === "(") depth++;
|
|
23
|
+
else if (char === ")") depth--;
|
|
24
|
+
endIndex++;
|
|
25
|
+
}
|
|
26
|
+
return endIndex;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Converts legacy `path-to-regexp` syntax that is ignored by URL generation into syntax accepted
|
|
30
|
+
* by the current parser.
|
|
31
|
+
*
|
|
32
|
+
* @param pattern - The route pattern to normalize.
|
|
33
|
+
* @returns The pattern with custom regexp constraints stripped and optional group suffixes removed.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* normalizeLegacyPathToRegexpSyntax('/blog/:id(\\d+){-:title}?')
|
|
38
|
+
* // '/blog/:id{-:title}'
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
function normalizeLegacyPathToRegexpSyntax(pattern) {
|
|
44
|
+
let normalized = "";
|
|
45
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
46
|
+
const char = pattern[i];
|
|
47
|
+
if (char === "\\") {
|
|
48
|
+
normalized += char;
|
|
49
|
+
if (i + 1 < pattern.length) normalized += pattern[++i];
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (char === ":" || char === "*") {
|
|
53
|
+
normalized += char;
|
|
54
|
+
while (i + 1 < pattern.length && /[$_\p{ID_Continue}]/u.test(pattern[i + 1])) normalized += pattern[++i];
|
|
55
|
+
if (pattern[i + 1] === "(") i = stripLegacyParamPattern(pattern, i + 1) - 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (char === "}" && pattern[i + 1] === "?") {
|
|
59
|
+
normalized += char;
|
|
60
|
+
i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
normalized += char;
|
|
64
|
+
}
|
|
65
|
+
return normalized;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Normalizes routes into objects with a shared matcher implementation.
|
|
69
|
+
*
|
|
70
|
+
* @param routes - The route definitions to normalize.
|
|
71
|
+
* @returns Routes with stable `name`, `pattern`, `component`, and `matches` fields.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* const normalized = normalizeRoutes(routes)
|
|
76
|
+
* const match = normalized[0]?.matches('/users/123')
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
function normalizeRoutes(routes) {
|
|
80
|
+
return routes.map((route) => {
|
|
81
|
+
const { name, pattern, component } = route;
|
|
82
|
+
if (typeof pattern !== "string") {
|
|
83
|
+
const urlPattern = toUrlPattern(pattern);
|
|
84
|
+
return {
|
|
85
|
+
name,
|
|
86
|
+
pattern,
|
|
87
|
+
component,
|
|
88
|
+
matches(pathname) {
|
|
89
|
+
const match = urlPattern.exec({ pathname });
|
|
90
|
+
if (!match) return null;
|
|
91
|
+
return match.pathname?.groups ?? {};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (pattern === "*" || pattern === "/*") return {
|
|
96
|
+
name,
|
|
97
|
+
pattern,
|
|
98
|
+
component,
|
|
99
|
+
matches: (_pathname) => ({})
|
|
100
|
+
};
|
|
101
|
+
let matcher;
|
|
102
|
+
let urlPattern;
|
|
103
|
+
try {
|
|
104
|
+
matcher = match(pattern, { decode: decodeURIComponent });
|
|
105
|
+
} catch {
|
|
106
|
+
try {
|
|
107
|
+
urlPattern = toUrlPattern(pattern);
|
|
108
|
+
} catch {
|
|
109
|
+
matcher = match(normalizeLegacyPathToRegexpSyntax(pattern), { decode: decodeURIComponent });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
name,
|
|
114
|
+
pattern,
|
|
115
|
+
component,
|
|
116
|
+
matches(pathname) {
|
|
117
|
+
if (urlPattern) {
|
|
118
|
+
const match = urlPattern.exec({ pathname });
|
|
119
|
+
if (!match) return null;
|
|
120
|
+
return decodeRouteParams(match.pathname?.groups ?? {});
|
|
121
|
+
}
|
|
122
|
+
if (!matcher) return null;
|
|
123
|
+
const match = matcher(pathname);
|
|
124
|
+
if (!match) return null;
|
|
125
|
+
const params = {};
|
|
126
|
+
for (const [key, value] of Object.entries(match.params)) if (value == null) params[key] = void 0;
|
|
127
|
+
else if (Array.isArray(value)) params[key] = value.join("/");
|
|
128
|
+
else params[key] = String(value);
|
|
129
|
+
return params;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
export { normalizeRoutes as n, normalizeLegacyPathToRegexpSyntax as t };
|