@orpc/server 0.0.0-next.c40d0c9 → 0.0.0-next.c4671e3

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.
Files changed (49) hide show
  1. package/README.md +13 -17
  2. package/dist/adapters/bun-ws/index.d.mts +35 -0
  3. package/dist/adapters/bun-ws/index.d.ts +35 -0
  4. package/dist/adapters/bun-ws/index.mjs +50 -0
  5. package/dist/adapters/crossws/index.d.mts +30 -0
  6. package/dist/adapters/crossws/index.d.ts +30 -0
  7. package/dist/adapters/crossws/index.mjs +50 -0
  8. package/dist/adapters/fetch/index.d.mts +33 -9
  9. package/dist/adapters/fetch/index.d.ts +33 -9
  10. package/dist/adapters/fetch/index.mjs +109 -8
  11. package/dist/adapters/node/index.d.mts +33 -9
  12. package/dist/adapters/node/index.d.ts +33 -9
  13. package/dist/adapters/node/index.mjs +22 -12
  14. package/dist/adapters/standard/index.d.mts +4 -4
  15. package/dist/adapters/standard/index.d.ts +4 -4
  16. package/dist/adapters/standard/index.mjs +3 -3
  17. package/dist/adapters/websocket/index.d.mts +27 -0
  18. package/dist/adapters/websocket/index.d.ts +27 -0
  19. package/dist/adapters/websocket/index.mjs +37 -0
  20. package/dist/adapters/ws/index.d.mts +28 -0
  21. package/dist/adapters/ws/index.d.ts +28 -0
  22. package/dist/adapters/ws/index.mjs +37 -0
  23. package/dist/index.d.mts +548 -10
  24. package/dist/index.d.ts +548 -10
  25. package/dist/index.mjs +122 -11
  26. package/dist/plugins/index.d.mts +132 -6
  27. package/dist/plugins/index.d.ts +132 -6
  28. package/dist/plugins/index.mjs +153 -9
  29. package/dist/shared/server.BRoxSiSC.d.mts +12 -0
  30. package/dist/shared/server.BW-nUGgA.mjs +36 -0
  31. package/dist/shared/{server.Cud5qk0c.d.ts → server.BjiJH9Vo.d.ts} +2 -2
  32. package/dist/shared/server.Cy1vfSiG.d.ts +12 -0
  33. package/dist/shared/{server.e3W6AG3-.mjs → server.DG7Tamti.mjs} +19 -22
  34. package/dist/shared/{server.DLt5njUb.d.mts → server.DPWk5pjW.d.mts} +54 -5
  35. package/dist/shared/{server.DLt5njUb.d.ts → server.DPWk5pjW.d.ts} +54 -5
  36. package/dist/shared/{server.DGnN7q3R.d.mts → server.QUe9N8P4.d.mts} +2 -2
  37. package/dist/shared/{server.DCCkTG29.mjs → server.SxlTJfG2.mjs} +27 -6
  38. package/dist/shared/{server.BHlRCrf_.d.ts → server.YZzrREz9.d.ts} +13 -5
  39. package/dist/shared/{server.CzxlqYZL.d.mts → server.eWLxY3lq.d.mts} +13 -5
  40. package/package.json +43 -20
  41. package/dist/adapters/hono/index.d.mts +0 -22
  42. package/dist/adapters/hono/index.d.ts +0 -22
  43. package/dist/adapters/hono/index.mjs +0 -32
  44. package/dist/adapters/next/index.d.mts +0 -29
  45. package/dist/adapters/next/index.d.ts +0 -29
  46. package/dist/adapters/next/index.mjs +0 -29
  47. package/dist/shared/server.B77ImKAP.d.mts +0 -8
  48. package/dist/shared/server.DUF89eb-.d.ts +0 -8
  49. package/dist/shared/server.Prfzu_zf.mjs +0 -98
