silgi 0.51.3 → 0.51.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present productdevbook
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.
package/README.md CHANGED
@@ -8,88 +8,7 @@
8
8
  <a href="https://github.com/productdevbook/silgi/blob/main/LICENSE"><img src="https://img.shields.io/github/license/productdevbook/silgi?style=flat&colorA=0a0908&colorB=edc462" alt="license"></a>
9
9
  </p>
10
10
 
11
- ## Quick Start
12
-
13
- ```bash
14
- npm install silgi
15
- ```
16
-
17
- ```ts
18
- import { silgi } from 'silgi'
19
- import { z } from 'zod'
20
-
21
- const s = silgi({ context: (req) => ({ db: getDB() }) })
22
-
23
- const appRouter = s.router({
24
- users: {
25
- list: k
26
- .$input(z.object({ limit: z.number().optional() }))
27
- .$resolve(({ input, ctx }) => ctx.db.users.find({ take: input.limit })),
28
- },
29
- })
30
-
31
- s.serve(appRouter, { port: 3000, scalar: true })
32
- ```
33
-
34
- ## Features
35
-
36
- - **Single package** — server, client, 15 plugins, 14 adapters. One install.
37
- - **Compiled pipeline** — guards unrolled, handlers pre-linked at startup.
38
- - **Guard / Wrap** — guards enrich context (flat, sync fast-path). Wraps run before + after (onion).
39
- - **Content negotiation** — JSON, MessagePack, devalue. Automatic from `Accept` header.
40
- - **Contract-first** — define API shape, share types, implement separately.
41
- - **Standard Schema** — Zod, Valibot, ArkType.
42
-
43
- ## Adapters
44
-
45
- | | Import |
46
- |---|---|
47
- | Standalone | `s.serve()` / `s.handler()` |
48
- | Nitro v3 | `serverEntry` + `s.handler()` |
49
- | Express | `silgi/express` |
50
- | Fastify | `silgi/fastify` |
51
- | Elysia | `silgi/elysia` |
52
- | Next.js | `silgi/nextjs` |
53
- | Nuxt | via Nitro `serverEntry` |
54
- | SvelteKit | `silgi/sveltekit` |
55
- | Remix | `silgi/remix` |
56
- | Astro | `silgi/astro` |
57
- | SolidStart | `silgi/solidstart` |
58
- | NestJS | `silgi/nestjs` |
59
- | AWS Lambda | `silgi/aws-lambda` |
60
- | MessagePort | `silgi/message-port` |
61
-
62
- ## Ecosystem
63
-
64
- Built-in re-exports — no extra dependencies needed:
65
-
66
- | Import | Package | Use case |
67
- |---|---|---|
68
- | `silgi/unstorage` | unstorage | Key-value storage (Redis, KV, S3) |
69
- | `silgi/ocache` | ocache | Cached functions with TTL + SWR |
70
- | `silgi/ofetch` | ofetch | Universal fetch with auto-retry |
71
- | `silgi/srvx` | srvx | Universal server (Node, Deno, Bun) |
72
-
73
- ## Integrations
74
-
75
- - **TanStack Query** — `queryOptions`, `mutationOptions`, `infiniteOptions`, `skipToken`
76
- - **React Server Actions** — `createAction`, `useServerAction`, `useOptimisticServerAction`
77
- - **AI SDK** — `routerToTools()` turns procedures into LLM tools
78
- - **tRPC Interop** — `fromTRPC()` for incremental migration
79
-
80
- ## Examples
81
-
82
- ```bash
83
- npx giget@latest gh:productdevbook/silgi/examples/standalone my-app
84
- npx giget@latest gh:productdevbook/silgi/examples/nextjs my-nextjs-app
85
- npx giget@latest gh:productdevbook/silgi/examples/nuxt my-nuxt-app
86
- ```
87
-
88
- 10 examples: standalone, bun, express, elysia, nitro, nitro-h3, nextjs, nuxt, sveltekit, client-react.
89
-
90
- ## Documentation
91
-
92
- [silgi.dev](https://silgi.dev)
11
+ End-to-end type-safe RPC framework for TypeScript. Single package — server, client, 15 plugins, 14 adapters. Full docs at [silgi.dev](https://silgi.dev).
93
12
 
94
13
  ## Credits
95
14
 
@@ -97,7 +16,6 @@ npx giget@latest gh:productdevbook/silgi/examples/nuxt my-nuxt-app
97
16
  - [tRPC](https://github.com/trpc/trpc) — Router/procedure model, end-to-end type inference
98
17
  - [Elysia](https://github.com/elysiajs/elysia) — Sucrose-style static handler analysis
99
18
 
100
-
101
19
  ## License
102
20
 
103
21
  MIT
@@ -86,7 +86,7 @@ function wrapHandler(handler, router, options, prefix) {
86
86
  if (options.analytics) {
87
87
  const { wrapWithAnalytics } = await import("../plugins/analytics.mjs");
88
88
  const analyticsOpts = typeof options.analytics === "object" ? options.analytics : {};
89
- h = wrapWithAnalytics(h, analyticsOpts);
89
+ h = wrapWithAnalytics(h, router, analyticsOpts);
90
90
  }
91
91
  wrapped = h;
92
92
  }
@@ -151,7 +151,7 @@ function tracing(config) {
151
151
  try {
152
152
  const request = ctx.request;
153
153
  if (!request) return;
154
- const silgiCtx = request.__silgiCtx;
154
+ const silgiCtx = request.__silgiCtx ?? getCtx();
155
155
  if (!silgiCtx) return;
156
156
  const reqTrace = silgiCtx.__analyticsTrace;
157
157
  if (!reqTrace) return;
@@ -16,6 +16,11 @@ declare class AnalyticsCollector {
16
16
  /** Cost tracker */
17
17
  costTracker: CostTracker;
18
18
  constructor(options?: AnalyticsOptions);
19
+ /** Set procedure schemas extracted from router definition. */
20
+ setProcedureSchemas(schemas: Map<string, {
21
+ input?: Record<string, unknown>;
22
+ output?: Record<string, unknown>;
23
+ }>): void;
19
24
  /** Check if a path is server-side ignored (from config). */
20
25
  isIgnored(pathname: string): boolean;
21
26
  /** Check if a path is hidden in the dashboard (from runtime API). */
@@ -38,6 +38,8 @@ var AnalyticsCollector = class {
38
38
  #ignorePaths;
39
39
  /** Client-side hide — from dashboard, filters display only */
40
40
  #hiddenPaths = /* @__PURE__ */ new Set();
41
+ /** Procedure input/output JSON schemas (set once from router) */
42
+ #procedureSchemas = null;
41
43
  /** SSE hub for real-time streaming */
42
44
  sseHub;
43
45
  /** Multi-tier time-series aggregation */
@@ -71,6 +73,10 @@ var AnalyticsCollector = class {
71
73
  for (const p of paths) this.#hiddenPaths.add(p);
72
74
  });
73
75
  }
76
+ /** Set procedure schemas extracted from router definition. */
77
+ setProcedureSchemas(schemas) {
78
+ this.#procedureSchemas = schemas;
79
+ }
74
80
  /** Check if a path is server-side ignored (from config). */
75
81
  isIgnored(pathname) {
76
82
  if (this.#ignorePaths.size === 0) return false;
@@ -222,7 +228,7 @@ var AnalyticsCollector = class {
222
228
  let totalLatencyCount = 0;
223
229
  for (const [path, entry] of this.#procedures) {
224
230
  const avg = entry.latencies.avg();
225
- procedures[path] = {
231
+ const snapshot = {
226
232
  count: entry.count,
227
233
  errors: entry.errors,
228
234
  errorRate: entry.count > 0 ? round(entry.errors / entry.count * 100) : 0,
@@ -235,6 +241,10 @@ var AnalyticsCollector = class {
235
241
  lastError: entry.lastError,
236
242
  lastErrorTime: entry.lastErrorTime || null
237
243
  };
244
+ const schemaInfo = this.#procedureSchemas?.get(path);
245
+ if (schemaInfo?.input) snapshot.inputSchema = schemaInfo.input;
246
+ if (schemaInfo?.output) snapshot.outputSchema = schemaInfo.output;
247
+ procedures[path] = snapshot;
238
248
  totalLatencySum += avg * entry.latencies.count;
239
249
  totalLatencyCount += entry.latencies.count;
240
250
  }
@@ -119,6 +119,8 @@ interface ProcedureSnapshot {
119
119
  };
120
120
  lastError: string | null;
121
121
  lastErrorTime: number | null;
122
+ inputSchema?: Record<string, unknown>;
123
+ outputSchema?: Record<string, unknown>;
122
124
  }
123
125
  interface TaskSnapshot {
124
126
  totalRuns: number;
@@ -1,3 +1,4 @@
1
+ import { RouterDef } from "../types.mjs";
1
2
  import { AnalyticsOptions, AnalyticsSnapshot, ErrorEntry, ProcedureCall, ProcedureSnapshot, RequestEntry, SpanKind, TaskExecution, TaskSnapshot, TraceSpan } from "./analytics/types.mjs";
2
3
  import { AnalyticsCollector } from "./analytics/collector.mjs";
3
4
  import { RequestTrace, trace } from "./analytics/trace.mjs";
@@ -9,10 +10,14 @@ import { analyticsTraceMap } from "../core/trace-map.mjs";
9
10
  import { FetchHandler } from "../core/handler.mjs";
10
11
 
11
12
  //#region src/plugins/analytics.d.ts
13
+ interface ProcedureSchemaInfo {
14
+ input?: Record<string, unknown>;
15
+ output?: Record<string, unknown>;
16
+ }
12
17
  /**
13
18
  * Wrap a fetch handler with analytics collection.
14
19
  * Intercepts analytics dashboard routes and instruments every request.
15
20
  */
16
- declare function wrapWithAnalytics(handler: FetchHandler, options?: AnalyticsOptions): FetchHandler;
21
+ declare function wrapWithAnalytics(handler: FetchHandler, router: RouterDef | undefined, options?: AnalyticsOptions): FetchHandler;
17
22
  //#endregion
18
- export { AnalyticsCollector, type AnalyticsOptions, type AnalyticsSnapshot, type ErrorEntry, type ProcedureCall, type ProcedureSnapshot, RequestAccumulator, type RequestEntry, RequestTrace, type SpanKind, type TaskExecution, type TaskSnapshot, type TraceSpan, analyticsAuthResponse, analyticsHTML, analyticsTraceMap, checkAnalyticsAuth, errorToMarkdown, requestToMarkdown, sanitizeHeaders, serveAnalyticsRoute, trace, wrapWithAnalytics };
23
+ export { AnalyticsCollector, type AnalyticsOptions, type AnalyticsSnapshot, type ErrorEntry, type ProcedureCall, ProcedureSchemaInfo, type ProcedureSnapshot, RequestAccumulator, type RequestEntry, RequestTrace, type SpanKind, type TaskExecution, type TaskSnapshot, type TraceSpan, analyticsAuthResponse, analyticsHTML, analyticsTraceMap, checkAnalyticsAuth, errorToMarkdown, requestToMarkdown, sanitizeHeaders, serveAnalyticsRoute, trace, wrapWithAnalytics };
@@ -2,6 +2,7 @@ import { ValidationError } from "../core/schema.mjs";
2
2
  import { SilgiError, toSilgiError } from "../core/error.mjs";
3
3
  import { analyticsTraceMap } from "../core/trace-map.mjs";
4
4
  import { parseUrlPathname } from "../core/url.mjs";
5
+ import { ZodSchemaConverter } from "../integrations/zod/converter.mjs";
5
6
  import { generateRequestId } from "./analytics/request-id.mjs";
6
7
  import { isTrackedRequestPath, normalizeAnalyticsPath, round, sanitizeHeaders } from "./analytics/utils.mjs";
7
8
  import { RequestAccumulator } from "./analytics/accumulator.mjs";
@@ -80,12 +81,48 @@ function extractResponseError(output, status, fallback) {
80
81
  message: `Request failed with status ${status}`
81
82
  };
82
83
  }
84
+ function isProcedureDef(value) {
85
+ return typeof value === "object" && value !== null && "type" in value && "resolve" in value && typeof value.resolve === "function";
86
+ }
87
+ const _zodConverter = new ZodSchemaConverter();
88
+ function schemaToJson(schema, strategy) {
89
+ if (!schema) return void 0;
90
+ const std = schema["~standard"];
91
+ if (std?.jsonSchema?.input) try {
92
+ const result = std.jsonSchema.input({ target: "draft-2020-12" });
93
+ if (result && typeof result === "object") {
94
+ const { $schema: _, ...rest } = result;
95
+ return rest;
96
+ }
97
+ } catch {}
98
+ if (_zodConverter.condition(schema)) try {
99
+ const [, json] = _zodConverter.convert(schema, { strategy });
100
+ return json;
101
+ } catch {}
102
+ }
103
+ function extractProcedureSchemas(router) {
104
+ const schemas = /* @__PURE__ */ new Map();
105
+ function walk(node, path) {
106
+ if (isProcedureDef(node)) {
107
+ const info = {};
108
+ if (node.input) info.input = schemaToJson(node.input, "input");
109
+ if (node.output) info.output = schemaToJson(node.output, "output");
110
+ if (info.input || info.output) schemas.set(path.join("/"), info);
111
+ return;
112
+ }
113
+ if (typeof node === "object" && node !== null) for (const [key, child] of Object.entries(node)) walk(child, [...path, key]);
114
+ }
115
+ walk(router, []);
116
+ return schemas;
117
+ }
83
118
  /**
84
119
  * Wrap a fetch handler with analytics collection.
85
120
  * Intercepts analytics dashboard routes and instruments every request.
86
121
  */
87
- function wrapWithAnalytics(handler, options = {}) {
122
+ function wrapWithAnalytics(handler, router, options = {}) {
88
123
  const collector = new AnalyticsCollector(options);
124
+ const procedureSchemas = router ? extractProcedureSchemas(router) : void 0;
125
+ if (procedureSchemas) collector.setProcedureSchemas(procedureSchemas);
89
126
  const dashboardHtml = analyticsHTML();
90
127
  const auth = options.auth;
91
128
  import("../core/task.mjs").then(({ setTaskAnalytics }) => {
package/dist/scalar.mjs CHANGED
@@ -1,5 +1,12 @@
1
+ import { ZodSchemaConverter } from "./integrations/zod/converter.mjs";
1
2
  //#region src/scalar.ts
2
3
  /**
4
+ * Scalar API Reference — v2 OpenAPI integration.
5
+ *
6
+ * Generates OpenAPI 3.1.0 spec from v2 RouterDef and serves
7
+ * Scalar UI at /api/reference + spec at /api/openapi.json.
8
+ */
9
+ /**
3
10
  * Generate OpenAPI 3.1.0 document from a v2 RouterDef.
4
11
  */
5
12
  /**
@@ -67,7 +74,7 @@ function generateOpenAPI(router, options = {}, basePath = "") {
67
74
  if (route?.security === false) operation.security = [];
68
75
  else if (route?.security) operation.security = route.security.map((s) => ({ [s]: [] }));
69
76
  else if (options.security) operation.security = [{ auth: [] }];
70
- const inputSchema = proc.input ? schemaToJsonSchema(proc.input) : null;
77
+ const inputSchema = proc.input ? schemaToJsonSchema(proc.input, "input") : null;
71
78
  const successStatus = route?.successStatus ?? 200;
72
79
  const successDesc = route?.successDescription ?? "Successful response";
73
80
  const guards = (proc.use ?? []).filter((m) => m.kind === "guard" && m.errors);
@@ -100,7 +107,7 @@ function generateOpenAPI(router, options = {}, basePath = "") {
100
107
  };
101
108
  if (params.length > 0) op.parameters = params;
102
109
  if (proc.type === "subscription") {
103
- const outputSchema = proc.output ? schemaToJsonSchema(proc.output) : { type: "string" };
110
+ const outputSchema = proc.output ? schemaToJsonSchema(proc.output, "output") : { type: "string" };
104
111
  op.responses[String(successStatus)] = {
105
112
  description: "SSE event stream",
106
113
  content: { "text/event-stream": { schema: {
@@ -110,7 +117,7 @@ function generateOpenAPI(router, options = {}, basePath = "") {
110
117
  };
111
118
  } else if (proc.output) op.responses[String(successStatus)] = {
112
119
  description: successDesc,
113
- content: { "application/json": { schema: schemaToJsonSchema(proc.output) } }
120
+ content: { "application/json": { schema: schemaToJsonSchema(proc.output, "output") } }
114
121
  };
115
122
  else op.responses[String(successStatus)] = { description: successDesc };
116
123
  if (proc.input) op.responses["400"] = {
@@ -263,20 +270,25 @@ function collectProcedures(node, path, cb) {
263
270
  if (typeof node === "object" && node !== null) for (const [key, child] of Object.entries(node)) collectProcedures(child, [...path, key], cb);
264
271
  }
265
272
  /**
266
- * Convert a Standard Schema to JSON Schema via `~standard.jsonSchema.input()`.
273
+ * Convert a Standard Schema to JSON Schema.
267
274
  *
268
- * Works with Zod v4, Valibot, ArkType — any validator implementing Standard Schema v1.
275
+ * Fast path: `~standard.jsonSchema.input()` (StandardJSONSchemaV1 implementors).
276
+ * Fallback: vendor-specific converters (Zod v4 via ZodSchemaConverter).
269
277
  */
270
- function schemaToJsonSchema(schema) {
278
+ const _zodConverter = new ZodSchemaConverter();
279
+ function schemaToJsonSchema(schema, strategy = "input") {
271
280
  const std = schema["~standard"];
272
- if (!std?.jsonSchema?.input) return {};
273
- try {
281
+ if (std?.jsonSchema?.input) try {
274
282
  const result = std.jsonSchema.input({ target: "draft-2020-12" });
275
283
  if (result && typeof result === "object") {
276
284
  const { $schema: _, ...rest } = result;
277
285
  return rest;
278
286
  }
279
287
  } catch {}
288
+ if (_zodConverter.condition(schema)) try {
289
+ const [, jsonSchema] = _zodConverter.convert(schema, { strategy });
290
+ return jsonSchema;
291
+ } catch {}
280
292
  return {};
281
293
  }
282
294
  function objectSchemaToParams(schema) {