@kitledger/server 0.0.4 → 0.0.6
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/dist/main.d.ts +39 -0
- package/dist/main.js +103 -0
- package/package.json +9 -6
- package/dist/server.d.ts +0 -12
- package/dist/server.js +0 -45
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type KitledgerConfig } from "@kitledger/core";
|
|
2
|
+
import { StaticUIConfig } from "@kitledger/core/ui";
|
|
3
|
+
import { Hono } from "hono";
|
|
4
|
+
type ServerConfig = ServerOptions;
|
|
5
|
+
type KitledgerContext = {
|
|
6
|
+
Variables: {
|
|
7
|
+
config: KitledgerConfig;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Options for configuring the Kitledger server.
|
|
12
|
+
*/
|
|
13
|
+
export type ServerOptions = {
|
|
14
|
+
systemConfig: KitledgerConfig;
|
|
15
|
+
staticPaths?: string[];
|
|
16
|
+
staticUIs?: StaticUIConfig[];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Factory function to define the server configuration.
|
|
20
|
+
*/
|
|
21
|
+
export declare function defineServerConfig(options: ServerOptions): ServerConfig;
|
|
22
|
+
/**
|
|
23
|
+
* Factory function to create a Hono server based on the detected runtime.
|
|
24
|
+
* * This is the main entry point for the server package. It returns an initialized
|
|
25
|
+
* Hono instance that has the system configuration injected into its context.
|
|
26
|
+
* * @param config - The {@link ServerConfig} object containing system settings, static paths, and UI definitions.
|
|
27
|
+
* @returns A Promise that resolves to the configured Hono application instance.
|
|
28
|
+
* * @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* // Basic usage with Bun
|
|
31
|
+
* const server = await createServer({
|
|
32
|
+
* systemConfig: myConfig,
|
|
33
|
+
* staticPaths: ['/public']
|
|
34
|
+
* });
|
|
35
|
+
* * export default { fetch: server.fetch };
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function createServer(config: ServerConfig): Promise<Hono<KitledgerContext, import("hono/types").BlankSchema, "/">>;
|
|
39
|
+
export {};
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
function detectRuntime() {
|
|
3
|
+
// @ts-ignore: Deno global detection
|
|
4
|
+
if (typeof Deno !== "undefined")
|
|
5
|
+
return "deno";
|
|
6
|
+
// @ts-ignore: Bun global detection
|
|
7
|
+
if (typeof Bun !== "undefined")
|
|
8
|
+
return "bun";
|
|
9
|
+
if (typeof process !== "undefined" && process.versions?.node)
|
|
10
|
+
return "node";
|
|
11
|
+
return "unknown";
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Factory function to define the server configuration.
|
|
15
|
+
*/
|
|
16
|
+
export function defineServerConfig(options) {
|
|
17
|
+
return options;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Factory function to create a Hono server based on the detected runtime.
|
|
21
|
+
* * This is the main entry point for the server package. It returns an initialized
|
|
22
|
+
* Hono instance that has the system configuration injected into its context.
|
|
23
|
+
* * @param config - The {@link ServerConfig} object containing system settings, static paths, and UI definitions.
|
|
24
|
+
* @returns A Promise that resolves to the configured Hono application instance.
|
|
25
|
+
* * @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* // Basic usage with Bun
|
|
28
|
+
* const server = await createServer({
|
|
29
|
+
* systemConfig: myConfig,
|
|
30
|
+
* staticPaths: ['/public']
|
|
31
|
+
* });
|
|
32
|
+
* * export default { fetch: server.fetch };
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export async function createServer(config) {
|
|
36
|
+
const server = new Hono();
|
|
37
|
+
/**
|
|
38
|
+
* Ensure all requests have access to the system config.
|
|
39
|
+
*/
|
|
40
|
+
server.use("*", (c, next) => {
|
|
41
|
+
c.set("config", config.systemConfig);
|
|
42
|
+
return next();
|
|
43
|
+
});
|
|
44
|
+
const runtime = detectRuntime();
|
|
45
|
+
// 4. Type-Safe Variable Definition
|
|
46
|
+
let serveStatic;
|
|
47
|
+
// 5. Dynamic Loading based on detected runtime
|
|
48
|
+
switch (runtime) {
|
|
49
|
+
case "node": {
|
|
50
|
+
const mod = await import("@hono/node-server/serve-static");
|
|
51
|
+
serveStatic = mod.serveStatic;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case "deno": {
|
|
55
|
+
const mod = await import("hono/deno");
|
|
56
|
+
serveStatic = mod.serveStatic;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case "bun": {
|
|
60
|
+
const mod = await import("hono/bun");
|
|
61
|
+
serveStatic = mod.serveStatic;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
default:
|
|
65
|
+
throw new Error(`Unsupported or undetected runtime: ${runtime}`);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Static Apps Serving
|
|
69
|
+
*/
|
|
70
|
+
if (config.staticUIs) {
|
|
71
|
+
for (const staticUI of config.staticUIs) {
|
|
72
|
+
server.get(`${staticUI.serverPath}/transactions/models`, (c) => {
|
|
73
|
+
return c.json(c.get("config").transactionModels);
|
|
74
|
+
});
|
|
75
|
+
server.get(`${staticUI.serverPath}/entities/models`, (c) => {
|
|
76
|
+
return c.json(c.get("config").entityModels);
|
|
77
|
+
});
|
|
78
|
+
const cleanPath = staticUI.basePath.endsWith("/") ? staticUI.basePath.slice(0, -1) : staticUI.basePath;
|
|
79
|
+
server.use(`${cleanPath}/*`, serveStatic({
|
|
80
|
+
root: staticUI.assetsPath,
|
|
81
|
+
rewriteRequestPath: (path) => {
|
|
82
|
+
const p = path.slice(cleanPath.length).replace(/^\//, "");
|
|
83
|
+
if (p === "" || p === "index.html")
|
|
84
|
+
return "__404__";
|
|
85
|
+
return p;
|
|
86
|
+
},
|
|
87
|
+
}));
|
|
88
|
+
server.get(`${cleanPath}/*`, (c) => c.html(staticUI.htmlContent));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Serve other static paths
|
|
93
|
+
*/
|
|
94
|
+
if (config.staticPaths) {
|
|
95
|
+
for (const staticPath of config.staticPaths) {
|
|
96
|
+
server.use(staticPath, serveStatic({
|
|
97
|
+
root: ".",
|
|
98
|
+
rewriteRequestPath: (path) => path.slice(staticPath.length).replace(/^\//, ""),
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return server;
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kitledger/server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"types": "./dist/
|
|
10
|
-
"default": "./dist/
|
|
9
|
+
"types": "./dist/main.d.ts",
|
|
10
|
+
"default": "./dist/main.js"
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
|
+
"!dist/**/*.test.js",
|
|
14
15
|
"dist"
|
|
15
16
|
],
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"@hono/node-server": "^1.19.6",
|
|
18
19
|
"hono": "^4.10.7",
|
|
19
|
-
"
|
|
20
|
+
"valibot": "^1.1.0",
|
|
21
|
+
"@kitledger/core": "0.0.6"
|
|
20
22
|
},
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@kitledger/admin": "0.0.4"
|
|
23
25
|
},
|
|
24
26
|
"publishConfig": {
|
|
25
27
|
"access": "public"
|
|
26
28
|
},
|
|
27
29
|
"scripts": {
|
|
28
30
|
"build": "tsc",
|
|
31
|
+
"test": "vitest run",
|
|
29
32
|
"typecheck": "tsc --noEmit"
|
|
30
33
|
}
|
|
31
34
|
}
|
package/dist/server.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type KitledgerConfig } from "@kitledger/core";
|
|
2
|
-
import { StaticUIConfig } from "@kitledger/core/ui";
|
|
3
|
-
import { Hono } from "hono";
|
|
4
|
-
export type ServerOptions = {
|
|
5
|
-
systemConfig: KitledgerConfig;
|
|
6
|
-
runtime: "node";
|
|
7
|
-
staticPaths?: string[];
|
|
8
|
-
staticUIs?: StaticUIConfig[];
|
|
9
|
-
};
|
|
10
|
-
export type ServerConfig = ServerOptions;
|
|
11
|
-
export declare function defineServerConfig(options: ServerOptions): ServerConfig;
|
|
12
|
-
export declare function createServer(config: ServerConfig): Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
|
package/dist/server.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { serveStatic } from "@hono/node-server/serve-static";
|
|
2
|
-
import { Hono } from "hono";
|
|
3
|
-
export function defineServerConfig(options) {
|
|
4
|
-
return options;
|
|
5
|
-
}
|
|
6
|
-
export function createServer(config) {
|
|
7
|
-
const server = new Hono();
|
|
8
|
-
/**
|
|
9
|
-
* Static Apps Serving
|
|
10
|
-
*/
|
|
11
|
-
if (config.staticUIs) {
|
|
12
|
-
for (const staticUI of config.staticUIs) {
|
|
13
|
-
server.get(`${staticUI.serverPath}/transactions/models`, (c) => {
|
|
14
|
-
return c.json(config.systemConfig.transactionModels);
|
|
15
|
-
});
|
|
16
|
-
server.get(`${staticUI.serverPath}/entities/models`, (c) => {
|
|
17
|
-
return c.json(config.systemConfig.entityModels);
|
|
18
|
-
});
|
|
19
|
-
// Remove trailing slash if path ends with '/'
|
|
20
|
-
const cleanPath = staticUI.basePath.endsWith("/") ? staticUI.basePath.slice(0, -1) : staticUI.basePath;
|
|
21
|
-
server.use(`${cleanPath}/*`, serveStatic({
|
|
22
|
-
root: staticUI.assetsPath,
|
|
23
|
-
rewriteRequestPath: (path) => {
|
|
24
|
-
const p = path.replace(new RegExp(`^${cleanPath}`), "").replace(/^\//, "");
|
|
25
|
-
if (p === "" || p === "index.html")
|
|
26
|
-
return "__404__";
|
|
27
|
-
return p;
|
|
28
|
-
},
|
|
29
|
-
}));
|
|
30
|
-
server.get(`${cleanPath}/*`, (c) => c.html(staticUI.htmlContent));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Server other static paths
|
|
35
|
-
*/
|
|
36
|
-
if (config.staticPaths) {
|
|
37
|
-
for (const staticPath of config.staticPaths) {
|
|
38
|
-
server.use(staticPath, serveStatic({
|
|
39
|
-
root: ".",
|
|
40
|
-
rewriteRequestPath: (path) => path.replace(new RegExp(`^${staticPath}`), "").replace(/^\//, ""),
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return server;
|
|
45
|
-
}
|