silgi 0.50.2 → 0.50.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.
@@ -2,7 +2,7 @@ import { WrapHandlerOptions } from "../core/handler.mjs";
2
2
 
3
3
  //#region src/adapters/_fetch-adapter.d.ts
4
4
  interface FetchAdapterConfig<TCtx extends Record<string, unknown>> extends WrapHandlerOptions {
5
- /** Route prefix to strip. Default: "/api/rpc" */
5
+ /** Route prefix to strip. Default: "/api" */
6
6
  prefix?: string;
7
7
  /** Context factory — receives the Request (or framework event via eventMap). */
8
8
  context?: (req: Request) => TCtx | Promise<TCtx>;
@@ -12,6 +12,7 @@ interface FetchAdapterConfig<TCtx extends Record<string, unknown>> extends WrapH
12
12
  * (SvelteKit RequestEvent, SolidStart event), use this extended config.
13
13
  */
14
14
  interface FetchAdapterConfigWithEvent<TCtx extends Record<string, unknown>, TEvent = any> extends WrapHandlerOptions {
15
+ /** Route prefix to strip. Default: "/api" */
15
16
  prefix?: string;
16
17
  /** Context factory — receives the framework event, not raw Request. */
17
18
  context?: (event: TEvent) => TCtx | Promise<TCtx>;
@@ -5,13 +5,13 @@ import { createFetchAdapter } from "./_fetch-adapter.mjs";
5
5
  *
6
6
  * @example
7
7
  * ```ts
8
- * // src/pages/api/rpc/[...path].ts
8
+ * // src/pages/api/[...path].ts
9
9
  * import { createHandler } from "silgi/astro"
10
10
  * import { appRouter } from "~/server/rpc"
11
11
  *
12
12
  * const handler = createHandler(appRouter, {
13
- * prefix: "/api/rpc",
14
13
  * context: (req) => ({ db: getDB() }),
14
+ * analytics: true,
15
15
  * })
16
16
  *
17
17
  * export const GET = handler
@@ -24,7 +24,7 @@ import { createFetchAdapter } from "./_fetch-adapter.mjs";
24
24
  * Astro passes { request, params } — we extract request and delegate.
25
25
  */
26
26
  function createHandler(router, options = {}) {
27
- const handler = createFetchAdapter(router, options, "/api/rpc");
27
+ const handler = createFetchAdapter(router, options, "/api");
28
28
  return ({ request }) => handler(request);
29
29
  }
30
30
  //#endregion
@@ -11,7 +11,7 @@ import { iteratorToEventStream } from "../core/sse.mjs";
11
11
  * import { createHandler } from "silgi/express"
12
12
  *
13
13
  * const app = express()
14
- * app.use("/rpc", createHandler(appRouter, {
14
+ * app.use("/api", createHandler(appRouter, {
15
15
  * context: (req) => ({ db: getDB(), user: req.user }),
16
16
  * }))
17
17
  * app.listen(3000)
@@ -5,12 +5,13 @@ import { createFetchAdapter } from "./_fetch-adapter.mjs";
5
5
  *
6
6
  * @example
7
7
  * ```ts
8
- * // app/api/rpc/[...path]/route.ts
8
+ * // app/api/[...path]/route.ts
9
9
  * import { createHandler } from "silgi/nextjs"
10
10
  * import { appRouter } from "~/server/rpc"
11
11
  *
12
12
  * const handler = createHandler(appRouter, {
13
13
  * context: (req) => ({ db: getDB() }),
14
+ * analytics: true,
14
15
  * })
15
16
  *
16
17
  * export { handler as GET, handler as POST }
@@ -23,7 +24,7 @@ import { createFetchAdapter } from "./_fetch-adapter.mjs";
23
24
  * including content negotiation (JSON, MessagePack, devalue).
24
25
  */
25
26
  function createHandler(router, options = {}) {
26
- return createFetchAdapter(router, options, "/api/rpc");
27
+ return createFetchAdapter(router, options, "/api");
27
28
  }
28
29
  //#endregion
29
30
  export { createHandler };
@@ -5,13 +5,13 @@ import { createFetchAdapter } from "./_fetch-adapter.mjs";
5
5
  *
6
6
  * @example
7
7
  * ```ts
8
- * // app/routes/rpc.$.tsx
8
+ * // app/routes/api.$.tsx
9
9
  * import { createHandler } from "silgi/remix"
10
10
  * import { appRouter } from "~/server/rpc"
11
11
  *
12
12
  * const handler = createHandler(appRouter, {
13
- * prefix: "/rpc",
14
13
  * context: (req) => ({ db: getDB() }),
14
+ * analytics: true,
15
15
  * })
16
16
  *
17
17
  * export const action = handler
@@ -23,7 +23,7 @@ import { createFetchAdapter } from "./_fetch-adapter.mjs";
23
23
  * Remix passes { request, params } — we extract request and delegate.
24
24
  */
25
25
  function createHandler(router, options = {}) {
26
- const handler = createFetchAdapter(router, options, "/rpc");
26
+ const handler = createFetchAdapter(router, options, "/api");
27
27
  return ({ request }) => handler(request);
28
28
  }
29
29
  //#endregion
@@ -5,13 +5,13 @@ import { createEventFetchAdapter } from "./_fetch-adapter.mjs";
5
5
  *
6
6
  * @example
7
7
  * ```ts
8
- * // src/routes/api/rpc/[...path].ts
8
+ * // src/routes/api/[...path].ts
9
9
  * import { createHandler } from "silgi/solidstart"
10
10
  * import { appRouter } from "~/server/rpc"
11
11
  *
12
12
  * const handler = createHandler(appRouter, {
13
- * prefix: "/api/rpc",
14
13
  * context: (event) => ({ db: getDB() }),
14
+ * analytics: true,
15
15
  * })
16
16
  *
17
17
  * export const GET = handler
@@ -23,7 +23,7 @@ import { createEventFetchAdapter } from "./_fetch-adapter.mjs";
23
23
  * SolidStart uses Fetch API events — uses Silgi's handler().
24
24
  */
25
25
  function createHandler(router, options = {}) {
26
- return createEventFetchAdapter(router, options, "/api/rpc", (event) => event.request ?? event);
26
+ return createEventFetchAdapter(router, options, "/api", (event) => event.request ?? event);
27
27
  }
28
28
  //#endregion
29
29
  export { createHandler };
@@ -5,12 +5,13 @@ import { createEventFetchAdapter } from "./_fetch-adapter.mjs";
5
5
  *
6
6
  * @example
7
7
  * ```ts
8
- * // src/routes/api/rpc/[...path]/+server.ts
8
+ * // src/routes/api/[...path]/+server.ts
9
9
  * import { createHandler } from "silgi/sveltekit"
10
10
  * import { appRouter } from "$lib/server/rpc"
11
11
  *
12
12
  * const handler = createHandler(appRouter, {
13
13
  * context: (event) => ({ db: getDB(), user: event.locals.user }),
14
+ * analytics: true,
14
15
  * })
15
16
  *
16
17
  * export const GET = handler
@@ -24,7 +25,7 @@ import { createEventFetchAdapter } from "./_fetch-adapter.mjs";
24
25
  * The handler uses Silgi's handler() for full protocol support.
25
26
  */
26
27
  function createHandler(router, options = {}) {
27
- return createEventFetchAdapter(router, options, "/api/rpc", (event) => event.request);
28
+ return createEventFetchAdapter(router, options, "/api", (event) => event.request);
28
29
  }
29
30
  //#endregion
30
31
  export { createHandler };
@@ -69,8 +69,9 @@ document.getElementById('f').onsubmit=function(e){
69
69
  e.preventDefault();
70
70
  var t=document.getElementById('t').value.trim();
71
71
  if(!t)return;
72
- document.cookie='silgi-auth='+encodeURIComponent(t)+';path=/api/analytics;samesite=strict';
73
- fetch('/api/analytics/stats',{headers:{'cookie':'silgi-auth='+encodeURIComponent(t)}}).then(function(){
72
+ var base=location.pathname.replace(/\/$/,'');
73
+ document.cookie='silgi-auth='+encodeURIComponent(t)+';path='+base+';samesite=strict';
74
+ fetch(base+'/stats',{headers:{'cookie':'silgi-auth='+encodeURIComponent(t)}}).then(function(){
74
75
  location.reload();
75
76
  }).catch(function(){location.reload()});
76
77
  };
@@ -45,12 +45,28 @@ function isTrackedRequestPath(pathname) {
45
45
  const normalized = pathname.startsWith("/") ? pathname.slice(1) : pathname;
46
46
  return normalized === "api" || normalized.startsWith("api/") || normalized === "graphql" || normalized.startsWith("graphql/");
47
47
  }
48
- function isAnalyticsPath(pathname) {
49
- return pathname === "api/analytics" || pathname.startsWith("api/analytics/");
48
+ /**
49
+ * Normalize an incoming path to its analytics sub-path.
50
+ *
51
+ * Returns the sub-path after `analytics/` (e.g. `'stats'`, `'requests/5'`),
52
+ * an empty string for the dashboard root, or `null` if this isn't an
53
+ * analytics path at all.
54
+ *
55
+ * Accepts both `api/analytics*` (mounted under an `/api` RPC prefix) and
56
+ * bare `analytics*` (mounted at a prefix that already ends with `/api`),
57
+ * so the dashboard works regardless of adapter prefix configuration.
58
+ */
59
+ function normalizeAnalyticsPath(pathname) {
60
+ const p = pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
61
+ if (p === "api/analytics") return "";
62
+ if (p.startsWith("api/analytics/")) return p.slice(14);
63
+ if (p === "analytics") return "";
64
+ if (p.startsWith("analytics/")) return p.slice(10);
65
+ return null;
50
66
  }
51
67
  /** Helper to safely cast unknown to Record. */
52
68
  function asRecord(value) {
53
69
  return value && typeof value === "object" ? value : null;
54
70
  }
55
71
  //#endregion
56
- export { asRecord, isAnalyticsPath, isTrackedRequestPath, matchesPathPrefix, redactHeaderValue, round, safeStringify, sanitizeHeaders };
72
+ export { asRecord, isTrackedRequestPath, matchesPathPrefix, normalizeAnalyticsPath, redactHeaderValue, round, safeStringify, sanitizeHeaders };
@@ -3,7 +3,7 @@ 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
5
  import { generateRequestId } from "./analytics/request-id.mjs";
6
- import { isAnalyticsPath, isTrackedRequestPath, round, sanitizeHeaders } from "./analytics/utils.mjs";
6
+ import { isTrackedRequestPath, normalizeAnalyticsPath, round, sanitizeHeaders } from "./analytics/utils.mjs";
7
7
  import { RequestAccumulator } from "./analytics/accumulator.mjs";
8
8
  import { AnalyticsCollector } from "./analytics/collector.mjs";
9
9
  import { errorToMarkdown, requestToMarkdown } from "./analytics/export.mjs";
@@ -96,12 +96,14 @@ function wrapWithAnalytics(handler, options = {}) {
96
96
  });
97
97
  return async (request) => {
98
98
  const pathname = parseUrlPathname(request.url);
99
- if (isAnalyticsPath(pathname)) {
99
+ const analyticsSub = normalizeAnalyticsPath(pathname);
100
+ if (analyticsSub !== null) {
101
+ const canonical = analyticsSub === "" ? "api/analytics" : `api/analytics/${analyticsSub}`;
100
102
  if (auth) {
101
103
  const authResult = checkAnalyticsAuth(request, auth);
102
- if (!(authResult instanceof Promise ? await authResult : authResult)) return analyticsAuthResponse(pathname);
104
+ if (!(authResult instanceof Promise ? await authResult : authResult)) return analyticsAuthResponse(canonical);
103
105
  }
104
- return serveAnalyticsRoute(pathname, request, collector, dashboardHtml);
106
+ return serveAnalyticsRoute(canonical, request, collector, dashboardHtml);
105
107
  }
106
108
  if (!isTrackedRequestPath(pathname) || collector.isIgnored(pathname)) return handler(request);
107
109
  const incomingTraceId = request.headers.get("x-trace-id");