silgi 0.1.0-beta.8 → 0.1.0-beta.9

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,11 +1,13 @@
1
1
  import { AnySchema, InferSchemaInput, InferSchemaOutput } from "./core/schema.mjs";
2
- import { ErrorDef, Meta, MiddlewareDef, ProcedureDef, ProcedureType, ResolveContext, Route } from "./types.mjs";
2
+ import { ErrorDef, GuardDef, Meta, ProcedureDef, ProcedureType, ResolveContext, Route, WrapDef } from "./types.mjs";
3
3
 
4
4
  //#region src/builder.d.ts
5
5
  /** Initial builder — no input, no output, no errors yet */
6
6
  interface ProcedureBuilder<TType extends ProcedureType, TBaseCtx extends Record<string, unknown>, TCtx extends Record<string, unknown> = TBaseCtx, TInput = undefined, TErrors extends ErrorDef = {}> {
7
- /** Add middleware (guards and wraps) */
8
- $use<_TReturn extends Record<string, unknown> | void>(...middleware: readonly MiddlewareDef[]): ProcedureBuilder<TType, TBaseCtx, TCtx, TInput, TErrors>;
7
+ /** Add a guard enriches context with guard return type */
8
+ $use<TReturn extends Record<string, unknown> | void, TGErrors extends ErrorDef = {}>(guard: GuardDef<TCtx, TReturn, TGErrors>): ProcedureBuilder<TType, TBaseCtx, TReturn extends Record<string, unknown> ? TCtx & TReturn : TCtx, TInput, TGErrors & TErrors>;
9
+ /** Add a wrap middleware — does not change context type */
10
+ $use(wrap: WrapDef<TCtx>): ProcedureBuilder<TType, TBaseCtx, TCtx, TInput, TErrors>;
9
11
  /** Set input schema */
10
12
  $input<TSchema extends AnySchema>(schema: TSchema): ProcedureBuilder<TType, TBaseCtx, TCtx, InferSchemaOutput<TSchema>, TErrors>;
11
13
  /** Set output schema — enables return type autocomplete */
@@ -21,8 +23,10 @@ interface ProcedureBuilder<TType extends ProcedureType, TBaseCtx extends Record<
21
23
  }
22
24
  /** Builder after .$output() — return type is constrained for autocomplete */