@@ -1,7 +1,110 @@
1
- import { value } from '@orpc/shared';
1
+ import { value, isAsyncIteratorObject } from '@orpc/shared';
2
+ import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
3
+ import { flattenHeader } from '@orpc/standard-server';
4
+ import { ORPCError } from '@orpc/client';
5
+ export { S as StrictGetMethodPlugin } from '../shared/server.BW-nUGgA.mjs';
6
+ import '@orpc/contract';
7
+
8
+ class BatchHandlerPlugin {
9
+ maxSize;
10
+ mapRequestItem;
11
+ successStatus;
12
+ headers;
13
+ order = 5e6;
14
+ constructor(options = {}) {
15
+ this.maxSize = options.maxSize ?? 10;
16
+ this.mapRequestItem = options.mapRequestItem ?? ((request, { request: batchRequest }) => ({
17
+ ...request,
18
+ headers: {
19
+ ...batchRequest.headers,
20
+ ...request.headers
21
+ }
22
+ }));
23
+ this.successStatus = options.successStatus ?? 207;
24
+ this.headers = options.headers ?? {};
25
+ }
26
+ init(options) {
27
+ options.rootInterceptors ??= [];
28
+ options.rootInterceptors.unshift(async (options2) => {
29
+ if (options2.request.headers["x-orpc-batch"] !== "1") {
30
+ return options2.next();
31
+ }
32
+ let isParsing = false;
33
+ try {
34
+ isParsing = true;
35
+ const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
36
+ isParsing = false;
37
+ const maxSize = await value(this.maxSize, options2);
38
+ if (parsed.length > maxSize) {
39
+ return {
40
+ matched: true,
41
+ response: {
42
+ status: 413,
43
+ headers: {},
44
+ body: "Batch request size exceeds the maximum allowed size"
45
+ }
46
+ };
47
+ }
48
+ const responses = parsed.map(
49
+ (request, index) => {
50
+ const mapped = this.mapRequestItem(request, options2);
51
+ return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
52
+ if (matched) {
53
+ if (response2.body instanceof Blob || response2.body instanceof FormData || isAsyncIteratorObject(response2.body)) {
54
+ return {
55
+ index,
56
+ status: 500,
57
+ headers: {},
58
+ body: "Batch responses do not support file/blob, or event-iterator. Please call this procedure separately outside of the batch request."
59
+ };
60
+ }
61
+ return { ...response2, index };
62
+ }
63
+ return { index, status: 404, headers: {}, body: "No procedure matched" };
64
+ }).catch(() => {
65
+ return { index, status: 500, headers: {}, body: "Internal server error" };
66
+ });
67
+ }
68
+ );
69
+ await Promise.race(responses);
70
+ const status = await value(this.successStatus, responses, options2);
71
+ const headers = await value(this.headers, responses, options2);
72
+ const response = toBatchResponse({
73
+ status,
74
+ headers,
75
+ body: async function* () {
76
+ const promises = [...responses];
77
+ while (true) {
78
+ const handling = promises.filter((p) => p !== void 0);
79
+ if (handling.length === 0) {
80
+ return;
81
+ }
82
+ const result = await Promise.race(handling);
83
+ promises[result.index] = void 0;
84
+ yield result;
85
+ }
86
+ }()
87
+ });
88
+ return {
89
+ matched: true,
90
+ response
91
+ };
92
+ } catch (cause) {
93
+ if (isParsing) {
94
+ return {
95
+ matched: true,
96
+ response: { status: 400, headers: {}, body: "Invalid batch request, this could be caused by a malformed request body or a missing header" }
97
+ };
98
+ }
99
+ throw cause;
100
+ }
101
+ });
102
+ }
103
+ }
2
104
 
