@kasperrt/smolter 0.0.0-stage → 0.0.1-rc.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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +470 -2
  3. package/dist/client.d.ts +2 -0
  4. package/dist/client.js +2 -0
  5. package/dist/core.d.ts +2 -0
  6. package/dist/core.js +2 -0
  7. package/dist/files.d.ts +2 -0
  8. package/dist/files.js +2 -0
  9. package/dist/internal/client/deserialize.d.ts +3 -0
  10. package/dist/internal/client/deserialize.js +11 -0
  11. package/dist/internal/client/index.d.ts +23 -0
  12. package/dist/internal/client/index.js +44 -0
  13. package/dist/internal/core/errors.d.ts +3 -0
  14. package/dist/internal/core/errors.js +6 -0
  15. package/dist/internal/core/index.d.ts +6 -0
  16. package/dist/internal/core/index.js +6 -0
  17. package/dist/internal/core/match.d.ts +5 -0
  18. package/dist/internal/core/match.js +27 -0
  19. package/dist/internal/core/router.d.ts +3 -0
  20. package/dist/internal/core/router.js +189 -0
  21. package/dist/internal/core/types.d.ts +119 -0
  22. package/dist/internal/core/types.js +5 -0
  23. package/dist/internal/core/url.d.ts +2 -0
  24. package/dist/internal/core/url.js +6 -0
  25. package/dist/internal/files/index.d.ts +11 -0
  26. package/dist/internal/files/index.js +77 -0
  27. package/dist/internal/preact/context.d.ts +18 -0
  28. package/dist/internal/preact/context.js +20 -0
  29. package/dist/internal/preact/fetcher.d.ts +22 -0
  30. package/dist/internal/preact/fetcher.js +28 -0
  31. package/dist/internal/preact/form.d.ts +17 -0
  32. package/dist/internal/preact/form.js +41 -0
  33. package/dist/internal/preact/hooks.d.ts +21 -0
  34. package/dist/internal/preact/hooks.js +35 -0
  35. package/dist/internal/preact/index.d.ts +9 -0
  36. package/dist/internal/preact/index.js +7 -0
  37. package/dist/internal/preact/link.d.ts +12 -0
  38. package/dist/internal/preact/link.js +28 -0
  39. package/dist/internal/preact/routes.d.ts +22 -0
  40. package/dist/internal/preact/routes.js +22 -0
  41. package/dist/internal/preact/runtime.d.ts +2 -0
  42. package/dist/internal/preact/runtime.js +1 -0
  43. package/dist/internal/react/context.d.ts +18 -0
  44. package/dist/internal/react/context.js +20 -0
  45. package/dist/internal/react/fetcher.d.ts +22 -0
  46. package/dist/internal/react/fetcher.js +28 -0
  47. package/dist/internal/react/form.d.ts +17 -0
  48. package/dist/internal/react/form.js +41 -0
  49. package/dist/internal/react/hooks.d.ts +21 -0
  50. package/dist/internal/react/hooks.js +35 -0
  51. package/dist/internal/react/index.d.ts +9 -0
  52. package/dist/internal/react/index.js +7 -0
  53. package/dist/internal/react/link.d.ts +12 -0
  54. package/dist/internal/react/link.js +28 -0
  55. package/dist/internal/react/routes.d.ts +22 -0
  56. package/dist/internal/react/routes.js +22 -0
  57. package/dist/internal/react/runtime.d.ts +2 -0
  58. package/dist/internal/react/runtime.js +2 -0
  59. package/dist/internal/server/index.d.ts +11 -0
  60. package/dist/internal/server/index.js +81 -0
  61. package/dist/internal/vite/index.d.ts +10 -0
  62. package/dist/internal/vite/index.js +132 -0
  63. package/dist/internal/vite/transport.d.ts +2 -0
  64. package/dist/internal/vite/transport.js +42 -0
  65. package/dist/preact.d.ts +2 -0
  66. package/dist/preact.js +2 -0
  67. package/dist/react.d.ts +2 -0
  68. package/dist/react.js +2 -0
  69. package/dist/server.d.ts +2 -0
  70. package/dist/server.js +2 -0
  71. package/dist/vite.d.ts +2 -0
  72. package/dist/vite.js +2 -0
  73. package/jsr.json +26 -0
  74. package/package.json +94 -4
  75. package/src/client.ts +2 -0
  76. package/src/core.ts +2 -0
  77. package/src/files.ts +2 -0
  78. package/src/internal/client/deserialize.ts +15 -0
  79. package/src/internal/client/index.ts +84 -0
  80. package/src/internal/core/errors.ts +13 -0
  81. package/src/internal/core/index.ts +7 -0
  82. package/src/internal/core/match.ts +45 -0
  83. package/src/internal/core/router.ts +251 -0
  84. package/src/internal/core/types.ts +151 -0
  85. package/src/internal/core/url.ts +6 -0
  86. package/src/internal/files/index.ts +112 -0
  87. package/src/internal/preact/context.ts +46 -0
  88. package/src/internal/preact/fetcher.ts +83 -0
  89. package/src/internal/preact/form.ts +79 -0
  90. package/src/internal/preact/hooks.ts +72 -0
  91. package/src/internal/preact/index.ts +13 -0
  92. package/src/internal/preact/link.ts +46 -0
  93. package/src/internal/preact/routes.ts +50 -0
  94. package/src/internal/preact/runtime.ts +15 -0
  95. package/src/internal/react/context.ts +46 -0
  96. package/src/internal/react/fetcher.ts +83 -0
  97. package/src/internal/react/form.ts +79 -0
  98. package/src/internal/react/hooks.ts +72 -0
  99. package/src/internal/react/index.ts +13 -0
  100. package/src/internal/react/link.ts +46 -0
  101. package/src/internal/react/routes.ts +50 -0
  102. package/src/internal/react/runtime.ts +18 -0
  103. package/src/internal/server/index.ts +120 -0
  104. package/src/internal/vite/index.ts +208 -0
  105. package/src/internal/vite/transport.ts +60 -0
  106. package/src/preact.ts +2 -0
  107. package/src/react.ts +2 -0
  108. package/src/server.ts +2 -0
  109. package/src/vite.ts +2 -0
@@ -0,0 +1,46 @@
1
+ /**
2
+ * React components and typed data hooks. Import from smolter/react.
3
+ * @module
4
+ */
5
+
6
+ import type { Router, Snapshot } from "../core/index.ts";
7
+ import type { ReactNode } from "./runtime.ts";
8
+ import {
9
+ createContext,
10
+ createElement,
11
+ useContext,
12
+ useEffect,
13
+ useSyncExternalStore,
14
+ } from "./runtime.ts";
15
+
16
+ const RouterContext = createContext<Router | undefined>(undefined);
17
+
18
+ /** Router instance and children rendered within its context. */
19
+ export interface RouterProviderProps {
20
+ router: Router;
21
+ children?: ReactNode;
22
+ }
23
+
24
+ /** Provide a router to descendant hooks and connect its browser lifecycle. */
25
+ export function RouterProvider({
26
+ router,
27
+ children,
28
+ }: RouterProviderProps): ReactNode {
29
+ useEffect(router.start, [router]);
30
+
31
+ return createElement(RouterContext.Provider, { value: router }, children);
32
+ }
33
+ /** Hooks require a RouterProvider. */
34
+ export function useRouter(): Router {
35
+ return useContext(RouterContext) as Router;
36
+ }
37
+
38
+ type SnapshotSelector<T> = (snapshot: Snapshot) => T;
39
+
40
+ export function useSnapshot<T>(select: SnapshotSelector<T>): T {
41
+ const router = useRouter();
42
+
43
+ const getSnapshot = () => select(router.getSnapshot());
44
+
45
+ return useSyncExternalStore(router.subscribe, getSnapshot, getSnapshot);
46
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * React components and typed data hooks. Import from smolter/react.
3
+ * @module
4
+ */
5
+
6
+ import type {
7
+ AnyDataFunction,
8
+ DataFunction,
9
+ DataOf,
10
+ FetcherSnapshot,
11
+ Result,
12
+ Route as RouteDefinition,
13
+ RouteModule,
14
+ SubmitOptions,
15
+ } from "../core/index.ts";
16
+ import { useRouter } from "./context.ts";
17
+ import { Form, type FormProps } from "./form.ts";
18
+ import type { ComponentType } from "./runtime.ts";
19
+ import {
20
+ createElement,
21
+ useEffect,
22
+ useMemo,
23
+ useSyncExternalStore,
24
+ } from "./runtime.ts";
25
+
26
+ /** Action method and optional explicit route URL for a fetcher submission. */
27
+ export interface FetcherSubmitOptions extends SubmitOptions {
28
+ action?: string;
29
+ }
30
+
31
+ /** Typed independent request state, methods, and a bound Form component. */
32
+ export interface TypedFetcher<T> extends Omit<FetcherSnapshot, "data"> {
33
+ data?: T;
34
+ Form: ComponentType<FormProps>;
35
+ load: (url?: string) => Promise<Result>;
36
+ submit: (body: FormData, options?: FetcherSubmitOptions) => Promise<Result>;
37
+ }
38
+
39
+ /** Create a fetcher for independent route reads and mutations; its data stays local. */
40
+ export function useFetcher<T extends RouteModule>(
41
+ route: RouteDefinition<T>,
42
+ ): TypedFetcher<
43
+ | DataOf<Extract<T["action"] | T["clientAction"], AnyDataFunction>>
44
+ | DataOf<Extract<T["loader"] | T["clientLoader"], AnyDataFunction>>
45
+ >;
46
+
47
+ /** Create a fetcher for independent route reads and mutations; its data stays local. */
48
+ export function useFetcher<
49
+ T extends AnyDataFunction = DataFunction,
50
+ >(): TypedFetcher<DataOf<T>>;
51
+
52
+ /** Create a fetcher for independent route reads and mutations; its data stays local. */
53
+ export function useFetcher(route?: RouteDefinition): TypedFetcher<unknown> {
54
+ const router = useRouter();
55
+ const path = route?.path;
56
+ const fetcher = useMemo(() => {
57
+ const store = router.createFetcher();
58
+ const target = () => {
59
+ const snapshot = router.getSnapshot();
60
+
61
+ return path === undefined || path === snapshot.path ? snapshot.url : path;
62
+ };
63
+
64
+ return {
65
+ ...store,
66
+ Form: (props: FormProps) =>
67
+ createElement(Form, { action: target(), ...props, fetcher: store }),
68
+ load: (url?: string) => store.load(url ?? target()),
69
+ submit: (body: FormData, options?: FetcherSubmitOptions) =>
70
+ store.submit(options?.action ?? target(), body, options),
71
+ };
72
+ }, [router, path]);
73
+
74
+ useEffect(() => fetcher.dispose, [fetcher]);
75
+
76
+ const state = useSyncExternalStore(
77
+ fetcher.subscribe,
78
+ fetcher.getSnapshot,
79
+ fetcher.getSnapshot,
80
+ );
81
+
82
+ return { ...fetcher, ...state };
83
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * React components and typed data hooks. Import from smolter/react.
3
+ * @module
4
+ */
5
+
6
+ import type { Fetcher, SubmitOptions } from "../core/index.ts";
7
+ import { internalURL } from "../core/index.ts";
8
+ import { useRouter } from "./context.ts";
9
+ import type { FormHTMLAttributes, ReactNode } from "./runtime.ts";
10
+ import { createElement } from "./runtime.ts";
11
+
12
+ /** Form attributes with GET loader requests or POST, PUT, PATCH, and DELETE actions. */
13
+ export interface FormProps
14
+ extends Omit<FormHTMLAttributes<HTMLFormElement>, "action" | "method"> {
15
+ action?: string;
16
+ method?: "get" | "post" | "put" | "patch" | "delete";
17
+ }
18
+
19
+ type FormSubmitEvent = Parameters<NonNullable<FormProps["onSubmit"]>>[0];
20
+
21
+ interface InternalFormProps extends FormProps {
22
+ fetcher?: Fetcher;
23
+ }
24
+
25
+ /** Submit form data to a registered route without a document reload. */
26
+ export function Form({
27
+ action,
28
+ method = "post",
29
+ fetcher,
30
+ onSubmit,
31
+ ...props
32
+ }: InternalFormProps): ReactNode {
33
+ const router = useRouter();
34
+ const submit = (event: FormSubmitEvent) => {
35
+ onSubmit?.(event);
36
+
37
+ if (event.defaultPrevented) return;
38
+
39
+ const submitter = (event.nativeEvent as SubmitEvent).submitter;
40
+ const target = internalURL(
41
+ router.getSnapshot().url,
42
+ submitter?.getAttribute("formaction") ?? action,
43
+ );
44
+
45
+ if (!target) return;
46
+ event.preventDefault();
47
+
48
+ const data = new FormData(event.currentTarget, submitter);
49
+ const verb = (
50
+ submitter?.getAttribute("formmethod") ?? method
51
+ ).toUpperCase();
52
+
53
+ if (verb === "GET") {
54
+ const search = new URLSearchParams();
55
+
56
+ for (const [key, value] of data)
57
+ search.append(key, typeof value === "string" ? value : value.name);
58
+
59
+ target.search = String(search);
60
+
61
+ void (fetcher ? fetcher.load : router.navigate)(target.href);
62
+
63
+ return;
64
+ }
65
+
66
+ const options: SubmitOptions = {
67
+ method: verb as SubmitOptions["method"],
68
+ };
69
+
70
+ void (fetcher ?? router).submit(target.href, data, options);
71
+ };
72
+
73
+ return createElement("form", {
74
+ ...props,
75
+ action,
76
+ method: method === "get" ? "get" : "post",
77
+ onSubmit: submit,
78
+ });
79
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * React components and typed data hooks. Import from smolter/react.
3
+ * @module
4
+ */
5
+
6
+ import type {
7
+ AnyDataFunction,
8
+ DataOf,
9
+ Route,
10
+ RouteModule,
11
+ Router,
12
+ Snapshot,
13
+ } from "../core/index.ts";
14
+
15
+ import { useRouter, useSnapshot } from "./context.ts";
16
+
17
+ /** A route module or loader function used for data inference. */
18
+ export type LoaderSource = RouteModule | AnyDataFunction;
19
+
20
+ /** Infer server and client loader data from a route module or function. */
21
+ export type LoaderData<T> = T extends AnyDataFunction
22
+ ? DataOf<T>
23
+ : T extends RouteModule
24
+ ? DataOf<Extract<T["loader"] | T["clientLoader"], AnyDataFunction>>
25
+ : unknown;
26
+
27
+ /** Infer server and client action data from a route module or function. */
28
+ export type ActionData<T> = T extends AnyDataFunction
29
+ ? DataOf<T>
30
+ : T extends RouteModule
31
+ ? DataOf<Extract<T["action"] | T["clientAction"], AnyDataFunction>>
32
+ : unknown;
33
+ function useData(key: "data" | "actionData", route?: LoaderSource): unknown {
34
+ const path = (route as Route | undefined)?.path;
35
+
36
+ return useSnapshot((snapshot) => {
37
+ if (path !== undefined && path !== snapshot.path) return undefined;
38
+
39
+ return snapshot[key];
40
+ });
41
+ }
42
+ /** Pass a route for inference, or use useLoaderData<typeof loader>() in its module. */
43
+ export function useLoaderData<T extends LoaderSource = RouteModule>(
44
+ route?: T,
45
+ ): LoaderData<T> | undefined {
46
+ return useData("data", route) as LoaderData<T> | undefined;
47
+ }
48
+
49
+ /** Read the last page action result, optionally inferring its type from a route. */
50
+ export function useActionData<T extends LoaderSource = RouteModule>(
51
+ route?: T,
52
+ ): ActionData<T> | undefined {
53
+ return useData("actionData", route) as ActionData<T> | undefined;
54
+ }
55
+
56
+ /** Read path parameters for the active route. */
57
+ export function useParams(): Snapshot["params"] {
58
+ return useSnapshot((snapshot) => snapshot.params);
59
+ }
60
+
61
+ /** Read the current path, search, and hash together with the navigate function. */
62
+ export function useLocation(): readonly [string, Router["navigate"]] {
63
+ const router = useRouter();
64
+ const url = useSnapshot((snapshot) => snapshot.url);
65
+
66
+ return [url.replace(/^\w+:\/\/[^/]+/, ""), router.navigate];
67
+ }
68
+
69
+ /** Get the function that revalidates the current loader subject to shouldRevalidate. */
70
+ export function useRevalidator(): Router["revalidate"] {
71
+ return useRouter().revalidate;
72
+ }
@@ -0,0 +1,13 @@
1
+ /** Framework components and typed data hooks. @module */
2
+ export type * from "../core/index.ts";
3
+ export { createRouter, defineRoute } from "../core/index.ts";
4
+ export {
5
+ RouterProvider,
6
+ type RouterProviderProps,
7
+ useRouter,
8
+ } from "./context.ts";
9
+ export * from "./fetcher.ts";
10
+ export * from "./form.ts";
11
+ export * from "./hooks.ts";
12
+ export * from "./link.ts";
13
+ export * from "./routes.ts";
@@ -0,0 +1,46 @@
1
+ /**
2
+ * React components and typed data hooks. Import from smolter/react.
3
+ * @module
4
+ */
5
+
6
+ import { internalURL } from "../core/index.ts";
7
+ import { useRouter } from "./context.ts";
8
+ import type { AnchorHTMLAttributes, MouseEvent, ReactNode } from "./runtime.ts";
9
+ import { createElement } from "./runtime.ts";
10
+
11
+ /** Anchor attributes with internal navigation and optional history replacement. */
12
+ export interface LinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
13
+ href: string;
14
+ replace?: boolean;
15
+ }
16
+
17
+ /** Render an anchor that handles internal navigation and preserves native modified clicks. */
18
+ export function Link({
19
+ href,
20
+ replace,
21
+ onClick,
22
+ ...props
23
+ }: LinkProps): ReactNode {
24
+ const router = useRouter();
25
+ const click = (event: MouseEvent<HTMLAnchorElement>) => {
26
+ onClick?.(event);
27
+
28
+ if (
29
+ event.defaultPrevented ||
30
+ event.button ||
31
+ event.metaKey ||
32
+ event.ctrlKey ||
33
+ event.altKey ||
34
+ event.shiftKey ||
35
+ props.download !== undefined ||
36
+ (props.target && props.target !== "_self")
37
+ )
38
+ return;
39
+
40
+ if (!internalURL(router.getSnapshot().url, href)) return;
41
+ event.preventDefault();
42
+ void router.navigate(href, { replace });
43
+ };
44
+
45
+ return createElement("a", { ...props, href, onClick: click });
46
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * React components and typed data hooks. Import from smolter/react.
3
+ * @module
4
+ */
5
+
6
+ import type { Route as RouteDefinition, RouteModule } from "../core/index.ts";
7
+ import { useRouter, useSnapshot } from "./context.ts";
8
+ import type { ComponentType, ReactNode } from "./runtime.ts";
9
+ import { createElement, useMemo } from "./runtime.ts";
10
+
11
+ /** Renderable route module with optional loading and error components. */
12
+ export interface ViewModule extends RouteModule {
13
+ default?: ComponentType;
14
+ HydrateFallback?: ComponentType;
15
+ ErrorBoundary?: ComponentType<ErrorProps>;
16
+ }
17
+
18
+ /** Normalized route error supplied to an ErrorBoundary component. */
19
+ export interface ErrorProps {
20
+ error: Error;
21
+ }
22
+
23
+ /** Fallback content used when no matching component can be rendered. */
24
+ export interface RoutesProps {
25
+ fallback?: ReactNode;
26
+ }
27
+ /** Renders the first matching registered module. */
28
+ export function Routes({ fallback = null }: RoutesProps): ReactNode {
29
+ const router = useRouter();
30
+ const state = useSnapshot((snapshot) => snapshot);
31
+ const module = router.routes.find((route) => route.path === state.path) as
32
+ | RouteDefinition<ViewModule>
33
+ | undefined;
34
+ const component = state.error
35
+ ? module?.ErrorBoundary
36
+ : state.state === "loading" && !state.loaded
37
+ ? module?.HydrateFallback
38
+ : module?.default;
39
+
40
+ const element = useMemo(
41
+ () =>
42
+ component &&
43
+ createElement(component as ComponentType<ErrorProps>, {
44
+ error: state.error!,
45
+ }),
46
+ [component, state.error],
47
+ );
48
+
49
+ return element ?? state.error?.message ?? fallback;
50
+ }
@@ -0,0 +1,18 @@
1
+ // @deno-types="npm:@types/react@19.3.0"
2
+
3
+ // @deno-types="npm:@types/react@19.3.0"
4
+ export type {
5
+ AnchorHTMLAttributes,
6
+ ComponentType,
7
+ FormHTMLAttributes,
8
+ MouseEvent,
9
+ ReactNode,
10
+ } from "react";
11
+ export {
12
+ createContext,
13
+ createElement,
14
+ useContext,
15
+ useEffect,
16
+ useMemo,
17
+ useSyncExternalStore,
18
+ } from "react";
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Request-scoped server rendering and HTTP data dispatch.
3
+ * @module
4
+ */
5
+
6
+ import {
7
+ createRouter,
8
+ type DataFunction,
9
+ matchRoutes,
10
+ type Route,
11
+ type Router,
12
+ type Snapshot,
13
+ safeWrapAsync,
14
+ } from "../core/index.ts";
15
+ /** Create one router per server request, await this, then render the provider. */
16
+ export async function loadRouter(
17
+ routes: readonly Route[],
18
+ input: string | Request,
19
+ ): Promise<Router> {
20
+ const url = typeof input === "string" ? input : input.url;
21
+ const wrap = (handler?: DataFunction): DataFunction | undefined => {
22
+ if (!handler || typeof input === "string") return handler;
23
+
24
+ return (args) => {
25
+ const signal = AbortSignal.any([input.signal, args.signal]);
26
+ const request = new Request(args.request, { signal });
27
+
28
+ input.headers.forEach((value, name) => {
29
+ if (!request.headers.has(name)) request.headers.set(name, value);
30
+ });
31
+
32
+ return handler({ ...args, request, signal });
33
+ };
34
+ };
35
+ const serverRoutes = routes.map((route) => ({
36
+ ...route,
37
+ clientLoader: undefined,
38
+ clientAction: undefined,
39
+ loader: wrap(route.loader),
40
+ action: wrap(route.action),
41
+ }));
42
+ const router = createRouter(serverRoutes, { url });
43
+
44
+ await router.navigate(url);
45
+
46
+ const snapshot = router.getSnapshot();
47
+
48
+ if (
49
+ !snapshot.error &&
50
+ snapshot.data === undefined &&
51
+ routes.some(
52
+ (route) =>
53
+ route.path === snapshot.path && route.clientLoader && !route.loader,
54
+ )
55
+ ) {
56
+ return createRouter(serverRoutes, {
57
+ url,
58
+ hydrationData: { ...snapshot, loaded: false, state: "loading" },
59
+ });
60
+ }
61
+
62
+ return router;
63
+ }
64
+ /** Safe inside a script element. Data must be JSON serializable; error stacks and causes are omitted. */
65
+ export function serialize(snapshot: Snapshot): string {
66
+ return JSON.stringify({
67
+ ...snapshot,
68
+ error: snapshot.error && {
69
+ name: snapshot.error.name,
70
+ message: snapshot.error.message,
71
+ },
72
+ })
73
+ .replace(/</g, "\\u003c")
74
+ .replace(/\u2028/g, "\\u2028")
75
+ .replace(/\u2029/g, "\\u2029");
76
+ }
77
+
78
+ /** Dispatch an HTTP data request to a registered server loader or action. */
79
+ export async function handleRequest(
80
+ routes: readonly Route[],
81
+ request: Request,
82
+ ): Promise<Response> {
83
+ const match = matchRoutes(routes, new URL(request.url).pathname);
84
+ const reading = request.method === "GET";
85
+
86
+ if (!match) return Response.json({ error: "No route" }, { status: 404 });
87
+
88
+ if (!reading && !["POST", "PUT", "PATCH", "DELETE"].includes(request.method))
89
+ return Response.json({ error: "Method not allowed" }, { status: 405 });
90
+
91
+ const handler = reading ? match.route.loader : match.route.action;
92
+
93
+ if (!handler)
94
+ return Response.json(
95
+ { error: `No ${reading ? "loader" : "action"}` },
96
+ { status: 405 },
97
+ );
98
+
99
+ const result = await safeWrapAsync(() =>
100
+ handler({ request, params: match.params, signal: request.signal }),
101
+ );
102
+ const error = result.error ?? (result.data instanceof Error && result.data);
103
+
104
+ if (error) return Response.json({ error: error.message }, { status: 500 });
105
+
106
+ if (result.data instanceof Response) {
107
+ const location = result.data.headers.get("Location");
108
+
109
+ if (location)
110
+ return new Response(null, {
111
+ headers: { "X-Smolter-Redirect": location },
112
+ });
113
+
114
+ return result.data;
115
+ }
116
+
117
+ return Response.json(result.data ?? null, {
118
+ headers: { "Cache-Control": "no-store" },
119
+ });
120
+ }