@reckona/mreact-router 0.0.140 → 0.0.142

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.
Files changed (62) hide show
  1. package/README.md +4 -2
  2. package/dist/actions.d.ts +1 -1
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +28 -8
  5. package/dist/actions.js.map +1 -1
  6. package/dist/adapters/cloudflare.d.ts.map +1 -1
  7. package/dist/adapters/cloudflare.js +31 -12
  8. package/dist/adapters/cloudflare.js.map +1 -1
  9. package/dist/build.d.ts.map +1 -1
  10. package/dist/build.js +470 -12
  11. package/dist/build.js.map +1 -1
  12. package/dist/cli-options.js +1 -1
  13. package/dist/cli-options.js.map +1 -1
  14. package/dist/client-route-inference.d.ts +1 -1
  15. package/dist/client-route-inference.d.ts.map +1 -1
  16. package/dist/client-route-inference.js +1 -1
  17. package/dist/client-route-inference.js.map +1 -1
  18. package/dist/client.d.ts +8 -1
  19. package/dist/client.d.ts.map +1 -1
  20. package/dist/client.js +16 -0
  21. package/dist/client.js.map +1 -1
  22. package/dist/config.js +1 -1
  23. package/dist/config.js.map +1 -1
  24. package/dist/import-policy.js +7 -0
  25. package/dist/import-policy.js.map +1 -1
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +1 -0
  29. package/dist/index.js.map +1 -1
  30. package/dist/metadata.d.ts +1 -0
  31. package/dist/metadata.d.ts.map +1 -1
  32. package/dist/metadata.js +288 -1
  33. package/dist/metadata.js.map +1 -1
  34. package/dist/middleware.d.ts +5 -0
  35. package/dist/middleware.d.ts.map +1 -1
  36. package/dist/middleware.js +13 -0
  37. package/dist/middleware.js.map +1 -1
  38. package/dist/render.d.ts.map +1 -1
  39. package/dist/render.js +41 -9
  40. package/dist/render.js.map +1 -1
  41. package/dist/typed-routes.d.ts +24 -0
  42. package/dist/typed-routes.d.ts.map +1 -0
  43. package/dist/typed-routes.js +60 -0
  44. package/dist/typed-routes.js.map +1 -0
  45. package/dist/vite.d.ts.map +1 -1
  46. package/dist/vite.js +22 -1
  47. package/dist/vite.js.map +1 -1
  48. package/package.json +11 -11
  49. package/src/actions.ts +39 -12
  50. package/src/adapters/cloudflare.ts +48 -12
  51. package/src/build.ts +491 -11
  52. package/src/cli-options.ts +1 -1
  53. package/src/client-route-inference.ts +1 -0
  54. package/src/client.ts +28 -1
  55. package/src/config.ts +1 -1
  56. package/src/import-policy.ts +8 -0
  57. package/src/index.ts +9 -0
  58. package/src/metadata.ts +388 -1
  59. package/src/middleware.ts +22 -0
  60. package/src/render.ts +58 -7
  61. package/src/typed-routes.ts +121 -0
  62. package/src/vite.ts +25 -0