3
105
  class CORSPlugin {
4
106
  options;
107
+ order = 9e6;
5
108
  constructor(options = {}) {
6
109
  const defaults = {
7
110
  origin: (origin) => origin,
@@ -21,13 +124,11 @@ class CORSPlugin {
21
124
  resHeaders["access-control-max-age"] = this.options.maxAge.toString();
22
125
  }
23
126
  if (this.options.allowMethods?.length) {
24
- resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
127
+ resHeaders["access-control-allow-methods"] = flattenHeader(this.options.allowMethods);
25
128
  }
26
129
  const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
27
- if (Array.isArray(allowHeaders) && allowHeaders.length) {
28
- resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
29
- } else if (typeof allowHeaders === "string") {
30
- resHeaders["access-control-allow-headers"] = allowHeaders;
130
+ if (typeof allowHeaders === "string" || allowHeaders?.length) {
131
+ resHeaders["access-control-allow-headers"] = flattenHeader(allowHeaders);
31
132
  }
32
133
  return {
33
134
  matched: true,
@@ -45,7 +146,7 @@ class CORSPlugin {
45
146
  if (!result.matched) {
46
147
  return result;
47
148
  }
48
- const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
149
+ const origin = flattenHeader(interceptorOptions.request.headers.origin) ?? "";
49
150
  const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
50
151
  const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
51
152
  if (allowedOriginArr.includes("*")) {
@@ -67,7 +168,7 @@ class CORSPlugin {
67
168
  result.response.headers["access-control-allow-credentials"] = "true";
68
169
  }
69
170
  if (this.options.exposeHeaders?.length) {
70
- result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
171
+ result.response.headers["access-control-expose-headers"] = flattenHeader(this.options.exposeHeaders);
71
172
  }
72
173
  return result;
73
174
  });
@@ -104,4 +205,47 @@ class ResponseHeadersPlugin {
104
205
  }
105
206
  }
106
207
 
107
- export { CORSPlugin, ResponseHeadersPlugin };
208
+ const SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL = Symbol("SIMPLE_CSRF_PROTECTION_CONTEXT");
209
+ class SimpleCsrfProtectionHandlerPlugin {
210
+ headerName;
211
+ headerValue;
212
+ exclude;
213
+ error;
214
+ constructor(options = {}) {
215
+ this.headerName = options.headerName ?? "x-csrf-token";
216
+ this.headerValue = options.headerValue ?? "orpc";
217
+ this.exclude = options.exclude ?? false;
218
+ this.error = options.error ?? new ORPCError("CSRF_TOKEN_MISMATCH", {
219
+ status: 403,
220
+ message: "Invalid CSRF token"
221
+ });
222
+ }
223
+ order = 8e6;
224
+ init(options) {
225
+ options.rootInterceptors ??= [];
226
+ options.clientInterceptors ??= [];
227
+ options.rootInterceptors.unshift(async (options2) => {
228
+ const headerName = await value(this.headerName, options2);
229
+ const headerValue = await value(this.headerValue, options2);
230
+ return options2.next({
231
+ ...options2,
232
+ context: {
233
+ ...options2.context,
234
+ [SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers[headerName] === headerValue
235
+ }
236
+ });
237
+ });
238
+ options.clientInterceptors.unshift(async (options2) => {
239
+ if (typeof options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL] !== "boolean") {
240
+ throw new TypeError("[SimpleCsrfProtectionHandlerPlugin] CSRF protection context has been corrupted or modified by another plugin or interceptor");
241
+ }
242
+ const excluded = await value(this.exclude, options2);
243
+ if (!excluded && !options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]) {
244
+ throw this.error;
245
+ }
246
+ return options2.next();
247
+ });
248
+ }
249
+ }
250
+
251
+ export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin };
@@ -0,0 +1,12 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context, R as Router } from './server.DPWk5pjW.mjs';
3
+ import { b as StandardHandlerOptions, i as StandardHandler } from './server.eWLxY3lq.mjs';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+ declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
8
+ constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
9
+ }
10
+
11
+ export { StandardRPCHandler as a };
12
+ export type { StandardRPCHandlerOptions as S };
@@ -0,0 +1,36 @@
1
+ import { ORPCError, fallbackContractConfig } from '@orpc/contract';
2
+
3
+ const STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL = Symbol("STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT");
4
+ class StrictGetMethodPlugin {
5
+ error;
6
+ order = 7e6;
7
+ constructor(options = {}) {
8
+ this.error = options.error ?? new ORPCError("METHOD_NOT_SUPPORTED");
9
+ }
10
+ init(options) {
11
+ options.rootInterceptors ??= [];
12
+ options.clientInterceptors ??= [];
13
+ options.rootInterceptors.unshift((options2) => {
14
+ const isGetMethod = options2.request.method === "GET";
15
+ return options2.next({
16
+ ...options2,
17
+ context: {
18
+ ...options2.context,
19
+ [STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL]: isGetMethod
20
+ }
21
+ });
22
+ });
23
+ options.clientInterceptors.unshift((options2) => {
24
+ if (typeof options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] !== "boolean") {
25
+ throw new TypeError("[StrictGetMethodPlugin] strict GET method context has been corrupted or modified by another plugin or interceptor");
26
+ }
27
+ const procedureMethod = fallbackContractConfig("defaultMethod", options2.procedure["~orpc"].route.method);
28
+ if (options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] && procedureMethod !== "GET") {
29
+ throw this.error;
30
+ }
31
+ return options2.next();
32
+ });
33
+ }
34
+ }
35
+
36
+ export { StrictGetMethodPlugin as S };
@@ -1,5 +1,5 @@
1
- import { C as Context } from './server.DLt5njUb.js';
2
- import { S as StandardHandleOptions } from './server.BHlRCrf_.js';
1
+ import { C as Context } from './server.DPWk5pjW.js';
2
+ import { g as StandardHandleOptions } from './server.YZzrREz9.js';
3
3
 
