@orpc/server 0.47.0 → 0.49.0

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 (35) hide show
  1. package/README.md +5 -1
  2. package/dist/adapters/fetch/index.d.mts +5 -3
  3. package/dist/adapters/fetch/index.d.ts +5 -3
  4. package/dist/adapters/fetch/index.mjs +4 -4
  5. package/dist/adapters/hono/index.d.mts +4 -2
  6. package/dist/adapters/hono/index.d.ts +4 -2
  7. package/dist/adapters/hono/index.mjs +4 -4
  8. package/dist/adapters/next/index.d.mts +4 -2
  9. package/dist/adapters/next/index.d.ts +4 -2
  10. package/dist/adapters/next/index.mjs +4 -4
  11. package/dist/adapters/node/index.d.mts +5 -3
  12. package/dist/adapters/node/index.d.ts +5 -3
  13. package/dist/adapters/node/index.mjs +8 -7
  14. package/dist/adapters/standard/index.d.mts +10 -9
  15. package/dist/adapters/standard/index.d.ts +10 -9
  16. package/dist/adapters/standard/index.mjs +3 -3
  17. package/dist/index.d.mts +140 -117
  18. package/dist/index.d.ts +140 -117
  19. package/dist/index.mjs +55 -46
  20. package/dist/plugins/index.d.mts +12 -12
  21. package/dist/plugins/index.d.ts +12 -12
  22. package/dist/plugins/index.mjs +2 -2
  23. package/dist/shared/server.B52eKaNe.d.mts +8 -0
  24. package/dist/shared/server.BA-onDGB.d.ts +8 -0
  25. package/dist/shared/{server.DKrKGnk2.mjs → server.BY9sDlwl.mjs} +13 -16
  26. package/dist/shared/server.BYTulgUc.d.mts +144 -0
  27. package/dist/shared/server.BYTulgUc.d.ts +144 -0
  28. package/dist/shared/{server.V6zT5iYQ.mjs → server.BtxZnWJ9.mjs} +155 -160
  29. package/dist/shared/{server.Drm1Lma3.d.ts → server.CDzXh8DM.d.mts} +12 -14
  30. package/dist/shared/{server.CtBp-i4f.d.mts → server.CkGvC2T0.d.ts} +12 -14
  31. package/dist/shared/{server.Q6ZmnTgO.mjs → server.Dba3Iiyp.mjs} +2 -2
  32. package/dist/shared/{server.BHIDiY4a.mjs → server.Del5OmaY.mjs} +6 -5
  33. package/package.json +7 -7
  34. package/dist/shared/server.ptXwNGQr.d.mts +0 -158
  35. package/dist/shared/server.ptXwNGQr.d.ts +0 -158
package/dist/index.mjs CHANGED
@@ -1,14 +1,15 @@
1
- import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure } from '@orpc/contract';
1
+ import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter } from '@orpc/contract';
2
2
  export { ValidationError, eventIterator, type } from '@orpc/contract';
3
- import { P as Procedure, d as addMiddleware, c as createProcedureClient, f as adaptRouter, h as flatLazy, l as lazy, s as setRouterContract, i as isProcedure, j as isLazy, k as createLazyProcedureFormAnyLazy, g as getRouterChild } from './shared/server.V6zT5iYQ.mjs';
4
- export { L as LAZY_LOADER_SYMBOL, a as convertPathToHttpPath, q as createAccessibleLazyRouter, b as createContractedProcedure, n as deepSetLazyRouterPrefix, r as eachAllContractProcedure, e as eachContractProcedure, o as getLazyRouterPrefix, m as getRouterContract, p as middlewareOutputFn, u as unlazy } from './shared/server.V6zT5iYQ.mjs';
3
+ import { P as Procedure, d as addMiddleware, c as createProcedureClient, e as enhanceRouter, l as lazy, s as setHiddenRouterContract, i as isProcedure, f as isLazy, h as createAssertedLazyProcedure, g as getRouter } from './shared/server.BtxZnWJ9.mjs';
4
+ export { L as LAZY_SYMBOL, q as call, w as createAccessibleLazyRouter, b as createContractedProcedure, j as createORPCErrorConstructorMap, r as getHiddenRouterContract, k as getLazyMeta, o as isStartWithMiddlewares, m as mergeCurrentContext, p as mergeMiddlewares, n as middlewareOutputFn, x as resolveContractProcedures, a as toHttpPath, t as traverseContractProcedures, u as unlazy, y as unlazyRouter, v as validateORPCError } from './shared/server.BtxZnWJ9.mjs';
5
5
  export { ORPCError, isDefinedError, safe } from '@orpc/client';
