@orpc/server 0.0.0-next.905e81c → 0.0.0-next.910f96e

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 (83) hide show
  1. package/README.md +15 -2
  2. package/dist/adapters/fetch/index.d.mts +54 -0
  3. package/dist/adapters/fetch/index.d.ts +54 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +22 -0
  6. package/dist/adapters/hono/index.d.ts +22 -0
  7. package/dist/{hono.js → adapters/hono/index.mjs} +11 -13
  8. package/dist/adapters/next/index.d.mts +29 -0
  9. package/dist/adapters/next/index.d.ts +29 -0
  10. package/dist/{next.js → adapters/next/index.mjs} +11 -13
  11. package/dist/adapters/node/index.d.mts +53 -0
  12. package/dist/adapters/node/index.d.ts +53 -0
  13. package/dist/adapters/node/index.mjs +85 -0
  14. package/dist/adapters/standard/index.d.mts +26 -0
  15. package/dist/adapters/standard/index.d.ts +26 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +291 -0
  18. package/dist/index.d.ts +291 -0
  19. package/dist/{index.js → index.mjs} +107 -157
  20. package/dist/plugins/index.d.mts +30 -0
  21. package/dist/plugins/index.d.ts +30 -0
  22. package/dist/{chunk-WQNNSBXW.js → plugins/index.mjs} +19 -32
  23. package/dist/shared/server.B77ImKAP.d.mts +8 -0
  24. package/dist/shared/server.BHlRCrf_.d.ts +66 -0
  25. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  26. package/dist/shared/server.Cud5qk0c.d.ts +10 -0
  27. package/dist/shared/server.CzxlqYZL.d.mts +66 -0
  28. package/dist/shared/server.DGnN7q3R.d.mts +10 -0
  29. package/dist/shared/server.DLt5njUb.d.mts +143 -0
  30. package/dist/shared/server.DLt5njUb.d.ts +143 -0
  31. package/dist/shared/server.DUF89eb-.d.ts +8 -0
  32. package/dist/{chunk-77FU7QSO.js → shared/server.Dfx1jV-K.mjs} +49 -69
  33. package/dist/shared/server.T5WmDoWQ.mjs +98 -0
  34. package/dist/{chunk-MHVECKBC.js → shared/server.e3W6AG3-.mjs} +162 -213
  35. package/package.json +30 -35
  36. package/dist/chunk-47YYO5JS.js +0 -32
  37. package/dist/fetch.js +0 -10
  38. package/dist/node.js +0 -31
  39. package/dist/plugins.js +0 -11
  40. package/dist/src/adapters/fetch/index.d.ts +0 -3
  41. package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -11
  42. package/dist/src/adapters/fetch/types.d.ts +0 -14
  43. package/dist/src/adapters/hono/index.d.ts +0 -3
  44. package/dist/src/adapters/hono/middleware.d.ts +0 -12
  45. package/dist/src/adapters/next/index.d.ts +0 -3
  46. package/dist/src/adapters/next/serve.d.ts +0 -19
  47. package/dist/src/adapters/node/index.d.ts +0 -3
  48. package/dist/src/adapters/node/rpc-handler.d.ts +0 -11
  49. package/dist/src/adapters/node/types.d.ts +0 -22
  50. package/dist/src/adapters/standard/handler.d.ts +0 -53
  51. package/dist/src/adapters/standard/index.d.ts +0 -6
  52. package/dist/src/adapters/standard/rpc-codec.d.ts +0 -16
  53. package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
  54. package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
  55. package/dist/src/adapters/standard/types.d.ts +0 -21
  56. package/dist/src/builder-variants.d.ts +0 -75
  57. package/dist/src/builder.d.ts +0 -58
  58. package/dist/src/config.d.ts +0 -6
  59. package/dist/src/context.d.ts +0 -8
  60. package/dist/src/error.d.ts +0 -12
  61. package/dist/src/hidden.d.ts +0 -8
  62. package/dist/src/implementer-procedure.d.ts +0 -33
  63. package/dist/src/implementer-variants.d.ts +0 -18
  64. package/dist/src/implementer.d.ts +0 -29
  65. package/dist/src/index.d.ts +0 -25
  66. package/dist/src/lazy-utils.d.ts +0 -6
  67. package/dist/src/lazy.d.ts +0 -22
  68. package/dist/src/middleware-decorated.d.ts +0 -11
  69. package/dist/src/middleware-utils.d.ts +0 -5
  70. package/dist/src/middleware.d.ts +0 -38
  71. package/dist/src/plugins/base.d.ts +0 -11
  72. package/dist/src/plugins/cors.d.ts +0 -19
  73. package/dist/src/plugins/index.d.ts +0 -4
  74. package/dist/src/plugins/response-headers.d.ts +0 -10
  75. package/dist/src/procedure-client.d.ts +0 -33
  76. package/dist/src/procedure-decorated.d.ts +0 -24
  77. package/dist/src/procedure-utils.d.ts +0 -19
  78. package/dist/src/procedure.d.ts +0 -31
  79. package/dist/src/router-accessible-lazy.d.ts +0 -8
  80. package/dist/src/router-client.d.ts +0 -12
  81. package/dist/src/router.d.ts +0 -30
  82. package/dist/src/utils.d.ts +0 -24
  83. package/dist/standard.js +0 -13
