@orpc/server 0.0.0-next.d16a1b6 → 0.0.0-next.d452413

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 (34) hide show
  1. package/README.md +3 -1
  2. package/dist/adapters/fetch/index.d.mts +4 -5
  3. package/dist/adapters/fetch/index.d.ts +4 -5
  4. package/dist/adapters/fetch/index.mjs +4 -4
  5. package/dist/adapters/hono/index.d.mts +2 -3
  6. package/dist/adapters/hono/index.d.ts +2 -3
  7. package/dist/adapters/hono/index.mjs +4 -4
  8. package/dist/adapters/next/index.d.mts +2 -3
  9. package/dist/adapters/next/index.d.ts +2 -3
  10. package/dist/adapters/next/index.mjs +4 -4
  11. package/dist/adapters/node/index.d.mts +4 -5
  12. package/dist/adapters/node/index.d.ts +4 -5
  13. package/dist/adapters/node/index.mjs +10 -7
  14. package/dist/adapters/standard/index.d.mts +7 -11
  15. package/dist/adapters/standard/index.d.ts +7 -11
  16. package/dist/adapters/standard/index.mjs +2 -3
  17. package/dist/index.d.mts +125 -111
  18. package/dist/index.d.ts +125 -111
  19. package/dist/index.mjs +40 -30
  20. package/dist/plugins/index.d.mts +12 -12
  21. package/dist/plugins/index.d.ts +12 -12
  22. package/dist/plugins/index.mjs +1 -1
  23. package/dist/shared/{server.V6zT5iYQ.mjs → server.CMrS28Go.mjs} +124 -157
  24. package/dist/shared/{server.BBGuTxHE.mjs → server.CSZRzcSW.mjs} +11 -16
  25. package/dist/shared/{server.pW-fRQNZ.mjs → server.Cq3B6PoL.mjs} +9 -5
  26. package/dist/shared/{server.B-ewprcf.d.ts → server.Ctc1Ekur.d.mts} +12 -14
  27. package/dist/shared/{server.DJrh0Ceu.d.mts → server.DyteP6Y8.d.ts} +12 -14
  28. package/dist/shared/server.Zu6nd6jA.d.mts +142 -0
  29. package/dist/shared/server.Zu6nd6jA.d.ts +142 -0
  30. package/package.json +7 -7
  31. package/dist/shared/server.CA-o8cUY.d.mts +0 -9
  32. package/dist/shared/server.Cn9ybJtE.d.mts +0 -152
  33. package/dist/shared/server.Cn9ybJtE.d.ts +0 -152
  34. package/dist/shared/server.DPQt9YYq.d.ts +0 -9
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
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.CMrS28Go.mjs';
4
+ export { L as LAZY_SYMBOL, k as call, o as createAccessibleLazyRouter, b as createContractedProcedure, n as getHiddenRouterContract, j as getLazyMeta, m as middlewareOutputFn, r as resolveContractProcedures, a as toHttpPath, t as traverseContractProcedures, u as unlazy, p as unlazyRouter } from './shared/server.CMrS28Go.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';
@@ -18,7 +18,7 @@ function fallbackConfig(key, value) {
18
18
  }
19
19
 