@@ -0,0 +1,121 @@
1
+ export type RouteSearchValue = boolean | number | string | null | undefined;
2
+ export type RouteSearchParams = Record<
3
+ string,
4
+ RouteSearchValue | readonly RouteSearchValue[]
5
+ >;
6
+
7
+ export type RouteParamsFor<Path extends `/${string}`> = Simplify<ExtractRouteParams<Path>>;
8
+
9
+ export type AppRouteHref<Path extends `/${string}`> = HasRouteParams<Path> extends true
10
+ ? (options: DynamicHrefOptions<Path>) => string
11
+ : (options?: StaticHrefOptions) => string;
12
+
13
+ export interface StaticHrefOptions {
14
+ hash?: string | undefined;
15
+ search?: RouteSearchParams | undefined;
16
+ }
17
+
18
+ export interface DynamicHrefOptions<Path extends `/${string}`> extends StaticHrefOptions {
19
+ params: RouteParamsFor<Path>;
20
+ }
21
+
22
+ type HasRouteParams<Path extends `/${string}`> = keyof RouteParamsFor<Path> extends never
23
+ ? false
24
+ : true;
25
+
26
+ type ExtractRouteParams<Path extends string> = Path extends `${infer Segment}/${infer Rest}`
27
+ ? SegmentRouteParam<Segment> & ExtractRouteParams<Rest>
28
+ : SegmentRouteParam<Path>;
29
+
30
+ type SegmentRouteParam<Segment extends string> = Segment extends `:...${infer Name}`
31
+ ? { [Key in Name]: readonly string[] }
32
+ : Segment extends `:${infer Name}`
33
+ ? { [Key in Name]: string }
34
+ : Record<never, never>;
35
+
36
+ type Simplify<T> = { [Key in keyof T]: T[Key] } & {};
37
+
38
+ export function href<const Path extends `/${string}`>(
39
+ path: Path,
40
+ ...args: HasRouteParams<Path> extends true
41
+ ? [options: DynamicHrefOptions<Path>]
42
+ : [options?: StaticHrefOptions]
43
+ ): string {
44
+ assertInternalRoutePath(path);
45
+
46
+ const options = args[0] as
47
+ | (StaticHrefOptions & { params?: Record<string, readonly string[] | string> | undefined })
48
+ | undefined;
49
+ const params =
50
+ options !== undefined && "params" in options && options.params !== undefined
51
+ ? (options.params as Record<string, readonly string[] | string>)
52
+ : {};
53
+ const pathname = path
54
+ .split("/")
55
+ .map((segment) => {
56
+ if (segment.startsWith(":...")) {
57
+ const name = segment.slice(4);
58
+ const value = params[name];
59
+
60
+ if (!Array.isArray(value)) {
61
+ throw new Error(`Missing catch-all route param ${JSON.stringify(name)} for ${path}.`);
62
+ }
63
+
64
+ return value.map((part) => encodeURIComponent(part)).join("/");
65
+ }
66
+
67
+ if (segment.startsWith(":")) {
68
+ const name = segment.slice(1);
69
+ const value = params[name];
70
+
71
+ if (typeof value !== "string") {
72
+ throw new Error(`Missing route param ${JSON.stringify(name)} for ${path}.`);
73
+ }
74
+
75
+ return encodeURIComponent(value);
76
+ }
77
+
78
+ return segment;
79
+ })
80
+ .join("/");
81
+ const search = searchString(options?.search);
82
+ const hash = options?.hash === undefined ? "" : `#${encodeURIComponent(options.hash)}`;
83
+
84
+ return `${pathname}${search}${hash}`;
85
+ }
86
+
87
+ function assertInternalRoutePath(path: string): void {
88
+ if (!path.startsWith("/") || path.startsWith("//")) {
89
+ throw new Error(`href() expected an internal route path, received ${JSON.stringify(path)}.`);
90
+ }
91
+
92
+ for (let index = 0; index < path.length; index += 1) {
93
+ const code = path.charCodeAt(index);
94
+
95
+ if (code <= 0x1f || code === 0x7f) {
96
+ throw new Error("href() route paths must not contain control characters.");
97
+ }
98
+ }
99
+ }
100
+
101
+ function searchString(search: RouteSearchParams | undefined): string {
102
+ if (search === undefined) {
103
+ return "";
104
+ }
105
+
106
+ const params = new URLSearchParams();
107
+
108
+ for (const [key, value] of Object.entries(search)) {
109
+ const values = Array.isArray(value) ? value : [value];
110
+
111
+ for (const entry of values) {
112
+ if (entry !== undefined && entry !== null) {
113
+ params.append(key, String(entry));
114
+ }
115
+ }
116
+ }
117
+
118
+ const value = params.toString();
119
+
120
+ return value === "" ? "" : `?${value}`;
121
+ }
package/src/vite.ts CHANGED
@@ -25,7 +25,9 @@ import type { AppRouterImportPolicy } from "./import-policy.js";
25
25
  import {
26
26
  collectClientRouteReferences,
27
27
  createClientRouteInferenceCache,
28
+ formatClientRouteInferenceDiagnostic,
28
29
  isClientRouteSource,
30
+ navigationRuntimeLinkDisabledDiagnostic,
29
31
  resolveNavigationRuntime,
30
32
  type ClientRouteInferenceCache,
31
33
  } from "./client-route-inference.js";
@@ -882,14 +884,37 @@ async function devNavigationScripts(
882
884
  if (source === undefined) {
883
885
  return undefined;
884
886
  }
887
+ const references = await collectClientRouteReferences({
888
+ appDir,
889
+ cache,
890
+ code: source,
891
+ filename: route.file,
892
+ routePath: route.path,
893
+ vitePlugins,
894
+ });
885
895
  const navigation = await resolveNavigationRuntime({
886
896
  appDir,
887
897
  cache,
888
898
  code: source,
889
899
  filename: route.file,
900
+ references,
890
901
  vitePlugins,
891
902
  });
892
903
 
904
+ for (const diagnostic of references.diagnostics) {
905
+ console.warn(formatClientRouteInferenceDiagnostic(diagnostic));
906
+ }
907
+ const navigationRuntimeDiagnostic = navigationRuntimeLinkDisabledDiagnostic({
908
+ filename: route.file,
909
+ references,
910
+ routePath: route.path,
911
+ source,
912
+ });
913
+
914
+ if (navigationRuntimeDiagnostic !== undefined) {
915
+ console.warn(formatClientRouteInferenceDiagnostic(navigationRuntimeDiagnostic));
916
+ }
917
+
893
918
  return navigation
894
919
  ? ([route.path, navigationRuntimeScriptForDev()] as const)
895
920
  : undefined;