@@ -0,0 +1,98 @@
1
+ import { ORPCError } from '@orpc/client';
2
+ import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
3
+ import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.Dfx1jV-K.mjs';
4
+ import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
5
+ import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
6
+ import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
7
+
8
+ class BodyLimitPlugin {
9
+ maxBodySize;
10
+ constructor(options) {
11
+ this.maxBodySize = options.maxBodySize;
12
+ }
13
+ initRuntimeAdapter(options) {
14
+ options.adapterInterceptors ??= [];
15
+ options.adapterInterceptors.push(async (options2) => {
16
+ if (!options2.request.body) {
17
+ return options2.next();
18
+ }
19
+ let currentBodySize = 0;
20
+ const rawReader = options2.request.body.getReader();
21
+ const reader = new ReadableStream({
22
+ start: async (controller) => {
23
+ try {
24
+ if (Number(options2.request.headers.get("content-length")) > this.maxBodySize) {
25
+ controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
26
+ return;
27
+ }
28
+ while (true) {
29
+ const { done, value } = await rawReader.read();
30
+ if (done) {
31
+ break;
32
+ }
33
+ currentBodySize += value.length;
34
+ if (currentBodySize > this.maxBodySize) {
35
+ controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
36
+ break;
37
+ }
38
+ controller.enqueue(value);
39
+ }
40
+ } finally {
41
+ controller.close();
42
+ }
43
+ }
44
+ });
45
+ const requestInit = { body: reader, duplex: "half" };
46
+ return options2.next({
47
+ ...options2,
48
+ request: new Request(options2.request, requestInit)
49
+ });
50
+ });
51
+ }
52
+ }
53
+
54
+ class FetchHandler {
55
+ constructor(standardHandler, options = {}) {
56
+ this.standardHandler = standardHandler;
57
+ for (const plugin of toArray(options.plugins)) {
58
+ plugin.initRuntimeAdapter?.(options);
59
+ }
60
+ this.adapterInterceptors = toArray(options.adapterInterceptors);
61
+ this.toFetchResponseOptions = options;
62
+ }
63
+ toFetchResponseOptions;
64
+ adapterInterceptors;
65
+ async handle(request, ...rest) {
66
+ return intercept(
67
+ this.adapterInterceptors,
68
+ {
69
+ ...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
70
+ request,
71
+ toFetchResponseOptions: this.toFetchResponseOptions
72
+ },
73
+ async ({ request: request2, toFetchResponseOptions, ...options }) => {
74
+ const standardRequest = toStandardLazyRequest(request2);
75
+ const result = await this.standardHandler.handle(standardRequest, options);
76
+ if (!result.matched) {
77
+ return result;
78
+ }
79
+ return {
80
+ matched: true,
81
+ response: toFetchResponse(result.response, toFetchResponseOptions)
82
+ };
83
+ }
84
+ );
85
+ }
86
+ }
87
+
88
+ class RPCHandler extends FetchHandler {
89
+ constructor(router, options = {}) {
90
+ const jsonSerializer = new StandardRPCJsonSerializer(options);
91
+ const serializer = new StandardRPCSerializer(jsonSerializer);
92
+ const matcher = new StandardRPCMatcher();
93
+ const codec = new StandardRPCCodec(serializer);
94
+ super(new StandardHandler(router, matcher, codec, options), options);
95
+ }
96
+ }
97
+
98
+ export { BodyLimitPlugin as B, FetchHandler as F, RPCHandler as R };
@@ -1,25 +1,56 @@
1
- // src/lazy.ts
2
- var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
3
- function lazy(loader) {
1
+ import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
2
+ import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
3
+ import { value, intercept } from '@orpc/shared';
4
+
5
+ const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
6
+ function lazy(loader, meta = {}) {
4
7
  return {
5
- [LAZY_LOADER_SYMBOL]: loader
8
+ [LAZY_SYMBOL]: {
9
+ loader,
10
+ meta
11
+ }
6
12
  };
7
13
  }
8
14
  function isLazy(item) {
9
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
15
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
16
+ }
17
+ function getLazyMeta(lazied) {
18
+ return lazied[LAZY_SYMBOL].meta;
10
19
  }
11
20
  function unlazy(lazied) {
12
- return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
21
+ return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
22
+ }
23
+
24
+ function isStartWithMiddlewares(middlewares, compare) {
25
+ if (compare.length > middlewares.length) {
26
+ return false;
27
+ }
28
+ for (let i = 0; i < middlewares.length; i++) {
29
+ if (compare[i] === void 0) {
30
+ return true;
31
+ }
32
+ if (middlewares[i] !== compare[i]) {
33
+ return false;
34
+ }
35
+ }
36
+ return true;
37
+ }
38
+ function mergeMiddlewares(first, second, options) {
39
+ if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
40
+ return second;
41
+ }
42
+ return [...first, ...second];
43
+ }
44
+ function addMiddleware(middlewares, addition) {
45
+ return [...middlewares, addition];
13
46
  }