20
20
  function decorateMiddleware(middleware) {
21
- const decorated = middleware;
21
+ const decorated = (...args) => middleware(...args);
22
22
  decorated.mapInput = (mapInput) => {
23
23
  const mapped = decorateMiddleware(
24
24
  (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
@@ -73,9 +73,13 @@ class DecoratedProcedure extends Procedure {
73
73
  * Make this procedure callable (works like a function while still being a procedure).
74
74
  */
75
75
  callable(...rest) {
76
- return Object.assign(createProcedureClient(this, ...rest), {
77
- "~type": "Procedure",
78
- "~orpc": this["~orpc"]
76
+ return new Proxy(createProcedureClient(this, ...rest), {
77
+ get: (target, key) => {
78
+ return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
79
+ },
80
+ has: (target, key) => {
81
+ return Reflect.has(this, key) || Reflect.has(target, key);
82
+ }
79
83
  });
80
84
  }
81
85
  /**
@@ -133,6 +137,12 @@ class Builder {
133
137
  route: initialRoute
134
138
  });
135
139
  }
140
+ $input(initialInputSchema) {
141
+ return new Builder({
142
+ ...this["~orpc"],
143
+ inputSchema: initialInputSchema
144
+ });
145
+ }
136
146
  middleware(middleware) {
137
147
  return decorateMiddleware(middleware);
138
148
  }
@@ -194,10 +204,10 @@ class Builder {
194
204
  });
195
205
  }
196
206
  router(router) {
197
- return adaptRouter(router, this["~orpc"]);
207
+ return enhanceRouter(router, this["~orpc"]);
198
208
  }
199
209
  lazy(loader) {
200
- return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
210
+ return enhanceRouter(lazy(loader), this["~orpc"]);
201
211
  }
202
212
  }
203
213
  const os = new Builder({
@@ -205,14 +215,12 @@ const os = new Builder({
205
215
  route: {},
206
216
  meta: {},
207
217
  errorMap: {},
208
- inputSchema: void 0,
209
- outputSchema: void 0,
210
218
  inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
211
219
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
212
220
  middlewares: []
213
221
  });
214
222
 
215
- function mergeContext(context, other) {
223
+ function mergeCurrentContext(context, other) {
216
224
  return { ...context, ...other };
217
225
  }
218
226
 
@@ -229,6 +237,9 @@ function implementerInternal(contract, config, middlewares) {
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,27 @@ 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
248
261
  });
249
- return setRouterContract(adapted, contract);
262
+ return setHiddenRouterContract(adapted, contract);
250
263
  };
251
264
  } else if (key === "lazy") {
252
265
  method = (loader) => {
253
- const adapted = adaptRouter(flatLazy(lazy(loader)), {
266
+ const adapted = enhanceRouter(lazy(loader), {
254
267
  middlewares,
255
- errorMap: {}
268
+ errorMap: {},
269
+ prefix: void 0,
270
+ tags: void 0
256
271
  });
257
- return setRouterContract(adapted, contract);
272
+ return setHiddenRouterContract(adapted, contract);
258
273
  };
259
274
  }
260
- const next = Reflect.get(target, key);
261
- if (!next || typeof next !== "function" && typeof next !== "object") {
275
+ const next = getContractRouter(target, [key]);
276
+ if (!next) {
262
277
  return method ?? next;
263
278
  }
264
279
  const nextImpl = implementerInternal(next, config, middlewares);
@@ -301,26 +316,21 @@ function implement(contract, config = {}) {
301
316
  return impl;
302
317
  }
303
318
 
304
- function call(procedure, input, ...rest) {
305
- return createProcedureClient(procedure, ...rest)(input);
306
- }
307
-
308
- function createRouterClient(router, ...rest) {
319
+ function createRouterClient(router, ...[options]) {
309
320
  if (isProcedure(router)) {
310
- const caller = createProcedureClient(router, ...rest);
321
+ const caller = createProcedureClient(router, options);
311
322
  return caller;
312
323
  }
313
- const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
324
+ const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), options) : {};
314
325
  const recursive = new Proxy(procedureCaller, {
315
326
  get(target, key) {
316
327
  if (typeof key !== "string") {
317
328
  return Reflect.get(target, key);
318
329
  }
319
- const next = getRouterChild(router, key);
330
+ const next = getRouter(router, [key]);
320
331
  if (!next) {
321
332
  return Reflect.get(target, key);
322
333
  }
323
- const [options] = rest;
324
334
  return createRouterClient(next, {
325
335
  ...options,
326
336
  path: [...options?.path ?? [], key]
@@ -330,4 +340,4 @@ function createRouterClient(router, ...rest) {
330
340
  return recursive;
331
341
  }
332
342
 
333
- export { Builder, DecoratedProcedure, Procedure, adaptRouter, call, createLazyProcedureFormAnyLazy, createProcedureClient, createRouterClient, decorateMiddleware, fallbackConfig, flatLazy, getRouterChild, implement, implementerInternal, isLazy, isProcedure, lazy, mergeContext, os, setRouterContract };
343
+ export { Builder, DecoratedProcedure, Procedure, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, isLazy, isProcedure, lazy, mergeCurrentContext, os, setHiddenRouterContract };
@@ -1,31 +1,31 @@
1
- import { d as StandardHandlerInterceptorOptions, P as Plugin, a as StandardHandlerOptions } from '../shared/server.DJrh0Ceu.mjs';
2
- export { C as CompositePlugin } from '../shared/server.DJrh0Ceu.mjs';
1
+ import { a as StandardHandlerInterceptorOptions, H as HandlerPlugin, b as StandardHandlerOptions } from '../shared/server.Ctc1Ekur.mjs';
2
+ export { C as CompositePlugin } from '../shared/server.Ctc1Ekur.mjs';
3
3
  import { Value } from '@orpc/shared';
4
- import { C as Context } from '../shared/server.Cn9ybJtE.mjs';
4
+ import { C as Context } from '../shared/server.Zu6nd6jA.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 { d as StandardHandlerInterceptorOptions, P as Plugin, a as StandardHandlerOptions } from '../shared/server.B-ewprcf.js';
2
- export { C as CompositePlugin } from '../shared/server.B-ewprcf.js';
1
+ import { a as StandardHandlerInterceptorOptions, H as HandlerPlugin, b as StandardHandlerOptions } from '../shared/server.DyteP6Y8.js';
2
+ export { C as CompositePlugin } from '../shared/server.DyteP6Y8.js';
3
3
  import { Value } from '@orpc/shared';
4
- import { C as Context } from '../shared/server.Cn9ybJtE.js';
4
+ import { C as Context } from '../shared/server.Zu6nd6jA.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 };
@@ -3,7 +3,7 @@ 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"]
@@ -1,18 +1,31 @@
1
+ import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
1
2
  import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
2
- import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, adaptRoute } from '@orpc/contract';
3
3
  import { value, intercept, toError } from '@orpc/shared';
4
4
 
5
- const LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
6
- function lazy(loader) {
5
+ const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
6
+ function lazy(loader, meta = {}) {
7
7
  return {
8
- [LAZY_LOADER_SYMBOL]: loader
8
+ [LAZY_SYMBOL]: {
9
+ loader,
10
+ meta
11
+ }
9
12
  };
10
13
  }
11
14
  function isLazy(item) {
12
- 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;
13
19
  }
14
20
  function unlazy(lazied) {
15
- 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 mergeMiddlewares(first, second) {
25
+ return [...first, ...second];
26
+ }
27
+ function addMiddleware(middlewares, addition) {
28
+ return [...middlewares, addition];
16
29
  }
17
30
 
18
31
  class Procedure {
@@ -28,52 +41,6 @@ function isProcedure(item) {
28
41
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
29
42
  }
30
43
 
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);
43
- }
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
- function dedupeMiddlewares(compare, middlewares) {
60
- let min = 0;
61
- for (let i = 0; i < middlewares.length; i++) {
62
- const index = compare.indexOf(middlewares[i], min);
63
- if (index === -1) {
64
- return middlewares.slice(i);
65
- }
66
- min = index + 1;
67
- }
68
- return [];
69
- }
70
- function mergeMiddlewares(first, second) {
71
- return [...first, ...dedupeMiddlewares(first, second)];
72
- }
73
- function addMiddleware(middlewares, addition) {
74
- return [...middlewares, addition];
75
- }
76
-
77
44
  function createORPCErrorConstructorMap(errors) {
78
45
  const proxy = new Proxy(errors, {
79
46
  get(target, code) {
@@ -205,169 +172,162 @@ async function executeProcedureInternal(procedure, options) {
205
172
  return (await next({})).output;
206
173
  }
207
174
 
208
- const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
209
- function setRouterContract(obj, contract) {
210
- return new Proxy(obj, {
175
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
176
+ function setHiddenRouterContract(router, contract) {
177
+ return new Proxy(router, {
211
178
  get(target, key) {
212
- if (key === ROUTER_CONTRACT_SYMBOL) {
179
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
213
180
  return contract;
214
181
  }
215
182
  return Reflect.get(target, key);
216
183
  }
217
184
  });
218
185
  }
219
- function getRouterContract(obj) {
220
- return obj[ROUTER_CONTRACT_SYMBOL];
186
+ function getHiddenRouterContract(router) {
187
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
221
188
  }
222
- const LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
223
- function deepSetLazyRouterPrefix(router, prefix) {
224
- return new Proxy(router, {
225
- get(target, key) {
226
- if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
227
- const val = Reflect.get(target, key);
228
- if (isLazy(val)) {
229
- return deepSetLazyRouterPrefix(val, prefix);
230
- }
231
- return val;
232
- }
233
- return prefix;
189
+
190
+ function getRouter(router, path) {
191
+ let current = router;
192
+ for (let i = 0; i < path.length; i++) {
193
+ const segment = path[i];
194
+ if (!current) {
195
+ return void 0;
234
196
  }
235
- });
236
- }
237
- function getLazyRouterPrefix(obj) {
238
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
197
+ if (isProcedure(current)) {
198
+ return void 0;
199
+ }
200
+ if (!isLazy(current)) {
201
+ current = current[segment];
202
+ continue;
203
+ }
204
+ const lazied = current;
205
+ const rest = path.slice(i);
206
+ return lazy(async () => {
207
+ const unwrapped = await unlazy(lazied);
208
+ const next = getRouter(unwrapped.default, rest);
209
+ return unlazy(next);
210
+ }, getLazyMeta(lazied));
211
+ }
212
+ return current;
239
213
  }
240
-
241
214
  function createAccessibleLazyRouter(lazied) {
242
- const flattenLazy = flatLazy(lazied);
243
- const recursive = new Proxy(flattenLazy, {
215
+ const recursive = new Proxy(lazied, {
244
216
  get(target, key) {
245
217
  if (typeof key !== "string") {
246
218
  return Reflect.get(target, key);
247
219
  }
248
- const next = getRouterChild(flattenLazy, key);
220
+ const next = getRouter(lazied, [key]);
249
221
  return createAccessibleLazyRouter(next);
250
222
  }
251
223
  });
252
224
  return recursive;
253
225
  }
254
-
255
- function adaptRouter(router, options) {
226
+ function enhanceRouter(router, options) {
256
227
  if (isLazy(router)) {
257
- const adapted2 = lazy(async () => {
258
- const unlaziedRouter = (await unlazy(router)).default;
259
- const adapted3 = adaptRouter(unlaziedRouter, options);
260
- return { default: adapted3 };
228
+ const laziedMeta = getLazyMeta(router);
229
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
230
+ const enhanced2 = lazy(async () => {
231
+ const { default: unlaziedRouter } = await unlazy(router);
232
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
233
+ return unlazy(enhanced3);
234
+ }, {
235
+ ...laziedMeta,
236
+ prefix: enhancedPrefix
261
237
  });
262
- const accessible = createAccessibleLazyRouter(adapted2);
263
- const currentPrefix = getLazyRouterPrefix(router);
264
- const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
265
- if (prefix) {
266
- return deepSetLazyRouterPrefix(accessible, prefix);
267
- }
238
+ const accessible = createAccessibleLazyRouter(enhanced2);
268
239
  return accessible;
269
240
  }
270
241
  if (isProcedure(router)) {
271
242
  const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
272
243
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
273
- const adapted2 = new Procedure({
244
+ const enhanced2 = new Procedure({
274
245
  ...router["~orpc"],
275
- route: adaptRoute(router["~orpc"].route, options),
246
+ route: enhanceRoute(router["~orpc"].route, options),
276
247
  errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
277
248
  middlewares: newMiddlewares,
278
249
  inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
279
250
  outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
280
251
  });
281
- return adapted2;
252
+ return enhanced2;
282
253
  }
283
- const adapted = {};
254
+ const enhanced = {};
284
255
  for (const key in router) {
285
- adapted[key] = adaptRouter(router[key], options);
286
- }
287
- return adapted;
288
- }
289
- function getRouterChild(router, ...path) {
290
- let current = router;
291
- for (let i = 0; i < path.length; i++) {
292
- const segment = path[i];
293
- if (!current) {
294
- return void 0;
295
- }
296
- if (isProcedure(current)) {
297
- return void 0;
298
- }
299
- if (!isLazy(current)) {
300
- current = current[segment];
301
- continue;
302
- }
303
- const lazied = current;
304
- const rest = path.slice(i);
305
- const newLazy = lazy(async () => {
306
- const unwrapped = await unlazy(lazied);
307
- if (!unwrapped.default) {
308
- return unwrapped;
309
- }
310
- const next = getRouterChild(unwrapped.default, ...rest);
311
- return { default: next };
312
- });
313
- return flatLazy(newLazy);
256
+ enhanced[key] = enhanceRouter(router[key], options);
314
257
  }
315
- return current;
258
+ return enhanced;
316
259
  }
317
-
318
- function eachContractProcedure(options, callback, laziedOptions = []) {
319
- const hiddenContract = getRouterContract(options.router);
320
- if (hiddenContract) {
321
- return eachContractProcedure(
322
- {
323
- router: hiddenContract,
324
- path: options.path
325
- },
326
- callback,
327
- laziedOptions
328
- );
260
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
261
+ let currentRouter = options.router;
262
+ const hiddenContract = getHiddenRouterContract(options.router);
263
+ if (hiddenContract !== void 0) {
264
+ currentRouter = hiddenContract;
329
265
  }
330
- if (isLazy(options.router)) {
331
- laziedOptions.push({
332
- lazied: options.router,
266
+ if (isLazy(currentRouter)) {
267
+ lazyOptions.push({
268
+ router: currentRouter,
333
269
  path: options.path
334
270
  });
335
- } else if (isContractProcedure(options.router)) {
271
+ } else if (isContractProcedure(currentRouter)) {
336
272
  callback({
337
- contract: options.router,
273
+ contract: currentRouter,
338
274
  path: options.path
339
275
  });
340
276
  } else {
341
- for (const key in options.router) {
342
- eachContractProcedure(
277
+ for (const key in currentRouter) {
278
+ traverseContractProcedures(
343
279
  {
344
- router: options.router[key],
280
+ router: currentRouter[key],
345
281
  path: [...options.path, key]
346
282
  },
347
283
  callback,
348
- laziedOptions
284
+ lazyOptions
349
285
  );
350
286
  }
351
287
  }
352
- return laziedOptions;
288
+ return lazyOptions;
353
289
  }
354
- async function eachAllContractProcedure(options, callback) {
290
+ async function resolveContractProcedures(options, callback) {
355
291
  const pending = [options];
356
- for (const item of pending) {
357
- const lazies = eachContractProcedure(item, callback);
358
- for (const lazy of lazies) {
359
- const { default: router } = await unlazy(lazy.lazied);
292
+ for (const options2 of pending) {
293
+ const lazyOptions = traverseContractProcedures(options2, callback);
294
+ for (const options3 of lazyOptions) {
295
+ const { default: router } = await unlazy(options3.router);
360
296
  pending.push({
361
- path: lazy.path,
362
- router
297
+ router,
298
+ path: options3.path
363
299
  });
364
300
  }
365
301
  }
366
302
  }
367
- function convertPathToHttpPath(path) {
368
- return `/${path.map(encodeURIComponent).join("/")}`;
303
+ async function unlazyRouter(router) {
304
+ if (isProcedure(router)) {
305
+ return router;
306
+ }
307
+ const unlazied = {};
308
+ for (const key in router) {
309
+ const item = router[key];
310
+ const { default: unlaziedRouter } = await unlazy(item);
311
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
312
+ }
313
+ return unlazied;
314
+ }
315
+
316
+ function createAssertedLazyProcedure(lazied) {
317
+ const lazyProcedure = lazy(async () => {
318
+ const { default: maybeProcedure } = await unlazy(lazied);
319
+ if (!isProcedure(maybeProcedure)) {
320
+ throw new Error(`
321
+ Expected a lazy<procedure> but got lazy<unknown>.
322
+ This should be caught by TypeScript compilation.
323
+ Please report this issue if this makes you feel uncomfortable.
324
+ `);
325
+ }
326
+ return { default: maybeProcedure };
327
+ }, getLazyMeta(lazied));
328
+ return lazyProcedure;
369
329
  }
370
- function createContractedProcedure(contract, procedure) {
330
+ function createContractedProcedure(procedure, contract) {
371
331
  return new Procedure({
372
332
  ...procedure["~orpc"],
373
333
  errorMap: contract["~orpc"].errorMap,
@@ -375,5 +335,12 @@ function createContractedProcedure(contract, procedure) {
375
335
  meta: contract["~orpc"].meta
376
336
  });
377
337
  }
338
+ function call(procedure, input, ...rest) {
339
+ return createProcedureClient(procedure, ...rest)(input);
340
+ }
341
+
342
+ function toHttpPath(path) {
343
+ return `/${path.map(encodeURIComponent).join("/")}`;
344
+ }
378
345
 
379
- export { LAZY_LOADER_SYMBOL as L, Procedure as P, convertPathToHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, eachContractProcedure as e, adaptRouter as f, getRouterChild as g, flatLazy as h, isProcedure as i, isLazy as j, createLazyProcedureFormAnyLazy as k, lazy as l, getRouterContract as m, deepSetLazyRouterPrefix as n, getLazyRouterPrefix as o, middlewareOutputFn as p, createAccessibleLazyRouter as q, eachAllContractProcedure as r, setRouterContract as s, unlazy as u };
346
+ export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, getLazyMeta as j, call as k, lazy as l, middlewareOutputFn as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, unlazyRouter as p, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };