@kitledger/server 0.0.5 → 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 CHANGED
@@ -1,12 +1,39 @@
1
1
  import { type KitledgerConfig } from "@kitledger/core";
2
2
  import { StaticUIConfig } from "@kitledger/core/ui";
3
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
+ */
4
13
  export type ServerOptions = {
5
14
  systemConfig: KitledgerConfig;
6
15
  staticPaths?: string[];
7
16
  staticUIs?: StaticUIConfig[];
8
17
  };
9
- type ServerConfig = ServerOptions;
18
+ /**
19
+ * Factory function to define the server configuration.
20
+ */
10
21
  export declare function defineServerConfig(options: ServerOptions): ServerConfig;
11
- export declare function createServer(config: ServerConfig): Promise<Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">>;
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, "/">>;
12
39
  export {};
package/dist/main.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Hono } from "hono";
2
- // 3. The Auto-Detection Logic
3
2
  function detectRuntime() {
4
3
  // @ts-ignore: Deno global detection
5
4
  if (typeof Deno !== "undefined")
@@ -11,11 +10,37 @@ function detectRuntime() {
11
10
  return "node";
12
11
  return "unknown";
13
12
  }
13
+ /**
14
+ * Factory function to define the server configuration.
15
+ */
14
16
  export function defineServerConfig(options) {
15
17
  return options;
16
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
+ */
17
35
  export async function createServer(config) {
18
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
+ });
19
44
  const runtime = detectRuntime();
20
45
  // 4. Type-Safe Variable Definition
21
46
  let serveStatic;
@@ -45,16 +70,16 @@ export async function createServer(config) {
45
70
  if (config.staticUIs) {
46
71
  for (const staticUI of config.staticUIs) {
47
72
  server.get(`${staticUI.serverPath}/transactions/models`, (c) => {
48
- return c.json(config.systemConfig.transactionModels);
73
+ return c.json(c.get("config").transactionModels);
49
74
  });
50
75
  server.get(`${staticUI.serverPath}/entities/models`, (c) => {
51
- return c.json(config.systemConfig.entityModels);
76
+ return c.json(c.get("config").entityModels);
52
77
  });
53
78
  const cleanPath = staticUI.basePath.endsWith("/") ? staticUI.basePath.slice(0, -1) : staticUI.basePath;
54
79
  server.use(`${cleanPath}/*`, serveStatic({
55
80
  root: staticUI.assetsPath,
56
81
  rewriteRequestPath: (path) => {
57
- const p = path.replace(new RegExp(`^${cleanPath}`), "").replace(/^\//, "");
82
+ const p = path.slice(cleanPath.length).replace(/^\//, "");
58
83
  if (p === "" || p === "index.html")
59
84
  return "__404__";
60
85
  return p;
@@ -70,7 +95,7 @@ export async function createServer(config) {
70
95
  for (const staticPath of config.staticPaths) {
71
96
  server.use(staticPath, serveStatic({
72
97
  root: ".",
73
- rewriteRequestPath: (path) => path.replace(new RegExp(`^${staticPath}`), "").replace(/^\//, ""),
98
+ rewriteRequestPath: (path) => path.slice(staticPath.length).replace(/^\//, ""),
74
99
  }));
75
100
  }
76
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kitledger/server",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "private": false,
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",