@octanejs/remix-router 0.1.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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -0
  3. package/package.json +53 -0
  4. package/src/dom.ts +16 -0
  5. package/src/index.ts +267 -0
  6. package/src/internal.ts +37 -0
  7. package/src/lib/Await.tsrx +145 -0
  8. package/src/lib/Await.tsrx.d.ts +6 -0
  9. package/src/lib/DefaultErrorComponent.tsrx +47 -0
  10. package/src/lib/DefaultErrorComponent.tsrx.d.ts +2 -0
  11. package/src/lib/RenderErrorBoundary.tsrx +117 -0
  12. package/src/lib/RenderErrorBoundary.tsrx.d.ts +14 -0
  13. package/src/lib/actions.ts +90 -0
  14. package/src/lib/components/MemoryRouter.tsrx +42 -0
  15. package/src/lib/components/MemoryRouter.tsrx.d.ts +10 -0
  16. package/src/lib/components/Navigate.ts +60 -0
  17. package/src/lib/components/Router.tsrx +88 -0
  18. package/src/lib/components/Router.tsrx.d.ts +13 -0
  19. package/src/lib/components/RouterProvider.tsrx +320 -0
  20. package/src/lib/components/RouterProvider.tsrx.d.ts +21 -0
  21. package/src/lib/components/Routes.tsrx +53 -0
  22. package/src/lib/components/Routes.tsrx.d.ts +7 -0
  23. package/src/lib/components/routes-collector.ts +317 -0
  24. package/src/lib/components/utils.ts +171 -0
  25. package/src/lib/components/with-props.ts +72 -0
  26. package/src/lib/context.ts +129 -0
  27. package/src/lib/dom/Form.tsrx +76 -0
  28. package/src/lib/dom/Form.tsrx.d.ts +22 -0
  29. package/src/lib/dom/Link.tsrx +91 -0
  30. package/src/lib/dom/Link.tsrx.d.ts +22 -0
  31. package/src/lib/dom/NavLink.tsrx +118 -0
  32. package/src/lib/dom/NavLink.tsrx.d.ts +33 -0
  33. package/src/lib/dom/dom.ts +344 -0
  34. package/src/lib/dom/hooks.ts +93 -0
  35. package/src/lib/dom/lib.ts +822 -0
  36. package/src/lib/dom/routers.tsrx +104 -0
  37. package/src/lib/dom/routers.tsrx.d.ts +23 -0
  38. package/src/lib/dom/server.tsrx +350 -0
  39. package/src/lib/dom/server.tsrx.d.ts +25 -0
  40. package/src/lib/errors.ts +84 -0
  41. package/src/lib/framework-stubs.ts +103 -0
  42. package/src/lib/hooks.ts +1797 -0
  43. package/src/lib/href.ts +71 -0
  44. package/src/lib/react-types.ts +10 -0
  45. package/src/lib/router/history.ts +763 -0
  46. package/src/lib/router/instrumentation.ts +496 -0
  47. package/src/lib/router/links.ts +192 -0
  48. package/src/lib/router/router.ts +7165 -0
  49. package/src/lib/router/server-runtime-types.ts +12 -0
  50. package/src/lib/router/url.ts +8 -0
  51. package/src/lib/router/utils.ts +2179 -0
  52. package/src/lib/server-runtime/cookies.ts +240 -0
  53. package/src/lib/server-runtime/crypto.ts +55 -0
  54. package/src/lib/server-runtime/mode.ts +16 -0
  55. package/src/lib/server-runtime/sessions/cookieStorage.ts +54 -0
  56. package/src/lib/server-runtime/sessions/memoryStorage.ts +59 -0
  57. package/src/lib/server-runtime/sessions.ts +291 -0
  58. package/src/lib/server-runtime/warnings.ts +10 -0
  59. package/src/lib/types/future.ts +16 -0
  60. package/src/lib/types/params.ts +8 -0
  61. package/src/lib/types/register.ts +39 -0
  62. package/src/lib/types/route-module.ts +17 -0
  63. package/src/lib/types/utils.ts +37 -0