6
6
  export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
7
7
  export { getEventMeta, withEventMeta } from '@orpc/standard-server';
8
8
 
9
9
  const DEFAULT_CONFIG = {
10
10
  initialInputValidationIndex: 0,
11
- initialOutputValidationIndex: 0
11
+ initialOutputValidationIndex: 0,
12
+ dedupeLeadingMiddlewares: true
12
13
  };
13
14
  function fallbackConfig(key, value) {
14
15
  if (value === void 0) {
@@ -18,7 +19,7 @@ function fallbackConfig(key, value) {
18
19
  }
19
20
 
20
21
  function decorateMiddleware(middleware) {
21
- const decorated = middleware;
22
+ const decorated = (...args) => middleware(...args);
22
23
  decorated.mapInput = (mapInput) => {
23
24
  const mapped = decorateMiddleware(
24
25
  (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
@@ -73,9 +74,13 @@ class DecoratedProcedure extends Procedure {
73
74
  * Make this procedure callable (works like a function while still being a procedure).
74
75
  */
75
76
  callable(...rest) {
76
- return Object.assign(createProcedureClient(this, ...rest), {
77
- "~type": "Procedure",
78
- "~orpc": this["~orpc"]
77
+ return new Proxy(createProcedureClient(this, ...rest), {
78
+ get: (target, key) => {
79
+ return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
80
+ },
81
+ has: (target, key) => {
82
+ return Reflect.has(this, key) || Reflect.has(target, key);
83
+ }
79
84
  });
80
85
  }
81
86
  /**
@@ -100,6 +105,7 @@ class Builder {
100
105
  return new Builder({
101
106
  ...this["~orpc"],
102
107
  config,
108
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares),
103
109
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
104
110
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
105
111
  });
@@ -133,6 +139,12 @@ class Builder {
133
139
  route: initialRoute
134
140
  });
135
141
  }
142
+ $input(initialInputSchema) {
143
+ return new Builder({
144
+ ...this["~orpc"],
145
+ inputSchema: initialInputSchema
146
+ });
147
+ }
136
148
  middleware(middleware) {
137
149
  return decorateMiddleware(middleware);
138
150
  }
@@ -194,10 +206,10 @@ class Builder {
194
206
  });
195
207
  }
196
208
  router(router) {
197
- return adaptRouter(router, this["~orpc"]);
209
+ return enhanceRouter(router, this["~orpc"]);
198
210
  }
199
211
  lazy(loader) {
200
- return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
212
+ return enhanceRouter(lazy(loader), this["~orpc"]);
201
213
  }
202
214
  }
203
215
  const os = new Builder({
@@ -205,17 +217,12 @@ const os = new Builder({
205
217
  route: {},
206
218
  meta: {},
207
219
  errorMap: {},
208
- inputSchema: void 0,
209
- outputSchema: void 0,
210
220
  inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
211
221
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
212
- middlewares: []
222
+ middlewares: [],
223
+ dedupeLeadingMiddlewares: true
213
224
  });
214
225
 
215
- function mergeContext(context, other) {
216
- return { ...context, ...other };
217
- }
218
-
219
226
  function implementerInternal(contract, config, middlewares) {
220
227
  if (isContractProcedure(contract)) {
221
228
  const impl2 = new Builder({
@@ -223,12 +230,16 @@ function implementerInternal(contract, config, middlewares) {
223
230
  config,
224
231
  middlewares,
225
232
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", config?.initialInputValidationIndex) + middlewares.length,
226
- outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length
233
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length,
234
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
227
235
  });
228
236
  return impl2;
229
237
  }
230
238
  const impl = new Proxy(contract, {
231
239
  get: (target, key) => {
240
+ if (typeof key !== "string") {
241
+ return Reflect.get(target, key);
242
+ }
232
243
  let method;
233
244
  if (key === "middleware") {
234
245
  method = (mid) => decorateMiddleware(mid);
@@ -242,23 +253,29 @@ function implementerInternal(contract, config, middlewares) {
242
253
  };
243
254
  } else if (key === "router") {
244
255
  method = (router) => {
245
- const adapted = adaptRouter(router, {
256
+ const adapted = enhanceRouter(router, {
246
257
  middlewares,
247
- errorMap: {}
258
+ errorMap: {},
259
+ prefix: void 0,
260
+ tags: void 0,
261
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
248
262
  });
249
- return setRouterContract(adapted, contract);
263
+ return setHiddenRouterContract(adapted, contract);
250
264
  };
251
265
  } else if (key === "lazy") {
252
266
  method = (loader) => {
253
- const adapted = adaptRouter(flatLazy(lazy(loader)), {
267
+ const adapted = enhanceRouter(lazy(loader), {
254
268
  middlewares,
255
- errorMap: {}
269
+ errorMap: {},
270
+ prefix: void 0,
271
+ tags: void 0,
272
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
256
273
  });
257
- return setRouterContract(adapted, contract);
274
+ return setHiddenRouterContract(adapted, contract);
258
275
  };
259
276
  }
260
- const next = Reflect.get(target, key);
261
- if (!next || typeof next !== "function" && typeof next !== "object") {
277
+ const next = getContractRouter(target, [key]);
278
+ if (!next) {
262
279
  return method ?? next;
263
280
  }
264
281
  const nextImpl = implementerInternal(next, config, middlewares);
@@ -285,42 +302,34 @@ function implement(contract, config = {}) {
285
302
  method = (config2) => implement(contract, config2);
286
303
  }
287
304
  const next = Reflect.get(target, key);
288
- if (!next || typeof next !== "function" && typeof next !== "object") {
289
- return method ?? next;
305
+ if (!method || !next || typeof next !== "function" && typeof next !== "object") {
306
+ return method || next;
290
307
  }
291
- if (method) {
292
- return new Proxy(method, {
293
- get(_, key2) {
294
- return Reflect.get(next, key2);
295
- }
296
- });
297
- }
298
- return next;
308
+ return new Proxy(method, {
309
+ get(_, key2) {
310
+ return Reflect.get(next, key2);
311
+ }
312
+ });
299
313
  }
300
314
  });
301
315
  return impl;
302
316
  }
303
317
 
304
- function call(procedure, input, ...rest) {
305
- return createProcedureClient(procedure, ...rest)(input);
306
- }
307
-
308
- function createRouterClient(router, ...rest) {
318
+ function createRouterClient(router, ...[options]) {
309
319
  if (isProcedure(router)) {
310
- const caller = createProcedureClient(router, ...rest);
320
+ const caller = createProcedureClient(router, options);
311
321
  return caller;
312
322
  }
313
- const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
323
+ const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), options) : {};
314
324
  const recursive = new Proxy(procedureCaller, {
315
325
  get(target, key) {
316
326
  if (typeof key !== "string") {
317
327
  return Reflect.get(target, key);
318
328
  }
319
- const next = getRouterChild(router, key);
329
+ const next = getRouter(router, [key]);
320
330
  if (!next) {
321
331
  return Reflect.get(target, key);
322
332
  }
323
- const [options] = rest;
324
333
  return createRouterClient(next, {
325
334
  ...options,
326
335
  path: [...options?.path ?? [], key]
@@ -330,4 +339,4 @@ function createRouterClient(router, ...rest) {
330
339
  return recursive;
331
340
  }
332
341
 
333
- export { Builder, DecoratedProcedure, Procedure, adaptRouter, call, createLazyProcedureFormAnyLazy, createProcedureClient, createRouterClient, decorateMiddleware, fallbackConfig, flatLazy, getRouterChild, implement, implementerInternal, isLazy, isProcedure, lazy, mergeContext, os, setRouterContract };
342
+ export { Builder, DecoratedProcedure, Procedure, addMiddleware, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, isLazy, isProcedure, lazy, os, setHiddenRouterContract };
@@ -1,31 +1,31 @@
1
- import { a as StandardHandlerInterceptorOptions, P as Plugin, b as StandardHandlerOptions } from '../shared/server.CtBp-i4f.mjs';
2
- export { C as CompositePlugin } from '../shared/server.CtBp-i4f.mjs';
1
+ import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.CDzXh8DM.mjs';
2
+ export { C as CompositeHandlerPlugin } from '../shared/server.CDzXh8DM.mjs';
3
3
  import { Value } from '@orpc/shared';
4
- import { C as Context } from '../shared/server.ptXwNGQr.mjs';
4
+ import { C as Context } from '../shared/server.BYTulgUc.mjs';
5
5
  import '@orpc/contract';
6
6
  import '@orpc/standard-server';
7
7
  import '@orpc/client';
8
8
 
9
9
  interface CORSOptions<TContext extends Context> {
10
- origin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
- timingOrigin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
- allowMethods?: string[];
13
- allowHeaders?: string[];
10
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
14
  maxAge?: number;
15
15
  credentials?: boolean;
16
- exposeHeaders?: string[];
16
+ exposeHeaders?: readonly string[];
17
17
  }
18
- declare class CORSPlugin<TContext extends Context> implements Plugin<TContext> {
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
19
  private readonly options;
20
- constructor(options?: Partial<CORSOptions<TContext>>);
20
+ constructor(options?: CORSOptions<TContext>);
21
21
  init(options: StandardHandlerOptions<TContext>): void;
22
22
  }
23
23
 
24
24
  interface ResponseHeadersPluginContext {
25
25
  resHeaders?: Headers;
26
26
  }
27
- declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements Plugin<TContext> {
27
+ declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements HandlerPlugin<TContext> {
28
28
  init(options: StandardHandlerOptions<TContext>): void;
29
29
  }
30
30
 
31
- export { type CORSOptions, CORSPlugin, Plugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -1,31 +1,31 @@
1
- import { a as StandardHandlerInterceptorOptions, P as Plugin, b as StandardHandlerOptions } from '../shared/server.Drm1Lma3.js';
2
- export { C as CompositePlugin } from '../shared/server.Drm1Lma3.js';
1
+ import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.CkGvC2T0.js';
2
+ export { C as CompositeHandlerPlugin } from '../shared/server.CkGvC2T0.js';
3
3
  import { Value } from '@orpc/shared';
4
- import { C as Context } from '../shared/server.ptXwNGQr.js';
4
+ import { C as Context } from '../shared/server.BYTulgUc.js';
5
5
  import '@orpc/contract';
6
6
  import '@orpc/standard-server';
7
7
  import '@orpc/client';
8
8
 
9
9
  interface CORSOptions<TContext extends Context> {
10
- origin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
- timingOrigin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
- allowMethods?: string[];
13
- allowHeaders?: string[];
10
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
14
  maxAge?: number;
15
15
  credentials?: boolean;
16
- exposeHeaders?: string[];
16
+ exposeHeaders?: readonly string[];
17
17
  }
18
- declare class CORSPlugin<TContext extends Context> implements Plugin<TContext> {
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
19
  private readonly options;
20
- constructor(options?: Partial<CORSOptions<TContext>>);
20
+ constructor(options?: CORSOptions<TContext>);
21
21
  init(options: StandardHandlerOptions<TContext>): void;
22
22
  }
23
23
 
24
24
  interface ResponseHeadersPluginContext {
25
25
  resHeaders?: Headers;
26
26
  }
27
- declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements Plugin<TContext> {
27
+ declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements HandlerPlugin<TContext> {
28
28
  init(options: StandardHandlerOptions<TContext>): void;
29
29
  }
30
30
 
31
- export { type CORSOptions, CORSPlugin, Plugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -1,9 +1,9 @@
1
- export { C as CompositePlugin } from '../shared/server.Q6ZmnTgO.mjs';
1
+ export { C as CompositeHandlerPlugin } from '../shared/server.Dba3Iiyp.mjs';
2
2
  import { value } from '@orpc/shared';
3
3
 
4
4
  class CORSPlugin {
5
5
  options;
6
- constructor(options) {
6
+ constructor(options = {}) {
7
7
  const defaults = {
8
8
  origin: (origin) => origin,
9
9
  allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.BYTulgUc.mjs';
3
+ import { a as StandardHandlerOptions } from './server.CDzXh8DM.mjs';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.BYTulgUc.js';
3
+ import { a as StandardHandlerOptions } from './server.CkGvC2T0.js';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -1,14 +1,14 @@
1
1
  import { ORPCError, toORPCError } from '@orpc/client';
2
2
  import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
- import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
4
- import { c as createProcedureClient, e as eachContractProcedure, a as convertPathToHttpPath, i as isProcedure, u as unlazy, g as getRouterChild, b as createContractedProcedure } from './server.V6zT5iYQ.mjs';
3
+ import { C as CompositeHandlerPlugin } from './server.Dba3Iiyp.mjs';
4
+ import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.BtxZnWJ9.mjs';
5
5
 
6
6
  class StandardHandler {
7
7
  constructor(router, matcher, codec, options) {
8
8
  this.matcher = matcher;
9
9
  this.codec = codec;
10
10
  this.options = options;
11
- this.plugin = new CompositePlugin(options.plugins);
11
+ this.plugin = new CompositeHandlerPlugin(options.plugins);
12
12
  this.plugin.init(this.options);
13
13
  this.matcher.init(router);
14
14
  }
@@ -69,7 +69,7 @@ class StandardHandler {
69
69
  }
70
70
  }
71
71
 
72
- class RPCCodec {
72
+ class StandardRPCCodec {
73
73
  constructor(serializer) {
74
74
  this.serializer = serializer;
75
75
  }
@@ -93,15 +93,12 @@ class RPCCodec {
93
93
  }
94
94
  }
95
95
 
96
- class RPCMatcher {
96
+ class StandardRPCMatcher {
97
97
  tree = {};
98
98
  pendingRouters = [];
99
99
  init(router, path = []) {
100
- const laziedOptions = eachContractProcedure({
101
- router,
102
- path
103
- }, ({ path: path2, contract }) => {
104
- const httpPath = convertPathToHttpPath(path2);
100
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
101
+ const httpPath = toHttpPath(path2);
105
102
  if (isProcedure(contract)) {
106
103
  this.tree[httpPath] = {
107
104
  path: path2,
@@ -121,7 +118,7 @@ class RPCMatcher {
121
118
  });
122
119
  this.pendingRouters.push(...laziedOptions.map((option) => ({
123
120
  ...option,
124
- httpPathPrefix: convertPathToHttpPath(option.path)
121
+ httpPathPrefix: toHttpPath(option.path)
125
122
  })));
126
123
  }
127
124
  async match(_method, pathname) {
@@ -129,7 +126,7 @@ class RPCMatcher {
129
126
  const newPendingRouters = [];
130
127
  for (const pendingRouter of this.pendingRouters) {
131
128
  if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
132
- const { default: router } = await unlazy(pendingRouter.lazied);
129
+ const { default: router } = await unlazy(pendingRouter.router);
133
130
  this.init(router, pendingRouter.path);
134
131
  } else {
135
132
  newPendingRouters.push(pendingRouter);
@@ -142,14 +139,14 @@ class RPCMatcher {
142
139
  return void 0;
143
140
  }
144
141
  if (!match.procedure) {
145
- const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
142
+ const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
146
143
  if (!isProcedure(maybeProcedure)) {
147
144
  throw new Error(`
148
- [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
145
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
149
146
  Ensure that the procedure is correctly defined and matches the expected contract.
150
147
  `);
151
148
  }
152
- match.procedure = createContractedProcedure(match.contract, maybeProcedure);
149
+ match.procedure = createContractedProcedure(maybeProcedure, match.contract);
153
150
  }
154
151
  return {
155
152
  path: match.path,
@@ -158,4 +155,4 @@ class RPCMatcher {
158
155
  }
159
156
  }
160
157
 
161
- export { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
158
+ export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
@@ -0,0 +1,144 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
4
+
5
+ type Context = Record<string, any>;
6
+ type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
7
+ type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
8
+ declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
9
+ type ContextExtendsGuard<T extends Context, U extends Context> = T extends U ? unknown : never;
10
+
11
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
12
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
13
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
14
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
15
+ };
16
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
17
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
18
+
19
+ declare const LAZY_SYMBOL: unique symbol;
20
+ interface LazyMeta {
21
+ prefix?: HTTPPath;
22
+ }
23
+ interface Lazy<T> {
24
+ [LAZY_SYMBOL]: {
25
+ loader: () => Promise<{
26
+ default: T;
27
+ }>;
28
+ meta: LazyMeta;
29
+ };
30
+ }
31
+ type Lazyable<T> = T | Lazy<T>;
32
+ declare function lazy<T>(loader: () => Promise<{
33
+ default: T;
34
+ }>, meta?: LazyMeta): Lazy<T>;
35
+ declare function isLazy(item: unknown): item is Lazy<any>;
36
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
37
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
38
+ default: T extends Lazy<infer U> ? U : T;
39
+ }>;
40
+
41
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
42
+ context: TCurrentContext;
43
+ input: TInput;
44
+ path: readonly string[];
45
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
46
+ signal?: AbortSignal;
47
+ lastEventId: string | undefined;
48
+ errors: TErrorConstructorMap;
49
+ }
50
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
51
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
52
+ }
53
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
54
+ __initialContext?: (type: TInitialContext) => unknown;
55
+ middlewares: readonly AnyMiddleware[];
56
+ inputValidationIndex: number;
57
+ outputValidationIndex: number;
58
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
59
+ }
60
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
61
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
62
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
63
+ }
64
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
65
+ declare function isProcedure(item: unknown): item is AnyProcedure;
66
+
67
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
68
+ output: TOutput;
69
+ context: TOutContext;
70
+ }>;
71
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
72
+ context?: TOutContext;
73
+ } : {
74
+ context: TOutContext;
75
+ };
76
+ interface MiddlewareNextFn<TOutput> {
77
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
78
+ }
79
+ interface MiddlewareOutputFn<TOutput> {
80
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
81
+ }
82
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
83
+ context: TInContext;
84
+ path: readonly string[];
85
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
86
+ signal?: AbortSignal;
87
+ lastEventId: string | undefined;
88
+ next: MiddlewareNextFn<TOutput>;
89
+ errors: TErrorConstructorMap;
90
+ }
91
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
92
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
93
+ }
94
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
95
+ interface MapInputMiddleware<TInput, TMappedInput> {
96
+ (input: TInput): TMappedInput;
97
+ }
98
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
99
+
100
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
101
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
102
+ context: TInitialContext;
103
+ input: InferSchemaInput<TInputSchema>;
104
+ errors: ORPCErrorConstructorMap<TErrorMap>;
105
+ path: readonly string[];
106
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
107
+ signal?: AbortSignal;
108
+ lastEventId: string | undefined;
109
+ }
110
+ /**
111
+ * Options for creating a procedure caller with comprehensive type safety
112
+ */
113
+ type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
114
+ /**
115
+ * This is helpful for logging and analytics.
116
+ */
117
+ path?: readonly string[];
118
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
119
+ } & (Record<never, never> extends TInitialContext ? {
120
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
121
+ } : {
122
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
123
+ });
124
+ 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, TInputSchema, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
125
+
126
+ 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> : {
127
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
128
+ };
129
+ type AnyRouter = Router<any, any>;
130
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
131
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
132
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
133
+ };
134
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
135
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
136
+ };
137
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
138
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
139
+ };
140
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
141
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
142
+ };
143
+
144
+ export { type AnyProcedure as A, type MiddlewareOptions as B, type Context as C, middlewareOutputFn as D, type ProcedureHandlerOptions as E, type ProcedureDef as F, isProcedure as G, createProcedureClient as H, type InferRouterInitialContext as I, type InferRouterInitialContexts as J, type InferRouterCurrentContexts as K, type Lazyable as L, type Middleware as M, type InferRouterInputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type InferRouterOutputs as Q, type Router as R, type AnyRouter as a, Procedure as b, type ContextExtendsGuard as c, type MergedCurrentContext as d, type MergedInitialContext as e, type MapInputMiddleware as f, type CreateProcedureClientOptions as g, type ProcedureClient as h, type AnyMiddleware as i, type Lazy as j, type ProcedureHandler as k, type ORPCErrorConstructorMapItemOptions as l, mergeCurrentContext as m, type ORPCErrorConstructorMapItem as n, createORPCErrorConstructorMap as o, LAZY_SYMBOL as p, type LazyMeta as q, lazy as r, isLazy as s, getLazyMeta as t, unlazy as u, validateORPCError as v, type MiddlewareResult as w, type MiddlewareNextFnOptions as x, type MiddlewareNextFn as y, type MiddlewareOutputFn as z };