silgi 0.51.1 → 0.51.2

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.
@@ -1,3 +1,4 @@
1
+ import { analyticsTraceMap } from "../core/trace-map.mjs";
1
2
  import { createFetchHandler, wrapHandler } from "../core/handler.mjs";
2
3
  //#region src/adapters/_fetch-adapter.ts
3
4
  /**
@@ -26,7 +27,12 @@ function rewriteRequest(request, prefix) {
26
27
  function createFetchAdapter(router, options, defaultPrefix) {
27
28
  const prefix = options.prefix ?? defaultPrefix;
28
29
  const inner = createFetchHandler(router, options.context ?? (() => ({})));
29
- return wrapHandler((request) => inner(rewriteRequest(request, prefix)), router, options);
30
+ return wrapHandler((request) => {
31
+ const rewritten = rewriteRequest(request, prefix);
32
+ const trace = analyticsTraceMap.get(request);
33
+ if (trace) analyticsTraceMap.set(rewritten, trace);
34
+ return inner(rewritten);
35
+ }, router, options, prefix);
30
36
  }
31
37
  /**
32
38
  * Create a fetch-passthrough adapter for frameworks that pass an event object
@@ -45,8 +51,10 @@ function createEventFetchAdapter(router, options, defaultPrefix, extractRequest)
45
51
  const rewritten = rewriteRequest(request, prefix);
46
52
  const eventRef = requestEventMap.get(request);
47
53
  if (eventRef) requestEventMap.set(rewritten, eventRef);
54
+ const trace = analyticsTraceMap.get(request);
55
+ if (trace) analyticsTraceMap.set(rewritten, trace);
48
56
  return inner(rewritten);
49
- }, router, options);
57
+ }, router, options, prefix);
50
58
  return (event) => {
51
59
  const request = extractRequest(event);
52
60
  requestEventMap.set(request, event);
@@ -72,7 +72,7 @@ function makeResponse(output, route, format, ctx) {
72
72
  * Returns a new handler that applies wrappers on first request (async import).
73
73
  * If no wrappers are needed, returns the original handler as-is.
74
74
  */
75
- function wrapHandler(handler, router, options) {
75
+ function wrapHandler(handler, router, options, prefix) {
76
76
  if (!options?.scalar && !options?.analytics) return handler;
77
77
  let wrapped;
78
78
  let initPromise;
@@ -81,7 +81,7 @@ function wrapHandler(handler, router, options) {
81
81
  if (options.scalar) {
82
82
  const { wrapWithScalar } = await import("../scalar.mjs");
83
83
  const scalarOpts = typeof options.scalar === "object" ? options.scalar : {};
84
- h = wrapWithScalar(h, router, scalarOpts);
84
+ h = wrapWithScalar(h, router, scalarOpts, prefix);
85
85
  }
86
86
  if (options.analytics) {
87
87
  const { wrapWithAnalytics } = await import("../plugins/analytics.mjs");
package/dist/scalar.d.mts CHANGED
@@ -43,7 +43,7 @@ interface ScalarOptions {
43
43
  */
44
44
  cdn?: 'cdn' | 'unpkg' | 'local' | (string & {});
45
45
  }
46
- declare function generateOpenAPI(router: RouterDef, options?: ScalarOptions): Record<string, unknown>;
46
+ declare function generateOpenAPI(router: RouterDef, options?: ScalarOptions, basePath?: string): Record<string, unknown>;
47
47
  declare function scalarHTML(specUrl: string, options?: ScalarOptions): string;
48
48
  //#endregion
49
49
  export { ScalarOptions, generateOpenAPI, scalarHTML };
package/dist/scalar.mjs CHANGED
@@ -24,12 +24,13 @@ function toOpenAPIPath(raw) {
24
24
  pathParams
25
25
  };
26
26
  }
27
- function generateOpenAPI(router, options = {}) {
27
+ function generateOpenAPI(router, options = {}, basePath = "") {
28
28
  const paths = {};
29
29
  const tags = /* @__PURE__ */ new Map();
30
30
  collectProcedures(router, [], (path, proc) => {
31
31
  const route = proc.route;
32
- const { httpPath, pathParams } = toOpenAPIPath(route?.path ?? "/" + path.join("/"));
32
+ const { httpPath: routePath, pathParams } = toOpenAPIPath(route?.path ?? "/" + path.join("/"));
33
+ const httpPath = basePath ? basePath.replace(/\/$/, "") + routePath : routePath;
33
34
  const rawMethod = route?.method?.toLowerCase() ?? "post";
34
35
  const methods = rawMethod === "*" ? [
35
36
  "get",
@@ -293,17 +294,20 @@ function objectSchemaToParams(schema) {
293
294
  * Wrap a fetch handler to serve Scalar API Reference at /api/reference and /api/openapi.json.
294
295
  * Scalar routes are intercepted before the handler — zero overhead for normal requests.
295
296
  */
296
- function wrapWithScalar(handler, routerDef, options = {}) {
297
- const specJson = JSON.stringify(generateOpenAPI(routerDef, options));
298
- const specHtml = scalarHTML("/api/openapi.json", options);
297
+ function wrapWithScalar(handler, routerDef, options = {}, prefix = "/api") {
298
+ const normPrefix = (prefix.startsWith("/") ? prefix : "/" + prefix).replace(/\/+$/, "");
299
+ const specJson = JSON.stringify(generateOpenAPI(routerDef, options, normPrefix));
300
+ const specHtml = scalarHTML(`${normPrefix}/openapi.json`, options);
301
+ const openapiMatch = `${normPrefix.slice(1)}/openapi.json`;
302
+ const referenceMatch = `${normPrefix.slice(1)}/reference`;
299
303
  return (request) => {
300
304
  const url = request.url;
301
305
  const pathStart = url.indexOf("/", url.indexOf("//") + 2);
302
306
  const qMark = url.indexOf("?", pathStart);
303
307
  const fullPath = qMark === -1 ? url.slice(pathStart) : url.slice(pathStart, qMark);
304
308
  const pathname = fullPath.length > 1 ? fullPath.slice(1) : "";
305
- if (pathname === "api/openapi.json") return new Response(specJson, { headers: { "content-type": "application/json" } });
306
- if (pathname === "api/reference") return new Response(specHtml, { headers: { "content-type": "text/html" } });
309
+ if (pathname === openapiMatch) return new Response(specJson, { headers: { "content-type": "application/json" } });
310
+ if (pathname === referenceMatch) return new Response(specHtml, { headers: { "content-type": "text/html" } });
307
311
  return handler(request);
308
312
  };
309
313
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "silgi",
3
- "version": "0.51.1",
3
+ "version": "0.51.2",
4
4
  "private": false,
5
5
  "description": "The fastest end-to-end type-safe RPC framework for TypeScript — compiled pipelines, single package, every runtime",
6
6
  "keywords": [