@@ -0,0 +1,71 @@
1
+ // Vendored from react-router@7.18.1 packages/react-router/lib/href.ts — unmodified.
2
+ // Re-vendor with `node scripts/vendor-remix-router.mjs`; never hand-edit.
3
+ import type { Pages } from './types/register';
4
+ import type { Equal } from './types/utils';
5
+
6
+ type Args = { [K in keyof Pages]: ToArgs<Pages[K]['params']> };
7
+
8
+ // prettier-ignore
9
+ type ToArgs<Params extends Record<string, string | undefined>> =
10
+ // path without params -> no `params` arg
11
+ Equal<Params, {}> extends true ? [] :
12
+ // path with only optional params -> optional `params` arg
13
+ Partial<Params> extends Params ? [Params] | [] :
14
+ // otherwise, require `params` arg
15
+ [Params];
16
+
17
+ /**
18
+ Returns a resolved URL path for the specified route.
19
+
20
+ ```tsx
21
+ const h = href("/:lang?/about", { lang: "en" })
22
+ // -> `/en/about`
23
+
24
+ <Link to={href("/products/:id", { id: "abc123" })} />
25
+ ```
26
+ */
27
+ export function href<Path extends keyof Args>(path: Path, ...args: Args[Path]): string {
28
+ let params = args[0];
29
+ let result = trimTrailingSplat(path) // Ignore trailing / and /*, we'll handle it below
30
+ .replace(
31
+ /\/:([\w-]+)(\?)?/g, // same regex as in .\router\utils.ts: compilePath().
32
+ (_: string, param: string, questionMark: string | undefined) => {
33
+ const isRequired = questionMark === undefined;
34
+ const value = params?.[param];
35
+ if (isRequired && value === undefined) {
36
+ throw new Error(`Path '${path}' requires param '${param}' but it was not provided`);
37
+ }
38
+ return value === undefined ? '' : '/' + value;
39
+ },
40
+ );
41
+
42
+ if (path.endsWith('*')) {
43
+ // treat trailing splat the same way as compilePath, and force it to be as if it were `/*`.
44
+ // `react-router typegen` will not generate the params for a malformed splat, causing a type error, but we can still do the correct thing here.
45
+ const value = params?.['*'];
46
+ if (value !== undefined) {
47
+ result += '/' + value;
48
+ }
49
+ }
50
+
51
+ return result || '/';
52
+ }
53
+
54
+ /**
55
+ * Removes a trailing splat and any number of slashes from the end of the path.
56
+ *
57
+ * Benchmarked to be faster than `path.replace(/\/*\*?$/, "")`, which backtracks.
58
+ */
59
+ function trimTrailingSplat(path: string): string {
60
+ let i = path.length - 1;
61
+ let char = path[i];
62
+ if (char !== '*' && char !== '/') return path;
63
+
64
+ // for/break benchmarks faster than do/while
65
+ i--;
66
+ for (; i >= 0; i--) {
67
+ if (path[i] !== '/') break;
68
+ }
69
+
70
+ return path.slice(0, i + 1);
71
+ }
@@ -0,0 +1,10 @@
1
+ // Local type shim for the vendored core's type-only `react` import
2
+ // (lib/router/utils.ts references React.ReactNode / React.ComponentType on
3
+ // RouteObject's element/Component fields). This package's consumers compile
4
+ // against octane, not @types/react — alias the two names onto octane's
5
+ // equivalents. Renderables in octane are element descriptors / primitives /
6
+ // arrays (see octane's ElementDescriptor), and components are ComponentBody.
7
+ import type { ComponentBody } from 'octane';
8
+
9
+ export type ReactNode = unknown;
10
+ export type ComponentType<P = any> = ComponentBody<P>;