14
47
 
15
- // src/procedure.ts
16
- import { isContractProcedure } from "@orpc/contract";
17
- var Procedure = class {
48
+ class Procedure {
18
49
  "~orpc";
19
50
  constructor(def) {
20
51
  this["~orpc"] = def;
21
52
  }
22
- };
53
+ }
23
54
  function isProcedure(item) {
24
55
  if (item instanceof Procedure) {
25
56
  return true;
@@ -27,47 +58,10 @@ function isProcedure(item) {
27
58
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
28
59
  }
29
60
 
30
- // src/lazy-utils.ts
31
- function flatLazy(lazied) {
32
- const flattenLoader = async () => {
33
- let current = await unlazy(lazied);
34
- while (true) {
35
- if (!isLazy(current.default)) {
36
- break;
37
- }
38
- current = await unlazy(current.default);
39
- }
40
- return current;
41
- };
42
- return lazy(flattenLoader);
61
+ function mergeCurrentContext(context, other) {
62
+ return { ...context, ...other };
43
63
  }
44
- function createLazyProcedureFormAnyLazy(lazied) {
45
- const lazyProcedure = lazy(async () => {
46
- const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
47
- if (!isProcedure(maybeProcedure)) {
48
- throw new Error(`
49
- Expected a lazy<procedure> but got lazy<unknown>.
50
- This should be caught by TypeScript compilation.
51
- Please report this issue if this makes you feel uncomfortable.
52
- `);
53
- }
54
- return { default: maybeProcedure };
55
- });
56
- return lazyProcedure;
57
- }
58
-
59
- // src/middleware.ts
60
- function middlewareOutputFn(output) {
61
- return { output, context: {} };
62
- }
63
-
64
- // src/procedure-client.ts
65
- import { ORPCError as ORPCError2 } from "@orpc/client";
66
- import { ValidationError } from "@orpc/contract";
67
- import { intercept, toError, value } from "@orpc/shared";
68
64
 
69
- // src/error.ts
70
- import { fallbackORPCErrorStatus, ORPCError } from "@orpc/client";
71
65
  function createORPCErrorConstructorMap(errors) {
72
66
  const proxy = new Proxy(errors, {
73
67
  get(target, code) {
@@ -105,7 +99,10 @@ async function validateORPCError(map, error) {
105
99
  return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
106
100
  }
107
101
 
108
- // src/procedure-client.ts
102
+ function middlewareOutputFn(output) {
103
+ return { output, context: {} };
104
+ }
105
+
109
106
  function createProcedureClient(lazyableProcedure, ...[options]) {
110
107
  return async (...[input, callerOptions]) => {
111
108
  const path = options?.path ?? [];
@@ -129,8 +126,8 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
129
126
  (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
130
127
  );
131
128
  } catch (e) {
132
- if (!(e instanceof ORPCError2)) {
133
- throw toError(e);
129
+ if (!(e instanceof ORPCError)) {
130
+ throw e;
134
131
  }
135
132
  const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
136
133
  throw validated;
@@ -144,7 +141,7 @@ async function validateInput(procedure, input) {
144
141
  }
145
142
  const result = await schema["~standard"].validate(input);
146
143
  if (result.issues) {
147
- throw new ORPCError2("BAD_REQUEST", {
144
+ throw new ORPCError("BAD_REQUEST", {
148
145
  message: "Input validation failed",
149
146
  data: {
150
147
  issues: result.issues
@@ -161,7 +158,7 @@ async function validateOutput(procedure, output) {
161
158
  }
162
159
  const result = await schema["~standard"].validate(output);
163
160
  if (result.issues) {
164
- throw new ORPCError2("INTERNAL_SERVER_ERROR", {
161
+ throw new ORPCError("INTERNAL_SERVER_ERROR", {
165
162
  message: "Output validation failed",
166
163
  cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
167
164
  });
@@ -177,117 +174,48 @@ async function executeProcedureInternal(procedure, options) {
177
174
  let currentInput = options.input;
178
175
  const next = async (...[nextOptions]) => {
179
176
  const index = currentIndex;
177
+ const midContext = nextOptions?.context ?? {};
180
178
  currentIndex += 1;
181
- currentContext = { ...currentContext, ...nextOptions?.context };
179
+ currentContext = mergeCurrentContext(currentContext, midContext);
182
180
  if (index === inputValidationIndex) {
183
181
  currentInput = await validateInput(procedure, currentInput);
184
182
  }
185
183
  const mid = middlewares[index];
186
- const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
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
+ };
187
191
  if (index === outputValidationIndex) {
188
192
  const validatedOutput = await validateOutput(procedure, result.output);
189
193
  return {
190
- ...result,
194
+ context: result.context,
191
195
  output: validatedOutput
192
196
  };
193
197
  }
194
198
  return result;
195
199
  };
196
- return (await next({})).output;
200
+ return (await next()).output;
197
201
  }
198
202
 
199
- // src/hidden.ts
200
- var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
201
- function setRouterContract(obj, contract) {
202
- return new Proxy(obj, {
203
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
204
+ function setHiddenRouterContract(router, contract) {
205
+ return new Proxy(router, {
203
206
  get(target, key) {
204
- if (key === ROUTER_CONTRACT_SYMBOL) {
207
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
205
208
  return contract;
206
209
  }
207
210
  return Reflect.get(target, key);
208
211
  }
209
212
  });
210
213
  }
211
- function getRouterContract(obj) {
212
- return obj[ROUTER_CONTRACT_SYMBOL];
213
- }
214
- var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
215
- function deepSetLazyRouterPrefix(router, prefix) {
216
- return new Proxy(router, {
217
- get(target, key) {
218
- if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
219
- const val = Reflect.get(target, key);
220
- if (isLazy(val)) {
221
- return deepSetLazyRouterPrefix(val, prefix);
222
- }
223
- return val;
224
- }
225
- return prefix;
226
- }
227
- });
228
- }
229
- function getLazyRouterPrefix(obj) {
230
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
214
+ function getHiddenRouterContract(router) {
215
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
231
216
  }
232
217
 
233
- // src/router.ts
234
- import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
235
-
236
- // src/middleware-utils.ts
237
- function dedupeMiddlewares(compare, middlewares) {
238
- let min = 0;
239
- for (let i = 0; i < middlewares.length; i++) {
240
- const index = compare.indexOf(middlewares[i], min);
241
- if (index === -1) {
242
- return middlewares.slice(i);
243
- }
244
- min = index + 1;
245
- }
246
- return [];
247
- }
248
- function mergeMiddlewares(first, second) {
249
- return [...first, ...dedupeMiddlewares(first, second)];
250
- }
251
- function addMiddleware(middlewares, addition) {
252
- return [...middlewares, addition];
253
- }
254
-
255
- // src/router.ts
256
- function adaptRouter(router, options) {
257
- if (isLazy(router)) {
258
- const adapted2 = lazy(async () => {
259
- const unlaziedRouter = (await unlazy(router)).default;
260
- const adapted3 = adaptRouter(unlaziedRouter, options);
261
- return { default: adapted3 };
262
- });
263
- const accessible = createAccessibleLazyRouter(adapted2);
264
- const currentPrefix = getLazyRouterPrefix(router);
265
- const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
266
- if (prefix) {
267
- return deepSetLazyRouterPrefix(accessible, prefix);
268
- }
269
- return accessible;
270
- }
271
- if (isProcedure(router)) {
272
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
273
- const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
274
- const adapted2 = new Procedure({
275
- ...router["~orpc"],
276
- route: adaptRoute(router["~orpc"].route, options),
277
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
278
- middlewares: newMiddlewares,
279
- inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
280
- outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
281
- });
282
- return adapted2;
283
- }
284
- const adapted = {};
285
- for (const key in router) {
286
- adapted[key] = adaptRouter(router[key], options);
287
- }
288
- return adapted;
289
- }
290
- function getRouterChild(router, ...path) {
218
+ function getRouter(router, path) {
291
219
  let current = router;
292
220
  for (let i = 0; i < path.length; i++) {
293
221
  const segment = path[i];
@@ -303,89 +231,131 @@ function getRouterChild(router, ...path) {
303
231
  }
304
232
  const lazied = current;
305
233
  const rest = path.slice(i);
306
- const newLazy = lazy(async () => {
234
+ return lazy(async () => {
307
235
  const unwrapped = await unlazy(lazied);
308
- if (!unwrapped.default) {
309
- return unwrapped;
310
- }
311
- const next = getRouterChild(unwrapped.default, ...rest);
312
- return { default: next };
313
- });
314
- return flatLazy(newLazy);
236
+ const next = getRouter(unwrapped.default, rest);
237
+ return unlazy(next);
238
+ }, getLazyMeta(lazied));
315
239
  }
316
240
  return current;
317
241
  }
318
-
319
- // src/router-accessible-lazy.ts
320
242
  function createAccessibleLazyRouter(lazied) {
321
- const flattenLazy = flatLazy(lazied);
322
- const recursive = new Proxy(flattenLazy, {
243
+ const recursive = new Proxy(lazied, {
323
244
  get(target, key) {
324
245
  if (typeof key !== "string") {
325
246
  return Reflect.get(target, key);
326
247
  }
327
- const next = getRouterChild(flattenLazy, key);
248
+ const next = getRouter(lazied, [key]);
328
249
  return createAccessibleLazyRouter(next);
329
250
  }
330
251
  });
331
252
  return recursive;
332
253
  }
333
-
334
- // src/utils.ts
335
- import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
336
- function eachContractProcedure(options, callback, laziedOptions = []) {
337
- const hiddenContract = getRouterContract(options.router);
338
- if (hiddenContract) {
339
- return eachContractProcedure(
340
- {
341
- router: hiddenContract,
342
- path: options.path
343
- },
344
- callback,
345
- laziedOptions
346
- );
254
+ function enhanceRouter(router, options) {
255
+ if (isLazy(router)) {
256
+ const laziedMeta = getLazyMeta(router);
257
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
258
+ const enhanced2 = lazy(async () => {
259
+ const { default: unlaziedRouter } = await unlazy(router);
260
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
261
+ return unlazy(enhanced3);
262
+ }, {
263
+ ...laziedMeta,
264
+ prefix: enhancedPrefix
265
+ });
266
+ const accessible = createAccessibleLazyRouter(enhanced2);
267
+ return accessible;
268
+ }
269
+ if (isProcedure(router)) {
270
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
271
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
272
+ const enhanced2 = new Procedure({
273
+ ...router["~orpc"],
274
+ route: enhanceRoute(router["~orpc"].route, options),
275
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
276
+ middlewares: newMiddlewares,
277
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
278
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
279
+ });
280
+ return enhanced2;
347
281
  }
348
- if (isLazy(options.router)) {
349
- laziedOptions.push({
350
- lazied: options.router,
282
+ const enhanced = {};
283
+ for (const key in router) {
284
+ enhanced[key] = enhanceRouter(router[key], options);
285
+ }
286
+ return enhanced;
287
+ }
288
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
289
+ let currentRouter = options.router;
290
+ const hiddenContract = getHiddenRouterContract(options.router);
291
+ if (hiddenContract !== void 0) {
292
+ currentRouter = hiddenContract;
293
+ }
294
+ if (isLazy(currentRouter)) {
295
+ lazyOptions.push({
296
+ router: currentRouter,
351
297
  path: options.path
352
298
  });
353
- } else if (isContractProcedure2(options.router)) {
299
+ } else if (isContractProcedure(currentRouter)) {
354
300
  callback({
355
- contract: options.router,
301
+ contract: currentRouter,
356
302
  path: options.path
357
303
  });
358
304
  } else {
359
- for (const key in options.router) {
360
- eachContractProcedure(
305
+ for (const key in currentRouter) {
306
+ traverseContractProcedures(
361
307
  {
362
- router: options.router[key],
308
+ router: currentRouter[key],
363
309
  path: [...options.path, key]
364
310
  },
365
311
  callback,
366
- laziedOptions
312
+ lazyOptions
367
313
  );
368
314
  }
369
315
  }
370
- return laziedOptions;
316
+ return lazyOptions;
371
317
  }
372
- async function eachAllContractProcedure(options, callback) {
318
+ async function resolveContractProcedures(options, callback) {
373
319
  const pending = [options];
374
- for (const item of pending) {
375
- const lazies = eachContractProcedure(item, callback);
376
- for (const lazy2 of lazies) {
377
- const { default: router } = await unlazy(lazy2.lazied);
320
+ for (const options2 of pending) {
321
+ const lazyOptions = traverseContractProcedures(options2, callback);
322
+ for (const options3 of lazyOptions) {
323
+ const { default: router } = await unlazy(options3.router);
378
324
  pending.push({
379
- path: lazy2.path,
380
- router
325
+ router,
326
+ path: options3.path
381
327
  });
382
328
  }
383
329
  }
384
330
  }
385
- function convertPathToHttpPath(path) {
386
- return `/${path.map(encodeURIComponent).join("/")}`;
331
+ async function unlazyRouter(router) {
332
+ if (isProcedure(router)) {
333
+ return router;
334
+ }
335
+ const unlazied = {};
336
+ for (const key in router) {
337
+ const item = router[key];
338
+ const { default: unlaziedRouter } = await unlazy(item);
339
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
340
+ }
341
+ return unlazied;
342
+ }
343
+
344
+ function createAssertedLazyProcedure(lazied) {
345
+ const lazyProcedure = lazy(async () => {
346
+ const { default: maybeProcedure } = await unlazy(lazied);
347
+ if (!isProcedure(maybeProcedure)) {
348
+ throw new Error(`
349
+ Expected a lazy<procedure> but got lazy<unknown>.
350
+ This should be caught by TypeScript compilation.
351
+ Please report this issue if this makes you feel uncomfortable.
352
+ `);
353
+ }
354
+ return { default: maybeProcedure };
355
+ }, getLazyMeta(lazied));
356
+ return lazyProcedure;
387
357
  }
388
- function createContractedProcedure(contract, procedure) {
358
+ function createContractedProcedure(procedure, contract) {
389
359
  return new Procedure({
390
360
  ...procedure["~orpc"],
391
361
  errorMap: contract["~orpc"].errorMap,
@@ -393,29 +363,8 @@ function createContractedProcedure(contract, procedure) {
393
363
  meta: contract["~orpc"].meta
394
364
  });
395
365
  }
366
+ function call(procedure, input, ...rest) {
367
+ return createProcedureClient(procedure, ...rest)(input);
368
+ }
396
369
 
397
- export {
398
- LAZY_LOADER_SYMBOL,
399
- lazy,
400
- isLazy,
401
- unlazy,
402
- Procedure,
403
- isProcedure,
404
- flatLazy,
405
- createLazyProcedureFormAnyLazy,
406
- addMiddleware,
407
- middlewareOutputFn,
408
- createProcedureClient,
409
- setRouterContract,
410
- getRouterContract,
411
- deepSetLazyRouterPrefix,
412
- getLazyRouterPrefix,
413
- createAccessibleLazyRouter,
414
- adaptRouter,
415
- getRouterChild,
416
- eachContractProcedure,
417
- eachAllContractProcedure,
418
- convertPathToHttpPath,
419
- createContractedProcedure
420
- };
421
- //# sourceMappingURL=chunk-MHVECKBC.js.map
370
+ export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };