@tyndall/plugin-lunarx 0.0.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 ADDED
@@ -0,0 +1,25 @@
1
+ # @tyndall/plugin-lunarx
2
+
3
+ Compatibility helpers for LunarX-style hooks and router APIs on top of Tyndall.
4
+ This package exposes `useServerFetches`, `useSSRError`, `useSoftReload`, and
5
+ re-exports Tyndall's `Link`, `useRouter`, and `useBlockRouting`.
6
+
7
+ ## Usage
8
+
9
+ ```ts
10
+ import { createLunarxHooksPlugin, useServerFetches } from "@tyndall/plugin-lunarx";
11
+
12
+ export default {
13
+ plugins: [createLunarxHooksPlugin()],
14
+ };
15
+
16
+ export default function Page() {
17
+ const fetches = useServerFetches<{ greeting: string }>();
18
+ return <div>{fetches.data?.greeting}</div>;
19
+ }
20
+ ```
21
+
22
+ ## Notes
23
+
24
+ `useServerFetches` resolves the current route by reading the route data map
25
+ and falls back to init data when no page data is present.
@@ -0,0 +1,25 @@
1
+ import type { HyperPlugin } from "@tyndall/core";
2
+ import { Link, useBlockRouting, useRouter } from "@tyndall/react";
3
+ export type ServerFetchResult<T> = {
4
+ data?: T;
5
+ };
6
+ export declare const useServerFetches: <T = unknown>() => ServerFetchResult<T>;
7
+ export declare const useSSRError: <T = {
8
+ status?: number;
9
+ message?: string;
10
+ }>() => T | undefined;
11
+ export declare const useSoftReload: () => (() => void);
12
+ export { Link, useBlockRouting, useRouter };
13
+ export declare class ServerFetchError extends Error {
14
+ readonly statusCode?: number;
15
+ readonly data?: unknown;
16
+ constructor(message: string, options?: {
17
+ statusCode?: number;
18
+ data?: unknown;
19
+ cause?: unknown;
20
+ });
21
+ }
22
+ export declare const NewServerFetchError: (error: Error, data?: unknown, message?: string, statusCode?: number) => ServerFetchError;
23
+ export declare const createLunarxHooksPlugin: () => HyperPlugin;
24
+ export declare const lunarxHooksPlugin: () => HyperPlugin;
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 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_KEY, ROUTE_DATA_INIT_KEY, routeDataKeyForPage, } from "@tyndall/core";
2
+ import { Link, useBlockRouting, useRouteData, useRouter } from "@tyndall/react";
3
+ const resolveRouteIdFromData = (routeData) => {
4
+ for (const key of Object.keys(routeData)) {
5
+ if (key.startsWith("page:")) {
6
+ return key.slice("page:".length) || undefined;
7
+ }
8
+ }
9
+ return undefined;
10
+ };
11
+ const resolveCurrentRouteId = (routeData) => {
12
+ if (typeof window !== "undefined") {
13
+ const candidate = window.__HYPER_ROUTE_ID__;
14
+ if (typeof candidate === "string" && candidate.length > 0) {
15
+ return candidate;
16
+ }
17
+ const app = window.document?.getElementById("app");
18
+ const attr = app?.getAttribute("data-hyper-route");
19
+ if (attr) {
20
+ return attr;
21
+ }
22
+ }
23
+ return resolveRouteIdFromData(routeData);
24
+ };
25
+ export const useServerFetches = () => {
26
+ const routeData = useRouteData();
27
+ const routeId = resolveCurrentRouteId(routeData);
28
+ if (routeId) {
29
+ const key = routeDataKeyForPage(routeId);
30
+ if (key in routeData) {
31
+ return { data: routeData[key] };
32
+ }
33
+ }
34
+ return { data: routeData[ROUTE_DATA_INIT_KEY] };
35
+ };
36
+ export const useSSRError = () => {
37
+ const routeData = useRouteData();
38
+ return routeData[ROUTE_DATA_ERROR_KEY];
39
+ };
40
+ export const useSoftReload = () => {
41
+ const router = useRouter();
42
+ return () => {
43
+ void router.softReload();
44
+ };
45
+ };
46
+ export { Link, useBlockRouting, useRouter };
47
+ export class ServerFetchError extends Error {
48
+ constructor(message, options) {
49
+ super(message);
50
+ this.name = "ServerFetchError";
51
+ this.statusCode = options?.statusCode;
52
+ this.data = options?.data;
53
+ if (options?.cause) {
54
+ this.cause = options.cause;
55
+ }
56
+ }
57
+ }
58
+ export const NewServerFetchError = (error, data, message, statusCode) => {
59
+ const msg = message && message.length > 0 ? message : error.message;
60
+ return new ServerFetchError(msg, { statusCode, data, cause: error });
61
+ };
62
+ export const createLunarxHooksPlugin = () => ({
63
+ name: "@tyndall/plugin-lunarx",
64
+ });
65
+ export const lunarxHooksPlugin = createLunarxHooksPlugin;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@tyndall/plugin-lunarx",
3
+ "version": "0.0.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "bun": "./src/index.ts",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc -p tsconfig.json",
22
+ "lint": "oxlint .",
23
+ "format": "oxfmt ."
24
+ },
25
+ "dependencies": {
26
+ "@tyndall/core": "^0.0.1",
27
+ "@tyndall/react": "^0.0.1"
28
+ }
29
+ }