23
25
  interface ProcedureBuilderWithOutput<TType extends ProcedureType, TBaseCtx extends Record<string, unknown>, TCtx extends Record<string, unknown>, TInput, TOutputResolved, TErrors extends ErrorDef> {
24
- /** Add middleware */
25
- $use(...middleware: readonly MiddlewareDef[]): ProcedureBuilderWithOutput<TType, TBaseCtx, TCtx, TInput, TOutputResolved, TErrors>;
26
+ /** Add a guard — enriches context with guard return type */
27
+ $use<TReturn extends Record<string, unknown> | void, TGErrors extends ErrorDef = {}>(guard: GuardDef<TCtx, TReturn, TGErrors>): ProcedureBuilderWithOutput<TType, TBaseCtx, TReturn extends Record<string, unknown> ? TCtx & TReturn : TCtx, TInput, TOutputResolved, TGErrors & TErrors>;
28
+ /** Add a wrap middleware — does not change context type */
29
+ $use(wrap: WrapDef<TCtx>): ProcedureBuilderWithOutput<TType, TBaseCtx, TCtx, TInput, TOutputResolved, TErrors>;
26
30
  /** Set typed errors */
27
31
  $errors<TNewErrors extends ErrorDef>(errors: TNewErrors): ProcedureBuilderWithOutput<TType, TBaseCtx, TCtx, TInput, TOutputResolved, TNewErrors & TErrors>;
28
32
  /** Set route metadata */
package/dist/builder.mjs CHANGED
@@ -13,7 +13,7 @@ var ProcBuilder = class {
13
13
  }
14
14
  $use(...middleware) {
15
15
  if (this.use) this.use.push(...middleware);
16
- else this.use = middleware;
16
+ else this.use = [...middleware];
17
17
  return this;
18
18
  }
19
19
  $input(schema) {
@@ -5,16 +5,11 @@ import { ClientContext, ClientLink } from "./types.mjs";
5
5
  /**
6
6
  * Create a type-safe client from a link.
7
7
  *
8
- * Accepts either a router type (auto-inferred) or a pre-inferred client type:
9
8
  * ```ts
10
- * // Recommended pass AppRouter directly
11
- * const client = createClient<AppRouter>(link)
12
- *
13
- * // Also works — explicit InferClient
14
- * const client = createClient<InferClient<AppRouter>>(link)
9
+ * const client = createClient<typeof appRouter>(link)
15
10
  * ```
16
11
  */
17
- declare function createClient<T, TClientContext extends ClientContext = Record<never, never>>(link: ClientLink<TClientContext>): InferClient<T> extends never ? T : InferClient<T>;
12
+ declare function createClient<T, TClientContext extends ClientContext = Record<never, never>>(link: ClientLink<TClientContext>): InferClient<T>;
18
13
  /**
19
14
  * Safe client wrapper — returns [error, data] tuples instead of throwing.
20
15
  */
@@ -2,13 +2,8 @@
2
2
  /**
3
3
  * Create a type-safe client from a link.
4
4
  *
5
- * Accepts either a router type (auto-inferred) or a pre-inferred client type:
6
5
  * ```ts
7
- * // Recommended pass AppRouter directly
8
- * const client = createClient<AppRouter>(link)
9
- *
10
- * // Also works — explicit InferClient
11
- * const client = createClient<InferClient<AppRouter>>(link)
6
+ * const client = createClient<typeof appRouter>(link)
12
7
  * ```
13
8
  */
14
9
  function createClient(link) {
@@ -44,8 +44,8 @@ async function createServeHandler(routerDef, contextFactory, hooks, options) {
44
44
  const url = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
45
45
  if (options?.ws && server.node?.server) {
46
46
  const { attachWebSocket } = await import("../ws.mjs");
47
- const nodeServer = server.node.server;
48
- await attachWebSocket(nodeServer, routerDef, typeof options.ws === "object" ? options.ws : void 0);
47
+ const wsOpts = typeof options.ws === "object" ? options.ws : void 0;
48
+ await attachWebSocket(server.node.server, routerDef, wsOpts);
49
49
  }
50
50
  console.log(`\nSilgi server running at ${url}`);
51
51
  if (options?.http2) console.log(` HTTP/2 enabled (with HTTP/1.1 fallback)`);
@@ -1,6 +1,6 @@
1
1
  import { BuildKeyOptions, PartialDeep, buildKey } from "./key.mjs";
2
2
  import { GeneralUtils, createGeneralUtils } from "./general-utils.mjs";
3
- import { MaybeOptionalOptions, MutationOptions, MutationOptionsIn, QueryOptions, QueryOptionsIn, SetOptional, UseQueryFnContext } from "./types.mjs";
3
+ import { MaybeOptionalOptions, MutationOptions, MutationOptionsIn, QueryOptions, QueryOptionsIn, UseQueryFnContext } from "./types.mjs";
4
4
  import { CreateProcedureUtilsOptions, ProcedureUtils, createProcedureUtils } from "./procedure-utils.mjs";
5
5
  import { CreateRouterUtilsOptions, RouterUtils, createRouterUtils } from "./router-utils.mjs";
6
- export { BuildKeyOptions, CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, MaybeOptionalOptions, MutationOptions, MutationOptionsIn, PartialDeep, ProcedureUtils, QueryOptions, QueryOptionsIn, RouterUtils, SetOptional, UseQueryFnContext, buildKey, createRouterUtils as createColadaUtils, createGeneralUtils, createProcedureUtils, createRouterUtils };
6
+ export { BuildKeyOptions, CreateProcedureUtilsOptions, CreateRouterUtilsOptions, GeneralUtils, MaybeOptionalOptions, MutationOptions, MutationOptionsIn, PartialDeep, ProcedureUtils, QueryOptions, QueryOptionsIn, RouterUtils, UseQueryFnContext, buildKey, createRouterUtils as createColadaUtils, createGeneralUtils, createProcedureUtils, createRouterUtils };
@@ -3,7 +3,6 @@ import { MaybeRefOrGetter } from "vue";
3
3
  import { UseMutationOptions, UseQueryOptions } from "@pinia/colada";
4
4
 
5
5
  //#region src/integrations/pinia-colada/types.d.ts
6
- type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
7
6
  type MaybeOptionalOptions<T> = Partial<T> extends T ? [options?: T] : [options: T];
8
7
  type UseQueryFnContext = Parameters<UseQueryOptions<any>['query']>[0];
9
8
  type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TInitialData extends TOutput | undefined> = (undefined extends TInput ? {
@@ -14,13 +13,13 @@ type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TErro
14
13
  context?: MaybeRefOrGetter<TClientContext>;
15
14
  } : {
16
15
  context: MaybeRefOrGetter<TClientContext>;
17
- }) & SetOptional<QueryOptions<TOutput, TError, TInitialData>, 'key' | 'query'>;
16
+ }) & Partial<QueryOptions<TOutput, TError, TInitialData>>;
18
17
  type QueryOptions<TOutput, TError, TInitialData extends TOutput | undefined> = UseQueryOptions<TOutput, TError, TInitialData>;
19
18
  type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext extends Record<any, any>> = (Record<never, never> extends TClientContext ? {
20
19
  context?: MaybeRefOrGetter<TClientContext>;
21
20
  } : {
22
21
  context: MaybeRefOrGetter<TClientContext>;
23
- }) & SetOptional<UseMutationOptions<TOutput, TInput, TError, TMutationContext>, 'mutation'>;
22
+ }) & Partial<MutationOptions<TInput, TOutput, TError, TMutationContext>>;
24
23
  type MutationOptions<TInput, TOutput, TError, TMutationContext extends Record<any, any>> = UseMutationOptions<TOutput, TInput, TError, TMutationContext>;
25
24
  //#endregion
26
- export { MaybeOptionalOptions, MutationOptions, MutationOptionsIn, QueryOptions, QueryOptionsIn, SetOptional, UseQueryFnContext };
25
+ export { MaybeOptionalOptions, MutationOptions, MutationOptionsIn, QueryOptions, QueryOptionsIn, UseQueryFnContext };
@@ -638,6 +638,9 @@ function analyticsAuthResponse(pathname) {
638
638
  headers: { "content-type": "text/html" }
639
639
  });
640
640
  }
641
+ function jsonResponse(data, headers) {
642
+ return new Response(JSON.stringify(data), { headers });
643
+ }
641
644
  /** Serve analytics dashboard and API routes. */
642
645
  async function serveAnalyticsRoute(pathname, collector, dashboardHtml) {
643
646
  const jsonCacheHeaders = {
@@ -648,15 +651,9 @@ async function serveAnalyticsRoute(pathname, collector, dashboardHtml) {
648
651
  "content-type": "text/markdown; charset=utf-8",
649
652
  "cache-control": "no-cache"
650
653
  };
651
- if (pathname === "analytics/_api/stats") return new Response(JSON.stringify(collector.toJSON()), { headers: jsonCacheHeaders });
652
- if (pathname === "analytics/_api/errors") {
653
- const errors = await collector.getErrors();
654
- return new Response(JSON.stringify(errors), { headers: jsonCacheHeaders });
655
- }
656
- if (pathname === "analytics/_api/requests") {
657
- const requests = await collector.getRequests();
658
- return new Response(JSON.stringify(requests), { headers: jsonCacheHeaders });
659
- }
654
+ if (pathname === "analytics/_api/stats") return jsonResponse(collector.toJSON(), jsonCacheHeaders);
655
+ if (pathname === "analytics/_api/errors") return jsonResponse(await collector.getErrors(), jsonCacheHeaders);
656
+ if (pathname === "analytics/_api/requests") return jsonResponse(await collector.getRequests(), jsonCacheHeaders);
660
657
  if (pathname.startsWith("analytics/_api/requests/") && pathname.endsWith("/md")) {
661
658
  const id = Number(pathname.slice(24, -3));
662
659
  const entry = (await collector.getRequests()).find((r) => r.id === id);
package/dist/silgi.mjs CHANGED
@@ -87,7 +87,11 @@ function silgi(config) {
87
87
  }),
88
88
  $resolve: ((fn) => createProcedure("query", fn)),
89
89
  $input: ((schema) => createProcedureBuilder("query").$input(schema)),
90
- $use: ((...middleware) => createProcedureBuilder("query").$use(...middleware)),
90
+ $use: ((...middleware) => {
91
+ const b = createProcedureBuilder("query");
92
+ for (const m of middleware) b.$use(m);
93
+ return b;
94
+ }),
91
95
  $output: ((schema) => createProcedureBuilder("query").$output(schema)),
92
96
  $errors: ((errors) => createProcedureBuilder("query").$errors(errors)),
93
97
  $route: ((route) => createProcedureBuilder("query").$route(route)),
package/dist/types.d.mts CHANGED
@@ -99,7 +99,9 @@ interface ResolveContext<TCtx, TInput, TErrors extends ErrorDef> {
99
99
  type RouterDef = {
100
100
  [key: string]: ProcedureDef<any, any, any, any> | RouterDef;
101
101
  };
102
+ /** Return type wrapper — subscription yields, query/mutation returns Promise */
103
+ type ProcedureResult<TType extends ProcedureType, TOutput> = TType extends 'subscription' ? AsyncIterableIterator<TOutput> : Promise<TOutput>;
102
104
  /** Infer client type from router */
103
- type InferClient<T> = T extends ProcedureDef<infer TType, infer TInput, infer TOutput> ? TType extends 'subscription' ? undefined extends TInput ? () => AsyncIterableIterator<TOutput> : (input: TInput) => AsyncIterableIterator<TOutput> : undefined extends TInput ? () => Promise<TOutput> : (input: TInput) => Promise<TOutput> : T extends Record<string, unknown> ? { [K in keyof T]: InferClient<T[K]> } : never;
105
+ type InferClient<T> = T extends ProcedureDef<infer TType, infer TInput, infer TOutput> ? undefined extends TInput ? () => ProcedureResult<TType, TOutput> : (input: TInput) => ProcedureResult<TType, TOutput> : T extends Record<string, unknown> ? { [K in keyof T]: InferClient<T[K]> } : never;
104
106
  //#endregion
105
107
  export { ErrorDef, ErrorDefItem, FailFn, GuardDef, GuardFn, InferClient, InferContextFromUse, InferGuardOutput, Meta, MiddlewareDef, ProcedureDef, ProcedureType, ResolveContext, Route, RouterDef, WrapDef, WrapFn };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "silgi",
3
- "version": "0.1.0-beta.8",
3
+ "version": "0.1.0-beta.9",
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": [
@@ -236,6 +236,7 @@
236
236
  "bench:http": "node --experimental-strip-types bench/http.ts",
237
237
  "bench:http:bun": "bun bench/http-bun.ts",
238
238
  "bench:memory": "node --expose-gc --experimental-strip-types bench/memory.ts",
239
+ "bench:types": "tsx bench/types.ts",
239
240
  "bench:bun": "bun test/bun-compat.ts",
240
241
  "play": "pnpm build && pnpm --filter silgi-playground dev",
241
242
  "lint": "oxlint . && oxfmt --check --ignore-path .oxfmtignore .",
@@ -250,7 +251,7 @@
250
251
  },
251
252
  "dependencies": {
252
253
  "@standard-schema/spec": "^1.1.0",
253
- "cookie-es": "^3.0.1",
254
+ "cookie-es": "^3.1.1",
254
255
  "crossws": "^0.4.4",
255
256
  "defu": "^6.1.4",
256
257
  "devalue": "^5.6.4",
@@ -260,23 +261,24 @@
260
261
  "ocache": "^0.1.4",
261
262
  "ofetch": "2.0.0-alpha.3",
262
263
  "ohash": "^2.0.11",
263
- "srvx": "^0.11.12",
264
+ "srvx": "^0.11.13",
264
265
  "unstorage": "^2.0.0-alpha.7"
265
266
  },
266
267
  "devDependencies": {
268
+ "@ark/attest": "^0.56.0",
267
269
  "@hono/node-server": "^1.19.11",
268
270
  "@nuxt/test-utils": "^4.0.0",
269
- "@orpc/client": "^1.13.9",
270
- "@orpc/contract": "^1.13.9",
271
- "@orpc/server": "^1.13.9",
271
+ "@orpc/client": "^1.13.13",
272
+ "@orpc/contract": "^1.13.13",
273
+ "@orpc/server": "^1.13.13",
272
274
  "@pinia/colada": "^1.0.0",
273
- "@trpc/client": "^11.14.1",
274
- "@trpc/server": "^11.14.1",
275
+ "@trpc/client": "^11.16.0",
276
+ "@trpc/server": "^11.16.0",
275
277
  "@types/express": "^5.0.6",
276
278
  "@types/node": "^25.5.0",
277
279
  "@types/ws": "^8.18.1",
278
280
  "@typescript/native-preview": "7.0.0-dev.20260316.1",
279
- "ai": "^6.0.134",
281
+ "ai": "^6.0.141",
280
282
  "elysia": "^1.4.28",
281
283
  "express": "^5.2.1",
282
284
  "fastify": "^5.8.4",
@@ -288,15 +290,15 @@
288
290
  "nitro": "3.0.260311-beta",
289
291
  "nuxt-nightly": "5.0.0-29563820.579db312",
290
292
  "obuild": "^0.4.32",
291
- "oxfmt": "^0.41.0",
292
- "oxlint": "^1.56.0",
293
+ "oxfmt": "^0.42.0",
294
+ "oxlint": "^1.57.0",
293
295
  "pinia": "^3.0.4",
294
296
  "rou3": "^0.8.1",
295
- "tsdown": "^0.21.4",
297
+ "tsdown": "^0.21.7",
296
298
  "typescript": "^6.0.2",
297
- "vitest": "^4.1.0",
299
+ "vitest": "^4.1.2",
298
300
  "vue": "^3.5.31",
299
- "ws": "^8.19.0",
301
+ "ws": "^8.20.0",
300
302
  "zod": "^4.3.6"
301
303
  },
302
304
  "peerDependencies": {
@@ -319,7 +321,7 @@
319
321
  "node": ">=24",
320
322
  "pnpm": ">=10"
321
323
  },
322
- "packageManager": "pnpm@10.32.1",
324
+ "packageManager": "pnpm@10.33.0",
323
325
  "pnpm": {
324
326
  "overrides": {
325
327
  "silgi": "workspace:*"