4
4
  type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
5
5
  context?: T;
@@ -0,0 +1,12 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context, R as Router } from './server.DPWk5pjW.js';
3
+ import { b as StandardHandlerOptions, i as StandardHandler } from './server.YZzrREz9.js';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+ declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
8
+ constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
9
+ }
10
+
11
+ export { StandardRPCHandler as a };
12
+ export type { StandardRPCHandlerOptions as S };
@@ -46,6 +46,9 @@ function addMiddleware(middlewares, addition) {
46
46
  }
47
47
 
48
48
  class Procedure {
49
+ /**
50
+ * This property holds the defined options.
51
+ */
49
52
  "~orpc";
50
53
  constructor(def) {
51
54
  this["~orpc"] = def;
@@ -169,35 +172,29 @@ async function executeProcedureInternal(procedure, options) {
169
172
  const middlewares = procedure["~orpc"].middlewares;
170
173
  const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
171
174
  const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
172
- let currentIndex = 0;
173
- let currentContext = options.context;
174
- let currentInput = options.input;
175
- const next = async (...[nextOptions]) => {
176
- const index = currentIndex;
177
- const midContext = nextOptions?.context ?? {};
178
- currentIndex += 1;
179
- currentContext = mergeCurrentContext(currentContext, midContext);
175
+ const next = async (index, context, input) => {
176
+ let currentInput = input;
180
177
  if (index === inputValidationIndex) {
181
178
  currentInput = await validateInput(procedure, currentInput);
182
179
  }
183
180
  const mid = middlewares[index];
184
- const result = mid ? {
185
- context: midContext,
186
- output: (await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn)).output
187
- } : {
188
- context: midContext,
189
- output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput })
190
- };
181
+ const output = mid ? (await mid({
182
+ ...options,
183
+ context,
184
+ next: async (...[nextOptions]) => {
185
+ const nextContext = nextOptions?.context ?? {};
186
+ return {
187
+ output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
188
+ context: nextContext
189
+ };
190
+ }
191
+ }, currentInput, middlewareOutputFn)).output : await procedure["~orpc"].handler({ ...options, context, input: currentInput });
191
192
  if (index === outputValidationIndex) {
192
- const validatedOutput = await validateOutput(procedure, result.output);
193
- return {
194
- context: result.context,
195
- output: validatedOutput
196
- };
193
+ return await validateOutput(procedure, output);
197
194
  }
198
- return result;
195
+ return output;
199
196
  };
200
- return (await next()).output;
197
+ return next(0, options.context, options.input);
201
198
  }
202
199
 
203
200
  const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
@@ -1,6 +1,6 @@
1
1
  import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
- import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
2
  import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
4
4
 
5
5
  type Context = Record<PropertyKey, any>;
6
6
  type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
@@ -28,6 +28,9 @@ interface Lazy<T> {
28
28
  };
29
29
  }
30
30
  type Lazyable<T> = T | Lazy<T>;
31
+ /**
32
+ * Create a lazy thing.
33
+ */
31
34
  declare function lazy<T>(loader: () => Promise<{
32
35
  default: T;
33
36
  }>, meta?: LazyMeta): Lazy<T>;
@@ -56,7 +59,15 @@ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends
56
59
  outputValidationIndex: number;
57
60
  handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
58
61
  }
62
+ /**
63
+ * This class represents a procedure.
64
+ *
65
+ * @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
66
+ */
59
67
  declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
68
+ /**
69
+ * This property holds the defined options.
70
+ */
60
71
  '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
61
72
  constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
62
73
  }
@@ -87,6 +98,11 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
87
98
  next: MiddlewareNextFn<TOutput>;
88
99
  errors: TErrorConstructorMap;
89
100
  }
101
+ /**
102
+ * A function that represents a middleware.
103
+ *
104
+ * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
105
+ */
90
106
  interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
91
107
  (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
92
108
  }
@@ -106,9 +122,6 @@ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TEr
106
122
  signal?: AbortSignal;
107
123
  lastEventId: string | undefined;
108
124
  }
109
- /**
110
- * Options for creating a procedure caller with comprehensive type safety
111
- */
112
125
  type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
126
  /**
114
127
  * This is helpful for logging and analytics.
@@ -120,24 +133,60 @@ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema
120
133
  } : {
121
134
  context: Value<TInitialContext, [clientContext: TClientContext]>;
122
135
  });
136
+ /**
137
+ * Create Server-side client from a procedure.
138
+ *
139
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
140
+ */
123
141
  declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
124
142
 
143
+ /**
144
+ * Represents a router, which defines a hierarchical structure of procedures.
145
+ *
146
+ * @info A procedure is a router too.
147
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
148
+ */
125
149
  type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
126
150
  [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
127
151
  };
128
152
  type AnyRouter = Router<any, any>;
129
153
  type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
154
+ /**
155
+ * Infer all initial context of the router.
156
+ *
157
+ * @info A procedure is a router too.
158
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
159
+ */
130
160
  type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
131
161
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
132
162
  };
163
+ /**
164
+ * Infer all current context of the router.
165
+ *
166
+ * @info A procedure is a router too.
167
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
168
+ */
133
169
  type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
134
170
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
135
171
  };
172
+ /**
173
+ * Infer all router inputs
174
+ *
175
+ * @info A procedure is a router too.
176
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
177
+ */
136
178
  type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
137
179
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
138
180
  };
181
+ /**
182
+ * Infer all router outputs
183
+ *
184
+ * @info A procedure is a router too.
185
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
186
+ */
139
187
  type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
140
188
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
189
  };
142
190
 
143
- export { type AnyProcedure as A, middlewareOutputFn as B, type Context as C, type ProcedureHandlerOptions as D, type ProcedureDef as E, isProcedure as F, createProcedureClient as G, type InferRouterInitialContexts as H, type InferRouterInitialContext as I, type InferRouterCurrentContexts as J, type InferRouterInputs as K, type Lazyable as L, type Middleware as M, type InferRouterOutputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type Router as R, type AnyRouter as a, Procedure as b, type MergedInitialContext as c, type MergedCurrentContext as d, type MapInputMiddleware as e, type CreateProcedureClientOptions as f, type ProcedureClient as g, type AnyMiddleware as h, type Lazy as i, type ProcedureHandler as j, type ORPCErrorConstructorMapItemOptions as k, type ORPCErrorConstructorMapItem as l, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, type LazyMeta as p, lazy as q, isLazy as r, getLazyMeta as s, type MiddlewareResult as t, unlazy as u, validateORPCError as v, type MiddlewareNextFnOptions as w, type MiddlewareNextFn as x, type MiddlewareOutputFn as y, type MiddlewareOptions as z };
191
+ export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
192
+ export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
@@ -1,6 +1,6 @@
1
1
  import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
- import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
2
  import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
4
4
 
5
5
  type Context = Record<PropertyKey, any>;
6
6
  type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
@@ -28,6 +28,9 @@ interface Lazy<T> {
28
28
  };
29
29
  }
30
30
  type Lazyable<T> = T | Lazy<T>;
31
+ /**
32
+ * Create a lazy thing.
33
+ */
31
34
  declare function lazy<T>(loader: () => Promise<{
32
35
  default: T;
33
36
  }>, meta?: LazyMeta): Lazy<T>;
@@ -56,7 +59,15 @@ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends
56
59
  outputValidationIndex: number;
57
60
  handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
58
61
  }
62
+ /**
63
+ * This class represents a procedure.
64
+ *
65
+ * @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
66
+ */
59
67
  declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
68
+ /**
69
+ * This property holds the defined options.
70
+ */
60
71
  '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
61
72
  constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
62
73
  }
@@ -87,6 +98,11 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
87
98
  next: MiddlewareNextFn<TOutput>;
88
99
  errors: TErrorConstructorMap;
89
100
  }
101
+ /**
102
+ * A function that represents a middleware.
103
+ *
104
+ * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
105
+ */
90
106
  interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
91
107
  (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
92
108
  }
@@ -106,9 +122,6 @@ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TEr
106
122
  signal?: AbortSignal;
107
123
  lastEventId: string | undefined;
108
124
  }
109
- /**
110
- * Options for creating a procedure caller with comprehensive type safety
111
- */
112
125
  type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
126
  /**
114
127
  * This is helpful for logging and analytics.
@@ -120,24 +133,60 @@ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema
120
133
  } : {
121
134
  context: Value<TInitialContext, [clientContext: TClientContext]>;
122
135
  });
136
+ /**
137
+ * Create Server-side client from a procedure.
138
+ *
139
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
140
+ */
123
141
  declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
124
142
 
143
+ /**
144
+ * Represents a router, which defines a hierarchical structure of procedures.
145
+ *
146
+ * @info A procedure is a router too.
147
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
148
+ */
125
149
  type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
126
150
  [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
127
151
  };
128
152
  type AnyRouter = Router<any, any>;
129
153
  type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
154
+ /**
155
+ * Infer all initial context of the router.
156
+ *
157
+ * @info A procedure is a router too.
158
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
159
+ */
130
160
  type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
131
161
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
132
162
  };
163
+ /**
164
+ * Infer all current context of the router.
165
+ *
166
+ * @info A procedure is a router too.
167
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
168
+ */
133
169
  type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
134
170
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
135
171
  };
172
+ /**
173
+ * Infer all router inputs
174
+ *
175
+ * @info A procedure is a router too.
176
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
177
+ */
136
178
  type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
137
179
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
138
180
  };
181
+ /**
182
+ * Infer all router outputs
183
+ *
184
+ * @info A procedure is a router too.
185
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
186
+ */
139
187
  type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
140
188
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
189
  };
142
190
 
143
- export { type AnyProcedure as A, middlewareOutputFn as B, type Context as C, type ProcedureHandlerOptions as D, type ProcedureDef as E, isProcedure as F, createProcedureClient as G, type InferRouterInitialContexts as H, type InferRouterInitialContext as I, type InferRouterCurrentContexts as J, type InferRouterInputs as K, type Lazyable as L, type Middleware as M, type InferRouterOutputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type Router as R, type AnyRouter as a, Procedure as b, type MergedInitialContext as c, type MergedCurrentContext as d, type MapInputMiddleware as e, type CreateProcedureClientOptions as f, type ProcedureClient as g, type AnyMiddleware as h, type Lazy as i, type ProcedureHandler as j, type ORPCErrorConstructorMapItemOptions as k, type ORPCErrorConstructorMapItem as l, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, type LazyMeta as p, lazy as q, isLazy as r, getLazyMeta as s, type MiddlewareResult as t, unlazy as u, validateORPCError as v, type MiddlewareNextFnOptions as w, type MiddlewareNextFn as x, type MiddlewareOutputFn as y, type MiddlewareOptions as z };
191
+ export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
192
+ export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
@@ -1,5 +1,5 @@
1
- import { C as Context } from './server.DLt5njUb.mjs';
2
- import { S as StandardHandleOptions } from './server.CzxlqYZL.mjs';
1
+ import { C as Context } from './server.DPWk5pjW.mjs';
2
+ import { g as StandardHandleOptions } from './server.eWLxY3lq.mjs';
3
3
 
4
4
  type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
5
5
  context?: T;
@@ -1,15 +1,26 @@
1
+ import { toHttpPath, StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
1
2
  import { ORPCError, toORPCError } from '@orpc/client';
2
3
  import { toArray, intercept, parseEmptyableJSON } from '@orpc/shared';
3
- import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.e3W6AG3-.mjs';
4
- import { toHttpPath } from '@orpc/client/standard';
4
+ import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.DG7Tamti.mjs';
5
+
6
+ class CompositeStandardHandlerPlugin {
7
+ plugins;
8
+ constructor(plugins = []) {
9
+ this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
10
+ }
11
+ init(options, router) {
12
+ for (const plugin of this.plugins) {
13
+ plugin.init?.(options, router);
14
+ }
15
+ }
16
+ }
5
17
 
6
18
  class StandardHandler {
7
19
  constructor(router, matcher, codec, options) {
8
20
  this.matcher = matcher;
9
21
  this.codec = codec;
10
- for (const plugin of toArray(options.plugins)) {
11
- plugin.init?.(options);
12
- }
22
+ const plugins = new CompositeStandardHandlerPlugin(options.plugins);
23
+ plugins.init(options, router);
13
24
  this.interceptors = toArray(options.interceptors);
14
25
  this.clientInterceptors = toArray(options.clientInterceptors);
15
26
  this.rootInterceptors = toArray(options.rootInterceptors);
@@ -159,4 +170,14 @@ class StandardRPCMatcher {
159
170
  }
160
171
  }
161
172
 
162
- export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
173
+ class StandardRPCHandler extends StandardHandler {
174
+ constructor(router, options = {}) {
175
+ const jsonSerializer = new StandardRPCJsonSerializer(options);
176
+ const serializer = new StandardRPCSerializer(jsonSerializer);
177
+ const matcher = new StandardRPCMatcher();
178
+ const codec = new StandardRPCCodec(serializer);
179
+ super(router, matcher, codec, options);
180
+ }
181
+ }
182
+
183
+ export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardRPCCodec as a, StandardRPCHandler as b, StandardRPCMatcher as c };