@thexjs/core 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Abdelkabir Ouadoukou
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,192 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { Database } from 'bun:sqlite';
4
+
5
+ interface RouteEntry {
6
+ filePath: string;
7
+ routePath: string;
8
+ paramNames: string[];
9
+ isApi: boolean;
10
+ }
11
+ interface MiddlewareEntry {
12
+ filePath: string;
13
+ dirPath: string;
14
+ }
15
+ /** Scan page routes — same as scanRoutes but never marks anything as api. */
16
+ declare function scanPages(rootDir: string): RouteEntry[];
17
+ /** Scan API routes from a separate directory (not nested under pages/routes).
18
+ * Route paths are prefixed with /api so they match requests to /api/…. */
19
+ declare function scanApiDir(rootDir: string): RouteEntry[];
20
+ declare function scanRoutes(rootDir: string): RouteEntry[];
21
+ interface LayoutEntry {
22
+ filePath: string;
23
+ dirPath: string;
24
+ }
25
+ declare function scanLayouts(rootDir: string): LayoutEntry[];
26
+ /**
27
+ * Scan a dedicated layouts directory. Every layout file found is registered
28
+ * as a root-level layout. The file name becomes the layout key (e.g.
29
+ * main.tsx -> "main"), and it covers the root path "/" so it wraps every page.
30
+ * Nested _layout.tsx files inside pages/ still work for directory-level
31
+ * nesting — this is additive, not exclusive.
32
+ */
33
+ declare function scanLayoutsDir(layoutsDir: string): LayoutEntry[];
34
+ declare function findLayoutChain(routeFilePath: string, layouts: LayoutEntry[], routesDir: string): LayoutEntry[];
35
+ interface NotFoundEntry {
36
+ filePath: string;
37
+ }
38
+ declare function scanNotFound(rootDir: string): NotFoundEntry | null;
39
+ declare function scanMiddleware(rootDir: string): MiddlewareEntry[];
40
+ declare function findMiddlewareChain(routeFilePath: string, middleware: MiddlewareEntry[], routesDir: string): MiddlewareEntry[];
41
+ declare function generateManifestSource(routes: RouteEntry[]): string;
42
+ declare function writeManifest(routes: RouteEntry[], routesDir: string): string;
43
+
44
+ interface RouteProps {
45
+ params: Record<string, string>;
46
+ loaderData?: Record<string, unknown>;
47
+ }
48
+ interface CreateAppOptions {
49
+ /** Primary route directory (legacy — mixes pages, api, layouts together). */
50
+ routesDir?: string;
51
+ /** Separate directory for page routes (preferred over routesDir when set). */
52
+ pagesDir?: string;
53
+ /** Separate directory for API routes. Defaults to routesDir/api if not set. */
54
+ apiDir?: string;
55
+ /** Separate directory for layout wrappers. */
56
+ layoutsDir?: string;
57
+ contentDir?: string;
58
+ actionsDir?: string;
59
+ port?: number;
60
+ development?: boolean;
61
+ }
62
+ interface AppServeOptions {
63
+ routes: Record<string, (req: Request) => Promise<Response> | Response>;
64
+ development: boolean;
65
+ port: number;
66
+ fetch: (req: Request) => Response | Promise<Response>;
67
+ }
68
+ interface RevalidateOptions {
69
+ revalidate?: number;
70
+ }
71
+ declare function defineConfig(config: CreateAppOptions): CreateAppOptions;
72
+ declare function createApp(options: CreateAppOptions): Promise<AppServeOptions>;
73
+
74
+ declare function DefaultNotFound(_props: RouteProps): react.JSX.Element;
75
+
76
+ interface RenderOptions {
77
+ title?: string;
78
+ islandScripts?: string[];
79
+ islandProps?: Record<string, string>;
80
+ /** Path to a stylesheet to <link> in <head>, e.g. "/styles.css". */
81
+ stylesheet?: string | undefined;
82
+ /** Set to false to omit the client-side navigation script. Defaults to true. */
83
+ clientNav?: boolean;
84
+ /** Inject live-reload script (development mode). */
85
+ liveReload?: boolean;
86
+ }
87
+ interface LoaderArgs {
88
+ params: Record<string, string>;
89
+ request: Request;
90
+ }
91
+ type LoaderReturn = Record<string, unknown> | Response;
92
+ declare function renderPage(node: ReactNode, options?: RenderOptions): string;
93
+ declare function renderStaticPage(node: ReactNode, options?: RenderOptions): string;
94
+ declare function renderStreamingPage(node: ReactNode, options?: RenderOptions): Promise<ReadableStream<Uint8Array>>;
95
+
96
+ /**
97
+ * Inline client-side navigation script.
98
+ *
99
+ * Embedded directly into every rendered page as a <script> tag (see render.ts).
100
+ * Intercepts same-origin <a> clicks, fetches the destination page, and swaps
101
+ * the #root element's content in place. Includes hover-prefetch and popstate
102
+ * (back/forward) support.
103
+ */
104
+ declare const CLIENT_NAV_SCRIPT = "(function () {\n if (window.__xNav) return;\n window.__xNav = true;\n\n var cache = new Map();\n var CACHE_LIMIT = 30;\n\n function isEligibleLink(link) {\n if (!link) return null;\n if (link.hasAttribute(\"data-no-nav\")) return null;\n if (link.target && link.target !== \"_self\") return null;\n if (link.hasAttribute(\"download\")) return null;\n var href = link.getAttribute(\"href\");\n if (!href) return null;\n if (/^(#|mailto:|tel:|javascript:)/i.test(href)) return null;\n\n var url;\n try {\n url = new URL(href, location.href);\n } catch (e) {\n return null;\n }\n if (url.origin !== location.origin) return null;\n\n var current = new URL(location.href);\n var samePage = url.pathname === current.pathname && url.search === current.search;\n if (samePage && url.hash) return null;\n\n return url;\n }\n\n function setLoading(isLoading) {\n document.documentElement.classList.toggle(\"x-nav-loading\", isLoading);\n }\n\n function runScripts(container) {\n var scripts = container.querySelectorAll(\"script\");\n for (var i = 0; i < scripts.length; i++) {\n var old = scripts[i];\n var fresh = document.createElement(\"script\");\n for (var j = 0; j < old.attributes.length; j++) {\n var attr = old.attributes[j];\n fresh.setAttribute(attr.name, attr.value);\n }\n fresh.textContent = old.textContent;\n old.replaceWith(fresh);\n }\n }\n\n function evictIfFull() {\n if (cache.size < CACHE_LIMIT) return;\n var oldest = cache.keys().next().value;\n if (oldest !== undefined) cache.delete(oldest);\n }\n\n function fetchPage(url) {\n var key = url.href;\n if (cache.has(key)) return cache.get(key);\n var promise = fetch(url.href, { headers: { \"X-X-Nav\": \"1\" } })\n .then(function (res) {\n return res.text().then(function (html) {\n var contentType = res.headers.get(\"content-type\") || \"\";\n return {\n html: html,\n ok: res.ok,\n finalUrl: res.url || url.href,\n isHtml: contentType.indexOf(\"text/html\") !== -1,\n };\n });\n })\n .catch(function (err) {\n cache.delete(key);\n throw err;\n });\n evictIfFull();\n cache.set(key, promise);\n return promise;\n }\n\n function prefetch(url) {\n if (cache.has(url.href)) return;\n fetchPage(url).catch(function () {});\n }\n\n function navigate(url, push) {\n setLoading(true);\n return fetchPage(url)\n .then(function (entry) {\n if (!entry.isHtml) {\n location.href = url.href;\n return;\n }\n var doc = new DOMParser().parseFromString(entry.html, \"text/html\");\n var newRoot = doc.getElementById(\"root\");\n var currentRoot = document.getElementById(\"root\");\n if (!newRoot || !currentRoot) {\n location.href = url.href;\n return;\n }\n\n if (push) history.pushState({ xNav: true }, \"\", entry.finalUrl);\n if (doc.title) document.title = doc.title;\n currentRoot.innerHTML = newRoot.innerHTML;\n runScripts(currentRoot);\n\n if (url.hash) {\n var target = document.getElementById(url.hash.slice(1));\n if (target) {\n target.scrollIntoView();\n return;\n }\n }\n window.scrollTo(0, 0);\n })\n .catch(function () {\n location.href = url.href;\n })\n .finally(function () {\n setLoading(false);\n });\n }\n\n document.addEventListener(\"click\", function (event) {\n if (event.defaultPrevented || event.button !== 0) return;\n if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;\n\n var link = event.target && event.target.closest ? event.target.closest(\"a[href]\") : null;\n var url = isEligibleLink(link);\n if (!url) return;\n\n event.preventDefault();\n if (url.href === location.href) return;\n navigate(url, true);\n });\n\n document.addEventListener(\n \"mouseover\",\n function (event) {\n var link = event.target && event.target.closest ? event.target.closest(\"a[href]\") : null;\n var url = isEligibleLink(link);\n if (url) prefetch(url);\n },\n { passive: true },\n );\n\n window.addEventListener(\"popstate\", function () {\n navigate(new URL(location.href), false);\n });\n\n var style = document.createElement(\"style\");\n style.textContent = [\n \".x-nav-loading{cursor:progress}\",\n \".x-nav-loading::after{content:'';position:fixed;top:0;left:0;height:2px;width:100%;\",\n \"background:var(--x-accent,#6ab0ff);z-index:9999;animation:x-nav-progress .8s ease-in-out infinite}\",\n \"@keyframes x-nav-progress{0%{transform:translateX(-100%)}50%{transform:translateX(0)}100%{transform:translateX(100%)}}\",\n ].join(\"\");\n document.head.appendChild(style);\n})();";
105
+
106
+ type RouteMode = "static" | "server";
107
+ interface BuildOptions {
108
+ routesDir?: string;
109
+ pagesDir?: string;
110
+ apiDir?: string;
111
+ layoutsDir?: string;
112
+ actionsDir?: string;
113
+ contentDir?: string;
114
+ outDir?: string;
115
+ }
116
+ declare function build(options: BuildOptions): Promise<void>;
117
+
118
+ interface Frontmatter {
119
+ title?: string;
120
+ [key: string]: unknown;
121
+ }
122
+ interface ContentEntry {
123
+ filePath: string;
124
+ routePath: string;
125
+ slug: string;
126
+ frontmatter: Frontmatter;
127
+ body: string;
128
+ }
129
+ declare function scanContent(rootDir: string): ContentEntry[];
130
+ declare function escapeHtml(s: string): string;
131
+ declare function renderMarkdown(md: string): string;
132
+
133
+ type IslandMode = "idle" | "visible" | "load";
134
+ interface IslandEntry {
135
+ name: string;
136
+ id: string;
137
+ }
138
+ declare function IslandProvider({ registry, children, }: {
139
+ registry: {
140
+ entries: IslandEntry[];
141
+ };
142
+ children?: ReactNode;
143
+ }): react.JSX.Element;
144
+ declare function Island({ name, client, children, }: {
145
+ name: string;
146
+ client?: IslandMode;
147
+ children: ReactNode;
148
+ }): react.JSX.Element;
149
+
150
+ interface MiddlewareContext {
151
+ params: Record<string, string>;
152
+ request: Request;
153
+ }
154
+ type MiddlewareNext = () => Promise<Response>;
155
+ type MiddlewareFn = (ctx: MiddlewareContext, next: MiddlewareNext) => Promise<Response>;
156
+ declare function composeMiddleware(fns: MiddlewareFn[], handler: (ctx: MiddlewareContext) => Promise<Response>): MiddlewareFn;
157
+
158
+ declare function generateServerFunctionClient(routeFilePath: string, fnNames: string[], endpointBase: string): string;
159
+ declare function resetServerFunctions(): void;
160
+ declare function registerServerFunctions(routePath: string, paramNames: string[], fns: Record<string, (...args: unknown[]) => Promise<unknown>>): void;
161
+ declare function getServerFunctionHandler(): (req: Request) => Promise<Response | null>;
162
+
163
+ declare function renderErrorOverlay(error: unknown): string;
164
+
165
+ interface SQLiteOptions {
166
+ path?: string;
167
+ wal?: boolean;
168
+ foreignKeys?: boolean;
169
+ }
170
+ declare function connectSQLite(options?: SQLiteOptions): Database;
171
+
172
+ interface PostgresClient {
173
+ unsafe(query: string): Promise<unknown>;
174
+ (strings: TemplateStringsArray, ...values: unknown[]): Promise<unknown>;
175
+ }
176
+ interface PostgresOptions {
177
+ url?: string;
178
+ }
179
+ declare function connectPostgres(options?: PostgresOptions): PostgresClient;
180
+
181
+ interface MigrationResult {
182
+ applied: string[];
183
+ skipped: string[];
184
+ }
185
+ declare function runSQLiteMigrations(db: Database, migrationsDir: string): MigrationResult;
186
+ interface PostgresMigrationResult {
187
+ applied: string[];
188
+ skipped: string[];
189
+ }
190
+ declare function runPostgresMigrations(client: PostgresClient, migrationsDir: string): Promise<PostgresMigrationResult>;
191
+
192
+ export { type BuildOptions, CLIENT_NAV_SCRIPT, type ContentEntry, type CreateAppOptions, DefaultNotFound, type Frontmatter, Island, type IslandEntry, type IslandMode, IslandProvider, type LayoutEntry, type LoaderArgs, type LoaderReturn, type MiddlewareContext, type MiddlewareEntry, type MiddlewareFn, type MiddlewareNext, type NotFoundEntry, type RevalidateOptions, type RouteEntry, type RouteMode, type RouteProps, build, composeMiddleware, connectPostgres, connectSQLite, createApp, defineConfig, escapeHtml, findLayoutChain, findMiddlewareChain, generateManifestSource, generateServerFunctionClient, getServerFunctionHandler, registerServerFunctions, renderErrorOverlay, renderMarkdown, renderPage, renderStaticPage, renderStreamingPage, resetServerFunctions, runPostgresMigrations, runSQLiteMigrations, scanApiDir, scanContent, scanLayouts, scanLayoutsDir, scanMiddleware, scanNotFound, scanPages, scanRoutes, writeManifest };