silgi 0.51.3 → 0.51.4

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.
@@ -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) {